r9186: add attribute caching, improve inititialize-database-type
[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')" :field-names nil)
133   (("10")))
134
135 (deftest :fdml/query/2
136     (multiple-value-bind (rows field-names)
137         (clsql:query
138          "SELECT FIRST_NAME,LAST_NAME FROM EMPLOYEE WHERE (EMPLID <= 5) ORDER BY LAST_NAME")
139       (values rows (mapcar 'string-upcase field-names)))
140   (("Leonid" "Brezhnev") ("Nikita" "Kruschev") ("Vladamir" "Lenin")
141    ("Josef" "Stalin") ("Leon" "Trotsky"))
142   ("FIRST_NAME" "LAST_NAME"))
143
144   
145 (deftest :fdml/execute-command/1
146     (values
147      (clsql:table-exists-p [foo] :owner *test-database-user*)
148      (progn
149        (clsql:execute-command "create table foo (bar integer)")
150        (clsql:table-exists-p [foo] :owner *test-database-user*))
151      (progn
152        (clsql:execute-command "drop table foo")
153        (clsql:table-exists-p [foo] :owner *test-database-user*)))
154   nil t nil)
155
156
157 ;; compare min, max and average hieghts in inches (they're quite short
158 ;; these guys!) 
159 (deftest :fdml/select/1
160     (let ((max (clsql:select [function "floor"
161                              [/ [* [max [height]] 100] 2.54]]
162                              :from [employee]
163                              :flatp t))
164           (min (clsql:select [function "floor"
165                              [/ [* [min [height]] 100] 2.54]]
166                              :from [employee]
167                              :flatp t))
168           (avg (clsql:select [function "floor"
169                              [avg [/ [* [height] 100] 2.54]]]
170                              :from [employee]
171                              :flatp t)))
172       (apply #'< (mapcar #'(lambda (s) (parse-integer s :junk-allowed t))
173                          (append min avg max))))
174   t)
175
176 (deftest :fdml/select/2
177     (clsql:select [first-name] :from [employee] :flatp t :distinct t
178                  :order-by [first-name])
179   ("Boris" "Josef" "Konstantin" "Leon" "Leonid" "Mikhail" "Nikita" "Vladamir"
180            "Yuri"))
181
182 (deftest :fdml/select/3
183     (clsql:select [first-name] [count [*]] :from [employee]
184                  :group-by [first-name]
185                  :order-by [first-name])
186   (("Boris" "1") ("Josef" "1") ("Konstantin" "1") ("Leon" "1") ("Leonid" "1")
187    ("Mikhail" "1") ("Nikita" "1") ("Vladamir" "2") ("Yuri" "1")))
188
189 (deftest :fdml/select/4
190     (clsql:select [last-name] :from [employee] :where [like [email] "%org"]
191                  :order-by [last-name]
192                  :flatp t)
193   ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
194               "Stalin" "Trotsky" "Yeltsin"))
195
196 (deftest :fdml/select/5
197     (clsql:select [email] :from [employee] :flatp t 
198                  :where [in [employee emplid]
199                             [select [managerid] :from [employee]]])
200   ("lenin@soviet.org"))
201
202 (deftest :fdml/select/6
203     (if (db-type-has-fancy-math? *test-database-underlying-type*)
204         (mapcar #'(lambda (s) (parse-integer s :junk-allowed t))
205          (clsql:select [function "trunc" [height]] :from [employee]
206                        :flatp t))
207       (mapcar #'(lambda (s) (truncate (parse-integer s :junk-allowed t)))
208        (clsql:select [height] :from [employee] :flatp t)))
209   (1 1 1 1 1 1 1 1 1 1))
210
211 (deftest :fdml/select/7
212     (clsql:select [max [emplid]] :from [employee] :flatp t)
213   ("10"))
214
215 (deftest :fdml/select/8
216     (clsql:select [min [emplid]] :from [employee] :flatp t)
217   ("1"))
218
219 (deftest :fdml/select/9
220     (subseq (car (clsql:select [avg [emplid]] :from [employee] :flatp t)) 0 3)
221   "5.5")
222
223 (deftest :fdml/select/10
224     (clsql:select [last-name] :from [employee]
225                 :where [not [in [emplid]
226                                 [select [managerid] :from [company]]]]
227                 :flatp t
228                 :order-by [last-name])
229   ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Putin" "Stalin"
230               "Trotsky" "Yeltsin"))
231
232 (deftest :fdml/select/11
233     (clsql:select [last-name] :from [employee] :where [married] :flatp t
234                  :order-by [emplid])
235   ("Lenin" "Stalin" "Trotsky"))
236
237 (deftest :fdml/select/12
238     (let ((v 1))
239       (clsql:select [last-name] :from [employee] :where [= [emplid] v]))
240   (("Lenin")))
241
242 ;(deftest :fdml/select/11
243 ;    (clsql:select [emplid] :from [employee]
244 ;                :where [= [emplid] [any [select [companyid] :from [company]]]]
245 ;                :flatp t)
246 ;  ("1"))
247
248 (deftest :fdml/do-query/1
249     (let ((result '()))
250     (clsql:do-query ((name) [select [last-name] :from [employee]
251                                    :order-by [last-name]])
252       (push name result))
253     result)
254  ("Yeltsin" "Trotsky" "Stalin" "Putin" "Lenin" "Kruschev" "Gorbachev"
255             "Chernenko" "Brezhnev" "Andropov")) 
256
257 (deftest :fdml/map-query/1
258     (clsql:map-query 'list #'identity
259                     [select [last-name] :from [employee] :flatp t
260                             :order-by [last-name]])
261   ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
262               "Stalin" "Trotsky" "Yeltsin"))
263
264 (deftest :fdml/map-query/2
265     (clsql:map-query 'vector #'identity
266                     [select [last-name] :from [employee] :flatp t
267                             :order-by [last-name]])
268   #("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
269     "Stalin" "Trotsky" "Yeltsin"))
270   
271 (deftest :fdml/loop/1
272     (loop for (forename surname)
273       being each tuple in
274       [select [first-name] [last-name] :from [employee] :order-by [last-name]]
275       collect (concatenate 'string forename " " surname))
276   ("Yuri Andropov" "Leonid Brezhnev" "Konstantin Chernenko" "Mikhail Gorbachev"
277                    "Nikita Kruschev" "Vladamir Lenin" "Vladamir Putin"
278                    "Josef Stalin" "Leon Trotsky" "Boris Yeltsin"))
279
280 ;; starts a transaction deletes a record and then rolls back the deletion 
281 (deftest :fdml/transaction/1
282     (let ((results '()))
283       ;; test if we are in a transaction
284       (push (clsql:in-transaction-p) results)
285       ;;start a transaction 
286       (clsql:start-transaction)
287       ;; test if we are in a transaction
288       (push (clsql:in-transaction-p) results)
289       ;;Putin has got to go
290       (clsql:delete-records :from [employee] :where [= [last-name] "Putin"])
291       ;;Should be nil 
292       (push 
293        (clsql:select [*] :from [employee] :where [= [last-name] "Putin"])
294        results)
295       ;;Oh no, he's still there
296       (clsql:rollback)
297       ;; test that we are out of the transaction
298       (push (clsql:in-transaction-p) results)
299       ;; Check that we got him back alright 
300       (push (clsql:select [email] :from [employee] :where [= [last-name] "Putin"]
301                          :flatp t)
302             results)
303       (apply #'values (nreverse results)))
304   nil t nil nil ("putin@soviet.org"))
305
306 ;; starts a transaction, updates a record and then rolls back the update
307 (deftest :fdml/transaction/2
308     (let ((results '()))
309       ;; test if we are in a transaction
310       (push (clsql:in-transaction-p) results)
311       ;;start a transaction 
312       (clsql:start-transaction)
313       ;; test if we are in a transaction
314       (push (clsql:in-transaction-p) results)
315       ;;Putin has got to go
316       (clsql:update-records [employee]
317        :av-pairs '((email "putin-nospam@soviet.org"))
318        :where [= [last-name] "Putin"])
319       ;;Should be new value  
320       (push (clsql:select [email] :from [employee]
321                          :where [= [last-name] "Putin"]
322                          :flatp t)
323             results)
324       ;;Oh no, he's still there
325       (clsql:rollback)
326       ;; test that we are out of the transaction
327       (push (clsql:in-transaction-p) results)
328       ;; Check that we got him back alright 
329       (push (clsql:select [email] :from [employee] :where [= [last-name] "Putin"]
330                          :flatp t)
331             results)
332       (apply #'values (nreverse results)))
333   nil t ("putin-nospam@soviet.org") nil ("putin@soviet.org")) 
334
335 ;; runs an update within a transaction and checks it is committed
336 (deftest :fdml/transaction/3
337     (let ((results '()))
338       ;; check status 
339       (push (clsql:in-transaction-p) results)
340       ;; update records 
341       (push
342        (clsql:with-transaction () 
343          (clsql:update-records [employee] 
344                               :av-pairs '((email "lenin-nospam@soviet.org"))
345                               :where [= [emplid] 1]))
346        results)
347       ;; check status 
348       (push (clsql:in-transaction-p) results)
349       ;; check that was committed 
350       (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
351                          :flatp t)
352             results)
353       ;; undo the changes 
354       (push
355        (clsql:with-transaction () 
356          (clsql:update-records [employee] 
357                               :av-pairs '((email "lenin@soviet.org"))
358                               :where [= [emplid] 1]))
359        results)
360       ;; and check status 
361       (push (clsql:in-transaction-p) results)
362       ;; check that was committed 
363       (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
364                          :flatp t)
365             results)
366       (apply #'values (nreverse results)))
367   nil :committed nil ("lenin-nospam@soviet.org") :committed
368   nil ("lenin@soviet.org"))
369
370 ;; runs a valid update and an invalid one within a transaction and checks
371 ;; that the valid update is rolled back when the invalid one fails. 
372 (deftest :fdml/transaction/4
373     (let ((results '()))
374       ;; check status
375       (push (clsql:in-transaction-p) results)
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-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)