1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; ======================================================================
3 ;;;; File: test-fdml.lisp
4 ;;;; Author: Marcus Pearce <m.t.pearce@city.ac.uk>
5 ;;;; Created: 30/03/2004
7 ;;;; ======================================================================
9 ;;;; Description ==========================================================
10 ;;;; ======================================================================
12 ;;;; Tests for the CLSQL Functional Data Manipulation Language
15 ;;;; ======================================================================
17 (in-package #:clsql-tests)
19 #.(clsql:locally-enable-sql-reader-syntax)
21 ;; inserts a record using all values only and then deletes it
22 (deftest :fdml/insert/1
24 (clsql:insert-records :into [employee]
25 :values `(11 1 "Yuri" "Gagarin" "gagarin@soviet.org"
26 1 1 1.85 t ,(clsql-base:get-time)))
28 (clsql:select [first-name] [last-name] [email]
29 :from [employee] :where [= [emplid] 11])
30 (progn (clsql:delete-records :from [employee] :where [= [emplid] 11])
31 (clsql:select [*] :from [employee] :where [= [emplid] 11]))))
32 (("Yuri" "Gagarin" "gagarin@soviet.org")) nil)
34 ;; inserts a record using attributes and values and then deletes it
35 (deftest :fdml/insert/2
37 (clsql:insert-records :into [employee]
38 :attributes '(emplid groupid first_name last_name
39 email companyid managerid)
40 :values '(11 1 "Yuri" "Gagarin" "gagarin@soviet.org"
43 (clsql:select [first-name] [last-name] [email] :from [employee]
44 :where [= [emplid] 11])
45 (progn (clsql:delete-records :from [employee] :where [= [emplid] 11])
46 (clsql:select [*] :from [employee] :where [= [emplid] 11]))))
47 (("Yuri" "Gagarin" "gagarin@soviet.org")) nil)
49 ;; inserts a record using av-pairs and then deletes it
50 (deftest :fdml/insert/3
52 (clsql:insert-records :into [employee]
53 :av-pairs'((emplid 11) (groupid 1)
56 (email "gagarin@soviet.org")
57 (companyid 1) (managerid 1)))
59 (clsql:select [first-name] [last-name] [email] :from [employee]
60 :where [= [emplid] 11])
61 (progn (clsql:delete-records :from [employee] :where [= [emplid] 11])
62 (clsql:select [first-name] [last-name] [email] :from [employee]
63 :where [= [emplid] 11]))))
64 (("Yuri" "Gagarin" "gagarin@soviet.org")) nil)
66 ;; inserts a records using a query from another table
67 (deftest :fdml/insert/4
69 (clsql:create-table [employee2] '(([forename] string)
72 (clsql:insert-records :into [employee2]
73 :query [select [first-name] [last-name] [email]
75 :attributes '(forename surname email))
77 (equal (clsql:select [*] :from [employee2])
78 (clsql:select [first-name] [last-name] [email]
80 (clsql:drop-table [employee2] :if-does-not-exist :ignore)))
83 ;; updates a record using attributes and values and then deletes it
84 (deftest :fdml/update/1
86 (clsql:update-records [employee]
87 :attributes '(first_name last_name email)
88 :values '("Yuri" "Gagarin" "gagarin@soviet.org")
89 :where [= [emplid] 1])
91 (clsql:select [first-name] [last-name] [email] :from [employee]
92 :where [= [emplid] 1])
94 (clsql:update-records [employee]
95 :av-pairs'((first_name "Vladamir")
97 (email "lenin@soviet.org"))
98 :where [= [emplid] 1])
99 (clsql:select [first-name] [last-name] [email] :from [employee]
100 :where [= [emplid] 1]))))
101 (("Yuri" "Gagarin" "gagarin@soviet.org"))
102 (("Vladamir" "Lenin" "lenin@soviet.org")))
104 ;; updates a record using av-pairs and then deletes it
105 (deftest :fdml/update/2
107 (clsql:update-records [employee]
108 :av-pairs'((first_name "Yuri")
109 (last_name "Gagarin")
110 (email "gagarin@soviet.org"))
111 :where [= [emplid] 1])
113 (clsql:select [first-name] [last-name] [email] :from [employee]
114 :where [= [emplid] 1])
116 (clsql:update-records [employee]
117 :av-pairs'((first_name "Vladamir")
119 (email "lenin@soviet.org"))
120 :where [= [emplid] 1])
121 (clsql:select [first-name] [last-name] [email]
122 :from [employee] :where [= [emplid] 1]))))
123 (("Yuri" "Gagarin" "gagarin@soviet.org"))
124 (("Vladamir" "Lenin" "lenin@soviet.org")))
127 (deftest :fdml/query/1
128 (clsql:query "SELECT COUNT(*) FROM EMPLOYEE WHERE (EMAIL LIKE '%org')")
131 (deftest :fdml/query/2
133 "SELECT FIRST_NAME,LAST_NAME FROM EMPLOYEE WHERE (EMPLID <= 5) ORDER BY LAST_NAME")
134 (("Leonid" "Brezhnev") ("Nikita" "Kruschev") ("Vladamir" "Lenin")
135 ("Josef" "Stalin") ("Leon" "Trotsky")))
138 (deftest :fdml/execute-command/1
140 (clsql:table-exists-p [foo] :owner *test-database-user*)
142 (clsql:execute-command "create table foo (bar integer)")
143 (clsql:table-exists-p [foo] :owner *test-database-user*))
145 (clsql:execute-command "drop table foo")
146 (clsql:table-exists-p [foo] :owner *test-database-user*)))
150 ;; compare min, max and average hieghts in inches (they're quite short
151 ;; these guys!) -- only works with pgsql
152 (deftest :fdml/select/1
153 (if (member *test-database-type* '(:postgresql-socket :postgresql))
154 (let ((max (clsql:select [function "floor"
155 [/ [* [max [height]] 100] 2.54]]
158 (min (clsql:select [function "floor"
159 [/ [* [min [height]] 100] 2.54]]
162 (avg (clsql:select [function "floor"
163 [avg [/ [* [height] 100] 2.54]]]
166 (apply #'< (mapcar #'parse-integer (append min avg max))))
170 (deftest :fdml/select/2
171 (clsql:select [first-name] :from [employee] :flatp t :distinct t
172 :order-by [first-name])
173 ("Boris" "Josef" "Konstantin" "Leon" "Leonid" "Mikhail" "Nikita" "Vladamir"
176 (deftest :fdml/select/3
177 (clsql:select [first-name] [count [*]] :from [employee]
178 :group-by [first-name]
179 :order-by [first-name])
180 (("Boris" "1") ("Josef" "1") ("Konstantin" "1") ("Leon" "1") ("Leonid" "1")
181 ("Mikhail" "1") ("Nikita" "1") ("Vladamir" "2") ("Yuri" "1")))
183 (deftest :fdml/select/4
184 (clsql:select [last-name] :from [employee] :where [like [email] "%org"]
185 :order-by [last-name]
187 ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
188 "Stalin" "Trotsky" "Yeltsin"))
190 (deftest :fdml/select/5
191 (clsql:select [email] :from [employee] :flatp t
192 :where [in [employee emplid]
193 [select [managerid] :from [employee]]])
194 ("lenin@soviet.org"))
196 (deftest :fdml/select/6
197 (if (member *test-database-type* '(:postgresql-socket :postgresql))
198 (mapcar #'parse-integer
199 (clsql:select [function "trunc" [height]] :from [employee]
201 (mapcar #'(lambda (s) (truncate (parse-integer s :junk-allowed t)))
202 (clsql:select [height] :from [employee] :flatp t)))
203 (1 1 1 1 1 1 1 1 1 1))
205 (deftest :fdml/select/7
206 (sql:select [max [emplid]] :from [employee] :flatp t)
209 (deftest :fdml/select/8
210 (sql:select [min [emplid]] :from [employee] :flatp t)
213 (deftest :fdml/select/9
214 (subseq (car (sql:select [avg [emplid]] :from [employee] :flatp t)) 0 3)
217 (deftest :fdml/select/10
218 (sql:select [last-name] :from [employee]
219 :where [not [in [emplid]
220 [select [managerid] :from [company]]]]
222 :order-by [last-name])
223 ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Putin" "Stalin"
224 "Trotsky" "Yeltsin"))
226 (deftest :fdml/select/11
227 (clsql:select [last-name] :from [employee] :where [married] :flatp t
229 ("Lenin" "Stalin" "Trotsky"))
231 (deftest :fdml/select/12
233 (clsql:select [last-name] :from [employee] :where [= [emplid] v]))
236 ;(deftest :fdml/select/11
237 ; (sql:select [emplid] :from [employee]
238 ; :where [= [emplid] [any [select [companyid] :from [company]]]]
242 (deftest :fdml/do-query/1
244 (clsql:do-query ((name) [select [last-name] :from [employee]
245 :order-by [last-name]])
248 ("Yeltsin" "Trotsky" "Stalin" "Putin" "Lenin" "Kruschev" "Gorbachev"
249 "Chernenko" "Brezhnev" "Andropov"))
251 (deftest :fdml/map-query/1
252 (clsql:map-query 'list #'identity
253 [select [last-name] :from [employee] :flatp t
254 :order-by [last-name]])
255 ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
256 "Stalin" "Trotsky" "Yeltsin"))
258 (deftest :fdml/map-query/2
259 (clsql:map-query 'vector #'identity
260 [select [last-name] :from [employee] :flatp t
261 :order-by [last-name]])
262 #("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
263 "Stalin" "Trotsky" "Yeltsin"))
265 (deftest :fdml/loop/1
266 (loop for (forename surname)
268 [select [first-name] [last-name] :from [employee] :order-by [last-name]]
269 collect (concatenate 'string forename " " surname))
270 ("Yuri Andropov" "Leonid Brezhnev" "Konstantin Chernenko" "Mikhail Gorbachev"
271 "Nikita Kruschev" "Vladamir Lenin" "Vladamir Putin"
272 "Josef Stalin" "Leon Trotsky" "Boris Yeltsin"))
274 ;; starts a transaction deletes a record and then rolls back the deletion
275 (deftest :fdml/transaction/1
277 ;; test if we are in a transaction
278 (push (clsql:in-transaction-p) results)
279 ;;start a transaction
280 (clsql:start-transaction)
281 ;; test if we are in a transaction
282 (push (clsql:in-transaction-p) results)
283 ;;Putin has got to go
284 (unless (eql *test-database-type* :mysql)
285 (clsql:delete-records :from [employee] :where [= [last-name] "Putin"]))
288 (clsql:select [*] :from [employee] :where [= [last-name] "Putin"])
290 ;;Oh no, he's still there
292 ;; test that we are out of the transaction
293 (push (clsql:in-transaction-p) results)
294 ;; Check that we got him back alright
295 (push (clsql:select [email] :from [employee] :where [= [last-name] "Putin"]
298 (apply #'values (nreverse results)))
299 nil t nil nil ("putin@soviet.org"))
301 ;; starts a transaction, updates a record and then rolls back the update
302 (deftest :fdml/transaction/2
304 ;; test if we are in a transaction
305 (push (clsql:in-transaction-p) results)
306 ;;start a transaction
307 (clsql:start-transaction)
308 ;; test if we are in a transaction
309 (push (clsql:in-transaction-p) results)
310 ;;Putin has got to go
311 (unless (eql *test-database-type* :mysql)
312 (clsql:update-records [employee]
313 :av-pairs '((email "putin-nospam@soviet.org"))
314 :where [= [last-name] "Putin"]))
315 ;;Should be new value
316 (push (clsql:select [email] :from [employee]
317 :where [= [last-name] "Putin"]
320 ;;Oh no, he's still there
322 ;; test that we are out of the transaction
323 (push (clsql:in-transaction-p) results)
324 ;; Check that we got him back alright
325 (push (clsql:select [email] :from [employee] :where [= [last-name] "Putin"]
328 (apply #'values (nreverse results)))
329 nil t ("putin-nospam@soviet.org") nil ("putin@soviet.org"))
331 ;; runs an update within a transaction and checks it is committed
332 (deftest :fdml/transaction/3
335 (push (clsql:in-transaction-p) results)
338 (clsql:with-transaction ()
339 (clsql:update-records [employee]
340 :av-pairs '((email "lenin-nospam@soviet.org"))
341 :where [= [emplid] 1]))
344 (push (clsql:in-transaction-p) results)
345 ;; check that was committed
346 (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
351 (clsql:with-transaction ()
352 (clsql:update-records [employee]
353 :av-pairs '((email "lenin@soviet.org"))
354 :where [= [emplid] 1]))
357 (push (clsql:in-transaction-p) results)
358 ;; check that was committed
359 (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
362 (apply #'values (nreverse results)))
363 nil :COMMITTED nil ("lenin-nospam@soviet.org") :COMMITTED
364 nil ("lenin@soviet.org"))
366 ;; runs a valid update and an invalid one within a transaction and checks
367 ;; that the valid update is rolled back when the invalid one fails.
368 (deftest :fdml/transaction/4
371 (push (clsql:in-transaction-p) results)
372 (unless (eql *test-database-type* :mysql)
374 (clsql:with-transaction ()
376 (clsql:update-records [employee]
377 :av-pairs '((email "lenin-nospam@soviet.org"))
378 :where [= [emplid] 1])
379 ;; invalid update which generates an error
380 (clsql:update-records [employee]
382 '((emale "lenin-nospam@soviet.org"))
383 :where [= [emplid] 1]))
384 (clsql:clsql-sql-error ()
387 (push (clsql:in-transaction-p) results)
388 ;; and check nothing done
389 (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
392 (apply #'values (nreverse results)))))))
393 nil nil ("lenin@soviet.org"))
395 #.(clsql:restore-sql-reader-syntax-state)