-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathmodel_statement_spec.rb
418 lines (361 loc) · 17.5 KB
/
model_statement_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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
require_relative 'spec_helper'
describe RDF::Statement do
let(:s) {RDF::URI.new("https://rubygems.org/gems/rdf")}
let(:p) {RDF::URI("http://purl.org/dc/terms/creator")}
let(:o) {RDF::URI.new("http://ar.to/#self")}
let(:stmt) {RDF::Statement.new(s, p, o)}
subject {stmt}
context "when initializing" do
it "should be instantiable with a hash argument" do
expect { RDF::Statement.new(subject: s,
predicate: p,
object: o) }.not_to raise_error
end
it "should not alter a hash argument" do
hash = { subject: s, predicate: p, object: o }
original_hash = hash.dup
RDF::Statement.new(hash)
expect(original_hash).to eq hash
end
it "should not alter its options argument" do
options = { graph_name: RDF::URI("URI:http://usefulinc.com/ns/doap#name") }
original_options = options.dup
RDF::Statement.new(s, p, o, options)
expect(options).to eq original_options
end
context "allows arguments to be term or implement #to_term" do
[
{subject: RDF::List("foo"), predicate: RDF::URI("p"), object: RDF::URI("o")},
{subject: RDF::URI("s"), predicate: RDF::URI("p"), object: RDF::List("foo")},
[RDF::List("foo"), RDF::URI("p"), RDF::URI("o")],
[RDF::URI("s"), RDF::URI("p"), RDF::List("foo")],
].each do |arg|
if arg.is_a?(Array)
specify {expect{RDF::Statement.new(*arg)}.not_to raise_error}
else
specify {expect{RDF::Statement.new(arg)}.not_to raise_error}
end
end
end
context "raises an error if any argument does not a term or does not implement #to_term" do
[
{subject: RDF::Graph.new, predicate: RDF::URI("p"), object: RDF::URI("o")},
{subject: RDF::URI("s"), predicate: RDF::URI("p"), object: RDF::Graph.new},
[RDF::Graph.new, RDF::URI("p"), RDF::URI("o")],
[RDF::URI("s"), RDF::URI("p"), RDF::Graph.new],
].each do |arg|
if arg.is_a?(Array)
specify {expect{RDF::Statement.new(*arg)}.to raise_error(NotImplementedError)}
else
specify {expect{RDF::Statement.new(arg)}.to raise_error(NotImplementedError)}
end
end
end
end
context "when created" do
it "should not require arguments" do
expect { RDF::Statement.new }.not_to raise_error
end
it {is_expected.to be_statement}
it {is_expected.to be_statement(subject)}
it {is_expected.not_to be_statement(RDF::Statement.new)}
it {is_expected.to have_subject}
its(:subject) {is_expected.not_to be_nil}
it {expect(subject.predicate?).to be_truthy}
its(:predicate) {is_expected.not_to be_nil}
it {is_expected.to have_object}
its(:object) {is_expected.not_to be_nil}
it {is_expected.to be_asserted}
it {is_expected.not_to be_tripleTerm}
it {is_expected.not_to be_quoted} # FIXME: quoted triples are deprecated
it {is_expected.to be_statement}
it {is_expected.not_to be_inferred}
its(:terms) {is_expected.to include(s, p, o)}
describe "#dup" do
its(:dup) {is_expected.to eql(subject)}
its(:dup) {is_expected.not_to equal(subject)}
[:subject, :predicate, :object].each do |c|
it "#{c} duplicated as appropriate" do
expect(subject.dup.send(c)).to eql(subject.send(c))
expect(subject.dup.send(c)).not_to equal(subject.send(c)) unless subject.send(c).is_a?(RDF::Node)
end
end
end
end
context "when created with a blank node subject" do
subject {RDF::Statement.new(RDF::Node.new, p, o)}
it {is_expected.to be_node}
end
context "when created with a blank node object" do
subject {RDF::Statement.new(s, p, RDF::Node.new)}
it {is_expected.to be_node}
end
context "when created without a graph_name" do
subject {RDF::Statement.new(s, p, o, graph_name: nil)}
its(:graph_name) {is_expected.to be_nil}
it {is_expected.not_to have_graph}
end
context "when created with a graph_name" do
subject {RDF::Statement.new(s, p, o, graph_name: s)}
it {is_expected.to have_graph}
its(:graph_name) {is_expected.not_to be_nil}
it {is_expected.to eq stmt}
it {is_expected.not_to eql stmt}
describe "#dup" do
its(:dup) {is_expected.to eql(subject)}
its(:dup) {is_expected.not_to equal(subject)}
[:subject, :predicate, :object, :graph_name].each do |c|
it "#{c} duplicated as appropriate" do
expect(subject.dup.send(c)).to eql(subject.send(c))
expect(subject.dup.send(c)).not_to equal(subject.send(c)) unless subject.send(c).is_a?(RDF::Node)
end
end
end
end
context "when created with a default graph" do
subject {RDF::Statement.new(s, p, o, graph_name: false)}
let(:stmtc) {RDF::Statement.new(s, p, o, graph_name: s)}
it {is_expected.not_to have_graph}
its(:graph_name) {is_expected.to eq false}
it {is_expected.to eq stmt}
it {is_expected.to eq stmtc}
it {is_expected.to_not eql stmtc}
end
context "when used with symbols" do
specify {expect(RDF::Statement(:s, p, o)).to eq (RDF::Statement(:s, p, o))}
specify {expect(RDF::Statement(:s, p, o)).to eql (RDF::Statement(:s, p, o))}
specify {expect(RDF::Statement(s, p, :o)).to eq (RDF::Statement(s, p, :o))}
specify {expect(RDF::Statement(s, p, :o)).to eql (RDF::Statement(s, p, :o))}
describe "#terms" do
subject {RDF::Statement(:s, p, o)}
specify {expect(subject.terms).not_to include(:s)}
specify {expect(subject.terms).to include(p, o)}
end
end
context "when used with strings" do
specify {expect(RDF::Statement(s, p, "o")).to eq (RDF::Statement(s, p, "o"))}
specify {expect(RDF::Statement(s, p, "o")).to eql (RDF::Statement(s, p, RDF::Literal("o")))}
specify {expect(RDF::Statement(s, p, RDF::Literal("o"))).to eql (RDF::Statement(s, p, "o"))}
end
context "when used like an Array" do
it {is_expected.to respond_to(:to_a)}
it {is_expected.to respond_to(:[])}
it {is_expected.to respond_to(:[]=)}
its(:to_a) {is_expected.to eql([stmt.subject, stmt.predicate, stmt.object])}
it "should support #[] for the subject" do
expect(subject[0]).to equal(subject.subject)
end
it "should support #[] for the predicate" do
expect(subject[1]).to equal(subject.predicate)
end
it "should support #[] for the object" do
expect(subject[2]).to equal(subject.object)
end
it "should support #[] for the graph_name" do
expect(subject[3]).to equal(subject.graph_name)
end
it "should support #[]= for the subject" do
stmt = subject.dup
stmt[0] = s = RDF::URI("http://example.org/subject")
expect(stmt.subject).to eq s
end
it "should support #[]= for the predicate" do
stmt = subject.dup
stmt[1] = p = RDF::URI("http://example.org/predicate")
expect(stmt.predicate).to eq p
end
it "should support #[]= for the object" do
stmt = subject.dup
stmt[2] = o = RDF::URI("http://example.org/object")
expect(stmt.object).to eq o
end
it "should support #[]= for the graph_name" do
stmt = subject.dup
stmt[3] = c = RDF::URI("http://example.org/graph_name")
expect(stmt.graph_name).to eq c
end
end
context "when marked as inferred" do
subject {RDF::Statement.new(RDF::Node.new, p, o, inferred: true)}
it {is_expected.to be_inferred}
end
context "when marked as tripleTerm" do
subject {RDF::Statement.new(RDF::Node.new, p, o, tripleTerm: true)}
it {is_expected.to be_tripleTerm}
end
# FIXME: quoted triples are deprecated
context "when marked as quoted" do
subject {RDF::Statement.new(RDF::Node.new, p, o, quoted: true)}
it {is_expected.to be_quoted}
end
it {is_expected.to respond_to(:to_h)}
its(:to_h) do
is_expected.to eql({
subject: stmt.subject,
predicate: stmt.predicate,
object: stmt.object,
graph_name: stmt.graph_name,
})
end
it {is_expected.to respond_to(:to_s)}
its(:to_s) {is_expected.to eql "<https://rubygems.org/gems/rdf> <http://purl.org/dc/terms/creator> <http://ar.to/#self> ."}
context "when comparing equality" do
let(:gn) {RDF::URI.parse("http://example.org/graph_name")}
let(:other_stmt) {RDF::Statement.new(s, p, o, graph_name: gn)}
it "should == regardless of graph_name" do
expect(subject).to eq other_stmt
end
it "should not be eql? with differing graph_names" do
expect(subject).not_to eql other_stmt
end
it "should match (===) a statement with a missing component to one with that component" do
expect(subject === other_stmt).to be_truthy
end
it "should not match (===) a statement with a component to one which is missing that component" do
expect(other_stmt === subject).not_to be_truthy
end
it "should only equals? with object equality" do
expect(subject).not_to equal RDF::Statement.new s, p, o
expect(subject).to equal subject
end
it "is not == a RDF::List" do
expect(subject).not_to eq RDF::List[*subject]
end
specify {expect(RDF::Statement(:s, p, o).hash).to eq (RDF::Statement(:s, p, o).hash)}
end
context "completness" do
{
RDF::Statement.new(RDF::URI("https://rubygems.org/gems/rdf"), RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) => true,
RDF::Statement.new(RDF::Node("node"), RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) => true,
RDF::Statement.new(RDF::URI("https://rubygems.org/gems/rdf"), RDF::URI("http://purl.org/dc/terms/creator"), RDF::Node("node")) => true,
RDF::Statement.new(RDF::URI("https://rubygems.org/gems/rdf"), RDF::URI("http://purl.org/dc/terms/creator"), RDF::Literal("literal")) => true,
RDF::Statement.new(RDF::URI('file:///path/to/file with spaces.txt'), RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) => true,
RDF::Statement.new(nil, RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) => false,
RDF::Statement.new(RDF::URI("https://rubygems.org/gems/rdf"), nil, RDF::URI("http://ar.to/#self")) => false,
RDF::Statement.new(RDF::URI("https://rubygems.org/gems/rdf"), RDF::URI("http://purl.org/dc/terms/creator"), nil) => false,
RDF::Statement.new(RDF::Literal("literal"), RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) => true,
RDF::Statement.new(RDF::URI("https://rubygems.org/gems/rdf"), RDF::Node("node"), RDF::URI("http://ar.to/#self")) => true,
RDF::Statement.new(RDF::URI("https://rubygems.org/gems/rdf"), RDF::Literal("literal"), RDF::URI("http://ar.to/#self")) => true,
}.each do |st, complete|
if complete
specify {expect(st).to be_complete}
specify {expect(st).not_to be_incomplete}
else
specify {expect(st).not_to be_complete}
specify {expect(st).to be_incomplete}
end
end
end
context "validatation" do
{
RDF::Statement.new(RDF::URI("https://rubygems.org/gems/rdf"), RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) => true,
RDF::Statement.new(RDF::Node("node"), RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) => true,
RDF::Statement.new(RDF::URI("https://rubygems.org/gems/rdf"), RDF::URI("http://purl.org/dc/terms/creator"), RDF::Node("node")) => true,
RDF::Statement.new(RDF::URI("https://rubygems.org/gems/rdf"), RDF::URI("http://purl.org/dc/terms/creator"), RDF::Literal("literal")) => true,
RDF::Statement.new(RDF::URI('file:///path/to/file with spaces.txt'), RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) => false,
RDF::Statement.new(nil, RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) => false,
RDF::Statement.new(RDF::URI("https://rubygems.org/gems/rdf"), nil, RDF::URI("http://ar.to/#self")) => false,
RDF::Statement.new(RDF::URI("https://rubygems.org/gems/rdf"), RDF::URI("http://purl.org/dc/terms/creator"), nil) => false,
RDF::Statement.new(RDF::Literal("literal"), RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) => false,
RDF::Statement.new(RDF::URI("https://rubygems.org/gems/rdf"), RDF::Node("node"), RDF::URI("http://ar.to/#self")) => false,
RDF::Statement.new(RDF::URI("https://rubygems.org/gems/rdf"), RDF::Literal("literal"), RDF::URI("http://ar.to/#self")) => false,
RDF::Statement.new(RDF::URI('scheme://auth/\\u0000'), RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) => false,
RDF::Statement.new(RDF::URI('scheme://auth/^'), RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) => false,
RDF::Statement.new(RDF::URI('scheme://auth/`'), RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) => false,
RDF::Statement.new(RDF::URI('scheme://auth/\\'), RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) => false,
}.each do |st, valid|
context "given #{st}" do
if valid
specify {expect(st).to be_valid}
specify {expect(st).not_to be_invalid}
describe "#validate!" do
specify {expect {st.validate!}.not_to raise_error}
end
else
specify {expect(st).not_to be_valid}
specify {expect(st).to be_invalid}
describe "#validate!" do
specify {expect {st.validate!}.to raise_error(ArgumentError)}
end
end
end
end
end
context "RDF-star" do
it "is not embedded for plain statements" do
expect(RDF::Statement(:s, :p, :o)).not_to be_embedded
end
it "is embedded for statements having a statement subject" do
expect(RDF::Statement(RDF::Statement(:s1, :p1, :o1), :p, :o)).to be_embedded
end
it "is embedded for statements having a statement object" do
expect(RDF::Statement(:s, :p, RDF::Statement(:s1, :p1, :o1))).to be_embedded
end
context "#terms" do
let(:s1) {RDF::URI.new("http://example.org/s1")}
let(:p1) {RDF::URI("http://example.org/p1")}
let(:o1) {RDF::URI.new("http://example.org/o1")}
subject {RDF::Statement(s, p, RDF::Statement(s1, p1, o1))}
specify {expect(subject.terms).to include(s, p, s1, p1, o1)}
end
end
context "c14n" do
{
RDF::Statement.new(RDF::URI("https://rubygems.org/gems/rdf"), RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) =>
RDF::Statement.new(RDF::URI("https://rubygems.org/gems/rdf"), RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")),
RDF::Statement.new(RDF::URI("https://rubygems.org/gems/rdf"), RDF::URI("http://purl.org/dc/terms/creator"), RDF::Literal("literal")) =>
RDF::Statement.new(RDF::URI("https://rubygems.org/gems/rdf"), RDF::URI("http://purl.org/dc/terms/creator"), RDF::Literal("literal")),
RDF::Statement.new(RDF::URI('file:///path/to/file with spaces.txt'), RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) =>
RDF::Statement.new(RDF::URI('file:///path/to/file%20with%20spaces.txt'), RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")),
RDF::Statement.new(nil, RDF::URI("http://purl.org/dc/terms/creator").dup, RDF::URI("http://ar.to/#self")) => nil,
RDF::Statement.new(RDF::URI("https://rubygems.org/gems/rdf"), nil, RDF::URI("http://ar.to/#self")) => nil,
RDF::Statement.new(RDF::URI("https://rubygems.org/gems/rdf"), RDF::URI("http://purl.org/dc/terms/creator"), nil) => nil,
RDF::Statement.new(RDF::Literal("literal"), RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) => nil,
RDF::Statement.new(RDF::URI("https://rubygems.org/gems/rdf"), RDF::Literal("literal"), RDF::URI("http://ar.to/#self")) => nil,
}.each do |st, result|
context "given #{st}" do
if result
specify {expect {st.canonicalize!}.not_to raise_error}
specify {expect(st.canonicalize!).to eq result}
specify {expect(st.canonicalize).to eq result}
specify {expect(st.canonicalize).to be_valid}
else
specify {expect {st.canonicalize!}.to raise_error(ArgumentError)}
specify {expect(st.canonicalize).to be_nil}
end
end
end
end
context "Examples" do
let(:uri) {RDF::URI("http://example/")}
it "Creating an RDF statement" do
expect(RDF::Statement.new(s, p, o)).to be_a_statement
end
it "Creating an RDF statement from a Hash" do
expect(RDF::Statement.new({
subject: RDF::URI.new("https://rubygems.org/gems/rdf"),
predicate: RDF::URI("http://purl.org/dc/terms/creator"),
object: RDF::URI.new("http://ar.to/#self"),
})).to be_a_statement
end
it "Creating an RDF statement with a graph_name" do
expect(RDF::Statement.new(s, p, o, graph_name: uri).graph_name).to eq uri
end
it "Creating an RDF statement from a Hash" do
expect(RDF::Statement.new({
subject: RDF::URI.new("https://rubygems.org/gems/rdf"),
predicate: RDF::URI("http://purl.org/dc/terms/creator"),
object: RDF::URI.new("http://ar.to/#self"),
})).to be_a_statement
end
it "Creating an RDF statement with interned nodes" do
s = RDF::Node.intern("s")
o = RDF::Node.intern("o")
expect(RDF::Statement.new(:s, p, :o)).to eql RDF::Statement.new(s, p, o)
end
it "Creating an RDF statement with interned nodes" do
o = RDF::Literal("o")
expect(RDF::Statement.new(s, p, "o")).to eql RDF::Statement.new(s, p, o)
end
end
end