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