rdf_context 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,378 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+ include RdfContext
3
+
4
+ FOO_NS = Namespace.new("http://foo/", "foo")
5
+
6
+ describe "XML Serializer" do
7
+ before(:each) do
8
+ @graph = Graph.new
9
+ end
10
+
11
+ describe "with types" do
12
+ it "should serialize resource without type" do
13
+ @graph.add_triple("http://release/", DC_NS.title, "foo")
14
+ check_xpaths(
15
+ serialize(:max_depth => 1, :attributes => :untyped),
16
+ "/rdf:RDF/rdf:Description/@rdf:about" => "http://release/",
17
+ "/rdf:RDF/rdf:Description/@dc:title" => "foo"
18
+ )
19
+ end
20
+
21
+ it "should serialize resource with type" do
22
+ @graph.add_triple("http://release/", RDF_TYPE, FOO_NS.Release)
23
+ @graph.add_triple("http://release/", DC_NS.title, "foo")
24
+ check_xpaths(
25
+ serialize(:max_depth => 1, :attributes => :untyped),
26
+ "/rdf:RDF/foo:Release/@rdf:about" => "http://release/",
27
+ "/rdf:RDF/foo:Release/@dc:title" =>"foo",
28
+ "/rdf:RDF/foo:Release/rdf:type" => ""
29
+ )
30
+ end
31
+
32
+ it "should serialize resource with two types as attribute" do
33
+ @graph.add_triple("http://release/", RDF_TYPE, FOO_NS.Release)
34
+ @graph.add_triple("http://release/", RDF_TYPE, FOO_NS.XtraRelease)
35
+ @graph.add_triple("http://release/", DC_NS.title, "foo")
36
+ check_xpaths(
37
+ serialize(:max_depth => 1, :attributes => :untyped),
38
+ "/rdf:RDF/foo:Release/@rdf:about" => "http://release/",
39
+ "/rdf:RDF/foo:Release/@dc:title" => "foo",
40
+ "/rdf:RDF/foo:Release/@rdf:type" => FOO_NS.XtraRelease.to_s
41
+ )
42
+ end
43
+
44
+ it "should serialize resource with two types as element" do
45
+ @graph.add_triple("http://release/", RDF_TYPE, FOO_NS.Release)
46
+ @graph.add_triple("http://release/", RDF_TYPE, FOO_NS.XtraRelease)
47
+ @graph.add_triple("http://release/", DC_NS.title, "foo")
48
+ check_xpaths(
49
+ serialize(:max_depth => 1, :attributes => :none),
50
+ "/rdf:RDF/foo:Release/@rdf:about" => "http://release/",
51
+ "/rdf:RDF/foo:Release/dc:title" => true,
52
+ "/rdf:RDF/foo:Release/rdf:type" => %(<rdf:type rdf:resource="#{FOO_NS.XtraRelease}"/>)
53
+ )
54
+ end
55
+
56
+ it "should serialize resource with three types as element" do
57
+ @graph.add_triple("http://release/", RDF_TYPE, FOO_NS.Release)
58
+ @graph.add_triple("http://release/", RDF_TYPE, FOO_NS.XtraRelease)
59
+ @graph.add_triple("http://release/", RDF_TYPE, FOO_NS.XXtraRelease)
60
+ @graph.add_triple("http://release/", DC_NS.title, "foo")
61
+ check_xpaths(
62
+ serialize(:max_depth => 1, :attributes => :typed),
63
+ "/rdf:RDF/foo:Release/@rdf:about" => "http://release/",
64
+ "/rdf:RDF/foo:Release/@dc:title" => true,
65
+ %(/rdf:RDF/foo:Release/rdf:type[@rdf:resource="#{FOO_NS.XtraRelease}"]) => true,
66
+ %(/rdf:RDF/foo:Release/rdf:type[@rdf:resource="#{FOO_NS.XXtraRelease}"]) => true
67
+ )
68
+ end
69
+ end
70
+
71
+ describe "with children" do
72
+ it "should serialize referenced resource by ref" do
73
+ @graph.add_triple("http://release/", RDF_TYPE, FOO_NS.Release)
74
+ @graph.add_triple("http://release/", DC_NS.title, "foo")
75
+ @graph.add_triple("http://release/contributor", RDF_TYPE, FOO_NS.Contributor)
76
+ @graph.add_triple("http://release/contributor", DC_NS.title, "bar")
77
+ @graph.add_triple("http://release/", FOO_NS.releaseContributor, "http://release/contributor")
78
+ check_xpaths(
79
+ serialize(:max_depth => 1, :attributes => :untyped),
80
+ "/rdf:RDF/foo:Release/@rdf:about" => "http://release/",
81
+ "/rdf:RDF/foo:Release/@dc:title" => "foo",
82
+ "/rdf:RDF/foo:Release/foo:releaseContributor/@rdf:resource" => "http://release/contributor",
83
+ "/rdf:RDF/foo:Contributor/@rdf:about" => "http://release/contributor",
84
+ "/rdf:RDF/foo:Contributor/@dc:title" => "bar"
85
+ )
86
+ end
87
+
88
+ it "should serialize referenced resource by inclusion" do
89
+ @graph.add_triple("http://release/", RDF_TYPE, FOO_NS.Release)
90
+ @graph.add_triple("http://release/", DC_NS.title, "foo")
91
+ @graph.add_triple("http://release/contributor", RDF_TYPE, FOO_NS.Contributor)
92
+ @graph.add_triple("http://release/contributor", DC_NS.title, "bar")
93
+ @graph.add_triple("http://release/", FOO_NS.releaseContributor, "http://release/contributor")
94
+ check_xpaths(
95
+ serialize(:max_depth => 3, :attributes => :untyped),
96
+ "/rdf:RDF/foo:Release/@rdf:about" => "http://release/",
97
+ "/rdf:RDF/foo:Release/@dc:title" => "foo",
98
+ "/rdf:RDF/foo:Release/foo:releaseContributor/foo:Contributor/@rdf:about" => "http://release/contributor"
99
+ )
100
+ end
101
+ end
102
+
103
+ describe "with sequences" do
104
+ it "should serialize rdf:Seq with rdf:li" do
105
+ @graph.add_triple("http://example/seq", RDF_TYPE, RDF_NS.Seq)
106
+ @graph.add_triple("http://example/seq", RDF_NS._1, "http://example/first")
107
+ @graph.add_triple("http://example/seq", RDF_NS._2, "http://example/second")
108
+ check_xpaths(
109
+ serialize(:max_depth => 1, :attributes => :untyped),
110
+ "/rdf:RDF/rdf:Seq/@rdf:about" => "http://example/seq",
111
+ %(/rdf:RDF/rdf:Seq/rdf:li[@rdf:resource="http://example/first"]) => true,
112
+ %(/rdf:RDF/rdf:Seq/rdf:li[@rdf:resource="http://example/second"]) => true
113
+ )
114
+ end
115
+
116
+ it "should serialize rdf:Seq with multiple rdf:li in proper sequence" do
117
+ @graph.add_triple("http://example/seq", RDF_TYPE, RDF_NS.Seq)
118
+ @graph.add_triple("http://example/seq", RDF_NS._2, "http://example/second")
119
+ @graph.add_triple("http://example/seq", RDF_NS._1, "http://example/first")
120
+ check_xpaths(
121
+ serialize(:max_depth => 1, :attributes => :untyped),
122
+ "/rdf:RDF/rdf:Seq/@rdf:about" => "http://example/seq",
123
+ %(/rdf:RDF/rdf:Seq/rdf:li[@rdf:resource="http://example/first"]) => true,
124
+ %(/rdf:RDF/rdf:Seq/rdf:li[@rdf:resource="http://example/second"]) => true
125
+ )
126
+ end
127
+
128
+ it "should serialize rdf:Bag with multiple rdf:li" do
129
+ @graph.add_triple("http://example/seq", RDF_TYPE, RDF_NS.Bag)
130
+ @graph.add_triple("http://example/seq", RDF_NS._2, "http://example/second")
131
+ @graph.add_triple("http://example/seq", RDF_NS._1, "http://example/first")
132
+ check_xpaths(
133
+ serialize(:max_depth => 1, :attributes => :untyped),
134
+ "/rdf:RDF/rdf:Bag/@rdf:about" => "http://example/seq",
135
+ %(/rdf:RDF/rdf:Bag/rdf:li[@rdf:resource="http://example/first"]) => true,
136
+ %(/rdf:RDF/rdf:Bag/rdf:li[@rdf:resource="http://example/second"]) => true
137
+ )
138
+ end
139
+
140
+ it "should serialize rdf:Alt with multiple rdf:li" do
141
+ @graph.add_triple("http://example/seq", RDF_TYPE, RDF_NS.Alt)
142
+ @graph.add_triple("http://example/seq", RDF_NS._2, "http://example/second")
143
+ @graph.add_triple("http://example/seq", RDF_NS._1, "http://example/first")
144
+ check_xpaths(
145
+ serialize(:max_depth => 1, :attributes => :untyped),
146
+ "/rdf:RDF/rdf:Alt/@rdf:about" => "http://example/seq",
147
+ %(/rdf:RDF/rdf:Alt/rdf:li[@rdf:resource="http://example/first"]) => true,
148
+ %(/rdf:RDF/rdf:Alt/rdf:li[@rdf:resource="http://example/second"]) => true
149
+ )
150
+ end
151
+ end
152
+
153
+ describe "with lists" do
154
+ it "should serialize List rdf:first/rdf:rest" do
155
+ @graph.parse(%(@prefix foo: <http://foo/> . foo:author foo:is (Gregg Barnum Kellogg).), "http://foo/", :type => :ttl)
156
+ check_xpaths(
157
+ serialize({}),
158
+ "/rdf:RDF/rdf:Description/@rdf:about" => "http://foo/author",
159
+ "/rdf:RDF/rdf:Description/foo:is/@rdf:parseType" => "Collection",
160
+ %(/rdf:RDF/rdf:Description/foo:is/rdf:Description[@rdf:about="http://foo/#Gregg"]) => true,
161
+ %(/rdf:RDF/rdf:Description/foo:is/rdf:Description[@rdf:about="http://foo/#Barnum"]) => true,
162
+ %(/rdf:RDF/rdf:Description/foo:is/rdf:Description[@rdf:about="http://foo/#Kellogg"]) => true,
163
+ %(//rdf:first) => false
164
+ )
165
+ end
166
+
167
+ it "should serialize resource with multiple rdf:li in proper sequence" do
168
+ @graph.add_triple("http://example/seq", RDF_TYPE, RDF_NS.Seq)
169
+ @graph.add_triple("http://example/seq", RDF_NS._2, "http://example/second")
170
+ @graph.add_triple("http://example/seq", RDF_NS._1, "http://example/first")
171
+ check_xpaths(
172
+ serialize(:max_depth => 1, :attributes => :untyped),
173
+ "/rdf:RDF/rdf:Seq/@rdf:about" => "http://example/seq",
174
+ %(/rdf:RDF/rdf:Seq/rdf:li[@rdf:resource="http://example/first"]) => true,
175
+ %(/rdf:RDF/rdf:Seq/rdf:li[@rdf:resource="http://example/second"]) => true
176
+ )
177
+ end
178
+ end
179
+
180
+ describe "with untyped literals" do
181
+ it "should seralize as element if :attributes == :none" do
182
+ @graph.add_triple("http://release/", DC_NS.title, "foo")
183
+ check_xpaths(
184
+ serialize(:attributes => :none),
185
+ "/rdf:RDF/rdf:Description/@rdf:about" => "http://release/",
186
+ "/rdf:RDF/rdf:Description/dc:title" => "<dc:title>foo</dc:title>"
187
+ )
188
+ end
189
+
190
+ it "should seralize as attribute if :attributes == :untyped or :typed" do
191
+ @graph.add_triple("http://release/", DC_NS.title, "foo")
192
+ check_xpaths(
193
+ serialize(:attributes => :untyped),
194
+ "/rdf:RDF/rdf:Description/@rdf:about" => "http://release/",
195
+ "/rdf:RDF/rdf:Description/@dc:title" => "foo"
196
+ )
197
+ check_xpaths(
198
+ serialize(:attributes => :typed),
199
+ "/rdf:RDF/rdf:Description/@rdf:about" => "http://release/",
200
+ "/rdf:RDF/rdf:Description/@dc:title" => "foo"
201
+ )
202
+ end
203
+
204
+ it "should output untyped without lang as attribute lang set" do
205
+ @graph.add_triple("http://release/", DC_NS.title, "foo")
206
+ check_xpaths(
207
+ serialize(:attributes => :untyped, :lang => "de"),
208
+ "/rdf:RDF/rdf:Description/@rdf:about" => "http://release/",
209
+ "/rdf:RDF/rdf:Description/@dc:title" => "foo"
210
+ )
211
+ end
212
+
213
+ describe "with language" do
214
+ it "should output property for title with language" do
215
+ @graph.add_triple("http://release/", DC_NS.title, Literal.untyped("foo", "en-us"))
216
+ check_xpaths(
217
+ serialize(:attributes => :untyped, :lang => "de"),
218
+ "/rdf:RDF/@xml:lang" => "de",
219
+ "/rdf:RDF/rdf:Description/@rdf:about" => "http://release/",
220
+ "/rdf:RDF/rdf:Description/dc:title" => %(<dc:title xml:lang="en-us">foo</dc:title>)
221
+ )
222
+ end
223
+ end
224
+
225
+ it "should output untyped as attribute if lang is default" do
226
+ @graph.add_triple("http://release/", DC_NS.title, Literal.untyped("foo", "de"))
227
+ check_xpaths(
228
+ serialize(:attributes => :untyped, :lang => "de"),
229
+ "/rdf:RDF/@xml:lang" => "de",
230
+ "/rdf:RDF/rdf:Description/@rdf:about" => "http://release/",
231
+ "/rdf:RDF/rdf:Description/@dc:title" => "foo"
232
+ )
233
+ end
234
+
235
+ it "should output untyped as property if lang set and no default" do
236
+ @graph.add_triple("http://release/", DC_NS.title, Literal.untyped("foo", "de"))
237
+ check_xpaths(
238
+ serialize(:attributes => :untyped),
239
+ "/rdf:RDF/@xml:lang" => false,
240
+ "/rdf:RDF/rdf:Description/@rdf:about" => "http://release/",
241
+ "/rdf:RDF/rdf:Description/dc:title" => %(<dc:title xml:lang="de">foo</dc:title>)
242
+ )
243
+ end
244
+
245
+ it "should output untyped as property if lang set and not default" do
246
+ @graph.add_triple("http://release/", DC_NS.title, Literal.untyped("foo", "de"))
247
+ check_xpaths(
248
+ serialize(:attributes => :untyped, :lang => "en-us"),
249
+ "/rdf:RDF/@xml:lang" => "en-us",
250
+ "/rdf:RDF/rdf:Description/@rdf:about" => "http://release/",
251
+ "/rdf:RDF/rdf:Description/dc:title" => %(<dc:title xml:lang="de">foo</dc:title>)
252
+ )
253
+ end
254
+
255
+ it "should output multiple untyped attributes values through properties" do
256
+ @graph.add_triple("http://release/", DC_NS.title, Literal.untyped("foo", "de"))
257
+ @graph.add_triple("http://release/", DC_NS.title, Literal.untyped("foo", "en-us"))
258
+ check_xpaths(
259
+ serialize(:attributes => :untyped, :lang => "en-us"),
260
+ "/rdf:RDF/@xml:lang" => "en-us",
261
+ "/rdf:RDF/rdf:Description/@rdf:about" => "http://release/",
262
+ "/rdf:RDF/rdf:Description/dc:title[lang('de')]" => %(<dc:title xml:lang="de">foo</dc:title>),
263
+ "/rdf:RDF/rdf:Description/dc:title[lang('en-us')]" => %(<dc:title>foo</dc:title>)
264
+ )
265
+ end
266
+
267
+ it "should output typed node as attribute" do
268
+ @graph.add_triple("http://release/", DC_NS.title, Literal.typed("foo", XSD_NS.string))
269
+ check_xpaths(
270
+ serialize(:attributes => :untyped),
271
+ "/rdf:RDF/rdf:Description/@rdf:about" => "http://release/",
272
+ "/rdf:RDF/rdf:Description/dc:title" => %(<dc:title rdf:datatype="#{XSD_NS.string}">foo</dc:title>)
273
+ )
274
+ check_xpaths(
275
+ serialize(:attributes => :typed),
276
+ "/rdf:RDF/rdf:Description/@rdf:about" => "http://release/",
277
+ "/rdf:RDF/rdf:Description/@dc:title" => "foo"
278
+ )
279
+ end
280
+
281
+ it "should output multiple typed values through properties" do
282
+ @graph.add_triple("http://release/", DC_NS.title, Literal.typed("foo", XSD_NS.string))
283
+ @graph.add_triple("http://release/", DC_NS.title, Literal.typed("bar", XSD_NS.string))
284
+ check_xpaths(
285
+ serialize(:attributes => :untyped),
286
+ "/rdf:RDF/rdf:Description/@rdf:about" => "http://release/",
287
+ "/rdf:RDF/rdf:Description/dc:title[contains(., 'foo')]" => %(<dc:title rdf:datatype="#{XSD_NS.string}">foo</dc:title>),
288
+ "/rdf:RDF/rdf:Description/dc:title[contains(., 'bar')]" => %(<dc:title rdf:datatype="#{XSD_NS.string}">bar</dc:title>)
289
+ )
290
+ end
291
+ end
292
+
293
+ describe "with default namespace" do
294
+ it "should serialize with default namespace" do
295
+ @graph.add_triple("http://release/", RDF_TYPE, FOO_NS.Release)
296
+ @graph.add_triple("http://release/", DC_NS.title, "foo")
297
+ @graph.add_triple("http://release/", FOO_NS.pred, FOO_NS.obj)
298
+ @graph.bind(Namespace.new(FOO_NS.uri, ""))
299
+
300
+ #$DEBUG = true
301
+ xml = serialize(:max_depth => 1, :attributes => :untyped)
302
+ #puts xml
303
+ xml.should =~ /<Release/
304
+ xml.should =~ /<pred/
305
+ doc = Nokogiri::XML.parse(xml)
306
+ doc.at_xpath("/rdf:RDF/#{FOO_NS.prefix}:Release/#{FOO_NS.prefix}:pred/@rdf:resource", doc.namespaces).to_s.should == FOO_NS.obj.to_s
307
+ end
308
+ end
309
+
310
+ describe "with base" do
311
+ it "should generate relative about URI" do
312
+ @graph.add_triple("http://release/a", FOO_NS.ref, "http://release/b")
313
+ check_xpaths(
314
+ serialize(:attributes => :untyped, :base => "http://release/"),
315
+ "/rdf:RDF/rdf:Description/@rdf:about" => "a",
316
+ "/rdf:RDF/rdf:Description/foo:ref/@rdf:resource" => "b"
317
+ )
318
+ end
319
+ end
320
+
321
+ describe "with bnodes" do
322
+ it "should not generate nodeID attribute unless node is referenced as an object" do
323
+ @graph.add_triple(BNode.new("a"), DC_NS.title, "foo")
324
+ check_xpaths(
325
+ serialize(:attributes => :untyped, :base => "http://release/"),
326
+ "/rdf:RDF/rdf:Description/@dc:title" => "foo",
327
+ "/rdf:RDF/rdf:Description/@rdf:nodeID" => false
328
+ )
329
+ end
330
+
331
+ it "should generate a nodeID attribute if node is referenced as an object" do
332
+ bn = BNode.new("a")
333
+ @graph.add_triple(bn, DC_NS.title, "foo")
334
+ @graph.add_triple(bn, OWL_NS.equals, bn)
335
+ check_xpaths(
336
+ serialize(:attributes => :untyped, :base => "http://release/"),
337
+ "/rdf:RDF/rdf:Description/@dc:title" => "foo",
338
+ "/rdf:RDF/rdf:Description/@rdf:nodeID" => /Na$/,
339
+ "/rdf:RDF/rdf:Description/owl:equals/@rdf:nodeID" => /Na$/
340
+ )
341
+ end
342
+ end
343
+
344
+ def check_xpaths(doc, paths)
345
+ puts doc if $DEBUG || $verbose
346
+ doc = Nokogiri::XML.parse(doc)
347
+ #puts "doc: #{doc.to_s}"
348
+ doc.should be_a(Nokogiri::XML::Document)
349
+ paths.each_pair do |path, value|
350
+ puts "xpath: #{path.inspect}" if $DEBUG
351
+ puts doc.root.at_xpath(path, @namespaces).inspect if $DEBUG
352
+ case value
353
+ when false
354
+ doc.root.at_xpath(path, doc.namespaces).should be_nil
355
+ when true
356
+ doc.root.at_xpath(path, doc.namespaces).should_not be_nil
357
+ when Array
358
+ doc.root.at_xpath(path, doc.namespaces).to_s.split(" ").should include(*value)
359
+ when Regexp
360
+ doc.root.at_xpath(path, doc.namespaces).to_s.should =~ value
361
+ else
362
+ doc.root.at_xpath(path, doc.namespaces).to_s.should == value
363
+ end
364
+ end
365
+
366
+ # Parse generated graph and compare to source
367
+ Graph.new.parse(doc, "http://release/", :format => :xml).should
368
+ be_equivalent_graph(@graph, :about => "http://release/")
369
+ end
370
+
371
+ # Serialize ntstr to a string and compare against regexps
372
+ def serialize(options)
373
+ #$DEBUG = true
374
+ result = @graph.serialize(options.merge(:format => :xml))
375
+ #$DEBUG = false
376
+ result
377
+ end
378
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 5
8
- - 0
9
- version: 0.5.0
8
+ - 1
9
+ version: 0.5.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Gregg Kellogg
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-02-22 00:00:00 -08:00
17
+ date: 2010-04-02 00:00:00 -07:00
18
18
  default_executable: rdf_context
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -151,6 +151,11 @@ files:
151
151
  - lib/rdf_context/quoted_graph.rb
152
152
  - lib/rdf_context/rdfaparser.rb
153
153
  - lib/rdf_context/rdfxmlparser.rb
154
+ - lib/rdf_context/serializer/abstract_serializer.rb
155
+ - lib/rdf_context/serializer/nt_serializer.rb
156
+ - lib/rdf_context/serializer/recursive_serializer.rb
157
+ - lib/rdf_context/serializer/turtle_serializer.rb
158
+ - lib/rdf_context/serializer/xml_serializer.rb
154
159
  - lib/rdf_context/store/abstract_sql_store.rb
155
160
  - lib/rdf_context/store/abstract_store.rb
156
161
  - lib/rdf_context/store/list_store.rb
@@ -845,8 +850,10 @@ files:
845
850
  - spec/turtle/test-29.ttl
846
851
  - spec/turtle/test-30.out
847
852
  - spec/turtle/test-30.ttl
853
+ - spec/turtle_serializer_spec.rb
848
854
  - spec/turtle_spec.rb
849
855
  - spec/uriref_spec.rb
856
+ - spec/xml_serializer_spec.rb
850
857
  - test/longtests_spec.rb
851
858
  - test/n3_tests/lcsh/sh85062913.n3
852
859
  - test/n3_tests/lcsh/sh85062913.nt
@@ -935,6 +942,8 @@ test_files:
935
942
  - spec/swap_helper.rb
936
943
  - spec/swap_spec.rb
937
944
  - spec/triple_spec.rb
945
+ - spec/turtle_serializer_spec.rb
938
946
  - spec/turtle_spec.rb
939
947
  - spec/uriref_spec.rb
948
+ - spec/xml_serializer_spec.rb
940
949
  - test/longtests_spec.rb