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>, Kevin Rosenberg
5 ;;;; Created: 30/03/2004
7 ;;;; Tests for the CLSQL Functional Data Manipulation Language
10 ;;;; This file is part of CLSQL.
12 ;;;; CLSQL users are granted the rights to distribute and use this software
13 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
14 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
15 ;;;; ======================================================================
17 (in-package #:clsql-tests)
19 (clsql-sys:file-enable-sql-reader-syntax)
20 ;;started defining an independent dataset that doesn't depend on the view-classes
21 ;; but there is a *lot* of stuff in the file assuming that dataset.
22 ;; (def-dataset *ds-fdml*
24 ;; (let ((*backend-warning-behavior*
25 ;; (if (member *test-database-type* '(:postgresql :postgresql-socket))
28 ;; (clsql-sys:execute-command "CREATE TABLE EMPLOYEE (
29 ;; emplid integer NOT NULL,
30 ;; groupid integer NOT NULL,
31 ;; first_name character varying(30),
32 ;; last_name character varying(30),
33 ;; email character varying(100),
34 ;; ecompanyid integer,
36 ;; height double precision,
38 ;; birthday timestamp,
40 ;; CONSTRAINT employeepk PRIMARY KEY (emplid, groupid),
41 ;; CONSTRAINT employee_emplid_key UNIQUE (emplid)
43 ;; (:sqldata "EMPLOYEE"
44 ;; "emplid,groupid,first_name,last_name,email,height,birthday"
45 ;; "10,1,'a','b','a@b.org',1.9,current_timestamp"
46 ;; "11,1,'x','y','x@y.org',null,current_timestamp"
48 ;; (:cleanup "DROP TABLE EMPLOYEE")
54 ;; Computed values are not always classified as numeric by psqlodbc
55 (deftest :fdml/query/1
56 (with-dataset *ds-employees*
57 (let ((count (caar (clsql:query "SELECT COUNT(*) FROM EMPLOYEE WHERE (EMAIL LIKE '%org')" :field-names nil))))
61 (deftest :fdml/query/2
62 (with-dataset *ds-employees*
63 (multiple-value-bind (rows field-names)
65 "SELECT FIRST_NAME,LAST_NAME FROM EMPLOYEE WHERE (EMPLID <= 5) ORDER BY LAST_NAME")
66 (values rows (mapcar 'string-upcase field-names))))
67 (("Leonid" "Brezhnev") ("Nikita" "Kruschev") ("Vladimir" "Lenin")
68 ("Josef" "Stalin") ("Leon" "Trotsky"))
69 ("FIRST_NAME" "LAST_NAME"))
71 (deftest :fdml/query/3
72 (with-dataset *ds-employees*
73 (caar (clsql:query "SELECT EMPLID FROM EMPLOYEE WHERE LAST_NAME = 'Andropov'" :field-names nil)))
76 (deftest :fdml/query/4
77 (with-dataset *ds-employees*
78 (typep (caar (clsql:query "SELECT HEIGHT FROM EMPLOYEE WHERE LAST_NAME = 'Andropov'" :field-names nil))
82 (deftest :fdml/query/5
83 (with-dataset *ds-employees*
84 (let ((res (clsql:query (clsql:sql [select [first-name] [sum [emplid]] :from [employee]]
85 [group-by [first-name]] [order-by [sum [emplid]]])
86 :field-names nil :result-types nil)))
87 (mapcar (lambda (p) (list (car p) (%get-int (second p))))
89 (("Josef" 2) ("Leon" 3) ("Nikita" 4) ("Leonid" 5) ("Yuri" 6)
90 ("Konstantin" 7) ("Mikhail" 8) ("Boris" 9) ("Vladimir" 11)))
92 (deftest :fdml/query/6
93 (with-dataset *ds-employees*
94 (let ((res (clsql:query (clsql:sql [union [select [emplid] :from [employee]]
95 [select [groupid] :from [company]]])
96 :field-names nil :result-types nil :flatp t
98 (values (or (eql *test-database-type* :postgresql-socket3)
99 (every #'stringp res))
100 (sort (mapcar #'%get-int res)
102 t (1 2 3 4 5 6 7 8 9 10))
104 (deftest :fdml/query/7
105 (with-dataset *ds-employees*
106 (let ((res (car (clsql:query (clsql:sql [intersect [select [emplid] :from [employee]]
107 [select [groupid] :from [company]]])
108 :field-names nil :result-types nil :flatp t))))
109 (values (or (stringp res)
110 (eql *test-database-type* :postgresql-socket3))
111 (nth-value 0 (%get-int res)))))
114 (deftest :fdml/query/8
115 (with-dataset *ds-employees*
116 (let ((res (clsql:query (clsql:sql [except [select [emplid] :from [employee]]
117 [select [groupid] :from [company]]])
118 :field-names nil :result-types nil :flatp t)))
119 (values (or (every #'stringp res)
120 (eql *test-database-type* :postgresql-socket3))
121 (sort (mapcar #'%get-int res)
123 t (2 3 4 5 6 7 8 9 10))
125 ;; compare min, max and average hieghts in inches (they're quite short
127 (deftest :fdml/select/1
128 (with-dataset *ds-employees*
129 (let ((max (clsql:select [function "floor"
130 [/ [* [max [height]] 100] 2.54]]
134 (min (clsql:select [function "floor"
135 [/ [* [min [height]] 100] 2.54]]
139 (avg (clsql:select [function "floor"
140 [avg [/ [* [height] 100] 2.54]]]
144 (apply #'< (mapcar #'%get-int (append min avg max)))))
147 (deftest :fdml/select/2
148 (with-dataset *ds-employees*
149 (clsql:select [first-name] :from [employee] :flatp t :distinct t
152 :order-by [first-name]))
153 ("Boris" "Josef" "Konstantin" "Leon" "Leonid" "Mikhail" "Nikita" "Vladimir"
156 (deftest :fdml/select/3
157 (with-dataset *ds-employees*
158 (let ((res (clsql:select [first-name] [count [*]] :from [employee]
160 :group-by [first-name]
161 :order-by [first-name]
163 (mapcar (lambda (p) (list (car p) (%get-int (second p))))
165 (("Boris" 1) ("Josef" 1) ("Konstantin" 1) ("Leon" 1) ("Leonid" 1)
166 ("Mikhail" 1) ("Nikita" 1) ("Vladimir" 2) ("Yuri" 1)))
168 (deftest :fdml/select/4
169 (with-dataset *ds-employees*
170 (clsql:select [last-name] :from [employee]
171 :where [like [email] "%org"]
172 :order-by [last-name]
176 ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
177 "Stalin" "Trotsky" "Yeltsin"))
179 (deftest :fdml/select/5
180 (with-dataset *ds-employees*
181 (clsql:select [email] :from [employee] :flatp t :result-types nil
182 :where [in [employee emplid]
183 [select [managerid] :from [employee]]]
185 ("lenin@soviet.org"))
187 (deftest :fdml/select/6
188 (with-dataset *ds-employees*
189 (if (clsql-sys:db-type-has-fancy-math? *test-database-underlying-type*)
191 (clsql:select [function "trunc" [height]] :from [employee]
196 (clsql:select [height] :from [employee] :flatp t
197 :field-names nil :result-types nil))))
198 (1 1 1 1 1 1 1 1 1 1))
200 (deftest :fdml/select/7
201 (with-dataset *ds-employees*
202 (let ((result (car (clsql:select [max [emplid]] :from [employee] :flatp t
203 :field-names nil :result-types nil))))
205 (nth-value 0 (%get-int result)))))
208 (deftest :fdml/select/8
209 (with-dataset *ds-employees*
210 (let ((result (car (clsql:select [min [emplid]] :from [employee] :flatp t
211 :field-names nil :result-types nil))))
213 (nth-value 0 (%get-int result)))))
216 (deftest :fdml/select/9
217 (with-dataset *ds-employees*
218 (let ((val (car (clsql:select
219 [avg [emplid]] :from [employee] :flatp t
220 :field-names nil :result-types nil))))
222 (string (subseq val 0 3))
223 (number (format nil "~,1F" val)))))
226 (deftest :fdml/select/10
227 (with-dataset *ds-employees*
228 (clsql:select [last-name] :from [employee]
229 :where [not [in [emplid]
230 [select [managerid] :from [company]]]]
234 :order-by [last-name]))
235 ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Putin" "Stalin"
236 "Trotsky" "Yeltsin"))
238 (deftest :fdml/select/11
239 (with-dataset *ds-employees*
240 (clsql:select [last-name] :from [employee] :where [married] :flatp t
241 :field-names nil :order-by [emplid] :result-types nil))
242 ("Lenin" "Stalin" "Trotsky"))
244 (deftest :fdml/select/12
245 (with-dataset *ds-employees*
247 (clsql:select [last-name] :from [employee] :where [= [emplid] v]
248 :field-names nil :result-types nil)))
251 (deftest :fdml/select/13
252 (with-dataset *ds-employees*
253 (multiple-value-bind (results field-names)
254 (clsql:select [emplid] [last-name] :from [employee]
255 :where [= [emplid] 1])
256 (values results (mapcar #'string-downcase field-names))))
258 ("emplid" "last_name"))
260 (deftest :fdml/select/14
261 (with-dataset *ds-employees*
262 (floatp (car (clsql:select [height] :from [employee] :where [= [emplid] 1]
266 (deftest :fdml/select/15
267 (with-dataset *ds-employees*
268 (multiple-value-bind (rows field-names)
269 (clsql:select [addressid] [street-number] [street-name] [city_field] [zip]
271 :where [= 1 [addressid]])
274 (mapcar #'string-downcase field-names))))
275 ((1 10 "Park Place" "Leningrad" 123))
276 ("addressid" "street_number" "street_name" "city_field" "zip"))
278 (deftest :fdml/select/16
279 (with-dataset *ds-employees*
280 (clsql:select [emplid] :from [employee] :where [= 1 [emplid]]
284 (deftest :fdml/select/17
285 (with-dataset *ds-employees*
286 (clsql:select [emplid] [last-name] :from [employee] :where [= 1 [emplid]]
290 (deftest :fdml/select/18
291 (with-dataset *ds-employees*
292 (clsql:select [emplid :string] [last-name] :from [employee] :where [= 1 [emplid]]
296 (deftest :fdml/select/19
297 (with-dataset *ds-employees*
300 (clsql:select [emplid] :from [employee] :order-by [emplid]
301 :where [between [* [emplid] 10] [* 5 10] [* 10 10]]
302 :field-names nil :result-types nil :flatp t)))
305 (deftest :fdml/select/20
306 (with-dataset *ds-employees*
308 (clsql:select [emplid] :from [employee] :order-by [emplid]
309 :where [not [between [* [emplid] 10] [* 5 10] [* 10 10]]]
310 :field-names nil :result-types nil :flatp t)))
313 (deftest :fdml/select/21
314 (with-dataset *ds-employees*
315 (clsql:select [substring [first-name] 1 4] :from [employee]
316 :flatp t :order-by [emplid] :field-names nil))
317 ("Vlad" "Jose" "Leon" "Niki" "Leon" "Yuri" "Kons" "Mikh" "Bori" "Vlad"))
319 (deftest :fdml/select/22
320 (with-dataset *ds-employees*
321 (case *test-database-underlying-type*
322 (:mssql (clsql:select [+ [first-name] " " [last-name]] :from [employee]
323 :flatp t :order-by [emplid] :field-names nil))
324 (t (clsql:select [|| [first-name] " " [last-name]] :from [employee]
325 :flatp t :order-by [emplid] :field-names nil))))
326 ("Vladimir Lenin" "Josef Stalin" "Leon Trotsky" "Nikita Kruschev"
327 "Leonid Brezhnev" "Yuri Andropov" "Konstantin Chernenko" "Mikhail Gorbachev"
328 "Boris Yeltsin" "Vladimir Putin"))
330 (deftest :fdml/select/23
331 (with-dataset *ds-employees*
333 (clsql:select [emplid] :from [employee] :where [in [emplid] '(1 2 3 4)]
334 :flatp t :order-by [emplid] :field-names nil
338 (deftest :fdml/select/24
339 (with-dataset *ds-employees*
340 (clsql:select [distinct [first-name]] :from [employee] :flatp t
341 :order-by [first-name] :field-names nil :result-types nil))
342 ("Boris" "Josef" "Konstantin" "Leon" "Leonid" "Mikhail" "Nikita" "Vladimir"
345 (deftest :fdml/select/25
346 (with-dataset *ds-employees*
347 (clsql:select [first-name] :from (clsql-sys:convert-to-db-default-case "employee" *default-database*)
351 :order-by [first-name]))
352 ("Boris" "Josef" "Konstantin" "Leon" "Leonid" "Mikhail" "Nikita" "Vladimir"
355 (deftest :fdml/select/26
356 (with-dataset *ds-employees*
357 (clsql:select ["table" first-name] ["table" last-name]
358 :from '([employee "table"] [employee "join"])
359 :where [and [= ["table" first-name]
361 [not [= ["table" emplid]
363 :order-by '(["table" last-name])
364 :result-types nil :field-names nil))
365 (("Vladimir" "Lenin") ("Vladimir" "Putin")))
367 (deftest :fdml/select/27
368 (with-dataset *ds-employees*
371 (clsql:select [coalesce [managerid] 10] :from [employee] :order-by [emplid]
372 :field-names nil :result-types nil :flatp t)))
373 (10 1 1 1 1 1 1 1 1 1))
375 (deftest :fdml/select/28
376 (with-dataset *ds-employees*
377 (loop for column in `([*] [emplid])
381 (clsql:select [count column] :from [employee]
382 :flatp t :result-types nil :field-names nil)))))
385 (deftest :fdml/select/29
386 (with-dataset *ds-employees*
387 (clsql:select [first-name] [last-name] :from [employee]
388 :result-types nil :field-names nil
389 :order-by '(([first-name] :asc) ([last-name] :desc))))
390 (("Boris" "Yeltsin") ("Josef" "Stalin") ("Konstantin" "Chernenko")
391 ("Leon" "Trotsky") ("Leonid" "Brezhnev") ("Mikhail" "Gorbachev")
392 ("Nikita" "Kruschev") ("Vladimir" "Putin") ("Vladimir" "Lenin")
393 ("Yuri" "Andropov")))
395 (deftest :fdml/select/30
396 (with-dataset *ds-employees*
397 (clsql:select [first-name] [last-name] :from [employee]
398 :result-types nil :field-names nil
399 :order-by '(([first-name] :asc) ([last-name] :asc))))
400 (("Boris" "Yeltsin") ("Josef" "Stalin") ("Konstantin" "Chernenko")
401 ("Leon" "Trotsky") ("Leonid" "Brezhnev") ("Mikhail" "Gorbachev")
402 ("Nikita" "Kruschev") ("Vladimir" "Lenin") ("Vladimir" "Putin")
403 ("Yuri" "Andropov")))
405 (deftest :fdml/select/31
406 (with-dataset *ds-employees*
407 (clsql:select [last-name] :from [employee]
408 :set-operation [union [select [first-name] :from [employee]
409 :order-by [last-name]]]
413 ("Andropov" "Boris" "Brezhnev" "Chernenko" "Gorbachev" "Josef" "Konstantin"
414 "Kruschev" "Lenin" "Leon" "Leonid" "Mikhail" "Nikita" "Putin" "Stalin"
415 "Trotsky" "Vladimir" "Yeltsin" "Yuri"))
417 (deftest :fdml/select/32
418 (with-dataset *ds-employees*
421 (clsql:select [emplid] :from [employee]
422 :where [= [emplid] [any [select [companyid] :from [company]]]]
423 :flatp t :result-types nil :field-names nil)))
426 (deftest :fdml/select/33
427 (with-dataset *ds-employees*
428 (clsql:select [last-name] :from [employee]
429 :where [> [emplid] [all [select [groupid] :from [employee]]]]
430 :order-by [last-name]
431 :flatp t :result-types nil :field-names nil))
432 ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Putin" "Stalin"
433 "Trotsky" "Yeltsin"))
435 (deftest :fdml/select/34
436 (with-dataset *ds-employees*
437 (loop for x from 1 below 5
440 (clsql:select [last-name] :from [employee]
441 :where [= [emplid] x]
442 :flatp t :result-types nil :field-names nil))))
443 ("Lenin" "Stalin" "Trotsky" "Kruschev"))
445 ;; test escaping of single quotes
446 (deftest :fdml/select/35
447 (with-dataset *ds-fddl*
448 (first (clsql:select "What's up doc?" :from [alpha] :flatp t :field-names nil)))
451 ;; test proper treatment of backslash (depending on backend)
452 (deftest :fdml/select/36
453 (with-dataset *ds-fddl*
454 (first (clsql:select "foo\\bar\\baz" :from [alpha] :flatp t :field-names nil)))
457 (deftest :fdml/select/37
458 (with-dataset *ds-employees*
459 (clsql:select [emplid] :from [employee]
466 (deftest :fdml/select/38
467 (with-dataset *ds-employees*
468 (clsql:select [emplid] :from [employee]
476 (deftest :fdml/do-query/1
477 (with-dataset *ds-employees*
479 (clsql:do-query ((name) [select [last-name] :from [employee]
480 :order-by [last-name]])
483 ("Yeltsin" "Trotsky" "Stalin" "Putin" "Lenin" "Kruschev" "Gorbachev"
484 "Chernenko" "Brezhnev" "Andropov"))
486 (deftest :fdml/map-query/1
487 (with-dataset *ds-employees*
488 (clsql:map-query 'list #'identity
489 [select [last-name] :from [employee] :flatp t
490 :order-by [last-name]]))
491 ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
492 "Stalin" "Trotsky" "Yeltsin"))
494 (deftest :fdml/map-query/2
495 (with-dataset *ds-employees*
496 (clsql:map-query 'vector #'identity
497 [select [last-name] :from [employee] :flatp t
498 :order-by [last-name]]))
499 #("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
500 "Stalin" "Trotsky" "Yeltsin"))
502 (deftest :fdml/map-query/3
503 (with-dataset *ds-employees*
504 (clsql:map-query 'list #'identity
505 [select [last-name] :from [employee] :order-by [last-name]]))
506 (("Andropov") ("Brezhnev") ("Chernenko") ("Gorbachev") ("Kruschev") ("Lenin")
507 ("Putin") ("Stalin") ("Trotsky") ("Yeltsin")))
509 (deftest :fdml/map-query/4
510 (with-dataset *ds-employees*
511 (clsql:map-query 'list #'identity
512 [select [first-name] [last-name] :from [employee]
513 :order-by [last-name]]))
514 (("Yuri" "Andropov") ("Leonid" "Brezhnev") ("Konstantin" "Chernenko")
515 ("Mikhail" "Gorbachev") ("Nikita" "Kruschev") ("Vladimir" "Lenin")
516 ("Vladimir" "Putin") ("Josef" "Stalin") ("Leon" "Trotsky")
517 ("Boris" "Yeltsin")))
519 (deftest :fdml/loop/1
520 (with-dataset *ds-employees*
521 (loop for (forename surname)
523 [select [first-name] [last-name] :from [employee] :order-by [last-name]]
524 collect (concatenate 'string forename " " surname)))
525 ("Yuri Andropov" "Leonid Brezhnev" "Konstantin Chernenko" "Mikhail Gorbachev"
526 "Nikita Kruschev" "Vladimir Lenin" "Vladimir Putin"
527 "Josef Stalin" "Leon Trotsky" "Boris Yeltsin"))
529 (deftest :fdml/loop/2
530 (with-dataset *ds-employees*
531 (loop for (addressid)
533 [select [addressid] :from [addr] :order-by [addressid]]
537 (deftest :fdml/loop/3
538 (with-dataset *ds-employees*
541 [select [addressid] :from [addr] :order-by [addressid]]
545 ;; inserts a record using all values only and then deletes it
546 (deftest :fdml/insert/1
547 (with-dataset *ds-employees*
548 (let ((now (get-universal-time)))
549 (clsql:insert-records :into [employee]
550 :values `(11 1 "Yuri" "Gagarin" "gagarin@soviet.org"
551 1 1 1.85 t ,(clsql:utime->time now) ,now))
553 (clsql:select [first-name] [last-name] [email]
554 :from [employee] :where [= [emplid] 11])
555 (progn (clsql:delete-records :from [employee] :where [= [emplid] 11])
556 (clsql:select [*] :from [employee] :where [= [emplid] 11])))))
557 (("Yuri" "Gagarin" "gagarin@soviet.org")) nil)
559 ;; inserts a record using attributes and values and then deletes it
560 (deftest :fdml/insert/2
561 (with-dataset *ds-employees*
563 (clsql:insert-records :into [employee]
564 :attributes '(emplid groupid first_name last_name
565 email ecompanyid managerid)
566 :values '(11 1 "Yuri" "Gagarin" "gagarin@soviet.org"
569 (clsql:select [first-name] [last-name] [email] :from [employee]
570 :where [= [emplid] 11])
571 (progn (clsql:delete-records :from [employee] :where [= [emplid] 11])
572 (clsql:select [*] :from [employee] :where [= [emplid] 11])))))
573 (("Yuri" "Gagarin" "gagarin@soviet.org")) nil)
575 ;; inserts a record using av-pairs and then deletes it
576 (deftest :fdml/insert/3
577 (with-dataset *ds-employees*
579 (clsql:insert-records :into [employee]
580 :av-pairs'((emplid 11) (groupid 1)
582 (last_name "Gagarin")
583 (email "gagarin@soviet.org")
584 (ecompanyid 1) (managerid 1)))
586 (clsql:select [first-name] [last-name] [email] :from [employee]
587 :where [= [emplid] 11])
588 (progn (clsql:delete-records :from [employee] :where [= [emplid] 11])
589 (clsql:select [first-name] [last-name] [email] :from [employee]
590 :where [= [emplid] 11])))))
591 (("Yuri" "Gagarin" "gagarin@soviet.org")) nil)
593 ;; inserts a records using a query from another table
594 (deftest :fdml/insert/4
595 (with-dataset *ds-employees*
597 (clsql:create-table [employee2] '(([forename] string)
600 (clsql:insert-records :into [employee2]
601 :query [select [first-name] [last-name] [email]
603 :attributes '(forename surname email))
605 (equal (clsql:select [*] :from [employee2])
606 (clsql:select [first-name] [last-name] [email]
608 (clsql:drop-table [employee2] :if-does-not-exist :ignore))))
611 ;; updates a record using attributes and values and then deletes it
612 (deftest :fdml/update/1
613 (with-dataset *ds-employees*
615 (clsql:update-records [employee]
616 :attributes '(first_name last_name email)
617 :values '("Yuri" "Gagarin" "gagarin@soviet.org")
618 :where [= [emplid] 1])
620 (clsql:select [first-name] [last-name] [email] :from [employee]
621 :where [= [emplid] 1])
623 (clsql:update-records [employee]
624 :av-pairs'((first_name "Vladimir")
626 (email "lenin@soviet.org"))
627 :where [= [emplid] 1])
628 (clsql:select [first-name] [last-name] [email] :from [employee]
629 :where [= [emplid] 1])))))
630 (("Yuri" "Gagarin" "gagarin@soviet.org"))
631 (("Vladimir" "Lenin" "lenin@soviet.org")))
633 ;; updates a record using av-pairs and then deletes it
634 (deftest :fdml/update/2
635 (with-dataset *ds-employees*
637 (clsql:update-records [employee]
638 :av-pairs'((first_name "Yuri")
639 (last_name "Gagarin")
640 (email "gagarin@soviet.org"))
641 :where [= [emplid] 1])
643 (clsql:select [first-name] [last-name] [email] :from [employee]
644 :where [= [emplid] 1])
646 (clsql:update-records [employee]
647 :av-pairs'((first_name "Vladimir")
649 (email "lenin@soviet.org"))
650 :where [= [emplid] 1])
651 (clsql:select [first-name] [last-name] [email]
652 :from [employee] :where [= [emplid] 1])))))
653 (("Yuri" "Gagarin" "gagarin@soviet.org"))
654 (("Vladimir" "Lenin" "lenin@soviet.org")))
656 ;; starts a transaction deletes a record and then rolls back the deletion
657 (deftest :fdml/transaction/1
658 (with-dataset *ds-employees*
660 ;; test if we are in a transaction
661 (push (clsql:in-transaction-p) results)
662 ;;start a transaction
663 (clsql:start-transaction)
664 ;; test if we are in a transaction
665 (push (clsql:in-transaction-p) results)
666 ;;Putin has got to go
667 (clsql:delete-records :from [employee] :where [= [last-name] "Putin"])
670 (clsql:select [*] :from [employee] :where [= [last-name] "Putin"])
672 ;;Oh no, he's still there
674 ;; test that we are out of the transaction
675 (push (clsql:in-transaction-p) results)
676 ;; Check that we got him back alright
677 (push (clsql:select [email] :from [employee] :where [= [last-name] "Putin"]
680 (apply #'values (nreverse results))))
681 nil t nil nil ("putin@soviet.org"))
683 ;; starts a transaction, updates a record and then rolls back the update
684 (deftest :fdml/transaction/2
685 (with-dataset *ds-employees*
687 ;; test if we are in a transaction
688 (push (clsql:in-transaction-p) results)
689 ;;start a transaction
690 (clsql:start-transaction)
691 ;; test if we are in a transaction
692 (push (clsql:in-transaction-p) results)
693 ;;Putin has got to go
694 (clsql:update-records [employee]
695 :av-pairs '((email "putin-nospam@soviet.org"))
696 :where [= [last-name] "Putin"])
697 ;;Should be new value
698 (push (clsql:select [email] :from [employee]
699 :where [= [last-name] "Putin"]
702 ;;Oh no, he's still there
704 ;; test that we are out of the transaction
705 (push (clsql:in-transaction-p) results)
706 ;; Check that we got him back alright
707 (push (clsql:select [email] :from [employee] :where [= [last-name] "Putin"]
710 (apply #'values (nreverse results))))
711 nil t ("putin-nospam@soviet.org") nil ("putin@soviet.org"))
713 ;; runs an update within a transaction and checks it is committed
714 (deftest :fdml/transaction/3
715 (with-dataset *ds-employees*
718 (push (clsql:in-transaction-p) results)
721 (clsql:with-transaction ()
722 (clsql:update-records [employee]
723 :av-pairs '((email "lenin-nospam@soviet.org"))
724 :where [= [emplid] 1]))
727 (push (clsql:in-transaction-p) results)
728 ;; check that was committed
729 (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
734 (clsql:with-transaction ()
735 (clsql:update-records [employee]
736 :av-pairs '((email "lenin@soviet.org"))
737 :where [= [emplid] 1]))
740 (push (clsql:in-transaction-p) results)
741 ;; check that was committed
742 (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
745 (apply #'values (nreverse results))))
746 nil nil nil ("lenin-nospam@soviet.org") nil nil ("lenin@soviet.org"))
748 ;; runs a valid update and an invalid one within a transaction and checks
749 ;; that the valid update is rolled back when the invalid one fails.
750 (deftest :fdml/transaction/4
751 (with-dataset *ds-employees*
754 (push (clsql:in-transaction-p) results)
756 (clsql:with-transaction ()
758 (clsql:update-records [employee]
759 :av-pairs '((email "lenin-nospam@soviet.org"))
760 :where [= [emplid] 1])
761 ;; invalid update which generates an error
762 (clsql:update-records [employee]
764 '((emale "lenin-nospam@soviet.org"))
765 :where [= [emplid] 1]))
766 (clsql:sql-database-error ()
769 (push (clsql:in-transaction-p) results)
770 ;; and check nothing done
771 (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
774 (apply #'values (nreverse results)))))))
775 nil nil ("lenin@soviet.org"))