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
8 ;;;; Tests for the CLSQL Functional Data Manipulation Language
11 ;;;; This file is part of CLSQL.
13 ;;;; CLSQL users are granted the rights to distribute and use this software
14 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
15 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
16 ;;;; ======================================================================
18 (in-package #:clsql-tests)
20 #.(clsql:locally-enable-sql-reader-syntax)
25 ;; inserts a record using all values only and then deletes it
26 (deftest :fdml/insert/1
28 (clsql:insert-records :into [employee]
29 :values `(11 1 "Yuri" "Gagarin" "gagarin@soviet.org"
30 1 1 1.85 t ,(clsql-base:get-time)))
32 (clsql:select [first-name] [last-name] [email]
33 :from [employee] :where [= [emplid] 11])
34 (progn (clsql:delete-records :from [employee] :where [= [emplid] 11])
35 (clsql:select [*] :from [employee] :where [= [emplid] 11]))))
36 (("Yuri" "Gagarin" "gagarin@soviet.org")) nil)
38 ;; inserts a record using attributes and values and then deletes it
39 (deftest :fdml/insert/2
41 (clsql:insert-records :into [employee]
42 :attributes '(emplid groupid first_name last_name
43 email companyid managerid)
44 :values '(11 1 "Yuri" "Gagarin" "gagarin@soviet.org"
47 (clsql:select [first-name] [last-name] [email] :from [employee]
48 :where [= [emplid] 11])
49 (progn (clsql:delete-records :from [employee] :where [= [emplid] 11])
50 (clsql:select [*] :from [employee] :where [= [emplid] 11]))))
51 (("Yuri" "Gagarin" "gagarin@soviet.org")) nil)
53 ;; inserts a record using av-pairs and then deletes it
54 (deftest :fdml/insert/3
56 (clsql:insert-records :into [employee]
57 :av-pairs'((emplid 11) (groupid 1)
60 (email "gagarin@soviet.org")
61 (companyid 1) (managerid 1)))
63 (clsql:select [first-name] [last-name] [email] :from [employee]
64 :where [= [emplid] 11])
65 (progn (clsql:delete-records :from [employee] :where [= [emplid] 11])
66 (clsql:select [first-name] [last-name] [email] :from [employee]
67 :where [= [emplid] 11]))))
68 (("Yuri" "Gagarin" "gagarin@soviet.org")) nil)
70 ;; inserts a records using a query from another table
71 (deftest :fdml/insert/4
73 (clsql:create-table [employee2] '(([forename] string)
76 (clsql:insert-records :into [employee2]
77 :query [select [first-name] [last-name] [email]
79 :attributes '(forename surname email))
81 (equal (clsql:select [*] :from [employee2])
82 (clsql:select [first-name] [last-name] [email]
84 (clsql:drop-table [employee2] :if-does-not-exist :ignore)))
87 ;; updates a record using attributes and values and then deletes it
88 (deftest :fdml/update/1
90 (clsql:update-records [employee]
91 :attributes '(first_name last_name email)
92 :values '("Yuri" "Gagarin" "gagarin@soviet.org")
93 :where [= [emplid] 1])
95 (clsql:select [first-name] [last-name] [email] :from [employee]
96 :where [= [emplid] 1])
98 (clsql:update-records [employee]
99 :av-pairs'((first_name "Vladamir")
101 (email "lenin@soviet.org"))
102 :where [= [emplid] 1])
103 (clsql:select [first-name] [last-name] [email] :from [employee]
104 :where [= [emplid] 1]))))
105 (("Yuri" "Gagarin" "gagarin@soviet.org"))
106 (("Vladamir" "Lenin" "lenin@soviet.org")))
108 ;; updates a record using av-pairs and then deletes it
109 (deftest :fdml/update/2
111 (clsql:update-records [employee]
112 :av-pairs'((first_name "Yuri")
113 (last_name "Gagarin")
114 (email "gagarin@soviet.org"))
115 :where [= [emplid] 1])
117 (clsql:select [first-name] [last-name] [email] :from [employee]
118 :where [= [emplid] 1])
120 (clsql:update-records [employee]
121 :av-pairs'((first_name "Vladamir")
123 (email "lenin@soviet.org"))
124 :where [= [emplid] 1])
125 (clsql:select [first-name] [last-name] [email]
126 :from [employee] :where [= [emplid] 1]))))
127 (("Yuri" "Gagarin" "gagarin@soviet.org"))
128 (("Vladamir" "Lenin" "lenin@soviet.org")))
131 (deftest :fdml/query/1
132 (clsql:query "SELECT COUNT(*) FROM EMPLOYEE WHERE (EMAIL LIKE '%org')")
135 (deftest :fdml/query/2
137 "SELECT FIRST_NAME,LAST_NAME FROM EMPLOYEE WHERE (EMPLID <= 5) ORDER BY LAST_NAME")
138 (("Leonid" "Brezhnev") ("Nikita" "Kruschev") ("Vladamir" "Lenin")
139 ("Josef" "Stalin") ("Leon" "Trotsky")))
142 (deftest :fdml/execute-command/1
144 (clsql:table-exists-p [foo] :owner *test-database-user*)
146 (clsql:execute-command "create table foo (bar integer)")
147 (clsql:table-exists-p [foo] :owner *test-database-user*))
149 (clsql:execute-command "drop table foo")
150 (clsql:table-exists-p [foo] :owner *test-database-user*)))
154 ;; compare min, max and average hieghts in inches (they're quite short
155 ;; these guys!) -- only works with pgsql
156 (deftest :fdml/select/1
157 (if (member *test-database-type* '(:postgresql-socket :postgresql))
158 (let ((max (clsql:select [function "floor"
159 [/ [* [max [height]] 100] 2.54]]
162 (min (clsql:select [function "floor"
163 [/ [* [min [height]] 100] 2.54]]
166 (avg (clsql:select [function "floor"
167 [avg [/ [* [height] 100] 2.54]]]
170 (apply #'< (mapcar #'parse-integer (append min avg max))))
174 (deftest :fdml/select/2
175 (clsql:select [first-name] :from [employee] :flatp t :distinct t
176 :order-by [first-name])
177 ("Boris" "Josef" "Konstantin" "Leon" "Leonid" "Mikhail" "Nikita" "Vladamir"
180 (deftest :fdml/select/3
181 (clsql:select [first-name] [count [*]] :from [employee]
182 :group-by [first-name]
183 :order-by [first-name])
184 (("Boris" "1") ("Josef" "1") ("Konstantin" "1") ("Leon" "1") ("Leonid" "1")
185 ("Mikhail" "1") ("Nikita" "1") ("Vladamir" "2") ("Yuri" "1")))
187 (deftest :fdml/select/4
188 (clsql:select [last-name] :from [employee] :where [like [email] "%org"]
189 :order-by [last-name]
191 ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
192 "Stalin" "Trotsky" "Yeltsin"))
194 (deftest :fdml/select/5
195 (clsql:select [email] :from [employee] :flatp t
196 :where [in [employee emplid]
197 [select [managerid] :from [employee]]])
198 ("lenin@soviet.org"))
200 (deftest :fdml/select/6
201 (if (member *test-database-type* '(:postgresql-socket :postgresql))
202 (mapcar #'parse-integer
203 (clsql:select [function "trunc" [height]] :from [employee]
205 (mapcar #'(lambda (s) (truncate (parse-integer s :junk-allowed t)))
206 (clsql:select [height] :from [employee] :flatp t)))
207 (1 1 1 1 1 1 1 1 1 1))
209 (deftest :fdml/select/7
210 (clsql:select [max [emplid]] :from [employee] :flatp t)
213 (deftest :fdml/select/8
214 (clsql:select [min [emplid]] :from [employee] :flatp t)
217 (deftest :fdml/select/9
218 (subseq (car (clsql:select [avg [emplid]] :from [employee] :flatp t)) 0 3)
221 (deftest :fdml/select/10
222 (clsql:select [last-name] :from [employee]
223 :where [not [in [emplid]
224 [select [managerid] :from [company]]]]
226 :order-by [last-name])
227 ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Putin" "Stalin"
228 "Trotsky" "Yeltsin"))
230 (deftest :fdml/select/11
231 (clsql:select [last-name] :from [employee] :where [married] :flatp t
233 ("Lenin" "Stalin" "Trotsky"))
235 (deftest :fdml/select/12
237 (clsql:select [last-name] :from [employee] :where [= [emplid] v]))
240 ;(deftest :fdml/select/11
241 ; (clsql:select [emplid] :from [employee]
242 ; :where [= [emplid] [any [select [companyid] :from [company]]]]
246 (deftest :fdml/do-query/1
248 (clsql:do-query ((name) [select [last-name] :from [employee]
249 :order-by [last-name]])
252 ("Yeltsin" "Trotsky" "Stalin" "Putin" "Lenin" "Kruschev" "Gorbachev"
253 "Chernenko" "Brezhnev" "Andropov"))
255 (deftest :fdml/map-query/1
256 (clsql:map-query 'list #'identity
257 [select [last-name] :from [employee] :flatp t
258 :order-by [last-name]])
259 ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
260 "Stalin" "Trotsky" "Yeltsin"))
262 (deftest :fdml/map-query/2
263 (clsql:map-query 'vector #'identity
264 [select [last-name] :from [employee] :flatp t
265 :order-by [last-name]])
266 #("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
267 "Stalin" "Trotsky" "Yeltsin"))
269 (deftest :fdml/loop/1
270 (loop for (forename surname)
272 [select [first-name] [last-name] :from [employee] :order-by [last-name]]
273 collect (concatenate 'string forename " " surname))
274 ("Yuri Andropov" "Leonid Brezhnev" "Konstantin Chernenko" "Mikhail Gorbachev"
275 "Nikita Kruschev" "Vladamir Lenin" "Vladamir Putin"
276 "Josef Stalin" "Leon Trotsky" "Boris Yeltsin"))
278 ;; starts a transaction deletes a record and then rolls back the deletion
279 (deftest :fdml/transaction/1
281 ;; test if we are in a transaction
282 (push (clsql:in-transaction-p) results)
283 ;;start a transaction
284 (clsql:start-transaction)
285 ;; test if we are in a transaction
286 (push (clsql:in-transaction-p) results)
287 ;;Putin has got to go
288 (unless (eql *test-database-type* :mysql)
289 (clsql:delete-records :from [employee] :where [= [last-name] "Putin"]))
292 (clsql:select [*] :from [employee] :where [= [last-name] "Putin"])
294 ;;Oh no, he's still there
296 ;; test that we are out of the transaction
297 (push (clsql:in-transaction-p) results)
298 ;; Check that we got him back alright
299 (push (clsql:select [email] :from [employee] :where [= [last-name] "Putin"]
302 (apply #'values (nreverse results)))
303 nil t nil nil ("putin@soviet.org"))
305 ;; starts a transaction, updates a record and then rolls back the update
306 (deftest :fdml/transaction/2
308 ;; test if we are in a transaction
309 (push (clsql:in-transaction-p) results)
310 ;;start a transaction
311 (clsql:start-transaction)
312 ;; test if we are in a transaction
313 (push (clsql:in-transaction-p) results)
314 ;;Putin has got to go
315 (unless (eql *test-database-type* :mysql)
316 (clsql:update-records [employee]
317 :av-pairs '((email "putin-nospam@soviet.org"))
318 :where [= [last-name] "Putin"]))
319 ;;Should be new value
320 (push (clsql:select [email] :from [employee]
321 :where [= [last-name] "Putin"]
324 ;;Oh no, he's still there
326 ;; test that we are out of the transaction
327 (push (clsql:in-transaction-p) results)
328 ;; Check that we got him back alright
329 (push (clsql:select [email] :from [employee] :where [= [last-name] "Putin"]
332 (apply #'values (nreverse results)))
333 nil t ("putin-nospam@soviet.org") nil ("putin@soviet.org"))
335 ;; runs an update within a transaction and checks it is committed
336 (deftest :fdml/transaction/3
339 (push (clsql:in-transaction-p) results)
342 (clsql:with-transaction ()
343 (clsql:update-records [employee]
344 :av-pairs '((email "lenin-nospam@soviet.org"))
345 :where [= [emplid] 1]))
348 (push (clsql:in-transaction-p) results)
349 ;; check that was committed
350 (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
355 (clsql:with-transaction ()
356 (clsql:update-records [employee]
357 :av-pairs '((email "lenin@soviet.org"))
358 :where [= [emplid] 1]))
361 (push (clsql:in-transaction-p) results)
362 ;; check that was committed
363 (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
366 (apply #'values (nreverse results)))
367 nil :COMMITTED nil ("lenin-nospam@soviet.org") :COMMITTED
368 nil ("lenin@soviet.org"))
370 ;; runs a valid update and an invalid one within a transaction and checks
371 ;; that the valid update is rolled back when the invalid one fails.
372 (deftest :fdml/transaction/4
375 (push (clsql:in-transaction-p) results)
376 (unless (eql *test-database-type* :mysql)
378 (clsql:with-transaction ()
380 (clsql:update-records [employee]
381 :av-pairs '((email "lenin-nospam@soviet.org"))
382 :where [= [emplid] 1])
383 ;; invalid update which generates an error
384 (clsql:update-records [employee]
386 '((emale "lenin-nospam@soviet.org"))
387 :where [= [emplid] 1]))
388 (clsql:clsql-sql-error ()
391 (push (clsql:in-transaction-p) results)
392 ;; and check nothing done
393 (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
396 (apply #'values (nreverse results)))))))
397 nil nil ("lenin@soviet.org"))
401 #.(clsql:restore-sql-reader-syntax-state)