r8910: rework so that tests are automatically run for multiple backends
[clsql.git] / tests / test-fdml.lisp
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
6 ;;;; Updated: $Id$
7 ;;;; ======================================================================
8 ;;;;
9 ;;;; Description ==========================================================
10 ;;;; ======================================================================
11 ;;;;
12 ;;;; Tests for the CLSQL Functional Data Manipulation Language
13 ;;;; (FDML).
14 ;;;; 
15 ;;;; ======================================================================
16
17 (in-package #:clsql-tests)
18
19 #.(clsql:locally-enable-sql-reader-syntax)
20
21 (setq *rt-fdml*
22       '(
23         
24 ;; inserts a record using all values only and then deletes it 
25 (deftest :fdml/insert/1
26     (progn
27       (clsql:insert-records :into [employee] 
28                            :values `(11 1 "Yuri" "Gagarin" "gagarin@soviet.org"
29                                      1 1 1.85 t ,(clsql-base:get-time)))
30       (values 
31        (clsql:select [first-name] [last-name] [email]
32                     :from [employee] :where [= [emplid] 11])
33        (progn (clsql:delete-records :from [employee] :where [= [emplid] 11])
34               (clsql:select [*] :from [employee] :where [= [emplid] 11]))))
35   (("Yuri" "Gagarin" "gagarin@soviet.org")) nil)
36
37 ;; inserts a record using attributes and values and then deletes it
38 (deftest :fdml/insert/2
39     (progn
40       (clsql:insert-records :into [employee] 
41                            :attributes '(emplid groupid first_name last_name
42                                          email companyid managerid)
43                            :values '(11 1 "Yuri" "Gagarin" "gagarin@soviet.org"
44                                      1 1))
45       (values 
46        (clsql:select [first-name] [last-name] [email] :from [employee]
47                     :where [= [emplid] 11])
48        (progn (clsql:delete-records :from [employee] :where [= [emplid] 11])
49               (clsql:select [*] :from [employee] :where [= [emplid] 11]))))
50   (("Yuri" "Gagarin" "gagarin@soviet.org")) nil)
51
52 ;; inserts a record using av-pairs and then deletes it
53 (deftest :fdml/insert/3
54     (progn
55       (clsql:insert-records :into [employee] 
56                            :av-pairs'((emplid 11) (groupid 1)
57                                       (first_name "Yuri")
58                                       (last_name "Gagarin")
59                                       (email "gagarin@soviet.org")
60                                       (companyid 1) (managerid 1)))
61       (values 
62        (clsql:select [first-name] [last-name] [email] :from [employee]
63                     :where [= [emplid] 11])
64        (progn (clsql:delete-records :from [employee] :where [= [emplid] 11])
65               (clsql:select [first-name] [last-name] [email] :from [employee]
66                            :where [= [emplid] 11]))))
67   (("Yuri" "Gagarin" "gagarin@soviet.org")) nil)
68
69 ;; inserts a records using a query from another table 
70 (deftest :fdml/insert/4
71     (progn
72       (clsql:create-table [employee2] '(([forename] string)
73                                  ([surname] string)
74                                  ([email] string)))
75       (clsql:insert-records :into [employee2] 
76                      :query [select [first-name] [last-name] [email] 
77                                     :from [employee]]
78                      :attributes '(forename surname email))
79       (prog1
80           (equal (clsql:select [*] :from [employee2])
81                  (clsql:select [first-name] [last-name] [email]
82                               :from [employee]))
83         (clsql:drop-table [employee2] :if-does-not-exist :ignore)))
84   t)
85
86 ;; updates a record using attributes and values and then deletes it
87 (deftest :fdml/update/1
88     (progn
89       (clsql:update-records [employee] 
90                            :attributes '(first_name last_name email)
91                            :values '("Yuri" "Gagarin" "gagarin@soviet.org")
92                            :where [= [emplid] 1])
93       (values 
94        (clsql:select [first-name] [last-name] [email] :from [employee]
95                     :where [= [emplid] 1])
96        (progn
97          (clsql:update-records [employee] 
98                               :av-pairs'((first_name "Vladamir")
99                                          (last_name "Lenin")
100                                          (email "lenin@soviet.org"))
101                               :where [= [emplid] 1])
102          (clsql:select [first-name] [last-name] [email] :from [employee]
103                       :where [= [emplid] 1]))))
104   (("Yuri" "Gagarin" "gagarin@soviet.org"))
105   (("Vladamir" "Lenin" "lenin@soviet.org")))
106
107 ;; updates a record using av-pairs and then deletes it
108 (deftest :fdml/update/2
109     (progn
110       (clsql:update-records [employee] 
111                            :av-pairs'((first_name "Yuri")
112                                       (last_name "Gagarin")
113                                       (email "gagarin@soviet.org"))
114                            :where [= [emplid] 1])
115       (values 
116        (clsql:select [first-name] [last-name] [email] :from [employee]
117                     :where [= [emplid] 1])
118        (progn
119          (clsql:update-records [employee]
120                               :av-pairs'((first_name "Vladamir")
121                                          (last_name "Lenin")
122                                          (email "lenin@soviet.org"))
123                               :where [= [emplid] 1])
124          (clsql:select [first-name] [last-name] [email]
125                       :from [employee] :where [= [emplid] 1]))))
126   (("Yuri" "Gagarin" "gagarin@soviet.org"))
127   (("Vladamir" "Lenin" "lenin@soviet.org")))
128
129
130 (deftest :fdml/query/1
131     (clsql:query "SELECT COUNT(*) FROM EMPLOYEE WHERE (EMAIL LIKE '%org')")
132   (("10")))
133
134 (deftest :fdml/query/2
135     (clsql:query
136      "SELECT FIRST_NAME,LAST_NAME FROM EMPLOYEE WHERE (EMPLID <= 5) ORDER BY LAST_NAME")
137   (("Leonid" "Brezhnev") ("Nikita" "Kruschev") ("Vladamir" "Lenin")
138  ("Josef" "Stalin") ("Leon" "Trotsky")))
139
140   
141 (deftest :fdml/execute-command/1
142     (values
143      (clsql:table-exists-p [foo] :owner *test-database-user*)
144      (progn
145        (clsql:execute-command "create table foo (bar integer)")
146        (clsql:table-exists-p [foo] :owner *test-database-user*))
147      (progn
148        (clsql:execute-command "drop table foo")
149        (clsql:table-exists-p [foo] :owner *test-database-user*)))
150   nil t nil)
151
152
153 ;; compare min, max and average hieghts in inches (they're quite short
154 ;; these guys!) -- only works with pgsql 
155 (deftest :fdml/select/1
156     (if (member *test-database-type* '(:postgresql-socket :postgresql))
157         (let ((max (clsql:select [function "floor"
158                                           [/ [* [max [height]] 100] 2.54]]
159                                 :from [employee]
160                                 :flatp t))
161               (min (clsql:select [function "floor"
162                                           [/ [* [min [height]] 100] 2.54]]
163                                 :from [employee]
164                                 :flatp t))
165               (avg (clsql:select [function "floor"
166                                           [avg [/ [* [height] 100] 2.54]]]
167                                 :from [employee]
168                                 :flatp t)))
169           (apply #'< (mapcar #'parse-integer (append min avg max))))
170         t)
171   t)
172
173 (deftest :fdml/select/2
174     (clsql:select [first-name] :from [employee] :flatp t :distinct t
175                  :order-by [first-name])
176   ("Boris" "Josef" "Konstantin" "Leon" "Leonid" "Mikhail" "Nikita" "Vladamir"
177            "Yuri"))
178
179 (deftest :fdml/select/3
180     (clsql:select [first-name] [count [*]] :from [employee]
181                  :group-by [first-name]
182                  :order-by [first-name])
183   (("Boris" "1") ("Josef" "1") ("Konstantin" "1") ("Leon" "1") ("Leonid" "1")
184    ("Mikhail" "1") ("Nikita" "1") ("Vladamir" "2") ("Yuri" "1")))
185
186 (deftest :fdml/select/4
187     (clsql:select [last-name] :from [employee] :where [like [email] "%org"]
188                  :order-by [last-name]
189                  :flatp t)
190   ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
191               "Stalin" "Trotsky" "Yeltsin"))
192
193 (deftest :fdml/select/5
194     (clsql:select [email] :from [employee] :flatp t 
195                  :where [in [employee emplid]
196                             [select [managerid] :from [employee]]])
197   ("lenin@soviet.org"))
198
199 (deftest :fdml/select/6
200     (if (member *test-database-type* '(:postgresql-socket :postgresql))
201         (mapcar #'parse-integer
202                 (clsql:select [function "trunc" [height]] :from [employee]
203                              :flatp t))
204         (mapcar #'(lambda (s) (truncate (parse-integer s :junk-allowed t)))
205                 (clsql:select [height] :from [employee] :flatp t)))
206   (1 1 1 1 1 1 1 1 1 1))
207
208 (deftest :fdml/select/7
209     (clsql:select [max [emplid]] :from [employee] :flatp t)
210   ("10"))
211
212 (deftest :fdml/select/8
213     (clsql:select [min [emplid]] :from [employee] :flatp t)
214   ("1"))
215
216 (deftest :fdml/select/9
217     (subseq (car (clsql:select [avg [emplid]] :from [employee] :flatp t)) 0 3)
218   "5.5")
219
220 (deftest :fdml/select/10
221     (clsql:select [last-name] :from [employee]
222                 :where [not [in [emplid]
223                                 [select [managerid] :from  [company]]]]
224                 :flatp t
225                 :order-by [last-name])
226   ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Putin" "Stalin"
227               "Trotsky" "Yeltsin"))
228
229 (deftest :fdml/select/11
230     (clsql:select [last-name] :from [employee] :where [married] :flatp t
231                  :order-by [emplid])
232   ("Lenin" "Stalin" "Trotsky"))
233
234 (deftest :fdml/select/12
235     (let ((v 1))
236       (clsql:select [last-name] :from [employee] :where [= [emplid] v]))
237   (("Lenin")))
238
239 ;(deftest :fdml/select/11
240 ;    (clsql:select [emplid] :from [employee]
241 ;                :where [= [emplid] [any [select [companyid] :from [company]]]]
242 ;                :flatp t)
243 ;  ("1"))
244
245 (deftest :fdml/do-query/1
246     (let ((result '()))
247     (clsql:do-query ((name) [select [last-name] :from [employee]
248                                    :order-by [last-name]])
249       (push name result))
250     result)
251  ("Yeltsin" "Trotsky" "Stalin" "Putin" "Lenin" "Kruschev" "Gorbachev"
252             "Chernenko" "Brezhnev" "Andropov")) 
253
254 (deftest :fdml/map-query/1
255     (clsql:map-query 'list #'identity
256                     [select [last-name] :from [employee] :flatp t
257                             :order-by [last-name]])
258   ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
259               "Stalin" "Trotsky" "Yeltsin"))
260
261 (deftest :fdml/map-query/2
262     (clsql:map-query 'vector #'identity
263                     [select [last-name] :from [employee] :flatp t
264                             :order-by [last-name]])
265   #("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
266     "Stalin" "Trotsky" "Yeltsin"))
267   
268 (deftest :fdml/loop/1
269     (loop for (forename surname)
270       being each tuple in
271       [select [first-name] [last-name] :from [employee] :order-by [last-name]]
272       collect (concatenate 'string forename " " surname))
273   ("Yuri Andropov" "Leonid Brezhnev" "Konstantin Chernenko" "Mikhail Gorbachev"
274                    "Nikita Kruschev" "Vladamir Lenin" "Vladamir Putin"
275                    "Josef Stalin" "Leon Trotsky" "Boris Yeltsin"))
276
277 ;; starts a transaction deletes a record and then rolls back the deletion 
278 (deftest :fdml/transaction/1
279     (let ((results '()))
280       ;; test if we are in a transaction
281       (push (clsql:in-transaction-p) results)
282       ;;start a transaction 
283       (clsql:start-transaction)
284       ;; test if we are in a transaction
285       (push (clsql:in-transaction-p) results)
286       ;;Putin has got to go
287       (unless (eql *test-database-type* :mysql)
288         (clsql:delete-records :from [employee] :where [= [last-name] "Putin"]))
289       ;;Should be nil 
290       (push 
291        (clsql:select [*] :from [employee] :where [= [last-name] "Putin"])
292        results)
293       ;;Oh no, he's still there
294       (clsql:rollback)
295       ;; test that we are out of the transaction
296       (push (clsql:in-transaction-p) results)
297       ;; Check that we got him back alright 
298       (push (clsql:select [email] :from [employee] :where [= [last-name] "Putin"]
299                          :flatp t)
300             results)
301       (apply #'values (nreverse results)))
302   nil t nil nil ("putin@soviet.org"))
303
304 ;; starts a transaction, updates a record and then rolls back the update
305 (deftest :fdml/transaction/2
306     (let ((results '()))
307       ;; test if we are in a transaction
308       (push (clsql:in-transaction-p) results)
309       ;;start a transaction 
310       (clsql:start-transaction)
311       ;; test if we are in a transaction
312       (push (clsql:in-transaction-p) results)
313       ;;Putin has got to go
314       (unless (eql *test-database-type* :mysql)
315         (clsql:update-records [employee]
316                              :av-pairs '((email "putin-nospam@soviet.org"))
317                              :where [= [last-name] "Putin"]))
318       ;;Should be new value  
319       (push (clsql:select [email] :from [employee]
320                          :where [= [last-name] "Putin"]
321                          :flatp t)
322             results)
323       ;;Oh no, he's still there
324       (clsql:rollback)
325       ;; test that we are out of the transaction
326       (push (clsql:in-transaction-p) results)
327       ;; Check that we got him back alright 
328       (push (clsql:select [email] :from [employee] :where [= [last-name] "Putin"]
329                          :flatp t)
330             results)
331       (apply #'values (nreverse results)))
332   nil t ("putin-nospam@soviet.org") nil ("putin@soviet.org")) 
333
334 ;; runs an update within a transaction and checks it is committed
335 (deftest :fdml/transaction/3
336     (let ((results '()))
337       ;; check status 
338       (push (clsql:in-transaction-p) results)
339       ;; update records 
340       (push
341        (clsql:with-transaction () 
342          (clsql:update-records [employee] 
343                               :av-pairs '((email "lenin-nospam@soviet.org"))
344                               :where [= [emplid] 1]))
345        results)
346       ;; check status 
347       (push (clsql:in-transaction-p) results)
348       ;; check that was committed 
349       (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
350                          :flatp t)
351             results)
352       ;; undo the changes 
353       (push
354        (clsql:with-transaction () 
355          (clsql:update-records [employee] 
356                               :av-pairs '((email "lenin@soviet.org"))
357                               :where [= [emplid] 1]))
358        results)
359       ;; and check status 
360       (push (clsql:in-transaction-p) results)
361       ;; check that was committed 
362       (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
363                          :flatp t)
364             results)
365       (apply #'values (nreverse results)))
366   nil :COMMITTED nil ("lenin-nospam@soviet.org") :COMMITTED
367   nil ("lenin@soviet.org"))
368
369 ;; runs a valid update and an invalid one within a transaction and checks
370 ;; that the valid update is rolled back when the invalid one fails. 
371 (deftest :fdml/transaction/4
372     (let ((results '()))
373       ;; check status
374       (push (clsql:in-transaction-p) results)
375       (unless (eql *test-database-type* :mysql)
376         (handler-case 
377             (clsql:with-transaction () 
378               ;; valid update
379               (clsql:update-records [employee] 
380                                    :av-pairs '((email "lenin-nospam@soviet.org"))
381                                  :where [= [emplid] 1])
382             ;; invalid update which generates an error 
383             (clsql:update-records [employee] 
384                                  :av-pairs
385                                  '((emale "lenin-nospam@soviet.org"))
386                                  :where [= [emplid] 1]))
387         (clsql:clsql-sql-error ()
388           (progn
389             ;; check status 
390             (push (clsql:in-transaction-p) results)
391             ;; and check nothing done 
392             (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
393                                :flatp t)
394                   results)
395             (apply #'values (nreverse results)))))))
396   nil nil ("lenin@soviet.org"))
397
398 ))
399
400 #.(clsql:restore-sql-reader-syntax-state)