-
Notifications
You must be signed in to change notification settings - Fork 1
/
odata-client-tests.lisp
220 lines (195 loc) · 6.64 KB
/
odata-client-tests.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
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
(defpackage :odata-client-tests
(:use :cl :stefil :odata-client :odata/lang :arrows :access)
(:export :odata-client-tests))
(in-package :odata-client-tests)
(defparameter +trip-pin-base+ "https://services.odata.org/V4/TripPinServiceRW")
(defparameter +trip-pin-modify+
(multiple-value-bind (response status headers modify-url)
(drakma:http-request +trip-pin-base+)
(quri:uri (princ-to-string modify-url))))
(stefil:in-root-suite)
(stefil:defsuite* odata-client-tests)
(stefil:in-suite odata-client-tests)
(deftest fetch-collection-test ()
(let ((results
(-> +trip-pin-modify+
(collection "People")
(fetch :collection))))
(is (listp results))
(mapcar (lambda (item)
(is (access item :user-name)))
results)))
(deftest fetch-entity-set-test ()
(let ((collection
(-> +trip-pin-modify+
(collection "People")
(fetch t))))
(is (typep collection 'odata/entity:odata-entity-set))
(mapcar (lambda (el)
(is (typep el 'odata/entity:odata-entity))
(is (odata/entity:get-property el :user-name)))
(odata/entity:entity-set-elements collection))))
(deftest fetch-entity-relation-test ()
(let ((entity
(-> +trip-pin-modify+
(collection "People")
(id "russellwhyte")
(fetch t))))
(let ((friends (-> entity
(property "Friends")
(fetch t))))
(is (typep friends 'odata/entity:odata-entity-set))
(is (> (length (odata/entity:entity-set-elements friends)) 0)))))
(deftest fetch-raw-entity-test ()
(let ((entity
(-> +trip-pin-modify+
(collection "People")
(id "russellwhyte")
(fetch))))
(is (not (null entity)))
(is (access entity :user-name))
(is (access entity :first-name))
(is (access entity :emails))))
(deftest fetch-entity-test ()
(let ((entity
(-> +trip-pin-modify+
(collection "People")
(id "russellwhyte")
(fetch t))))
(is (not (null entity)))
(odata/entity:with-properties (user-name first-name emails) entity
(is (and user-name first-name emails)))))
(deftest fetch-property-test ()
(let ((name (-> +trip-pin-modify+
(collection "Airports")
(id "KSFO")
(property "Name")
(fetch :value))))
(is (stringp name))
(is (string= name "San Francisco International Airport"))))
(deftest $filter-test ()
(let ((filtered
(-> +trip-pin-modify+
(collection "People")
($filter "FirstName eq 'Scott'")
(fetch :collection))))
(mapcar (lambda (user)
(is (string= (access user :first-name) "Scott")))
filtered)))
(deftest compiled-$filter-test ()
(let ((filtered
(-> +trip-pin-modify+
(collection "People")
($filter '(:= "FirstName" "Scott"))
(fetch :collection))))
(mapcar (lambda (user)
(is (string= (access user :first-name) "Scott")))
filtered)))
(deftest complex-filter-test ()
(let ((filtered
(-> +trip-pin-modify+
(collection "Airports")
($filter "contains(Location/Address, 'San Francisco')")
(fetch :collection))))
(mapcar (lambda (airport)
(is (search "San Francisco" (accesses airport :location :address)
:test 'string=)))
filtered))
(let ((filtered
(-> +trip-pin-modify+
(collection "Airports")
($filter '(:contains ("Location" "Address") "San Francisco"))
(fetch :collection))))
(mapcar (lambda (airport)
(is (search "San Francisco" (accesses airport :location :address)
:test 'string=)))
filtered))
)
(deftest order-by-test ()
(let ((trips
(-> +trip-pin-modify+
(collection "People")
(id "scottketchum")
(property "Trips")
($orderby "EndsAt" :desc)
(fetch :collection))))
(is (listp trips))
;; Check that the list is ordered
(labels ((zip-lists (l1 l2)
(if (or (null l1)
(null l2))
(return-from zip-lists nil)
(cons (cons l1 l2)
(zip-lists (rest l1) (rest l2))))))
(mapcar (lambda (elem)
(is (equalp (car elem) (cdr elem))))
(zip-lists
(mapcar (lambda (x)
(alexandria:assoc-value x :trip-id))
trips)
(mapcar (lambda (x)
(alexandria:assoc-value x :trip-id))
(sort trips 'local-time:timestamp> :key
(lambda (trip)
(local-time:parse-timestring (access trip :ends-at)))))))))
(let ((trips
(-> +trip-pin-modify+
(collection "People")
(id "scottketchum")
(property "Trips")
($orderby "EndsAt" :asc)
(fetch :collection))))
(is (listp trips))
;; Check that the list is ordered
(labels ((zip-lists (l1 l2)
(if (or (null l1)
(null l2))
(return-from zip-lists nil)
(cons (cons l1 l2)
(zip-lists (rest l1) (rest l2))))))
(mapcar (lambda (elem)
(is (equalp (car elem) (cdr elem))))
(zip-lists
(mapcar (lambda (x)
(alexandria:assoc-value x :trip-id))
trips)
(mapcar (lambda (x)
(alexandria:assoc-value x :trip-id))
(sort trips 'local-time:timestamp< :key
(lambda (trip)
(local-time:parse-timestring (access trip :ends-at)))))))))
)
(deftest $filter-syntax-test ()
(is (string= (odata-client::compile-$filter '(:= "foo" "bar"))
"foo eq 'bar'"))
(is (string= (odata-client::compile-$filter '(:> "foo" "bar"))
"foo gt 'bar'"))
(is (string= (odata-client::compile-$filter '(:and (:= "foo" "bar") (:> "foo" "bar")))
"foo eq 'bar' and foo gt 'bar'"))
(is (string= (odata-client::compile-$filter '(:or (:= "foo" "bar") (:> "foo" "bar")))
"foo eq 'bar' or foo gt 'bar'"))
(is (string= (odata-client::compile-$filter '(:contains ("Location" "Address") "San Francisco" ))
"contains(Location/Address, 'San Francisco')"))
(is (string= (odata-client::compile-$filter '(:not (:contains ("Location" "Address") "San Francisco")))
"not contains(Location/Address, 'San Francisco')"
)))
(deftest $compile-syntax-test ()
(is (string= (odata-client::compile-$expand "asdf")
"asdf"))
(is (string= (odata-client::compile-$expand '("asdf"))
"asdf"))
(is (string= (odata-client::compile-$expand '("asdf" "foo"))
"asdf,foo"))
(is (string= (odata-client::compile-$expand '("asdf" "foo" ("Bar" "Baz")))
"asdf,foo,Bar/Baz")))
(deftest $select-syntax-test ()
(is (string= (odata-client::compile-$select "foo,bar")
"foo,bar"))
(is (odata-client::compile-$select '("foo" "bar"))
"foo,bar"))
(deftest links-test ()
(-> +trip-pin-modify+
(collection "People") (id "scottketchum")
(property "Friends") ($ref)
(link `(("@odata.context" . ,(quri:render-uri +trip-pin-modify+))
("@odata.id" . "People('vincentcalabrese')")))))