-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_support.cljc
257 lines (236 loc) · 9.28 KB
/
test_support.cljc
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
(ns ont-app.rdf.test-support
"These are tests that should be applicable to implementations of the igraph/rdf. query-for-failures should be empty."
{
:voc/mapsTo 'ont-app.rdf.ont
;; ;; this metadata can be used by downstream libraries that rely on RDF URIs...
;; :vann/preferredNamespacePrefix
;; "rdf-test" ;; matches :: declarations directly
;; :vann/preferredNamespaceUri
;; "http://rdf.naturallexicon.org/ont-app/igraph/rdf-test#"
:clj-kondo/config '{:linters {:redundant-let {:level :off}
:unresolved-symbol {:level :off}}}
}
(:require
[clojure.set]
[ont-app.vocabulary.core :as voc]
[ont-app.igraph.core :as igraph :refer [
add
add!
subjects
unique
]]
[ont-app.igraph.graph :as native-normal]
[ont-app.igraph.test-support :as igts]
[ont-app.rdf.core :as rdf]
[ont-app.graph-log.levels :refer [trace]]
))
(def bnode-test-data
"A small turtle file containing blank nodes"
(rdf/cljc-resource "test_support/bnode-test.ttl"))
(defn prepare-report
"Returns a graph initialized for RDFImplementationReport"
[make-graph-fn load-file-fn]
(-> (native-normal/make-graph)
(add [:rdf-app/RDFImplementationReport
:rdf-app/makeGraphFn make-graph-fn
:rdf-app/loadFileFn load-file-fn
])))
(def super-sub-query
"A query to test for sub/super relations"
(voc/prepend-prefix-declarations
"
Select ?sub ?super
Where
{
?super rdf-app:hasSubordinate ?sub.
?super a rdf-app:Test.
?sub a rdf-app:Subordinate.
}
"
))
(defn test-bnode-support
"Side-effects: Updates `report` with results of tests related to blank nodes
Returns: modified `report`
- Where
- `report` is an atom containing a native-normal graph
- (keys (@`report` :rdf-app/RDFImplementationReport)) :~ #{:rdf-app/loadFileFn}
- `loadFileFn` links to fn[file-name] -> RDF IGraph implementation
"
[report]
(let [load-rdf-file (unique (@report :rdf-app/RDFImplementationReport :rdf-app/loadFileFn))
bnode-graph (load-rdf-file bnode-test-data)
assert-and-report! (partial igts/do-assert-and-report! report #'test-bnode-support)
]
(assert-and-report!
:rdf-app/AllSubjectsAreBnodes
"All subjects should match the bnode spec"
(mapv (comp some? rdf/bnode-kwi?) (subjects bnode-graph))
[true true]
)
(assert-and-report!
:rdf-app/BnodeQueryForSuperSub
"Query for super/sub"
(empty? (unique (igraph/query bnode-graph super-sub-query)))
false
)
(assert-and-report!
:rdf-app/BnodeSuperShouldRoundTrip
"Super/sub query returns a round-trippable blank node"
(let [binding (unique (igraph/query bnode-graph super-sub-query))
]
(if binding
(empty? (bnode-graph (:super binding)))
;; else
::no-binding))
false)
(assert-and-report!
:rdf-app/BnodeTraversalsShouldWork
"A call to t-comp should work across blank nodes"
(let [binding (unique (igraph/query bnode-graph super-sub-query))
super (:super binding)
]
(if super
(empty? (bnode-graph super
(igraph/t-comp [:rdf-app/hasSubordinate
:rdf-app/value])))
::no-super))
false)
report
))
(defn test-load-of-web-resource
"Updates and returns a report on tests for loading web resources
- Where
- `report` is an atom containing a native-normal graph
- (keys (@`report` :rdf-app/RDFImplementationReport)) :~ #{:rdf-app/loadFileFn}
- `loadFileFn` links to fn[file-name] -> RDF IGraph implementation
"
[report]
(let [load-rdf-file (unique (@report :rdf-app/RDFImplementationReport
:rdf-app/loadFileFn))
rdfs-graph (load-rdf-file (java.net.URL. "http://www.w3.org/2000/01/rdf-schema#"))
assert-and-report! (partial igts/do-assert-and-report! report #'test-load-of-web-resource)
]
(assert-and-report!
:rdf-app/RdfsSubjectsShouldNotBeEmpty
"Loading a URL for a web resource"
(let [subjects (igraph/subjects rdfs-graph)
]
(empty? subjects))
false))
report)
(defn test-read-rdf-methods
"Updates and returns a report testing read-rdf
- Where
- `report` is an atom containing a native-normal graph
- (keys (@`report` :rdf-app/RDFImplementationReport)) :~ #{:rdf-app/makeGraphFn, :rdf-app/readFileFn}
- `makeGraphFn` := fn [] -> `graph`
- `readFileFn` := fn[`graph` `file-url`] -> graph'
- `file-url` is suitable as the `to-read` argument to rdf/read-rdf
"
[report]
(trace ::starting-read-rdf-methods ::report report)
(let [make-graph (unique (@report
:rdf-app/RDFImplementationReport
:rdf-app/makeGraphFn))
read-rdf-file (unique (@report
:rdf-app/RDFImplementationReport
:rdf-app/readFileFn))
assert-and-report! (partial igts/do-assert-and-report! report #'test-load-of-web-resource)
]
;; Read web resource (rdfs schema)
(let [rdfs-graph (read-rdf-file (make-graph)
(java.net.URL.
"http://www.w3.org/2000/01/rdf-schema#"))
]
(assert-and-report!
:rdf-app/RdfsSubjectsShouldNotBeEmpty
"Loading a URL for a web resource"
(let [subjects (igraph/subjects rdfs-graph)
]
(empty? subjects))
false)))
report)
(defn test-write-rdf-methods
"Updates and returns a report testing write-rdf
- Where
- `report` is an atom containing a native-normal graph
- (keys (@`report` :rdf-app/RDFImplementationReport)) :~ #{:rdf-app/readFileFn, :rdf-app/writeFileFn}
- `readFileFn` := fn[`graph` `file-url`] -> `graph`
- `writeFileFn` := fn[`graph` `output-file`] -> `output-file`
- `file-url` is suitable as the `to-read` argument to rdf/read-rdf
- `output-file` is a path to a file for containing contents of `graph`
"
[report]
(let [load-rdf-file (unique (@report
:rdf-app/RDFImplementationReport
:rdf-app/loadFileFn))
write-rdf-file (unique (@report
:rdf-app/RDFImplementationReport
:rdf-app/writeFileFn))
assert-and-report! (partial igts/do-assert-and-report! report #'test-load-of-web-resource)
]
(let [g (load-rdf-file (java.net.URL. "http://www.w3.org/2000/01/rdf-schema#"))
output (rdf/cljc-make-file "/tmp/test-write-methods.ttl")
]
(write-rdf-file g output)
(assert-and-report!
:rdf-app/WrittenFileShouldExist
"Writing the file should exist"
(rdf/cljc-file-exists? (rdf/cljc-make-file output))
true)
(assert-and-report!
:rdf-app/WrittenFileShouldNotBeEmpty
"Written file should not be empty"
(> (rdf/cljc-file-length (rdf/cljc-make-file output)) 0)
true))
report))
(def transit-test-map
"A map containing clojure constructs serializable under transit"
{:sub-map {:a "this is a string"}
:vector [1 2 3]
:set #{:a :b :c}
:list '(1 2 3)
:lstr #voc/lstr "jail@en-US"
})
(defn test-transit-support
"Side-effects: Updates `report` with results of tests related to transit serialization
Returns: modified `report`
- Where
- `report` is an atom containing a native-normal graph
- (keys (@`report` :rdf-app/RDFImplementationReport)) :~ #{:rdf-app/makeGraphFn, :rdf-app/loadFileFn, :rdf-app/writeFileFn}
- `makeGraphFn` links to fn[] -> empty RDF IGraph implemenation
- `loadFileFn` links to fn[file-name] -> RDF IGraph implementation
- `writeFileFn` links to fn[graph file-name] -> ?, with side-effects that contents
of `graph` are written to a file readable by `loadFileFn`.
"
[report]
(let [make-graph (unique (@report :rdf-app/RDFImplementationReport :rdf-app/makeGraphFn))
load-rdf-file (unique (@report :rdf-app/RDFImplementationReport :rdf-app/loadFileFn))
write-rdf-file (unique (@report :rdf-app/RDFImplementationReport :rdf-app/writeFileFn))
assert-and-report! (partial igts/do-assert-and-report!
report
#'test-transit-support)
]
(assert-and-report!
:rdf-app/TransitDataShouldRoundTrip
"Writing a graph with transit-test-map to a test file and reading back in"
(let [g (make-graph)
temp-file (rdf/cljc-create-temp-file "test-transit-support" ".ttl")
]
(add! g [:rdf-app/TransitTestMap
:rdf-app/hasMap transit-test-map])
(write-rdf-file g temp-file)
(let [g' (load-rdf-file temp-file)]
(= (unique (g' :rdf-app/TransitTestMap
:rdf-app/hasMap))
transit-test-map)))
true)
report))
;;;;;;;;;;;;;
;; UTILIITES
;;;;;;;;;;;;;
(comment
(defn trace-taps
[]
(add-tap (fn [tap] (trace (or (:type tap) ::unclassified-tap) :tap tap))))
) ;;/comment