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: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 ecompanyid 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 (ecompanyid 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/query/5
158 (clsql:query (clsql:sql [select [first-name] [sum [emplid]] :from [employee]]
159 [group-by [first-name]] [order-by [sum [emplid]]])
160 :field-names nil :result-types nil)
161 (("Josef" "2") ("Leon" "3") ("Nikita" "4") ("Leonid" "5") ("Yuri" "6")
162 ("Konstantin" "7") ("Mikhail" "8") ("Boris" "9") ("Vladamir" "11")))
164 (deftest :fdml/query/6
165 (clsql:query (clsql:sql [union [select [emplid] :from [employee]]
166 [select [groupid] :from [company]]])
167 :field-names nil :result-types nil :flatp t)
168 ("1" "2" "3" "4" "5" "6" "7" "8" "9" "10"))
170 (deftest :fdml/query/7
171 (clsql:query (clsql:sql [intersect [select [emplid] :from [employee]]
172 [select [groupid] :from [company]]])
173 :field-names nil :result-types nil :flatp t)
176 (deftest :fdml/query/8
177 (clsql:query (clsql:sql [except [select [emplid] :from [employee]]
178 [select [groupid] :from [company]]])
179 :field-names nil :result-types nil :flatp t)
180 ("2" "3" "4" "5" "6" "7" "8" "9" "10"))
182 (deftest :fdml/execute-command/1
184 (clsql:table-exists-p [foo] :owner *test-database-user*)
186 (clsql:execute-command "create table foo (bar integer)")
187 (clsql:table-exists-p [foo] :owner *test-database-user*))
189 (clsql:execute-command "drop table foo")
190 (clsql:table-exists-p [foo] :owner *test-database-user*)))
194 ;; compare min, max and average hieghts in inches (they're quite short
196 (deftest :fdml/select/1
197 (let ((max (clsql:select [function "floor"
198 [/ [* [max [height]] 100] 2.54]]
202 (min (clsql:select [function "floor"
203 [/ [* [min [height]] 100] 2.54]]
207 (avg (clsql:select [function "floor"
208 [avg [/ [* [height] 100] 2.54]]]
212 (apply #'< (mapcar #'(lambda (s) (parse-integer s :junk-allowed t))
213 (append min avg max))))
216 (deftest :fdml/select/2
217 (clsql:select [first-name] :from [employee] :flatp t :distinct t
220 :order-by [first-name])
221 ("Boris" "Josef" "Konstantin" "Leon" "Leonid" "Mikhail" "Nikita" "Vladamir"
224 (deftest :fdml/select/3
225 (clsql:select [first-name] [count [*]] :from [employee]
227 :group-by [first-name]
228 :order-by [first-name]
230 (("Boris" "1") ("Josef" "1") ("Konstantin" "1") ("Leon" "1") ("Leonid" "1")
231 ("Mikhail" "1") ("Nikita" "1") ("Vladamir" "2") ("Yuri" "1")))
233 (deftest :fdml/select/4
234 (clsql:select [last-name] :from [employee]
235 :where [like [email] "%org"]
236 :order-by [last-name]
240 ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
241 "Stalin" "Trotsky" "Yeltsin"))
243 (deftest :fdml/select/5
244 (clsql:select [email] :from [employee] :flatp t :result-types nil
245 :where [in [employee emplid]
246 [select [managerid] :from [employee]]]
248 ("lenin@soviet.org"))
250 (deftest :fdml/select/6
251 (if (clsql-sys:db-type-has-fancy-math? *test-database-underlying-type*)
252 (mapcar #'(lambda (s) (parse-integer s :junk-allowed t))
253 (clsql:select [function "trunc" [height]] :from [employee]
257 (mapcar #'(lambda (s) (truncate (parse-integer s :junk-allowed t)))
258 (clsql:select [height] :from [employee] :flatp t
259 :field-names nil :result-types nil)))
260 (1 1 1 1 1 1 1 1 1 1))
262 (deftest :fdml/select/7
263 (clsql:select [max [emplid]] :from [employee] :flatp t
264 :field-names nil :result-types nil)
267 (deftest :fdml/select/8
268 (clsql:select [min [emplid]] :from [employee] :flatp t
269 :field-names nil :result-types nil)
272 (deftest :fdml/select/9
275 (clsql:select [avg [emplid]] :from [employee] :flatp t
276 :field-names nil :result-types nil))
280 (deftest :fdml/select/10
281 (clsql:select [last-name] :from [employee]
282 :where [not [in [emplid]
283 [select [managerid] :from [company]]]]
287 :order-by [last-name])
288 ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Putin" "Stalin"
289 "Trotsky" "Yeltsin"))
291 (deftest :fdml/select/11
292 (clsql:select [last-name] :from [employee] :where [married] :flatp t
293 :field-names nil :order-by [emplid] :result-types nil)
294 ("Lenin" "Stalin" "Trotsky"))
296 (deftest :fdml/select/12
298 (clsql:select [last-name] :from [employee] :where [= [emplid] v]
299 :field-names nil :result-types nil))
302 (deftest :fdml/select/13
303 (multiple-value-bind (results field-names)
304 (clsql:select [emplid] [last-name] :from [employee]
305 :where [= [emplid] 1])
306 (values results (mapcar #'string-downcase field-names)))
308 ("emplid" "last_name"))
310 (deftest :fdml/select/14
311 (floatp (car (clsql:select [height] :from [employee] :where [= [emplid] 1]
315 (deftest :fdml/select/15
316 (multiple-value-bind (rows field-names)
317 (clsql:select [addressid] [street-number] [street-name] [city_field] [zip]
319 :where [= 1 [addressid]])
322 (mapcar #'string-downcase field-names)))
323 ((1 10 "Park Place" "Leningrad" 123))
324 ("addressid" "street_number" "street_name" "city_field" "zip"))
326 (deftest :fdml/select/16
327 (clsql:select [emplid] :from [employee] :where [= 1 [emplid]]
331 (deftest :fdml/select/17
332 (clsql:select [emplid] [last-name] :from [employee] :where [= 1 [emplid]]
336 (deftest :fdml/select/18
337 (clsql:select [emplid :string] [last-name] :from [employee] :where [= 1 [emplid]]
341 (deftest :fdml/select/19
342 (clsql:select [emplid] :from [employee] :order-by [emplid]
343 :where [between [* [emplid] 10] [* 5 10] [* 10 10]]
344 :field-names nil :result-types nil :flatp t)
345 ("5" "6" "7" "8" "9" "10"))
347 (deftest :fdml/select/20
348 (clsql:select [emplid] :from [employee] :order-by [emplid]
349 :where [not [between [* [emplid] 10] [* 5 10] [* 10 10]]]
350 :field-names nil :result-types nil :flatp t)
353 (deftest :fdml/select/21
354 (clsql:select [substr [first-name] 1 4] :from [employee]
355 :flatp t :order-by [emplid] :field-names nil)
356 ("Vlad" "Jose" "Leon" "Niki" "Leon" "Yuri" "Kons" "Mikh" "Bori" "Vlad"))
358 (deftest :fdml/select/22
359 (clsql:select [|| [first-name] " " [last-name]] :from [employee]
360 :flatp t :order-by [emplid] :field-names nil)
361 ("Vladamir Lenin" "Josef Stalin" "Leon Trotsky" "Nikita Kruschev"
362 "Leonid Brezhnev" "Yuri Andropov" "Konstantin Chernenko" "Mikhail Gorbachev"
363 "Boris Yeltsin" "Vladamir Putin"))
365 (deftest :fdml/select/23
366 (clsql:select [emplid] :from [employee] :where [in [emplid] '(1 2 3 4)]
367 :flatp t :order-by [emplid] :field-names nil
371 (deftest :fdml/select/24
372 (clsql:select [distinct [first-name]] :from [employee] :flatp t
373 :order-by [first-name] :field-names nil :result-types nil)
374 ("Boris" "Josef" "Konstantin" "Leon" "Leonid" "Mikhail" "Nikita" "Vladamir"
377 (deftest :fdml/select/25
378 (clsql:select [first-name] :from "employee" :flatp t :distinct t
381 :order-by [first-name])
382 ("Boris" "Josef" "Konstantin" "Leon" "Leonid" "Mikhail" "Nikita" "Vladamir"
385 (deftest :fdml/select/26
386 (clsql:select ["table" first-name] ["table" last-name]
387 :from '([employee "table"] [employee "join"])
388 :where [and [= ["table" first-name]
390 [not [= ["table" emplid]
392 :order-by '(["table" last-name])
393 :result-types nil :field-names nil)
394 (("Vladamir" "Lenin") ("Vladamir" "Putin")))
396 (deftest :fdml/select/27
397 (clsql:select [coalesce [managerid] 10] :from [employee] :order-by [emplid]
398 :field-names nil :result-types nil :flatp t)
399 ("10" "1" "1" "1" "1" "1" "1" "1" "1" "1"))
401 (deftest :fdml/select/28
402 (loop for column in `([*] [emplid]) collect
403 (clsql:select [count column] :from [employee]
404 :flatp t :result-types nil :field-names nil))
407 (deftest :fdml/select/29
408 (clsql:select [first-name] [last-name] :from [employee]
409 :result-types nil :field-names nil
410 :order-by '(([first-name] :asc) ([last-name] :desc)))
411 (("Boris" "Yeltsin") ("Josef" "Stalin") ("Konstantin" "Chernenko")
412 ("Leon" "Trotsky") ("Leonid" "Brezhnev") ("Mikhail" "Gorbachev")
413 ("Nikita" "Kruschev") ("Vladamir" "Putin") ("Vladamir" "Lenin")
414 ("Yuri" "Andropov")))
416 (deftest :fdml/select/30
417 (clsql:select [first-name] [last-name] :from [employee]
418 :result-types nil :field-names nil
419 :order-by '(([first-name] :asc) ([last-name] :asc)))
420 (("Boris" "Yeltsin") ("Josef" "Stalin") ("Konstantin" "Chernenko")
421 ("Leon" "Trotsky") ("Leonid" "Brezhnev") ("Mikhail" "Gorbachev")
422 ("Nikita" "Kruschev") ("Vladamir" "Lenin") ("Vladamir" "Putin")
423 ("Yuri" "Andropov")))
425 (deftest :fdml/select/31
426 (clsql:select [last-name] :from [employee]
427 :set-operation [union [select [first-name] :from [employee]
428 :order-by [last-name]]]
432 ("Andropov" "Boris" "Brezhnev" "Chernenko" "Gorbachev" "Josef" "Konstantin"
433 "Kruschev" "Lenin" "Leon" "Leonid" "Mikhail" "Nikita" "Putin" "Stalin"
434 "Trotsky" "Vladamir" "Yeltsin" "Yuri"))
436 (deftest :fdml/select/32
437 (clsql:select [emplid] :from [employee]
438 :where [= [emplid] [any [select [companyid] :from [company]]]]
439 :flatp t :result-types nil :field-names nil)
442 (deftest :fdml/select/33
443 (clsql:select [last-name] :from [employee]
444 :where [> [emplid] [all [select [groupid] :from [employee]]]]
445 :order-by [last-name]
446 :flatp t :result-types nil :field-names nil)
447 ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Putin" "Stalin"
448 "Trotsky" "Yeltsin"))
450 (deftest :fdml/select/34
451 (loop for x from 1 below 5
454 (clsql:select [last-name] :from [employee]
455 :where [= [emplid] x]
456 :flatp t :result-types nil :field-names nil)))
457 ("Lenin" "Stalin" "Trotsky" "Kruschev"))
459 (deftest :fdml/do-query/1
461 (clsql:do-query ((name) [select [last-name] :from [employee]
462 :order-by [last-name]])
465 ("Yeltsin" "Trotsky" "Stalin" "Putin" "Lenin" "Kruschev" "Gorbachev"
466 "Chernenko" "Brezhnev" "Andropov"))
468 (deftest :fdml/map-query/1
469 (clsql:map-query 'list #'identity
470 [select [last-name] :from [employee] :flatp t
471 :order-by [last-name]])
472 ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
473 "Stalin" "Trotsky" "Yeltsin"))
475 (deftest :fdml/map-query/2
476 (clsql:map-query 'vector #'identity
477 [select [last-name] :from [employee] :flatp t
478 :order-by [last-name]])
479 #("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
480 "Stalin" "Trotsky" "Yeltsin"))
482 (deftest :fdml/map-query/3
483 (clsql:map-query 'list #'identity
484 [select [last-name] :from [employee] :order-by [last-name]])
485 (("Andropov") ("Brezhnev") ("Chernenko") ("Gorbachev") ("Kruschev") ("Lenin")
486 ("Putin") ("Stalin") ("Trotsky") ("Yeltsin")))
488 (deftest :fdml/map-query/4
489 (clsql:map-query 'list #'identity
490 [select [first-name] [last-name] :from [employee]
491 :order-by [last-name]])
492 (("Yuri" "Andropov") ("Leonid" "Brezhnev") ("Konstantin" "Chernenko")
493 ("Mikhail" "Gorbachev") ("Nikita" "Kruschev") ("Vladamir" "Lenin")
494 ("Vladamir" "Putin") ("Josef" "Stalin") ("Leon" "Trotsky")
495 ("Boris" "Yeltsin")))
497 (deftest :fdml/loop/1
498 (loop for (forename surname)
500 [select [first-name] [last-name] :from [employee] :order-by [last-name]]
501 collect (concatenate 'string forename " " surname))
502 ("Yuri Andropov" "Leonid Brezhnev" "Konstantin Chernenko" "Mikhail Gorbachev"
503 "Nikita Kruschev" "Vladamir Lenin" "Vladamir Putin"
504 "Josef Stalin" "Leon Trotsky" "Boris Yeltsin"))
506 (deftest :fdml/loop/2
507 (loop for (addressid)
509 [select [addressid] :from [addr] :order-by [addressid]]
513 (deftest :fdml/loop/3
516 [select [addressid] :from [addr] :order-by [addressid]]
520 ;; starts a transaction deletes a record and then rolls back the deletion
521 (deftest :fdml/transaction/1
523 ;; test if we are in a transaction
524 (push (clsql:in-transaction-p) results)
525 ;;start a transaction
526 (clsql:start-transaction)
527 ;; test if we are in a transaction
528 (push (clsql:in-transaction-p) results)
529 ;;Putin has got to go
530 (clsql:delete-records :from [employee] :where [= [last-name] "Putin"])
533 (clsql:select [*] :from [employee] :where [= [last-name] "Putin"])
535 ;;Oh no, he's still there
537 ;; test that we are out of the transaction
538 (push (clsql:in-transaction-p) results)
539 ;; Check that we got him back alright
540 (push (clsql:select [email] :from [employee] :where [= [last-name] "Putin"]
543 (apply #'values (nreverse results)))
544 nil t nil nil ("putin@soviet.org"))
546 ;; starts a transaction, updates a record and then rolls back the update
547 (deftest :fdml/transaction/2
549 ;; test if we are in a transaction
550 (push (clsql:in-transaction-p) results)
551 ;;start a transaction
552 (clsql:start-transaction)
553 ;; test if we are in a transaction
554 (push (clsql:in-transaction-p) results)
555 ;;Putin has got to go
556 (clsql:update-records [employee]
557 :av-pairs '((email "putin-nospam@soviet.org"))
558 :where [= [last-name] "Putin"])
559 ;;Should be new value
560 (push (clsql:select [email] :from [employee]
561 :where [= [last-name] "Putin"]
564 ;;Oh no, he's still there
566 ;; test that we are out of the transaction
567 (push (clsql:in-transaction-p) results)
568 ;; Check that we got him back alright
569 (push (clsql:select [email] :from [employee] :where [= [last-name] "Putin"]
572 (apply #'values (nreverse results)))
573 nil t ("putin-nospam@soviet.org") nil ("putin@soviet.org"))
575 ;; runs an update within a transaction and checks it is committed
576 (deftest :fdml/transaction/3
579 (push (clsql:in-transaction-p) results)
582 (clsql:with-transaction ()
583 (clsql:update-records [employee]
584 :av-pairs '((email "lenin-nospam@soviet.org"))
585 :where [= [emplid] 1]))
588 (push (clsql:in-transaction-p) results)
589 ;; check that was committed
590 (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
595 (clsql:with-transaction ()
596 (clsql:update-records [employee]
597 :av-pairs '((email "lenin@soviet.org"))
598 :where [= [emplid] 1]))
601 (push (clsql:in-transaction-p) results)
602 ;; check that was committed
603 (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
606 (apply #'values (nreverse results)))
607 nil :committed nil ("lenin-nospam@soviet.org") :committed
608 nil ("lenin@soviet.org"))
610 ;; runs a valid update and an invalid one within a transaction and checks
611 ;; that the valid update is rolled back when the invalid one fails.
612 (deftest :fdml/transaction/4
615 (push (clsql:in-transaction-p) results)
617 (clsql:with-transaction ()
619 (clsql:update-records [employee]
620 :av-pairs '((email "lenin-nospam@soviet.org"))
621 :where [= [emplid] 1])
622 ;; invalid update which generates an error
623 (clsql:update-records [employee]
625 '((emale "lenin-nospam@soviet.org"))
626 :where [= [emplid] 1]))
627 (clsql:sql-database-error ()
630 (push (clsql:in-transaction-p) results)
631 ;; and check nothing done
632 (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
635 (apply #'values (nreverse results))))))
636 nil nil ("lenin@soviet.org"))
640 #.(clsql:restore-sql-reader-syntax-state)