-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.lisp
103 lines (84 loc) · 3.41 KB
/
test.lisp
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
(defpackage :cl-sparql-test
(:use :cl :sparql :fiveam)
(:export #:run-tests))
(in-package :cl-sparql-test)
#.(sparql:enable-uri-syntax)
(defun run-tests ()
(run! 'cl-sparql-test))
(def-suite cl-sparql-test)
(in-suite cl-sparql-test)
(test parse-literal-test
(is (equalp (parse-literal "2") 2))
(is (equalp (parse-literal "\"2\"^^<http://www.w3.org/2001/XMLSchema#integer>") 2))
(is (equalp (parse-literal "\"hello\"") "hello"))
(is (equalp (parse-literal "\"hello\"^^<http://www.w3.org/2001/XMLSchema#string>") "hello"))
(is (puri:uri= (parse-literal "<http://bonanza.cl.no>") (puri:parse-uri "http://bonanza.cl.no")))
(is (equalp (parse-literal "\"true\"^^<http://www.w3.org/2001/XMLSchema#boolean>") t))
(is (equalp (parse-literal "\"false\"^^<http://www.w3.org/2001/XMLSchema#boolean>") nil))
(signals error
(parse-literal "\"hello\"^^<http://www.w3.org/2001/XMLSchema#integer>")))
(defun remove-spaces (string)
(with-input-from-string (stream string)
(loop
with new-string
for char = (read-char stream nil nil)
while char
when (not (equalp char #\ ))
do (push char new-string)
finally (return (coerce (nreverse new-string) 'string)))))
(defun sparql-str-equalp (str1 str2)
(string-equal (remove-spaces str1)
(remove-spaces str2)))
(test sparql-syntax-test
;; Basic tests
(is (sparql-str-equalp
(sparql (:select * :where (?x ?y ?z)))
"SELECT * WHERE { ?X ?Y ?Z}"))
(is (sparql-str-equalp
(sparql (:select ?x :where (?x ?y ?z)))
"SELECT ?x WHERE { ?X ?Y ?Z}"))
;; Literals
(is (sparql-str-equalp
(sparql (:select * :where (?x ?y ?z)
(?x #u<rdf:type> "MyType")))
"SELECT * WHERE { ?X ?Y ?Z . ?X <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> \"MyType\"}"))
;; Select options
(is (sparql-str-equalp
(sparql (:select *
:where (?x ?y ?z)
:order-by ?z))
"SELECT * WHERE { ?X ?Y ?Z} ORDER BY ASC(?Z)"))
(is (sparql-str-equalp
(sparql (:select *
:where (?x ?y ?z)
:limit 20
:offset 10))
"SELECT * WHERE { ?X ?Y ?Z} LIMIT 20 OFFSET 10"))
;; Subqueries
(is (sparql-str-equalp
(sparql (:select *
:where (?x ?y ?z)
(:select *
:where (?x ?y ?z)
:limit 20
:offset 10)
:limit 20
:offset 10))
"SELECT * WHERE { ?X ?Y ?Z . { SELECT * WHERE { ?X ?Y ?Z} LIMIT 20 OFFSET 10 } } LIMIT 20 OFFSET 10"))
;; Variable capture
(is (sparql-str-equalp
(let ((value nil))
(sparql (:select * :where (?x ?y value))))
"SELECT * WHERE { ?X ?Y \"false\"^^<http://www.w3.org/2001/XMLSchema#boolean>}"))
(is (sparql-str-equalp
(let ((value "hello"))
(sparql (:select * :where (?x ?y value))))
"SELECT * WHERE { ?X ?Y \"hello\"}"))
;; SPARQL evaluation
(is (sparql-str-equalp
(let ((order-by (list :order-by '?timestamp))
(value "Foo"))
(sparql-eval `(:select * :where
(?x #u<rdfs:label> ,value)
,@order-by)))
"SELECT * WHERE { ?X <http://www.w3.org/2000/01/rdf-schema#label> \"Foo\"} ORDER BY ASC(?TIMESTAMP)")))