r9212: Automated commit for Debian build of clsql upstream-version-2.10.8
[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 ;; 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))))
134       (if (stringp count)
135           (nth-value 0 (parse-integer count))
136         count))
137   10)
138
139 (deftest :fdml/query/2
140     (multiple-value-bind (rows field-names)
141         (clsql:query
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"))
147
148 (deftest :fdml/query/3
149     (caar (clsql:query "SELECT EMPLID FROM EMPLOYEE WHERE LAST_NAME = 'Andropov'" :field-names nil))
150   6)
151   
152 (deftest :fdml/query/4
153     (typep (caar (clsql:query "SELECT HEIGHT FROM EMPLOYEE WHERE LAST_NAME = 'Andropov'" :field-names nil))
154      'float)
155   t)
156   
157 (deftest :fdml/execute-command/1
158     (values
159      (clsql:table-exists-p [foo] :owner *test-database-user*)
160      (progn
161        (clsql:execute-command "create table foo (bar integer)")
162        (clsql:table-exists-p [foo] :owner *test-database-user*))
163      (progn
164        (clsql:execute-command "drop table foo")
165        (clsql:table-exists-p [foo] :owner *test-database-user*)))
166   nil t nil)
167
168
169 ;; compare min, max and average hieghts in inches (they're quite short
170 ;; these guys!) 
171 (deftest :fdml/select/1
172     (let ((max (clsql:select [function "floor"
173                              [/ [* [max [height]] 100] 2.54]]
174                              :from [employee]
175                              :result-types nil 
176                              :flatp t))
177           (min (clsql:select [function "floor"
178                              [/ [* [min [height]] 100] 2.54]]
179                              :from [employee]
180                              :result-types nil 
181                              :flatp t))
182           (avg (clsql:select [function "floor"
183                              [avg [/ [* [height] 100] 2.54]]]
184                              :from [employee]
185                              :result-types nil 
186                              :flatp t)))
187       (apply #'< (mapcar #'(lambda (s) (parse-integer s :junk-allowed t))
188                          (append min avg max))))
189  t)
190
191 (deftest :fdml/select/2
192  (clsql:select [first-name] :from [employee] :flatp t :distinct t
193                             :field-names nil 
194                             :result-types nil 
195                             :order-by [first-name])
196  ("Boris" "Josef" "Konstantin" "Leon" "Leonid" "Mikhail" "Nikita" "Vladamir"
197   "Yuri"))
198
199 (deftest :fdml/select/3
200     (clsql:select [first-name] [count [*]] :from [employee]
201                           :result-types nil 
202                           :group-by [first-name]
203                           :order-by [first-name]
204                           :field-names nil)
205  (("Boris" "1") ("Josef" "1") ("Konstantin" "1") ("Leon" "1") ("Leonid" "1")
206   ("Mikhail" "1") ("Nikita" "1") ("Vladamir" "2") ("Yuri" "1")))
207
208 (deftest :fdml/select/4
209     (clsql:select [last-name] :from [employee] 
210                           :where [like [email] "%org"]
211                           :order-by [last-name]
212                           :field-names nil 
213                           :result-types nil 
214                           :flatp t)
215  ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
216   "Stalin" "Trotsky" "Yeltsin"))
217
218 (deftest :fdml/select/5
219     (clsql:select [email] :from [employee] :flatp t :result-types nil 
220                           :where [in [employee emplid]
221                           [select [managerid] :from [employee]]]
222                           :field-names nil)
223   ("lenin@soviet.org"))
224
225 (deftest :fdml/select/6
226     (if (db-type-has-fancy-math? *test-database-underlying-type*)
227         (mapcar #'(lambda (s) (parse-integer s :junk-allowed t))
228                 (clsql:select [function "trunc" [height]] :from [employee]
229                               :result-types nil 
230                               :field-names nil
231                               :flatp t))
232         (mapcar #'(lambda (s) (truncate (parse-integer s :junk-allowed t)))
233                 (clsql:select [height] :from [employee] :flatp t 
234                               :field-names nil :result-types nil)))
235  (1 1 1 1 1 1 1 1 1 1))
236
237 (deftest :fdml/select/7
238     (clsql:select [max [emplid]] :from [employee] :flatp t 
239                   :field-names nil :result-types nil)
240   ("10"))
241
242 (deftest :fdml/select/8
243     (clsql:select [min [emplid]] :from [employee] :flatp t 
244                   :field-names nil :result-types nil)
245   ("1"))
246
247 (deftest :fdml/select/9
248     (subseq 
249      (car 
250       (clsql:select [avg [emplid]] :from [employee] :flatp t 
251                     :field-names nil :result-types nil)) 
252      0 3)
253   "5.5")
254
255 (deftest :fdml/select/10
256     (clsql:select [last-name] :from [employee]
257                   :where [not [in [emplid]
258                   [select [managerid] :from [company]]]]
259                   :result-types nil 
260                   :field-names nil 
261                   :flatp t
262                   :order-by [last-name])
263  ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Putin" "Stalin"
264   "Trotsky" "Yeltsin"))
265
266 (deftest :fdml/select/11
267     (clsql:select [last-name] :from [employee] :where [married] :flatp t
268                   :field-names nil :order-by [emplid] :result-types nil)
269   ("Lenin" "Stalin" "Trotsky"))
270
271 (deftest :fdml/select/12
272     (let ((v 1))
273       (clsql:select [last-name] :from [employee] :where [= [emplid] v]
274                     :field-names nil :result-types nil))
275   (("Lenin")))
276
277 (deftest :fdml/select/13
278      (multiple-value-bind (results field-names) 
279          (clsql:select [emplid] [last-name] :from [employee] 
280                        :where [= [emplid] 1])
281        (values results (mapcar #'string-downcase field-names)))
282  ((1 "Lenin"))
283  ("emplid" "last_name"))
284
285 (deftest :fdml/select/14
286      (floatp (car (clsql:select [height] :from [employee] :where [= [emplid] 1] 
287                                 :flatp t)))
288   t)
289
290 (deftest :fdml/select/15
291     (multiple-value-bind (rows field-names)
292         (clsql:select [emplid] [street-number] [street-name] [city_field] [zip] 
293          :from [address]
294          :where [= 1 [emplid]])
295       (values
296        rows
297        (mapcar #'string-downcase field-names)))
298   ((1 10 "Park Place" "Leningrad" 123))
299   ("emplid" "street_number" "street_name" "city_field" "zip"))
300
301 ;(deftest :fdml/select/11
302 ;    (clsql:select [emplid] :from [employee]
303 ;                :where [= [emplid] [any [select [companyid] :from [company]]]]
304 ;                :flatp t)
305 ;  ("1"))
306
307 (deftest :fdml/do-query/1
308     (let ((result '()))
309     (clsql:do-query ((name) [select [last-name] :from [employee]
310                                    :order-by [last-name]])
311       (push name result))
312     result)
313  ("Yeltsin" "Trotsky" "Stalin" "Putin" "Lenin" "Kruschev" "Gorbachev"
314             "Chernenko" "Brezhnev" "Andropov")) 
315
316 (deftest :fdml/map-query/1
317     (clsql:map-query 'list #'identity
318                     [select [last-name] :from [employee] :flatp t
319                             :order-by [last-name]])
320   ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
321               "Stalin" "Trotsky" "Yeltsin"))
322
323 (deftest :fdml/map-query/2
324     (clsql:map-query 'vector #'identity
325                     [select [last-name] :from [employee] :flatp t
326                             :order-by [last-name]])
327   #("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
328     "Stalin" "Trotsky" "Yeltsin"))
329   
330 (deftest :fdml/loop/1
331     (loop for (forename surname)
332       being each tuple in
333       [select [first-name] [last-name] :from [employee] :order-by [last-name]]
334       collect (concatenate 'string forename " " surname))
335   ("Yuri Andropov" "Leonid Brezhnev" "Konstantin Chernenko" "Mikhail Gorbachev"
336                    "Nikita Kruschev" "Vladamir Lenin" "Vladamir Putin"
337                    "Josef Stalin" "Leon Trotsky" "Boris Yeltsin"))
338
339 ;; starts a transaction deletes a record and then rolls back the deletion 
340 (deftest :fdml/transaction/1
341     (let ((results '()))
342       ;; test if we are in a transaction
343       (push (clsql:in-transaction-p) results)
344       ;;start a transaction 
345       (clsql:start-transaction)
346       ;; test if we are in a transaction
347       (push (clsql:in-transaction-p) results)
348       ;;Putin has got to go
349       (clsql:delete-records :from [employee] :where [= [last-name] "Putin"])
350       ;;Should be nil 
351       (push 
352        (clsql:select [*] :from [employee] :where [= [last-name] "Putin"])
353        results)
354       ;;Oh no, he's still there
355       (clsql:rollback)
356       ;; test that we are out of the transaction
357       (push (clsql:in-transaction-p) results)
358       ;; Check that we got him back alright 
359       (push (clsql:select [email] :from [employee] :where [= [last-name] "Putin"]
360                          :flatp t)
361             results)
362       (apply #'values (nreverse results)))
363   nil t nil nil ("putin@soviet.org"))
364
365 ;; starts a transaction, updates a record and then rolls back the update
366 (deftest :fdml/transaction/2
367     (let ((results '()))
368       ;; test if we are in a transaction
369       (push (clsql:in-transaction-p) results)
370       ;;start a transaction 
371       (clsql:start-transaction)
372       ;; test if we are in a transaction
373       (push (clsql:in-transaction-p) results)
374       ;;Putin has got to go
375       (clsql:update-records [employee]
376        :av-pairs '((email "putin-nospam@soviet.org"))
377        :where [= [last-name] "Putin"])
378       ;;Should be new value  
379       (push (clsql:select [email] :from [employee]
380                          :where [= [last-name] "Putin"]
381                          :flatp t)
382             results)
383       ;;Oh no, he's still there
384       (clsql:rollback)
385       ;; test that we are out of the transaction
386       (push (clsql:in-transaction-p) results)
387       ;; Check that we got him back alright 
388       (push (clsql:select [email] :from [employee] :where [= [last-name] "Putin"]
389                          :flatp t)
390             results)
391       (apply #'values (nreverse results)))
392   nil t ("putin-nospam@soviet.org") nil ("putin@soviet.org")) 
393
394 ;; runs an update within a transaction and checks it is committed
395 (deftest :fdml/transaction/3
396     (let ((results '()))
397       ;; check status 
398       (push (clsql:in-transaction-p) results)
399       ;; update records 
400       (push
401        (clsql:with-transaction () 
402          (clsql:update-records [employee] 
403                               :av-pairs '((email "lenin-nospam@soviet.org"))
404                               :where [= [emplid] 1]))
405        results)
406       ;; check status 
407       (push (clsql:in-transaction-p) results)
408       ;; check that was committed 
409       (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
410                          :flatp t)
411             results)
412       ;; undo the changes 
413       (push
414        (clsql:with-transaction () 
415          (clsql:update-records [employee] 
416                               :av-pairs '((email "lenin@soviet.org"))
417                               :where [= [emplid] 1]))
418        results)
419       ;; and check status 
420       (push (clsql:in-transaction-p) results)
421       ;; check that was committed 
422       (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
423                          :flatp t)
424             results)
425       (apply #'values (nreverse results)))
426   nil :committed nil ("lenin-nospam@soviet.org") :committed
427   nil ("lenin@soviet.org"))
428
429 ;; runs a valid update and an invalid one within a transaction and checks
430 ;; that the valid update is rolled back when the invalid one fails. 
431 (deftest :fdml/transaction/4
432     (let ((results '()))
433       ;; check status
434       (push (clsql:in-transaction-p) results)
435       (handler-case 
436           (clsql:with-transaction () 
437             ;; valid update
438             (clsql:update-records [employee] 
439                                   :av-pairs '((email "lenin-nospam@soviet.org"))
440                                   :where [= [emplid] 1])
441             ;; invalid update which generates an error 
442             (clsql:update-records [employee] 
443                                   :av-pairs
444                                   '((emale "lenin-nospam@soviet.org"))
445                                   :where [= [emplid] 1]))
446         (clsql:clsql-error ()
447           (progn
448             ;; check status 
449             (push (clsql:in-transaction-p) results)
450             ;; and check nothing done 
451             (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
452                                :flatp t)
453                   results)
454             (apply #'values (nreverse results))))))
455   nil nil ("lenin@soviet.org"))
456
457 ))
458
459 #.(clsql:restore-sql-reader-syntax-state)