added better debugging info when failing to load foreign library
[clsql.git] / tests / test-syntax.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:     clsql.asd
6 ;;;; Purpose:  Tests for the CLSQL Symbolic SQL syntax.
7 ;;;; Authors:  Marcus Pearce and Kevin M. Rosenberg
8 ;;;; Created:  March 2004
9 ;;;;
10 ;;;; CLSQL users are granted the rights to distribute and use this software
11 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
12 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
13 ;;;; *************************************************************************
14
15 (in-package #:clsql-tests)
16
17 (clsql-sys:file-enable-sql-reader-syntax)
18
19
20 (setq *rt-syntax*
21       '(
22
23 (deftest :syntax/generic/1
24     (clsql:sql "foo")
25   "'foo'")
26
27 (deftest :syntax/generic/2
28     (clsql:sql 23)
29   "23")
30
31 (deftest :syntax/generic/3
32     (clsql:sql 'bar)
33   "BAR")
34
35 (deftest :syntax/generic/4
36     (clsql:sql '("ten" 10 ten))
37   "('ten',10,TEN)")
38
39 (deftest :syntax/generic/5
40     (clsql:sql ["SELECT FOO,BAR FROM BAZ"])
41   "SELECT FOO,BAR FROM BAZ")
42
43 (deftest :syntax/generic/6
44     (clsql:sql "What's up Doc?")
45   "'What''s up Doc?'")
46
47 (deftest :syntax/ident/1
48     (clsql:sql [foo])
49   "FOO")
50
51 (deftest :syntax/ident/2
52     (clsql:sql [foo bar])
53   "FOO.BAR")
54
55 (deftest :syntax/ident/3
56     (clsql:sql [foo :integer])
57   "FOO")
58
59 (deftest :syntax/ident/4
60     (clsql:sql [foo bar :integer])
61   "FOO.BAR")
62
63 (deftest :syntax/ident/5
64     (clsql:sql [foo "bar"])
65     "FOO \"bar\"")
66
67 (deftest :syntax/ident/6
68     (clsql:sql ["foo" bar])
69  "\"foo\".BAR")
70
71 (deftest :syntax/ident/7
72     (clsql:sql ["foo" bar :integer])
73  "\"foo\".BAR")
74
75
76 (deftest :syntax/attribute/1
77     (clsql:sql (clsql:sql-expression :table 'foo :attribute 'bar))
78   "FOO.BAR")
79
80 (deftest :syntax/attribute/2
81     (clsql:sql (clsql:sql-expression :table 'foo :attribute "bar"))
82   "FOO.\"bar\"")
83
84 (deftest :syntax/attribute/3
85     (clsql:sql (clsql:sql-expression :table "foo" :attribute 'bar))
86   "\"foo\".BAR")
87
88 (deftest :syntax/attribute/4
89     (clsql:sql (clsql:sql-expression :table "foo" :attribute "bar"))
90   "\"foo\".\"bar\"")
91
92
93 (deftest :syntax/subquery/1
94     (clsql:sql [any '(3 4)])
95  "ANY((3,4))")
96
97 (deftest :syntax/subquery/2
98     (clsql:sql [in [foo] '(foo bar baz)])
99   "(FOO IN (FOO,BAR,BAZ))")
100
101 (deftest :syntax/subquery/3
102     (clsql:sql [all '(foo bar baz)])
103   "ALL((FOO,BAR,BAZ))")
104
105 (deftest :syntax/subquery/4
106     (clsql:sql [exists '(foo bar baz)])
107   "EXISTS((FOO,BAR,BAZ))")
108
109 (deftest :syntax/subquery/5
110     (clsql:sql [some '(foo bar baz)])
111   "SOME((FOO,BAR,BAZ))")
112
113
114 (deftest :syntax/aggregate/1
115     (clsql:sql [max [+ [foo] [* 1000 [bar]]]])
116  "MAX((FOO + (1000 * BAR)))")
117
118 (deftest :syntax/aggregate/2
119     (clsql:sql [avg [+ [foo] [* 1000 [bar]]]])
120  "AVG((FOO + (1000 * BAR)))")
121
122 (deftest :syntax/aggregate/3
123     (clsql:sql [min [+ [foo] [* 1000 [bar]]]])
124  "MIN((FOO + (1000 * BAR)))")
125
126 (deftest :syntax/aggregate/4
127     (clsql:sql [sum [foo] [bar]])
128  "SUM(FOO,BAR)")
129
130 (deftest :syntax/aggregate/5
131     (clsql:sql [count [foo]])
132  "COUNT(FOO)")
133
134
135 (deftest :syntax/logical/1
136     (values (clsql:sql [and [foo] [bar]])
137             (clsql:sql [or [foo] [bar]]))
138   "(FOO AND BAR)"
139   "(FOO OR BAR)")
140
141 (deftest :syntax/logical/2
142     (clsql:sql [not [foo]])
143   "(NOT (FOO))")
144
145 ;;; Test how we apply logical operators when we have different numbers of children
146 ;;; This is useful if we wish to (apply #'sql-and some-list) without having to do
147 ;;; alot of length checking
148 (deftest :syntax/logical/3
149     (values (clsql:sql [and ])
150             (clsql:sql [and [foo]])
151             (clsql:sql [and [not [foo]]])
152             (clsql:sql [and [foo] [bar] [baz]]))
153   ""
154   "FOO"
155   "(NOT (FOO))"
156   "(FOO AND BAR AND BAZ)")
157
158 (deftest :syntax/logical/4
159     (clsql:sql [and [= [foo] [bar]]])
160   "(FOO = BAR)")
161
162 (deftest :syntax/logical/5
163   (clsql:sql [and [= [foo] [bar]]
164                   [= [bar] [bast]]
165                   [= [block] [blech]]])
166   "((FOO = BAR) AND (BAR = BAST) AND (BLOCK = BLECH))")
167
168 (deftest :syntax/logical/6
169     (clsql:sql
170      (apply #'sql-and
171             (list [= [foo] [bar]]
172                   [and ]
173                   [and [= [bar] [bast]]])))
174   "((FOO = BAR) AND (BAR = BAST))")
175
176
177 (deftest :syntax/null/1
178     (clsql:sql [null [foo]])
179   "(FOO IS NULL)")
180
181 (deftest :syntax/null/2
182     (clsql:sql [not [null [foo]]])
183   "(NOT ((FOO IS NULL)))")
184
185 (deftest :syntax/null/3
186     (clsql:sql [null])
187   "NULL")
188
189 (deftest :syntax/null/4
190     (clsql:sql [not [null]])
191   "(NOT (NULL))")
192
193 (deftest :syntax/null/5
194     (clsql:sql [is [foo.bar] [null]])
195   "(FOO.BAR IS NULL)")
196
197 (deftest :syntax/null/6
198     (clsql:sql [is [foo.bar] [not-null]])
199   "(FOO.BAR IS NOT NULL)")
200
201 (deftest :syntax/null/7
202     (clsql:sql [not-null [foo.bar]])
203   "(FOO.BAR IS NOT NULL)")
204
205
206
207 (deftest :syntax/relational/1
208     (clsql:sql [> [baz] [beep]])
209   "(BAZ > BEEP)")
210
211 (deftest :syntax/relational/2
212     (let ((x 10))
213       (clsql:sql [> [foo] x]))
214   "(FOO > 10)")
215
216 (deftest :syntax/relational/3
217     (clsql:sql [>= [baz] [beep]])
218   "(BAZ >= BEEP)")
219
220 (deftest :syntax/relational/4
221     (clsql:sql [< [baz] [beep]])
222   "(BAZ < BEEP)")
223
224 (deftest :syntax/relational/5
225     (clsql:sql [= [baz] [beep]])
226   "(BAZ = BEEP)")
227
228 (deftest :syntax/relational/6
229     (clsql:sql [<> [baz] [beep]])
230   "(BAZ <> BEEP)")
231
232
233 (deftest :syntax/between/1
234     (clsql:sql [between [- [foo] 1] [* [bar] 5] [/ [baz] 9]])
235   "(FOO - 1) BETWEEN (BAR * 5) AND (BAZ / 9)")
236
237 (deftest :syntax/between/2
238     (clsql:sql [not [between [- [foo] 1] [* [bar] 5] [/ [baz] 9]]])
239   "(NOT ((FOO - 1) BETWEEN (BAR * 5) AND (BAZ / 9)))")
240
241
242 (deftest :syntax/arithmetic/1
243     (clsql:sql [+ [foo bar] [baz]])
244  "(FOO.BAR + BAZ)")
245
246 (deftest :syntax/arithmetic/2
247     (clsql:sql [- [foo bar] [baz]])
248  "(FOO.BAR - BAZ)")
249
250 (deftest :syntax/arithmetic/3
251     (clsql:sql [/ [foo bar] [baz]])
252  "(FOO.BAR / BAZ)")
253
254 (deftest :syntax/arithmetic/4
255     (clsql:sql [* [foo bar] [baz]])
256  "(FOO.BAR * BAZ)")
257
258 (deftest :syntax/arithmetic/5
259     (clsql:sql [- [foo bar]])
260  "(- (FOO.BAR))")
261
262 (deftest :syntax/arithmetic/6
263     (clsql:sql [* 2 3])
264   "(2 * 3)")
265
266
267 (deftest :syntax/substr/1
268     (clsql:sql [substr [hello] 1 4])
269  "SUBSTR(HELLO,1,4)")
270
271 (deftest :syntax/substring/1
272     (clsql:sql [substring [hello] 1 4])
273  "SUBSTRING(HELLO,1,4)")
274
275
276 (deftest :syntax/concat/1
277     (clsql:sql [|| [foo] [bar] [baz]])
278  "(FOO || BAR || BAZ)")
279
280 (deftest :syntax/concat/2
281     (clsql:sql [concat [foo] [bar]])
282  "CONCAT(FOO,BAR)")
283
284
285 (deftest :syntax/pattern/1
286     (clsql:sql [like [foo] "%v"])
287   "(FOO LIKE '%v')")
288
289 (deftest :syntax/pattern/2
290     (clsql:sql [not [like [foo] "%v"]])
291   "(NOT ((FOO LIKE '%v')))")
292
293
294 (deftest :syntax/distinct/1
295     (clsql:sql [distinct [foo bar :string]])
296  "DISTINCT FOO.BAR")
297
298 (deftest :syntax/distinct/2
299     (clsql:sql [distinct [foo :string] [bar :integer]])
300  "DISTINCT FOO, BAR")
301
302
303 (deftest :syntax/order-by/1
304     (clsql:sql [order-by [foo]])
305  "ORDER BY FOO")
306
307 (deftest :syntax/group-by/1
308     (clsql:sql [group-by [foo]])
309  "GROUP BY FOO")
310
311 (deftest :syntax/group-by/2
312     (clsql:sql
313      (clsql-sys::make-query [foo] [bar] [count [foo]]
314       :from [table]
315       :group-by '([foo] [bar])
316       :order-by '([foo] [bar])))
317   "SELECT FOO,BAR,COUNT(FOO) FROM TABLE GROUP BY FOO,BAR ORDER BY FOO,BAR")
318
319
320 (deftest :syntax/coalesce/1
321     (clsql:sql [coalesce [foo] [bar] "not specified"])
322  "COALESCE(FOO,BAR,'not specified')")
323
324 (deftest :syntax/coalesce/2
325     (clsql:sql [nvl [foo] "not specified"])
326  "COALESCE(FOO,'not specified')")
327
328 (deftest :syntax/nvl/1
329     (clsql:sql [nvl [foo] "not specified"])
330  "COALESCE(FOO,'not specified')")
331
332
333
334 (deftest :syntax/sets/1
335     (clsql:sql [union [select [foo] :from [bar]] [select [baz] :from [bar]]])
336  "SELECT FOO FROM BAR UNION SELECT BAZ FROM BAR")
337
338 (deftest :syntax/sets/2
339     (clsql:sql [intersect [select [foo] :from [bar]] [select [baz] :from [bar]]])
340  "SELECT FOO FROM BAR INTERSECT SELECT BAZ FROM BAR")
341
342 (deftest :syntax/sets/3
343     (clsql:sql [except [select [foo] :from [bar]] [select [baz] :from [bar]]])
344  "SELECT FOO FROM BAR EXCEPT SELECT BAZ FROM BAR")
345
346 (deftest :syntax/sets/4
347     (clsql:sql [minus [select [foo] :from [bar]] [select [baz] :from [bar]]])
348  "SELECT FOO FROM BAR MINUS SELECT BAZ FROM BAR")
349
350
351 (deftest :syntax/function/1
352     (clsql:sql [function "COS" [age]])
353   "COS(AGE)")
354
355 (deftest :syntax/function/2
356     (clsql:sql [function "TO_DATE" "02/06/99" "mm/DD/RR"])
357   "TO_DATE('02/06/99','mm/DD/RR')")
358
359
360 (deftest :syntax/query/1
361     (clsql:sql [select [person_id] [surname] :from [person]])
362   "SELECT PERSON_ID,SURNAME FROM PERSON")
363
364 (deftest :syntax/query/2
365     (clsql:sql [select [foo] [bar *]
366                       :from '([baz] [bar])
367                       :where [or [= [foo] 3]
368                                  [> [baz.quux] 10]]])
369   "SELECT FOO,BAR.* FROM BAZ,BAR WHERE ((FOO = 3) OR (BAZ.QUUX > 10))")
370
371 (deftest :syntax/query/3
372     (clsql:sql [select [foo bar] [baz]
373                       :from '([foo] [quux])
374                       :where [or [> [baz] 3]
375                                  [like [foo bar] "SU%"]]])
376   "SELECT FOO.BAR,BAZ FROM FOO,QUUX WHERE ((BAZ > 3) OR (FOO.BAR LIKE 'SU%'))")
377
378 (deftest :syntax/query/4
379     (clsql:sql [select [count [*]] :from [emp]])
380   "SELECT COUNT(*) FROM EMP")
381
382
383 (deftest :syntax/expression/1
384     (clsql:sql
385      (clsql:sql-operation
386       'select
387       (clsql:sql-expression :table 'foo :attribute 'bar)
388       (clsql:sql-expression :attribute 'baz)
389       :from (list
390              (clsql:sql-expression :table 'foo)
391              (clsql:sql-expression :table 'quux))
392       :where
393       (clsql:sql-operation 'or
394                           (clsql:sql-operation
395                            '>
396                            (clsql:sql-expression :attribute 'baz)
397                            3)
398                           (clsql:sql-operation
399                            'like
400                            (clsql:sql-expression :table 'foo
401                                                 :attribute 'bar)
402                            "SU%"))))
403   "SELECT FOO.BAR,BAZ FROM FOO,QUUX WHERE ((BAZ > 3) OR (FOO.BAR LIKE 'SU%'))")
404
405 (deftest :syntax/expression/2
406     (clsql:sql
407      (apply (clsql:sql-operator 'and)
408             (loop for table in '(thistime nexttime sometime never)
409                   for count from 42
410                   collect
411                   [function "BETWEEN"
412                             (clsql:sql-expression :table table
413                                                  :attribute 'bar)
414                             (clsql:sql-operation '* [hip] [hop])
415                             count]
416                   collect
417                   [like (clsql:sql-expression :table table
418                                              :attribute 'baz)
419                         (clsql:sql table)])))
420   "(BETWEEN(THISTIME.BAR,(HIP * HOP),42) AND (THISTIME.BAZ LIKE 'THISTIME') AND BETWEEN(NEXTTIME.BAR,(HIP * HOP),43) AND (NEXTTIME.BAZ LIKE 'NEXTTIME') AND BETWEEN(SOMETIME.BAR,(HIP * HOP),44) AND (SOMETIME.BAZ LIKE 'SOMETIME') AND BETWEEN(NEVER.BAR,(HIP * HOP),45) AND (NEVER.BAZ LIKE 'NEVER'))"
421  )
422
423 (deftest :syntax/subqueries/query
424  (clsql:sql
425   (clsql:sql-operation 'select [*]
426    :from [foo]
427    :where [in [id] [select [id] :from [bar]]]))
428  "SELECT * FROM FOO WHERE (ID IN (SELECT ID FROM BAR))")
429
430 (deftest :syntax/subqueries/delete
431  (clsql:sql
432   (make-instance 'clsql-sys::sql-delete
433    :from [foo]
434    :where [in [id] [select [id] :from [bar]]]))
435  "DELETE FROM FOO WHERE (ID IN (SELECT ID FROM BAR))")
436
437 (deftest :syntax/subqueries/update
438  (clsql:sql
439   (make-instance 'clsql-sys::sql-update
440    :attributes (list [id])
441    :values '(0)
442    :table [foo]
443    :where [in [id] [select [id] :from [bar]]]))
444  "UPDATE FOO SET ID = 0 WHERE (ID IN (SELECT ID FROM BAR))")
445
446  ))
447
448 (let ((tests '(((:foo) "FOO")
449                ((:foo-bar) "FOO_BAR")
450                (("foo") "\"foo\"")
451                (('|foo bar|) "\"foo bar\"")
452                ((:foo :table-alias :bar) "FOO BAR" )
453                ((:foo_bar :table-alias :bar-bast) "FOO_BAR BAR_BAST")
454                (("foo" :table-alias "Bar") "\"foo\" \"Bar\"")
455                (('|foo bar| :table-alias :bast) "\"foo bar\" BAST"))))
456
457   (push
458    `(deftest :syntax/sql-ident-table
459         (values ,@(mapcar
460                    #'(lambda (args)
461                        `(clsql:sql (make-instance 'clsql-sys:sql-ident-table
462                                                   :name ,@args)))
463                    (mapcar #'first tests)))
464       ,@(mapcar #'second tests))
465    *rt-syntax*))