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 ;; Computed values are not always classified as numeric by psqlodbc
132 (deftest :fdml/query/1
133 (let ((count (caar (clsql:query "SELECT COUNT(*) FROM EMPLOYEE WHERE (EMAIL LIKE '%org')" :field-names nil))))
135 (nth-value 0 (parse-integer count))
139 (deftest :fdml/query/2
140 (multiple-value-bind (rows field-names)
142 "SELECT FIRST_NAME,LAST_NAME FROM EMPLOYEE WHERE (EMPLID <= 5) ORDER BY LAST_NAME")
143 (values rows (mapcar 'string-upcase field-names)))
144 (("Leonid" "Brezhnev") ("Nikita" "Kruschev") ("Vladamir" "Lenin")
145 ("Josef" "Stalin") ("Leon" "Trotsky"))
146 ("FIRST_NAME" "LAST_NAME"))
148 (deftest :fdml/query/3
149 (caar (clsql:query "SELECT EMPLID FROM EMPLOYEE WHERE LAST_NAME = 'Andropov'" :field-names nil))
152 (deftest :fdml/query/4
153 (typep (caar (clsql:query "SELECT HEIGHT FROM EMPLOYEE WHERE LAST_NAME = 'Andropov'" :field-names nil))
157 (deftest :fdml/execute-command/1
159 (clsql:table-exists-p [foo] :owner *test-database-user*)
161 (clsql:execute-command "create table foo (bar integer)")
162 (clsql:table-exists-p [foo] :owner *test-database-user*))
164 (clsql:execute-command "drop table foo")
165 (clsql:table-exists-p [foo] :owner *test-database-user*)))
169 ;; compare min, max and average hieghts in inches (they're quite short
171 (deftest :fdml/select/1
172 (let ((max (clsql:select [function "floor"
173 [/ [* [max [height]] 100] 2.54]]
177 (min (clsql:select [function "floor"
178 [/ [* [min [height]] 100] 2.54]]
182 (avg (clsql:select [function "floor"
183 [avg [/ [* [height] 100] 2.54]]]
187 (apply #'< (mapcar #'(lambda (s) (parse-integer s :junk-allowed t))
188 (append min avg max))))
191 (deftest :fdml/select/2
192 (values (clsql:select [first-name] :from [employee] :flatp t :distinct t
194 :order-by [first-name]))
195 ("Boris" "Josef" "Konstantin" "Leon" "Leonid" "Mikhail" "Nikita" "Vladamir"
198 (deftest :fdml/select/3
199 (values (clsql:select [first-name] [count [*]] :from [employee]
201 :group-by [first-name]
202 :order-by [first-name]))
203 (("Boris" "1") ("Josef" "1") ("Konstantin" "1") ("Leon" "1") ("Leonid" "1")
204 ("Mikhail" "1") ("Nikita" "1") ("Vladamir" "2") ("Yuri" "1")))
206 (deftest :fdml/select/4
207 (values (clsql:select [last-name] :from [employee]
208 :where [like [email] "%org"]
209 :order-by [last-name]
212 ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
213 "Stalin" "Trotsky" "Yeltsin"))
215 (deftest :fdml/select/5
216 (values (clsql:select [email] :from [employee] :flatp t :result-types nil
217 :where [in [employee emplid]
218 [select [managerid] :from [employee]]]))
219 ("lenin@soviet.org"))
221 (deftest :fdml/select/6
222 (if (db-type-has-fancy-math? *test-database-underlying-type*)
223 (mapcar #'(lambda (s) (parse-integer s :junk-allowed t))
224 (clsql:select [function "trunc" [height]] :from [employee]
227 (mapcar #'(lambda (s) (truncate (parse-integer s :junk-allowed t)))
228 (clsql:select [height] :from [employee] :flatp t
230 (1 1 1 1 1 1 1 1 1 1))
232 (deftest :fdml/select/7
234 (clsql:select [max [emplid]] :from [employee] :flatp t :result-types nil))
237 (deftest :fdml/select/8
239 (clsql:select [min [emplid]] :from [employee] :flatp t :result-types nil))
242 (deftest :fdml/select/9
245 (clsql:select [avg [emplid]] :from [employee] :flatp t :result-types nil))
249 (deftest :fdml/select/10
250 (values (clsql:select [last-name] :from [employee]
251 :where [not [in [emplid]
252 [select [managerid] :from [company]]]]
255 :order-by [last-name]))
256 ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Putin" "Stalin"
257 "Trotsky" "Yeltsin"))
259 (deftest :fdml/select/11
260 (values (clsql:select [last-name] :from [employee] :where [married] :flatp t
261 :order-by [emplid] :result-types nil))
262 ("Lenin" "Stalin" "Trotsky"))
264 (deftest :fdml/select/12
266 (values (clsql:select [last-name] :from [employee] :where [= [emplid] v]
270 (deftest :fdml/select/13
271 (multiple-value-bind (results field-names)
272 (clsql:select [emplid] [last-name] [married] :from [employee]
273 :where [= [emplid] 1])
274 (values results (mapcar #'string-downcase field-names)))
276 ("emplid" "last_name" "married"))
278 (deftest :fdml/select/14
279 (floatp (car (clsql:select [height] :from [employee] :where [= [emplid] 1]
283 ;(deftest :fdml/select/11
284 ; (clsql:select [emplid] :from [employee]
285 ; :where [= [emplid] [any [select [companyid] :from [company]]]]
289 (deftest :fdml/do-query/1
291 (clsql:do-query ((name) [select [last-name] :from [employee]
292 :order-by [last-name]])
295 ("Yeltsin" "Trotsky" "Stalin" "Putin" "Lenin" "Kruschev" "Gorbachev"
296 "Chernenko" "Brezhnev" "Andropov"))
298 (deftest :fdml/map-query/1
299 (clsql:map-query 'list #'identity
300 [select [last-name] :from [employee] :flatp t
301 :order-by [last-name]])
302 ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
303 "Stalin" "Trotsky" "Yeltsin"))
305 (deftest :fdml/map-query/2
306 (clsql:map-query 'vector #'identity
307 [select [last-name] :from [employee] :flatp t
308 :order-by [last-name]])
309 #("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
310 "Stalin" "Trotsky" "Yeltsin"))
312 (deftest :fdml/loop/1
313 (loop for (forename surname)
315 [select [first-name] [last-name] :from [employee] :order-by [last-name]]
316 collect (concatenate 'string forename " " surname))
317 ("Yuri Andropov" "Leonid Brezhnev" "Konstantin Chernenko" "Mikhail Gorbachev"
318 "Nikita Kruschev" "Vladamir Lenin" "Vladamir Putin"
319 "Josef Stalin" "Leon Trotsky" "Boris Yeltsin"))
321 ;; starts a transaction deletes a record and then rolls back the deletion
322 (deftest :fdml/transaction/1
324 ;; test if we are in a transaction
325 (push (clsql:in-transaction-p) results)
326 ;;start a transaction
327 (clsql:start-transaction)
328 ;; test if we are in a transaction
329 (push (clsql:in-transaction-p) results)
330 ;;Putin has got to go
331 (clsql:delete-records :from [employee] :where [= [last-name] "Putin"])
334 (clsql:select [*] :from [employee] :where [= [last-name] "Putin"])
336 ;;Oh no, he's still there
338 ;; test that we are out of the transaction
339 (push (clsql:in-transaction-p) results)
340 ;; Check that we got him back alright
341 (push (clsql:select [email] :from [employee] :where [= [last-name] "Putin"]
344 (apply #'values (nreverse results)))
345 nil t nil nil ("putin@soviet.org"))
347 ;; starts a transaction, updates a record and then rolls back the update
348 (deftest :fdml/transaction/2
350 ;; test if we are in a transaction
351 (push (clsql:in-transaction-p) results)
352 ;;start a transaction
353 (clsql:start-transaction)
354 ;; test if we are in a transaction
355 (push (clsql:in-transaction-p) results)
356 ;;Putin has got to go
357 (clsql:update-records [employee]
358 :av-pairs '((email "putin-nospam@soviet.org"))
359 :where [= [last-name] "Putin"])
360 ;;Should be new value
361 (push (clsql:select [email] :from [employee]
362 :where [= [last-name] "Putin"]
365 ;;Oh no, he's still there
367 ;; test that we are out of the transaction
368 (push (clsql:in-transaction-p) results)
369 ;; Check that we got him back alright
370 (push (clsql:select [email] :from [employee] :where [= [last-name] "Putin"]
373 (apply #'values (nreverse results)))
374 nil t ("putin-nospam@soviet.org") nil ("putin@soviet.org"))
376 ;; runs an update within a transaction and checks it is committed
377 (deftest :fdml/transaction/3
380 (push (clsql:in-transaction-p) results)
383 (clsql:with-transaction ()
384 (clsql:update-records [employee]
385 :av-pairs '((email "lenin-nospam@soviet.org"))
386 :where [= [emplid] 1]))
389 (push (clsql:in-transaction-p) results)
390 ;; check that was committed
391 (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
396 (clsql:with-transaction ()
397 (clsql:update-records [employee]
398 :av-pairs '((email "lenin@soviet.org"))
399 :where [= [emplid] 1]))
402 (push (clsql:in-transaction-p) results)
403 ;; check that was committed
404 (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
407 (apply #'values (nreverse results)))
408 nil :committed nil ("lenin-nospam@soviet.org") :committed
409 nil ("lenin@soviet.org"))
411 ;; runs a valid update and an invalid one within a transaction and checks
412 ;; that the valid update is rolled back when the invalid one fails.
413 (deftest :fdml/transaction/4
416 (push (clsql:in-transaction-p) results)
418 (clsql:with-transaction ()
420 (clsql:update-records [employee]
421 :av-pairs '((email "lenin-nospam@soviet.org"))
422 :where [= [emplid] 1])
423 ;; invalid update which generates an error
424 (clsql:update-records [employee]
426 '((emale "lenin-nospam@soviet.org"))
427 :where [= [emplid] 1]))
428 (clsql:clsql-error ()
431 (push (clsql:in-transaction-p) results)
432 ;; and check nothing done
433 (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
436 (apply #'values (nreverse results))))))
437 nil nil ("lenin@soviet.org"))
441 #.(clsql:restore-sql-reader-syntax-state)