-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathquery_solutions_spec.rb
390 lines (359 loc) · 15.7 KB
/
query_solutions_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
require_relative 'spec_helper'
describe RDF::Query::Solutions do
let(:uri) {
RDF::Query::Solution.new(
author: RDF::URI("http://ar.to/#self"),
age: RDF::Literal(0),
name: RDF::Literal("Arto Bendiken", language: :es),
description: RDF::Literal("Description"),
updated: RDF::Literal(Date.today),
created: RDF::Literal(Date.parse("1970-01-01")),
title: RDF::Literal("RDF 1.1"),
price: RDF::Literal(30)
)
}
let(:lit) {
RDF::Query::Solution.new(
author: RDF::Literal("Gregg Kellogg"),
age: RDF::Literal(0),
name: RDF::Literal("Gregg Kellogg", language: :en),
description: RDF::Literal("Description"),
updated: RDF::Literal(Date.today - 1),
created: RDF::Literal(Date.parse("1970-01-01")),
title: RDF::Literal("SPARQL 1.1 Query"),
price: RDF::Literal(40),
date: RDF::Literal(Date.parse("1957-02-27"))
)
}
let(:solutions) {
solns = RDF::Query::Solutions()
solns.concat [uri, lit]
solns
}
subject {solutions}
describe "new" do
it "is instantiable" do
expect { RDF::Query::Solutions.new }.not_to raise_error
expect(RDF::Query::Solutions.new).to be_a(Array)
end
end
describe "#filter" do
it "using a hash" do
{
{author: RDF::URI("http://ar.to/#self")} => [uri],
{author: "Gregg Kellogg"} => [lit],
{author: [RDF::URI("http://ar.to/#self"), "Gregg Kellogg"]} => [uri, lit],
{updated: RDF::Literal(Date.today)} => [uri],
}.each do |arg, result|
expect(solutions.dup.filter(arg)).to eq result
end
end
it "using a block" do
{
lambda { |solution| solution.author.literal? } => [lit],
lambda { |solution| solution.title.to_s =~ /^SPARQL/ } => [lit],
lambda { |solution| solution.price < 30.5 } => [uri],
lambda { |solution| solution.bound?(:date) } => [lit],
lambda { |solution| solution.age.datatype == RDF::XSD.integer } => [uri, lit],
lambda { |solution| solution.name.language == :es } => [uri]
}.each do |block, result|
expect(solutions.dup.filter(&block)).to eq result
end
end
end
describe "merge" do
{
"add x dijoint" => [
RDF::Query::Solutions.new.concat([
RDF::Query::Solution.new(animal: RDF::URI("http://example.org/a")),
RDF::Query::Solution.new(animal: RDF::URI("http://example.org/b")),
RDF::Query::Solution.new(animal: RDF::URI("http://example.org/c")),
]),
RDF::Query::Solutions.new.concat([
RDF::Query::Solution.new(x: RDF::URI("http://example.org/x")),
]),
RDF::Query::Solutions.new.concat([
RDF::Query::Solution.new(animal: RDF::URI("http://example.org/a"), x: RDF::URI("http://example.org/x")),
RDF::Query::Solution.new(animal: RDF::URI("http://example.org/b"), x: RDF::URI("http://example.org/x")),
RDF::Query::Solution.new(animal: RDF::URI("http://example.org/c"), x: RDF::URI("http://example.org/x")),
]),
],
"add x shared" => [
RDF::Query::Solutions.new.concat([
RDF::Query::Solution.new(animal: RDF::URI("http://example.org/a"), x: RDF::URI("http://example.org/x")),
RDF::Query::Solution.new(animal: RDF::URI("http://example.org/b"), x: RDF::URI("http://example.org/x")),
RDF::Query::Solution.new(animal: RDF::URI("http://example.org/c"), x: RDF::URI("http://example.org/x")),
]),
RDF::Query::Solutions.new.concat([
RDF::Query::Solution.new(animal: RDF::URI("http://example.org/a"), y: RDF::URI("http://example.org/y")),
RDF::Query::Solution.new(animal: RDF::URI("http://example.org/b"), y: RDF::URI("http://example.org/y")),
RDF::Query::Solution.new(animal: RDF::URI("http://example.org/c"), y: RDF::URI("http://example.org/y")),
]),
RDF::Query::Solutions.new.concat([
RDF::Query::Solution.new(animal: RDF::URI("http://example.org/a"), x: RDF::URI("http://example.org/x"), y: RDF::URI("http://example.org/y")),
RDF::Query::Solution.new(animal: RDF::URI("http://example.org/b"), x: RDF::URI("http://example.org/x"), y: RDF::URI("http://example.org/y")),
RDF::Query::Solution.new(animal: RDF::URI("http://example.org/c"), x: RDF::URI("http://example.org/x"), y: RDF::URI("http://example.org/y")),
]),
],
"add x disjoint" => [
RDF::Query::Solutions.new.concat([
RDF::Query::Solution.new(animal: RDF::URI("http://example.org/a"), x: RDF::URI("http://example.org/x")),
RDF::Query::Solution.new(animal: RDF::URI("http://example.org/b"), x: RDF::URI("http://example.org/x")),
RDF::Query::Solution.new(animal: RDF::URI("http://example.org/c"), x: RDF::URI("http://example.org/x")),
]),
RDF::Query::Solutions.new.concat([
RDF::Query::Solution.new(animal: RDF::URI("http://example.org/a"), y: RDF::URI("http://example.org/y")),
RDF::Query::Solution.new(animal: RDF::URI("http://example.org/d"), y: RDF::URI("http://example.org/y")),
RDF::Query::Solution.new(animal: RDF::URI("http://example.org/e"), y: RDF::URI("http://example.org/y")),
]),
RDF::Query::Solutions.new.concat([
RDF::Query::Solution.new(animal: RDF::URI("http://example.org/a"), x: RDF::URI("http://example.org/x"), y: RDF::URI("http://example.org/y")),
]),
],
}.each do |name, (left, right, result)|
it name do
expect(left.merge(right)).to be_a(Enumerable)
expect(left.merge(right)).to be_a(RDF::Query::Solutions)
expect(left.merge(right).to_a).to eq result.to_a
end
end
end
describe "#-" do
{
"subsetByExcl01" => [
RDF::Query::Solutions.new.concat([
RDF::Query::Solution.new(animal: RDF::URI("http://www.w3.org/2009/sparql/docs/tests/data-sparql11/negation#lifeForm1")),
RDF::Query::Solution.new(animal: RDF::URI("http://www.w3.org/2009/sparql/docs/tests/data-sparql11/negation#lifeForm2")),
RDF::Query::Solution.new(animal: RDF::URI("http://www.w3.org/2009/sparql/docs/tests/data-sparql11/negation#lifeForm3")),
]),
RDF::Query::Solutions.new.concat([
RDF::Query::Solution.new(animal: RDF::URI("http://www.w3.org/2009/sparql/docs/tests/data-sparql11/negation#lifeForm2"),
type: RDF::URI("http://www.w3.org/2009/sparql/docs/tests/data-sparql11/negation#Reptile")),
RDF::Query::Solution.new(animal: RDF::URI("http://www.w3.org/2009/sparql/docs/tests/data-sparql11/negation#lifeForm3"),
type: RDF::URI("http://www.w3.org/2009/sparql/docs/tests/data-sparql11/negation#Insect")),
]),
RDF::Query::Solutions.new.concat([
RDF::Query::Solution.new(animal: RDF::URI("http://www.w3.org/2009/sparql/docs/tests/data-sparql11/negation#lifeForm1")),
])
],
"exists-02" => [
RDF::Query::Solutions.new.concat([
RDF::Query::Solution.new(a: RDF::URI("http://example/a0"), b: RDF::URI("http://example/b0"), c: RDF::URI("http://example/c0")),
RDF::Query::Solution.new(a: RDF::URI("http://example/a1"), b: RDF::URI("http://example/b1"), c: RDF::URI("http://example/c1")),
RDF::Query::Solution.new(a: RDF::URI("http://example/a2"), b: RDF::URI("http://example/b2"), c: RDF::URI("http://example/c2")),
RDF::Query::Solution.new(a: RDF::URI("http://example/a3"), b: RDF::URI("http://example/b3"), c: RDF::URI("http://example/c3")),
]),
RDF::Query::Solutions.new.concat([
RDF::Query::Solution.new(d: RDF::URI("http://example/d0")),
RDF::Query::Solution.new(d: RDF::URI("http://example/d1"), b: RDF::URI("http://example/b1"), c: RDF::URI("http://example/c1")),
RDF::Query::Solution.new(d: RDF::URI("http://example/d2"), b: RDF::URI("http://example/b2")),
RDF::Query::Solution.new(d: RDF::URI("http://example/d3"), b: RDF::URI("http://example/b3"), c: RDF::URI("http://example/cx")),
]),
RDF::Query::Solutions.new.concat([
RDF::Query::Solution.new(a: RDF::URI("http://example/a0"), b: RDF::URI("http://example/b0"), c: RDF::URI("http://example/c0")),
RDF::Query::Solution.new(a: RDF::URI("http://example/a3"), b: RDF::URI("http://example/b3"), c: RDF::URI("http://example/c3")),
])
],
"full-minuend" => [
RDF::Query::Solutions.new.concat([
RDF::Query::Solution.new(a: RDF::URI("http://example/a1"), b: RDF::URI("http://example/b1")),
RDF::Query::Solution.new(a: RDF::URI("http://example/a2"), b: RDF::URI("http://example/b2")),
RDF::Query::Solution.new(a: RDF::URI("http://example/a3"), b: RDF::URI("http://example/b3")),
RDF::Query::Solution.new(a: RDF::URI("http://example/a4")),
]),
RDF::Query::Solutions.new.concat([
RDF::Query::Solution.new(d: RDF::URI("http://example/d1"), b: RDF::URI("http://example/b1")),
RDF::Query::Solution.new(d: RDF::URI("http://example/d3"), b: RDF::URI("http://example/b3"), c: RDF::URI("http://example/c3")),
RDF::Query::Solution.new(d: RDF::URI("http://example/d4"), b: RDF::URI("http://example/b4"), c: RDF::URI("http://example/c4")),
RDF::Query::Solution.new(d: RDF::URI("http://example/d5")),
]),
RDF::Query::Solutions.new.concat([
RDF::Query::Solution.new(a: RDF::URI("http://example/a2"), b: RDF::URI("http://example/b2")),
RDF::Query::Solution.new(a: RDF::URI("http://example/a4")),
])
],
}.each do |name, (left, right, result)|
it name do
expect(left.minus(right)).to be_a(Enumerable)
expect(left.minus(right)).to be_a(RDF::Query::Solutions)
expect(left.minus(right).to_a).to eq result.to_a
end
end
end
describe "#order_by" do
subject {solutions.order_by(:updated, lambda {|a, b| b <=> a})}
it {is_expected.to be_a(Enumerable)}
it {is_expected.to be_a(RDF::Query::Solutions)}
it "contains solutions in specified order" do
expect(subject).to include(lit, uri)
end
end
describe "#select" do
context "one variable" do
subject {solutions.select(:title)}
it {is_expected.to be_a(Enumerable)}
it {is_expected.to be_a(RDF::Query::Solutions)}
it "contains particular variables only" do
expect(subject).to include(
RDF::Query::Solution.new(title: RDF::Literal("RDF 1.1")),
RDF::Query::Solution.new(title: RDF::Literal("SPARQL 1.1 Query"))
)
end
end
context "two variables" do
subject {solutions.select(:title, :description)}
it {is_expected.to be_a(Enumerable)}
it {is_expected.to be_a(RDF::Query::Solutions)}
it "contains particular variables only" do
expect(subject).to include(
RDF::Query::Solution.new(title: RDF::Literal("RDF 1.1"), description: RDF::Literal("Description")),
RDF::Query::Solution.new(title: RDF::Literal("SPARQL 1.1 Query"), description: RDF::Literal("Description"))
)
end
end
end
describe "#project" do
subject {solutions.project(:title)}
it {is_expected.to be_a(Enumerable)}
it {is_expected.to be_a(RDF::Query::Solutions)}
it "contains particular variables only" do
expect(subject).to include(
RDF::Query::Solution.new(title: RDF::Literal("RDF 1.1")),
RDF::Query::Solution.new(title: RDF::Literal("SPARQL 1.1 Query"))
)
end
end
describe "#distinct" do
subject {RDF::Query::Solutions(solutions.to_a << uri).distinct}
it {is_expected.to be_a(Enumerable)}
it {is_expected.to be_a(RDF::Query::Solutions)}
it "contains distinct solutions" do
expect(subject).to include(uri, lit)
end
describe "has stable count and size" do
subject {solutions.offset(1)}
it "should have count 1" do
expect(subject.count).to eq 1
expect(subject.count).to eq 1
end
it "should have size 1" do
expect(subject.size).to eq 1
expect(subject.size).to eq 1
end
end
end
describe "#offset" do
subject {solutions.offset(20)}
it {is_expected.to be_a(Enumerable)}
it {is_expected.to be_a(RDF::Query::Solutions)}
it {is_expected.to be_empty}
describe "has stable count and size" do
subject {solutions.offset(1)}
it "should have count 1" do
expect(subject.count).to eq 1
expect(subject.count).to eq 1
end
it "should have size 1" do
expect(subject.size).to eq 1
expect(subject.size).to eq 1
end
end
end
describe "#limit" do
subject {solutions.limit(1)}
it {is_expected.to be_a(Enumerable)}
it {is_expected.to be_a(RDF::Query::Solutions)}
describe "has stable count and size" do
subject {solutions.offset(1)}
it "should have count 1" do
expect(subject.count).to eq 1
expect(subject.count).to eq 1
end
it "should have size 1" do
expect(subject.size).to eq 1
expect(subject.size).to eq 1
end
end
end
describe "#offset+#limit" do
subject {solutions.offset(20).limit(20)}
it {is_expected.to be_a(Enumerable)}
it {is_expected.to be_a(RDF::Query::Solutions)}
it {is_expected.to be_empty}
end
describe "#variable_names" do
subject {solutions.variable_names}
specify {is_expected.to include(:author, :age, :name, :description, :updated, :created, :title, :price, :date)}
end
describe "#variable_names=" do
it "can set variable names from constants" do
solutions.variable_names = %i{author age foo}
expect(solutions.variable_names).to include(:author, :age, :foo)
expect(solutions.variable_names).not_to include(:name)
end
it "can set variable names from variables" do
solutions.variable_names = %w{author age foo}.map {|n| RDF::Query::Variable.new(n)}
expect(solutions.variable_names).to include(:author, :age, :foo)
expect(solutions.variable_names).not_to include(:name)
end
end
describe "#count" do
its(:count) {is_expected.to eq 2}
it "Counting the number of matching solutions" do
expect(subject.count { |solution| solution.price < 30.5 }).to eq 1
end
end
describe "#each" do
it "Iterating over all found solutions" do
expect {|b| solutions.each(&b)}.to yield_successive_args(uri, lit)
end
end
describe "#eql?" do
it "is true for equivalent solutions" do
expect(solutions).to eql solutions.dup
end
it "is false for different solutions" do
solns2 = RDF::Query::Solutions(uri)
expect(solutions).not_to eql solns2
end
it "is false for the same solution with different variable_names" do
solns2 = solutions.dup
solns2.variable_names = %i{foo bar}
expect(solutions).not_to eql solns2
end
end
describe "#==" do
it "is true for equivalent solutions" do
expect(solutions).to eq solutions.dup
end
it "is false for different solutions" do
solns2 = RDF::Query::Solutions(uri)
expect(solutions).not_to eq solns2
end
it "is false for the same solution with different variable_names" do
solns2 = solutions.dup
solns2.variable_names = %i{foo bar}
expect(solutions).not_to eq solns2
end
end
describe "#bindings" do
subject {
RDF::Query::Solutions(
RDF::Query::Solution.new(
author: RDF::URI("http://ar.to/#self"),
age: RDF::Literal(0)
),
RDF::Query::Solution.new(
author: RDF::Literal("Gregg Kellogg"),
age: RDF::Literal(0)
)
)
}
it "binds author twice" do
expect(subject.bindings).to include(author: [RDF::URI("http://ar.to/#self"), RDF::Literal("Gregg Kellogg")])
end
it "binds age twice" do
expect(subject.bindings).to include(age: [RDF::Literal(0), RDF::Literal(0)])
end
end
end