1+ $:. unshift File . expand_path ( "../.." , __FILE__ )
2+ require 'spec_helper'
3+ require 'algebra/algebra_helper'
4+ require 'sparql/client'
5+
6+ include SPARQL ::Algebra
7+
8+ describe SPARQL ::Algebra ::Query do
9+ describe :optimize do
10+ {
11+ "prefix" : {
12+ input : %q(
13+ (prefix ((ex: <http://example.org/>))
14+ (bgp (triple ex:x1 ex:p2 ex:x2)))) ,
15+ expect : %q(
16+ (bgp (triple <http://example.org/x1> <http://example.org/p2> <http://example.org/x2>)))
17+ } ,
18+ "base" : {
19+ input : %q(
20+ (base <http://example.org/>
21+ (bgp (triple <x1> <p2> <x2>)))) ,
22+ expect : %q(
23+ (bgp (triple <http://example.org/x1> <http://example.org/p2> <http://example.org/x2>)))
24+ } ,
25+ "join – empty (lhs)" : {
26+ input : %q(
27+ (prefix ((ex: <http://example.org/>))
28+ (join
29+ (bgp)
30+ (bgp (triple ex:who ex:schoolHomepage ?schoolPage))))) ,
31+ expect : %q(
32+ (bgp (triple <http://example.org/who> <http://example.org/schoolHomepage> ?schoolPage)))
33+ } ,
34+ "join – empty (rhs)" : {
35+ input : %q(
36+ (prefix ((ex: <http://example.org/>))
37+ (join
38+ (bgp (triple ex:who ex:schoolHomepage ?schoolPage))
39+ (bgp)))) ,
40+ expect : %q(
41+ (bgp (triple <http://example.org/who> <http://example.org/schoolHomepage> ?schoolPage)))
42+ } ,
43+ "join empty (both)" : {
44+ input : %q(
45+ (prefix ((ex: <http://example.org/>))
46+ (join (bgp) (bgp)))) ,
47+ expect : %q(
48+ (bgp))
49+ } ,
50+ "left join empty lhs with filter expression" : {
51+ input : %q{
52+ (prefix ((: <http://example/>))
53+ (leftjoin
54+ (bgp)
55+ (bgp (triple ?y :q ?w))
56+ (= ?v 2)))} ,
57+ expect : %q{
58+ (filter (= ?v 2)
59+ (bgp (triple ?y <http://example/q> ?w)))} ,
60+ pending : "Figure out LHS optimization"
61+ } ,
62+ "left join empty lhs with no filter expression" : {
63+ input : %q{
64+ (prefix ((: <http://example/>))
65+ (leftjoin
66+ (bgp)
67+ (bgp (triple ?y :q ?w))))} ,
68+ expect : %q{
69+ (bgp (triple ?y <http://example/q> ?w))} ,
70+ pending : "Figure out LHS optimization"
71+ } ,
72+ "left join empty rhs with filter expression" : {
73+ input : %q{
74+ (prefix ((: <http://example/>))
75+ (leftjoin
76+ (bgp (triple ?y :q ?w))
77+ (bgp)
78+ (= ?v 2)))} ,
79+ expect : %q{
80+ (bgp (triple ?y <http://example/q> ?w))s}
81+ } ,
82+ "left join empty rhs with no filter expression" : {
83+ input : %q{
84+ (prefix ((: <http://example/>))
85+ (leftjoin
86+ (bgp (triple ?y :q ?w))
87+ (bgp)))} ,
88+ expect : %q{
89+ (bgp (triple ?y <http://example/q> ?w))}
90+ } ,
91+ "left join empty both with filter expression" : {
92+ input : %q{
93+ (prefix ((: <http://example/>))
94+ (leftjoin (bgp) (bgp) (= ?v 2)))} ,
95+ expect : %q{(bgp)}
96+ } ,
97+ "left join empty both with no filter expression" : {
98+ input : %q{
99+ (prefix ((: <http://example/>))
100+ (leftjoin (bgp) (bgp)))} ,
101+ expect : %q{(bgp)} ,
102+ } ,
103+ "mimus empty lhs" : { pending : true } ,
104+ "mimus empty rhs" : { pending : true } ,
105+ "mimus empty both" : { pending : true } ,
106+ "path reverse" : {
107+ input : %q{(prefix ((: <http://example.org/>)) (path :z (reverse :p) ?v))} ,
108+ expect : %q{(path ?v <http://example.org/p> <http://example.org/z>)}
109+ } ,
110+ "path path*" : {
111+ input : %q{
112+ (prefix ((: <http://example.org/>))
113+ (path :a (seq (seq :p0 (path* :p1)) :p2) ?v))
114+ } ,
115+ expect : %q{
116+ (sequence
117+ (bgp
118+ (triple ??s <http://example.org/p2> <http://example.org/a>)
119+ (triple ?v <http://example.org/p1> ??o))
120+ (path ??o (path* <http://example.org/p2>) ??s))
121+ } ,
122+ pending : "How to compare ND variables"
123+ } ,
124+ "sameTerm" : { pending : true } ,
125+ "service" : { pending : true } ,
126+ "union" : { pending : true } ,
127+ } . each do |name , params |
128+ it name do
129+ pending ( params [ :pending ] ) if params [ :pending ]
130+ query = SPARQL ::Algebra . parse ( params [ :input ] )
131+ optimized = query . optimize
132+ expected = SPARQL ::Algebra . parse ( params [ :expect ] )
133+ expect ( optimized ) . to produce ( expected , { } )
134+ end
135+ end
136+ end
137+ end
0 commit comments