r9119: Automated commit for Debian build of clsql upstream-version-2.9.2
[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 ;;;; Tests for the CLSQL Functional Data Manipulation Language
9 ;;;; (FDML).
10 ;;;;
11 ;;;; This file is part of CLSQL.
12 ;;;;
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 ;;;; ======================================================================
17
18 (in-package #:clsql-tests)
19
20 #.(clsql:locally-enable-sql-reader-syntax)
21
22 (setq *rt-fdml*
23       '(
24         
25 ;; inserts a record using all values only and then deletes it 
26 (deftest :fdml/insert/1
27     (progn
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)))
31       (values 
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)
37
38 ;; inserts a record using attributes and values and then deletes it
39 (deftest :fdml/insert/2
40     (progn
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"
45                                      1 1))
46       (values 
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)
52
53 ;; inserts a record using av-pairs and then deletes it
54 (deftest :fdml/insert/3
55     (progn
56       (clsql:insert-records :into [employee] 
57                            :av-pairs'((emplid 11) (groupid 1)
58                                       (first_name "Yuri")
59                                       (last_name "Gagarin")
60                                       (email "gagarin@soviet.org")
61                                       (companyid 1) (managerid 1)))
62       (values 
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)
69
70 ;; inserts a records using a query from another table 
71 (deftest :fdml/insert/4
72     (progn
73       (clsql:create-table [employee2] '(([forename] string)
74                                  ([surname] string)
75                                  ([email] string)))
76       (clsql:insert-records :into [employee2] 
77                      :query [select [first-name] [last-name] [email] 
78                                     :from [employee]]
79                      :attributes '(forename surname email))
80       (prog1
81           (equal (clsql:select [*] :from [employee2])
82                  (clsql:select [first-name] [last-name] [email]
83                               :from [employee]))
84         (clsql:drop-table [employee2] :if-does-not-exist :ignore)))
85   t)
86
87 ;; updates a record using attributes and values and then deletes it
88 (deftest :fdml/update/1
89     (progn
90       (clsql:update-records [employee] 
91                            :attributes '(first_name last_name email)
92                            :values '("Yuri" "Gagarin" "gagarin@soviet.org")
93                            :where [= [emplid] 1])
94       (values 
95        (clsql:select [first-name] [last-name] [email] :from [employee]
96                     :where [= [emplid] 1])
97        (progn
98          (clsql:update-records [employee] 
99                               :av-pairs'((first_name "Vladamir")
100                                          (last_name "Lenin")
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")))
107
108 ;; updates a record using av-pairs and then deletes it
109 (deftest :fdml/update/2
110     (progn
111       (clsql:update-records [employee] 
112                            :av-pairs'((first_name "Yuri")
113                                       (last_name "Gagarin")
114                                       (email "gagarin@soviet.org"))
115                            :where [= [emplid] 1])
116       (values 
117        (clsql:select [first-name] [last-name] [email] :from [employee]
118                     :where [= [emplid] 1])
119        (progn
120          (clsql:update-records [employee]
121                               :av-pairs'((first_name "Vladamir")
122                                          (last_name "Lenin")
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")))
129
130
131 (deftest :fdml/query/1
132     (clsql:query "SELECT COUNT(*) FROM EMPLOYEE WHERE (EMAIL LIKE '%org')")
133   (("10")))
134
135 (deftest :fdml/query/2
136     (clsql:query
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")))
140
141   
142 (deftest :fdml/execute-command/1
143     (values
144      (clsql:table-exists-p [foo] :owner *test-database-user*)
145      (progn
146        (clsql:execute-command "create table foo (bar integer)")
147        (clsql:table-exists-p [foo] :owner *test-database-user*))
148      (progn
149        (clsql:execute-command "drop table foo")
150        (clsql:table-exists-p [foo] :owner *test-database-user*)))
151   nil t nil)
152
153
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]]
160                                 :from [employee]
161                                 :flatp t))
162               (min (clsql:select [function "floor"
163                                           [/ [* [min [height]] 100] 2.54]]
164                                 :from [employee]
165                                 :flatp t))
166               (avg (clsql:select [function "floor"
167                                           [avg [/ [* [height] 100] 2.54]]]
168                                 :from [employee]
169                                 :flatp t)))
170           (apply #'< (mapcar #'parse-integer (append min avg max))))
171         t)
172   t)
173
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"
178            "Yuri"))
179
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")))
186
187 (deftest :fdml/select/4
188     (clsql:select [last-name] :from [employee] :where [like [email] "%org"]
189                  :order-by [last-name]
190                  :flatp t)
191   ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
192               "Stalin" "Trotsky" "Yeltsin"))
193
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"))
199
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]
204                              :flatp t))
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))
208
209 (deftest :fdml/select/7
210     (clsql:select [max [emplid]] :from [employee] :flatp t)
211   ("10"))
212
213 (deftest :fdml/select/8
214     (clsql:select [min [emplid]] :from [employee] :flatp t)
215   ("1"))
216
217 (deftest :fdml/select/9
218     (subseq (car (clsql:select [avg [emplid]] :from [employee] :flatp t)) 0 3)
219   "5.5")
220
221 (deftest :fdml/select/10
222     (clsql:select [last-name] :from [employee]
223                 :where [not [in [emplid]
224                                 [select [managerid] :from [company]]]]
225                 :flatp t
226                 :order-by [last-name])
227   ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Putin" "Stalin"
228               "Trotsky" "Yeltsin"))
229
230 (deftest :fdml/select/11
231     (clsql:select [last-name] :from [employee] :where [married] :flatp t
232                  :order-by [emplid])
233   ("Lenin" "Stalin" "Trotsky"))
234
235 (deftest :fdml/select/12
236     (let ((v 1))
237       (clsql:select [last-name] :from [employee] :where [= [emplid] v]))
238   (("Lenin")))
239
240 ;(deftest :fdml/select/11
241 ;    (clsql:select [emplid] :from [employee]
242 ;                :where [= [emplid] [any [select [companyid] :from [company]]]]
243 ;                :flatp t)
244 ;  ("1"))
245
246 (deftest :fdml/do-query/1
247     (let ((result '()))
248     (clsql:do-query ((name) [select [last-name] :from [employee]
249                                    :order-by [last-name]])
250       (push name result))
251     result)
252  ("Yeltsin" "Trotsky" "Stalin" "Putin" "Lenin" "Kruschev" "Gorbachev"
253             "Chernenko" "Brezhnev" "Andropov")) 
254
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"))
261
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"))
268   
269 (deftest :fdml/loop/1
270     (loop for (forename surname)
271       being each tuple in
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"))
277
278 ;; starts a transaction deletes a record and then rolls back the deletion 
279 (deftest :fdml/transaction/1
280     (let ((results '()))
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       (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       (clsql:update-records [employee]
315        :av-pairs '((email "putin-nospam@soviet.org"))
316        :where [= [last-name] "Putin"])
317       ;;Should be new value  
318       (push (clsql:select [email] :from [employee]
319                          :where [= [last-name] "Putin"]
320                          :flatp t)
321             results)
322       ;;Oh no, he's still there
323       (clsql:rollback)
324       ;; test that we are out of the transaction
325       (push (clsql:in-transaction-p) results)
326       ;; Check that we got him back alright 
327       (push (clsql:select [email] :from [employee] :where [= [last-name] "Putin"]
328                          :flatp t)
329             results)
330       (apply #'values (nreverse results)))
331   nil t ("putin-nospam@soviet.org") nil ("putin@soviet.org")) 
332
333 ;; runs an update within a transaction and checks it is committed
334 (deftest :fdml/transaction/3
335     (let ((results '()))
336       ;; check status 
337       (push (clsql:in-transaction-p) results)
338       ;; update records 
339       (push
340        (clsql:with-transaction () 
341          (clsql:update-records [employee] 
342                               :av-pairs '((email "lenin-nospam@soviet.org"))
343                               :where [= [emplid] 1]))
344        results)
345       ;; check status 
346       (push (clsql:in-transaction-p) results)
347       ;; check that was committed 
348       (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
349                          :flatp t)
350             results)
351       ;; undo the changes 
352       (push
353        (clsql:with-transaction () 
354          (clsql:update-records [employee] 
355                               :av-pairs '((email "lenin@soviet.org"))
356                               :where [= [emplid] 1]))
357        results)
358       ;; and check status 
359       (push (clsql:in-transaction-p) results)
360       ;; check that was committed 
361       (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
362                          :flatp t)
363             results)
364       (apply #'values (nreverse results)))
365   nil :COMMITTED nil ("lenin-nospam@soviet.org") :COMMITTED
366   nil ("lenin@soviet.org"))
367
368 ;; runs a valid update and an invalid one within a transaction and checks
369 ;; that the valid update is rolled back when the invalid one fails. 
370 (deftest :fdml/transaction/4
371     (let ((results '()))
372       ;; check status
373       (push (clsql:in-transaction-p) results)
374       (handler-case 
375           (clsql:with-transaction () 
376             ;; valid update
377             (clsql:update-records [employee] 
378                                   :av-pairs '((email "lenin-nospam@soviet.org"))
379                                   :where [= [emplid] 1])
380             ;; invalid update which generates an error 
381             (clsql:update-records [employee] 
382                                   :av-pairs
383                                   '((emale "lenin-nospam@soviet.org"))
384                                   :where [= [emplid] 1]))
385         (clsql:clsql-error ()
386           (progn
387             ;; check status 
388             (push (clsql:in-transaction-p) results)
389             ;; and check nothing done 
390             (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
391                                :flatp t)
392                   results)
393             (apply #'values (nreverse results))))))
394   nil nil ("lenin@soviet.org"))
395
396 ))
397
398 #.(clsql:restore-sql-reader-syntax-state)