Skip to content

Commit 0d7b9f3

Browse files
committed
Support using graph_name in addition to (as eventual replacement for) context.
1 parent f0be752 commit 0d7b9f3

5 files changed

Lines changed: 174 additions & 52 deletions

File tree

lib/rdf/spec/enumerable.rb

Lines changed: 73 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
end
2020
end
2121

22-
@supports_context = enumerable.supports?(:context) rescue true
22+
@supports_named_graphs = enumerable.supports?(:graph_name) rescue true
2323
end
2424

2525
let(:subject_count) {@statements.map(&:subject).uniq.length}
@@ -82,19 +82,19 @@
8282
it {is_expected.to respond_to(:has_statement?)}
8383
context "#has_statement?" do
8484
let(:unknown_statement) {RDF::Statement.new(RDF::Node.new, RDF::URI.new("http://example.org/unknown"), RDF::Node.new)}
85-
it "is_expected.to have all statements" do
85+
it "should have all statements" do
8686
# Don't check for BNodes, as equivalence depends on their being exactly the same, not just the same identifier. If subject is loaded separately, these won't match.
8787
non_bnode_statements.each do |statement|
8888
is_expected.to have_statement(statement)
8989
end
9090
end
9191

92-
it "does not have statement in different context" do
93-
if @supports_context
94-
context = RDF::URI.new("urn:context:1")
92+
it "does not have statement in different named graph" do
93+
if @supports_named_graphs
94+
graph_name = RDF::URI.new("urn:graph_name:1")
9595
non_bnode_statements.each do |statement|
9696
s = statement.dup
97-
s.context = context
97+
s.graph_name = graph_name
9898
is_expected.not_to have_statement(s)
9999
end
100100
end
@@ -115,7 +115,7 @@
115115
its(:enum_statement) {is_expected.to be_enumerable}
116116
its(:enum_statement) {is_expected.to be_queryable}
117117
context "#enum_statement" do
118-
it "is_expected.to enumerate all statements" do
118+
it "should enumerate all statements" do
119119
expect(subject.enum_statement.count).to eq enumerable.each_statement.count
120120
subject.enum_statement.each do |s|
121121
expect(s).to be_a_statement
@@ -148,7 +148,7 @@
148148
its(:each_triple) {is_expected.to be_an_enumerator}
149149
context "#each_triple" do
150150
specify {expect(subject.each_triple).to all(be_a_triple)}
151-
it "is_expected.to iterate over all triples" do
151+
it "should iterate over all triples" do
152152
subject.each_triple do |*triple|
153153
expect(triple.each).to all(be_a_term)
154154
expect(enumerable).to have_triple(triple) unless triple.any?(&:node?)
@@ -159,7 +159,7 @@
159159
its(:enum_triple) {is_expected.to be_an_enumerator}
160160
its(:enum_triple) {is_expected.to be_countable}
161161
context "#enum_triple" do
162-
it "is_expected.to enumerate all triples" do
162+
it "should enumerate all triples" do
163163
expect(subject.enum_triple.count).to eq enumerable.each_triple.count
164164
subject.enum_triple.each do |s, p, o|
165165
expect([s, p, o]).to all(be_a_term)
@@ -183,7 +183,7 @@
183183

184184
context "#has_quad?" do
185185
specify do
186-
if @supports_context
186+
if @supports_named_graphs
187187
non_bnode_statements.each do |statement|
188188
is_expected.to have_quad(statement.to_quad)
189189
end
@@ -194,7 +194,7 @@
194194
its(:each_quad) {is_expected.to be_an_enumerator}
195195
context "#each_quad" do
196196
specify {expect(subject.each_quad).to all(be_a_quad)}
197-
it "is_expected.to iterate over all quads" do
197+
it "should iterate over all quads" do
198198
subject.each_quad do |*quad|
199199
expect(quad.compact).to all(be_a_term)
200200
expect(enumerable).to have_quad(quad) unless quad.compact.any?(&:node?)
@@ -205,7 +205,7 @@
205205
its(:enum_quad) {is_expected.to be_an_enumerator}
206206
its(:enum_quad) {is_expected.to be_countable}
207207
context "#enum_quad" do
208-
it "is_expected.to enumerate all quads" do
208+
it "should enumerate all quads" do
209209
expect(subject.enum_quad.count).to eq enumerable.each_quad.count
210210
subject.enum_quad.each do |s, p, o, c|
211211
expect([s, p, o, c].compact).to all(be_a_term)
@@ -257,7 +257,7 @@
257257
its(:enum_subject) {is_expected.to be_countable}
258258
context "#enum_subject" do
259259
specify {expect(subject.enum_subject.to_a.reject(&:node?).size).to eq subjects.reject(&:node?).size}
260-
it "is_expected.to enumerate all subjects" do
260+
it "should enumerate all subjects" do
261261
subject.enum_subject.each do |s|
262262
expect(s).to be_a_resource
263263
expect(subjects.to_a).to include(s) unless s.node?
@@ -307,7 +307,7 @@
307307
its(:enum_predicate) {is_expected.to be_an_enumerator}
308308
its(:enum_predicate) {is_expected.to be_countable}
309309
context "#enum_predicate" do
310-
it "is_expected.to enumerate all predicates" do
310+
it "should enumerate all predicates" do
311311
expect(subject.enum_predicate.to_a).to include(*predicates)
312312
end
313313
end
@@ -354,7 +354,7 @@
354354
its(:enum_object) {is_expected.to be_an_enumerator}
355355
its(:enum_object) {is_expected.to be_countable}
356356
context "#enum_object" do
357-
it "is_expected.to enumerate all objects" do
357+
it "should enumerate all objects" do
358358
subject.enum_object.each do |o|
359359
expect(o).to be_a_term
360360
expect(objects.to_a).to include(o) unless o.node?
@@ -374,15 +374,15 @@
374374
subject { enumerable.contexts }
375375
specify {is_expected.to be_an_enumerator}
376376
specify {is_expected.to all(be_a_resource)}
377-
context ":unique => false" do
377+
context "unique: false" do
378378
subject { enumerable.contexts(:unique => false) }
379379
specify {is_expected.to be_an_enumerator}
380380
specify {is_expected.to all(be_a_resource)}
381381
end
382382
end
383383

384384
it "is_expected.to implement #has_context?" do
385-
if @supports_context
385+
if @supports_named_graphs
386386
@statements.each do |statement|
387387
if statement.has_context?
388388
expect(enumerable).to have_context(statement.context)
@@ -397,7 +397,7 @@
397397
context "#each_context" do
398398
let(:contexts) {@statements.map { |s| s.context }.uniq.compact}
399399
it "has appropriate number of contexts" do
400-
if @supports_context
400+
if @supports_named_graphs
401401
expect(subject.each_context.to_a.size).to eq contexts.size
402402
end
403403
end
@@ -410,7 +410,7 @@
410410
its(:enum_context) {is_expected.to be_an_enumerator}
411411
its(:enum_context) {is_expected.to be_countable}
412412
context "#enum_context" do
413-
it "is_expected.to enumerate all contexts" do
413+
it "should enumerate all contexts" do
414414
expect(subject.enum_context.to_a).to include(*enumerable.each_context.to_a)
415415
end
416416
end
@@ -436,11 +436,63 @@
436436
end
437437
end
438438

439+
context "when enumerating graphs" do
440+
it {is_expected.to respond_to(:graph_names)}
441+
it {is_expected.to respond_to(:has_graph?)}
442+
it {is_expected.to respond_to(:each_graph)}
443+
it {is_expected.to respond_to(:enum_graph)}
444+
445+
its(:graph_names) {is_expected.to be_a(Array)}
446+
describe "#graph_names" do
447+
subject { enumerable.graph_names }
448+
specify {is_expected.to be_a(Array)}
449+
specify {is_expected.to all(be_a_resource)}
450+
context ":unique => false" do
451+
subject { enumerable.graph_names(:unique => false) }
452+
specify {is_expected.to be_a(Array)}
453+
specify {is_expected.to all(be_a_resource)}
454+
end
455+
end
456+
457+
it "should implement #has_graph?" do
458+
if @supports_named_graphs
459+
@statements.each do |statement|
460+
if statement.has_graph?
461+
expect(enumerable).to have_graph(statement.graph_name)
462+
end
463+
end
464+
uri = RDF::URI.new('http://example.org/does/not/have/this/uri')
465+
expect(enumerable).not_to have_graph(uri)
466+
end
467+
end
468+
469+
its(:each_graph) {is_expected.to be_an_enumerator}
470+
471+
describe "#each_graph" do
472+
let(:graph_names) {@statements.map { |s| s.graph_name }.uniq.compact}
473+
subject { enumerable.each_graph }
474+
it {is_expected.to be_an_enumerator}
475+
specify {expect(subject.each_graph).to all(be_a_graph)}
476+
it "has appropriate number of graphs" do
477+
expect(subject.each_graph.to_a.size).to eq (graph_names.size + 1)
478+
end if @supports_named_graphs
479+
end
480+
481+
describe "#enum_graph" do
482+
subject { enumerable.enum_graph }
483+
it {is_expected.to be_an_enumerator}
484+
it {is_expected.to be_countable}
485+
it "enumerates the same as #each_graph" do
486+
expect(subject.to_a).to include(*enumerable.each_graph.to_a)
487+
end if @supports_named_graphs # expect with match problematic
488+
end
489+
end
490+
439491
context "when converting" do
440492
it {is_expected.to respond_to(:to_hash)}
441493
its(:to_hash) {is_expected.to be_instance_of(Hash)}
442494
context "#to_hash" do
443-
it "is_expected.to have as many keys as subjects" do
495+
it "should have as many keys as subjects" do
444496
expect(subject.to_hash.keys.size).to eq enumerable.subjects.to_a.size
445497
end
446498
end
@@ -449,7 +501,7 @@
449501
context "when dumping" do
450502
it {is_expected.to respond_to(:dump)}
451503

452-
it "is_expected.to implement #dump" do
504+
it "should implement #dump" do
453505
expect(subject.dump(:ntriples)).to eq RDF::NTriples::Writer.buffer() {|w| w << enumerable}
454506
end
455507

lib/rdf/spec/mutable.rb

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
raise 'mutable must be defined with let(:mutable)' unless
99
defined? mutable
1010

11-
@supports_context = mutable.respond_to?(:supports?) && mutable.supports?(:context)
11+
@supports_named_graphs = mutable.respond_to?(:supports?) && mutable.supports?(:graph_name)
1212
end
1313

1414
let(:resource) { RDF::URI('http://rubygems.org/gems/rdf') }
15-
let(:context) { RDF::URI('http://example.org/context') }
15+
let(:graph_name) { RDF::URI('http://example.org/graph_name') }
1616

1717
describe RDF::Mutable do
1818
subject { mutable }
@@ -65,10 +65,17 @@
6565
end
6666

6767
it "is_expected.to load statements with a context override" do
68-
skip("mutability and contextuality") unless (subject.mutable? && @supports_context)
69-
subject.load RDF::Spec::TRIPLES_FILE, :context => context
70-
is_expected.to have_context(context)
71-
expect(subject.query(:context => context).size).to eq subject.size
68+
skip("mutability and contextuality") unless (subject.mutable? && @supports_named_graphs)
69+
subject.load RDF::Spec::TRIPLES_FILE, context: graph_name
70+
is_expected.to have_context(graph_name)
71+
expect(subject.query(context: graph_name).size).to eq subject.size
72+
end
73+
74+
it "is_expected.to load statements with a graph_name override" do
75+
skip("mutability and contextuality") unless (subject.mutable? && @supports_named_graphs)
76+
subject.load RDF::Spec::TRIPLES_FILE, graph_name: graph_name
77+
is_expected.to have_graph(graph_name)
78+
expect(subject.query(graph_name: graph_name).size).to eq subject.size
7279
end
7380
end
7481

@@ -119,27 +126,27 @@
119126
is_expected.to be_empty
120127
end
121128

122-
it "is_expected.to only delete statements when the context matches" do
129+
it "is_expected.to only delete statements when the graph_name matches" do
123130
skip("mutability") unless subject.mutable?
124-
# Setup three statements identical except for context
125-
count = subject.count + (@supports_context ? 3 : 1)
131+
# Setup three statements identical except for graph_name
132+
count = subject.count + (@supports_named_graphs ? 3 : 1)
126133
s1 = RDF::Statement.new(resource, RDF::URI.new("urn:predicate:1"), RDF::URI.new("urn:object:1"))
127134
s2 = s1.dup
128-
s2.context = RDF::URI.new("urn:context:1")
135+
s2.graph_name = RDF::URI.new("urn:graph_name:1")
129136
s3 = s1.dup
130-
s3.context = RDF::URI.new("urn:context:2")
137+
s3.graph_name = RDF::URI.new("urn:graph_name:2")
131138
subject.insert(s1)
132139
subject.insert(s2)
133140
subject.insert(s3)
134141
expect(subject.count).to eq count
135142

136143
# Delete one by one
137144
subject.delete(s1)
138-
expect(subject.count).to eq count - (@supports_context ? 1 : 1)
145+
expect(subject.count).to eq count - (@supports_named_graphs ? 1 : 1)
139146
subject.delete(s2)
140-
expect(subject.count).to eq count - (@supports_context ? 2 : 1)
147+
expect(subject.count).to eq count - (@supports_named_graphs ? 2 : 1)
141148
subject.delete(s3)
142-
expect(subject.count).to eq count - (@supports_context ? 3 : 1)
149+
expect(subject.count).to eq count - (@supports_named_graphs ? 3 : 1)
143150
end
144151
end
145152
end

lib/rdf/spec/queryable.rb

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,42 @@
320320
end
321321
end
322322
end
323+
324+
context "with graph_name" do
325+
it "returns statements from all graphs with no graph_name" do
326+
pattern = RDF::Query::Pattern.new(nil, nil, nil, graph_name: nil)
327+
solutions = []
328+
subject.send(:query_pattern, pattern) {|s| solutions << s}
329+
expect(solutions.size).to eq @statements.size
330+
end
331+
332+
it "returns statements from unnamed graphss with false graph_name" do
333+
pattern = RDF::Query::Pattern.new(nil, nil, nil, graph_name: false)
334+
solutions = []
335+
subject.send(:query_pattern, pattern) {|s| solutions << s}
336+
named_statements = subject.statements.reject {|st| st.has_name?}.length
337+
expect(solutions.size).to eq named_statements
338+
end
339+
340+
it "returns statements from named graphss with variable graph_name" do
341+
unless subject.graph_names.to_a.empty?
342+
pattern = RDF::Query::Pattern.new(nil, nil, nil, graph_name: :c)
343+
solutions = []
344+
subject.send(:query_pattern, pattern) {|s| solutions << s}
345+
named_statements = subject.statements.select {|st| st.has_name?}.length
346+
expect(solutions.size).to eq named_statements
347+
end
348+
end
349+
350+
it "returns statements from specific graph with URI graph_name" do
351+
unless subject.graph_names.to_a.empty?
352+
pattern = RDF::Query::Pattern.new(nil, nil, nil, graph_name: RDF::URI("http://ar.to/#self"))
353+
solutions = []
354+
subject.send(:query_pattern, pattern) {|s| solutions << s}
355+
expect(solutions.size).to eq File.readlines(@doap).grep(/^<http:\/\/ar.to\/\#self>/).size
356+
end
357+
end
358+
end
323359
end
324360
end
325361

@@ -328,7 +364,7 @@
328364
describe "#first" do
329365
let(:failing_pattern) {[RDF::URI("http://no-such-resource"), RDF.type, RDF::Node.new]}
330366

331-
it "is_expected.to respond to #first" do
367+
it "should respond to #first" do
332368
is_expected.to respond_to(:first)
333369
end
334370

@@ -360,7 +396,7 @@
360396
describe "#first_subject" do
361397
let(:failing_pattern) {[RDF::URI("http://no-such-resource"), nil, nil]}
362398

363-
it "is_expected.to respond to #first_subject" do
399+
it "should respond to #first_subject" do
364400
is_expected.to respond_to(:first_subject)
365401
end
366402

lib/rdf/spec/transaction.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
shared_examples "an RDF::Transaction" do |klass|
77
include RDF::Spec::Matchers
88

9-
subject {klass.new(:context => RDF::URI("name"), :insert => RDF::Graph.new, :delete => RDF::Graph.new)}
9+
subject {klass.new(graph_name: RDF::URI("name"), inser: RDF::Graph.new, delete: RDF::Graph.new)}
1010

1111
describe "#initialize" do
1212
subject {klass}
@@ -27,6 +27,17 @@
2727
expect(this.context).to eq c
2828
end
2929

30+
it "accepts a graph_name" do
31+
c = double("graph_name")
32+
this = subject.new(:graph => c)
33+
expect(this.graph).to eq c
34+
expect(this.graph_name).to eq c
35+
36+
this = subject.new(:graph_name => c)
37+
expect(this.graph).to eq c
38+
expect(this.graph_name).to eq c
39+
end
40+
3041
it "accepts inserts" do
3142
g = double("inserts")
3243
this = subject.new(:insert => g)

0 commit comments

Comments
 (0)