File: test-parse-serialize.R

package info (click to toggle)
r-cran-rdflib 0.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 572 kB
  • sloc: xml: 66; sh: 13; makefile: 2
file content (228 lines) | stat: -rw-r--r-- 6,103 bytes parent folder | download | duplicates (3)
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
testthat::context("Parsers and Serializers")

doc <- system.file("extdata/example.rdf", package="redland")
out <- "testing.rdf"



testthat::test_that("we can serialize to character", {
                      rdf <- rdf_parse(doc) 
                      txt <- rdf_serialize(rdf, format = "nquads")
                      testthat::expect_is(txt, "character")
                      testthat::expect_match(txt, "John Smith")
                      rdf_free(rdf)
                      })


testthat::test_that("we can parse (in rdfxml)
                    and serialize (in nquads) a simple rdf graph", {
                      rdf <- rdf_parse(doc) 
                      rdf_serialize(rdf, out, "nquads")
                      roundtrip <- rdf_parse(out, "nquads")
                      testthat::expect_is(roundtrip, "rdf")
                      
                      rdf_free(roundtrip)
                      rdf_free(rdf)
                      })

testthat::test_that("we can add a namespace on serializing", {
  rdf <- rdf_parse(doc)
  rdf_serialize(rdf,
                out,
                namespace = "http://purl.org/dc/elements/1.1/",
                prefix = "dc")
  roundtrip <- rdf_parse(doc)
  testthat::expect_is(roundtrip, "rdf")
  
  rdf_free(rdf)
  rdf_free(roundtrip)
  
})

################################################################


testthat::test_that("we can parse and serialize json-ld", {
  rdf <- rdf_parse(doc)
  rdf_serialize(rdf, "out.json")
  roundtrip <- rdf_parse("out.json")
  testthat::expect_is(roundtrip, "rdf")
  rdf_serialize(rdf, "out.jsonld")
  unlink("out.json")
  unlink("out.jsonld")
  rdf_free(roundtrip)
  rdf_free(rdf)
  
})

testthat::test_that("we can parse and serialize nquads", {
  rdf <- rdf_parse(doc)
  rdf_serialize(rdf, "out.nquads")
  roundtrip <- rdf_parse("out.nquads")
  testthat::expect_is(roundtrip, "rdf")
  unlink("nquads")
  rdf_free(roundtrip)
  rdf_free(rdf)
  
})
testthat::test_that("we can parse and serialize ntriples", {
  rdf <- rdf_parse(doc)
  rdf_serialize(rdf, "out.nt")
  roundtrip <- rdf_parse("out.nt")
  testthat::expect_is(roundtrip, "rdf")
  unlink("out.nt")
  rdf_serialize(rdf, "out.ntriples")
  unlink("out.ntriples")

  rdf_free(roundtrip)
  rdf_free(rdf)
})
testthat::test_that("we can parse and serialize tutle", {
  rdf <- rdf_parse(doc)
  rdf_serialize(rdf, "out.ttl")
  roundtrip <- rdf_parse("out.ttl")
  testthat::expect_is(roundtrip, "rdf")
  unlink("out.ttl")
  rdf_serialize(rdf, "out.turtle")
  unlink("out.turtle")
  
  rdf_free(roundtrip)
  rdf_free(rdf)
})
testthat::test_that("we can parse and serialize rdfxml", {
  rdf <- rdf_parse(doc)
  rdf_serialize(rdf, "out.rdf")
  roundtrip <- rdf_parse("out.rdf")
  testthat::expect_is(roundtrip, "rdf")

  unlink("out.rdf")
  rdf_serialize(rdf, "out.xml")
  unlink("out.xml")
  
  rdf_free(roundtrip)
  rdf_free(rdf)
})

################################################################


testthat::test_that("we can parse by guessing on the file extension", {
  ex <- system.file("extdata/person.nq", package="rdflib")
  rdf <- rdf_parse(ex)
  rdf_serialize(rdf, "tmp.nq", base = "http://schema.org/")
  roundtrip <- rdf_parse("tmp.nq", "turtle")
  testthat::expect_is(roundtrip, "rdf")
  unlink("tmp.nq")
  rdf_free(rdf)
})


testthat::test_that("we can serialize turtle with a baseUri", {
  ex <- system.file("extdata/person.nq", package="rdflib")
  rdf <- rdf_parse(ex, "nquads")
  rdf_serialize(rdf, out, "turtle", base = "http://schema.org/")
  roundtrip <- rdf_parse(out, "turtle")
  testthat::expect_is(roundtrip, "rdf")
  rdf_free(rdf)
})


## JSON-LD tests with default base uri

  testthat::test_that("@id is not a URI, we should get localhost", {
    ex <- '{
      "@context": "http://schema.org/",
      "@id": "person_id",
      "name": "Jane Doe"
    }'
    
    rdf <- rdf_parse(ex, "jsonld")
    testthat::expect_output(cat(format(rdf, "nquads")), "localhost")
    rdf_free(rdf)
  })

  testthat::test_that("@id is a URI, we should not get localhost", {
    ex <- '{
      "@context": "http://schema.org/",
      "@id": "uri:person_id",
      "name": "Jane Doe"
    }'
    rdf <- rdf_parse(ex, "jsonld")
    testthat::expect_false(grepl("localhost", format(rdf, "nquads")))
    rdf_free(rdf)
  })  
  
  testthat::test_that("we can alter the base URI", {
    ex <- '{
      "@id": "person_id",
      "schema:name": "Jane Doe"
    }'
    options(rdf_base_uri = "http://example.com/")
    rdf <- rdf_parse(ex, "jsonld")
    testthat::expect_output(cat(format(rdf, "nquads")), "http://example.com")
    rdf_free(rdf)
    
    
    options(rdf_base_uri = "")
    rdf <- rdf_parse(ex, "jsonld")
    testthat::expect_length(rdf, 0)
    rdf_free(rdf)
    
    options(rdf_base_uri = NULL)
  })
  
  
  
  

  
  
testthat::test_that("we can parse into an existing rdf model", {
    rdf1 <- rdf_parse(system.file("extdata/ex.xml", package = "rdflib"))
    rdf2 <- rdf_parse(system.file("extdata/ex2.xml", package = "rdflib"),
                      rdf = rdf1)
    
    testthat::expect_is(rdf1, "rdf")
    testthat::expect_is(rdf2, "rdf")
    testthat::expect_identical(rdf1, rdf2)
    rdf_free(rdf1)
    
    ## NOTE: rdf is same pointer as rdf1, not a new pointer.  cannot free twice
})
  
  

testthat::test_that("we can parse from a url", {
  # CRAN seems okay with tests requiring an internet connection
  #testthat::skip_on_cran()
  rdf <- rdf_parse("https://tinyurl.com/ycf95c9h")
  testthat::expect_is(rdf, "rdf")
  
  rdf_free(rdf)
})

testthat::test_that("we can parse from a text string", {
  
  
  rdf <- rdf_parse(doc)
  txt <- rdf_serialize(rdf, format = "rdfxml")
  testthat::expect_is(txt, "character")
  roundtrip <- rdf_parse(txt, format="rdfxml")
  testthat::expect_is(roundtrip, "rdf")
  rdf_free(rdf)
  
  string <-
    '
  _:b0 <http://schema.org/jobTitle> "Professor" .
  _:b0 <http://schema.org/name> "Jane Doe" .
  _:b0 <http://schema.org/age> "35" .
  _:b0 <http://schema.org/url> <http://www.janedoe.com> .
  '
  rdf <- rdf_parse(string, "nquads")
  testthat::expect_is(rdf, "rdf")
  
  rdf_free(rdf)
})

unlink(out)