r9359: Fixes for PRINT-QUERY and sql concatenation operator (||).
[clsql.git] / tests / test-syntax.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; ======================================================================
3 ;;;; File:    test-syntax.lisp
4 ;;;; Author:  Marcus Pearce <m.t.pearce@city.ac.uk>
5 ;;;; Created: 30/03/2004
6 ;;;; Updated: $Id$
7 ;;;; ======================================================================
8 ;;;;
9 ;;;; Description ==========================================================
10 ;;;; ======================================================================
11 ;;;;
12 ;;;; Tests for the CLSQL Symbolic SQL syntax. 
13 ;;;;
14 ;;;; ======================================================================
15
16 (in-package #:clsql-tests)
17
18 #.(clsql:locally-enable-sql-reader-syntax)
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
44 (deftest :syntax/ident/1
45     (clsql:sql [foo])
46   "FOO")
47
48 (deftest :syntax/ident/2
49     (clsql:sql [foo bar])
50   "FOO.BAR")
51
52 (deftest :syntax/ident/3
53     (clsql:sql ["foo" bar])
54   "FOO.BAR")
55
56 ;(deftest :syntax/ident/4
57 ;    (clsql:sql [foo "bar"])
58 ;  "FOO \"bar\"")
59
60 (deftest :syntax/ident/5
61     (clsql:sql [foo :integer])
62   "FOO")
63
64 (deftest :syntax/ident/6
65     (clsql:sql [foo bar :integer])
66   "FOO.BAR")
67
68 (deftest :syntax/ident/7
69     (clsql:sql ["foo" bar :integer])
70   "FOO.BAR")
71
72
73 (deftest :syntax/subquery/1
74     (clsql:sql [any '(3 4)])
75   "(ANY ((3,4)))")
76
77 (deftest :syntax/subquery/2
78     (clsql:sql [in [foo] '(foo bar baz)])
79   "(FOO IN (FOO,BAR,BAZ))")
80
81 (deftest :syntax/subquery/3
82     (clsql:sql [all '(foo bar baz)])
83   "(ALL ((FOO,BAR,BAZ)))")
84
85 (deftest :syntax/subquery/4
86     (clsql:sql [exists '(foo bar baz)])
87   "(EXISTS ((FOO,BAR,BAZ)))")
88
89 (deftest :syntax/subquery/5
90     (clsql:sql [some '(foo bar baz)])
91   "(SOME ((FOO,BAR,BAZ)))")
92
93
94 (deftest :syntax/aggregate/1 
95     (clsql:sql [max [+ [foo] [* 1000 [bar]]]])
96  "MAX((FOO + (1000 * BAR)))")
97
98 (deftest :syntax/aggregate/2
99     (clsql:sql [avg [+ [foo] [* 1000 [bar]]]])
100  "AVG((FOO + (1000 * BAR)))")
101
102 (deftest :syntax/aggregate/3
103     (clsql:sql [min [+ [foo] [* 1000 [bar]]]])
104  "MIN((FOO + (1000 * BAR)))")
105
106 (deftest :syntax/aggregate/4
107     (clsql:sql [sum [foo] [bar]])
108  "SUM(FOO,BAR)")
109
110 (deftest :syntax/aggregate/5
111     (clsql:sql [count [foo]])
112  "COUNT(FOO)")
113
114
115 (deftest :syntax/logical/1 
116     (clsql:sql [and [foo] [bar]])
117   "(FOO AND BAR)")
118
119 (deftest :syntax/logical/2
120     (clsql:sql [or [foo] [bar]])
121   "(FOO OR BAR)")
122
123 (deftest :syntax/logical/3 
124     (clsql:sql [not [foo]])
125   "(NOT (FOO))")
126
127
128 (deftest :syntax/null/1 
129     (clsql:sql [null [foo]])
130   "(FOO IS NULL)")
131
132 (deftest :syntax/null/2
133     (clsql:sql [not [null [foo]]])
134   "(NOT ((FOO IS NULL)))")
135  
136 (deftest :syntax/null/3
137     (clsql:sql [null])
138   "NULL")
139
140 (deftest :syntax/null/4
141     (clsql:sql [not [null]])
142   "(NOT (NULL))")
143
144
145
146 (deftest :syntax/relational/1
147     (clsql:sql [> [baz] [beep]])
148   "(BAZ > BEEP)")
149
150 (deftest :syntax/relational/2
151     (let ((x 10))
152       (clsql:sql [> [foo] x]))
153   "(FOO > 10)")
154
155 (deftest :syntax/relational/3
156     (clsql:sql [>= [baz] [beep]])
157   "(BAZ >= BEEP)")
158
159 (deftest :syntax/relational/4
160     (clsql:sql [< [baz] [beep]])
161   "(BAZ < BEEP)")
162
163 (deftest :syntax/relational/5
164     (clsql:sql [= [baz] [beep]])
165   "(BAZ = BEEP)")
166
167 (deftest :syntax/relational/6
168     (clsql:sql [<> [baz] [beep]])
169   "(BAZ <> BEEP)")
170
171
172 (deftest :syntax/between/1 
173     (clsql:sql [between [- [foo] 1] [* [bar] 5] [/ [baz] 9]])
174   "(FOO - 1) BETWEEN (BAR * 5) AND (BAZ / 9)")
175
176 (deftest :syntax/between/2 
177     (clsql:sql [not [between [- [foo] 1] [* [bar] 5] [/ [baz] 9]]])
178   "(NOT ((FOO - 1) BETWEEN (BAR * 5) AND (BAZ / 9)))")
179
180
181 (deftest :syntax/arithmetic/1 
182     (clsql:sql [+ [foo bar] [baz]])
183  "(FOO.BAR + BAZ)")
184
185 (deftest :syntax/arithmetic/2
186     (clsql:sql [- [foo bar] [baz]])
187  "(FOO.BAR - BAZ)")
188
189 (deftest :syntax/arithmetic/3
190     (clsql:sql [/ [foo bar] [baz]])
191  "(FOO.BAR / BAZ)")
192
193 (deftest :syntax/arithmetic/4
194     (clsql:sql [* [foo bar] [baz]])
195  "(FOO.BAR * BAZ)")
196
197 (deftest :syntax/arithmetic/5
198     (clsql:sql [- [foo bar]])
199  "(- (FOO.BAR))")
200
201 (deftest :syntax/arithmetic/6
202     (clsql:sql [* 2 3])
203   "(2 * 3)")
204
205
206 (deftest :syntax/substr/1 
207     (clsql:sql [substr [hello] 1 4])
208  "SUBSTRING(HELLO,1,4)")
209
210
211 (deftest :syntax/concat/1 
212     (clsql:sql [|| [foo] [bar] [baz]])
213  "(FOO || BAR || BAZ)")
214
215
216 (deftest :syntax/pattern/1 
217     (clsql:sql [like [foo] "%v"])
218   "(FOO LIKE '%v')")
219
220 (deftest :syntax/pattern/2
221     (clsql:sql [not [like [foo] "%v"]])
222   "(NOT ((FOO LIKE '%v')))")
223
224
225 (deftest :syntax/distinct/1 
226     (clsql:sql [distinct [foo bar :string]])
227  "DISTINCT FOO.BAR")
228
229 (deftest :syntax/distinct/2
230     (clsql:sql [distinct [foo :string] [bar :integer]])
231  "DISTINCT FOO, BAR")
232
233
234 (deftest :syntax/order-by/1 
235     (clsql:sql [order-by [foo]])
236  "ORDER BY FOO")
237
238 (deftest :syntax/group-by/1 
239     (clsql:sql [group-by [foo]])
240  "GROUP BY FOO")
241
242
243 (deftest :syntax/sets/1 
244     (clsql:sql [union [select [foo] :from [bar]] [select [baz] :from [bar]]])
245  "SELECT FOO FROM BAR UNION SELECT BAZ FROM BAR")
246
247 (deftest :syntax/sets/2 
248     (clsql:sql [intersect [select [foo] :from [bar]] [select [baz] :from [bar]]])
249  "SELECT FOO FROM BAR INTERSECT SELECT BAZ FROM BAR")
250
251 (deftest :syntax/sets/3
252     (clsql:sql [except [select [foo] :from [bar]] [select [baz] :from [bar]]])
253  "SELECT FOO FROM BAR EXCEPT SELECT BAZ FROM BAR")
254
255
256 (deftest :syntax/function/1
257     (clsql:sql [function "COS" [age]])
258   "COS(AGE)")
259
260 (deftest :syntax/function/2
261     (clsql:sql [function "TO_DATE" "02/06/99" "mm/DD/RR"])
262   "TO_DATE('02/06/99','mm/DD/RR')")
263
264
265 (deftest :syntax/query/1
266     (clsql:sql [select [person_id] [surname] :from [person]])
267   "SELECT PERSON_ID,SURNAME FROM PERSON")
268
269 (deftest :syntax/query/2 
270     (clsql:sql [select [foo] [bar *]
271                       :from '([baz] [bar])
272                       :where [or [= [foo] 3]
273                                  [> [baz.quux] 10]]])
274   "SELECT FOO,BAR.* FROM BAZ,BAR WHERE ((FOO = 3) OR (BAZ.QUUX > 10))")
275
276 (deftest :syntax/query/3
277     (clsql:sql [select [foo bar] [baz]
278                       :from '([foo] [quux])
279                       :where [or [> [baz] 3]
280                                  [like [foo bar] "SU%"]]])
281   "SELECT FOO.BAR,BAZ FROM FOO,QUUX WHERE ((BAZ > 3) OR (FOO.BAR LIKE 'SU%'))")
282
283 (deftest :syntax/query/4
284     (clsql:sql [select [count [*]] :from [emp]])
285   "SELECT COUNT(*) FROM EMP")
286   
287
288 (deftest :syntax/expression1
289     (clsql:sql
290      (clsql:sql-operation
291       'select
292       (clsql:sql-expression :table 'foo :attribute 'bar)
293       (clsql:sql-expression :attribute 'baz)
294       :from (list 
295              (clsql:sql-expression :table 'foo)
296              (clsql:sql-expression :table 'quux))
297       :where
298       (clsql:sql-operation 'or 
299                           (clsql:sql-operation
300                            '>
301                            (clsql:sql-expression :attribute 'baz)
302                            3)
303                           (clsql:sql-operation
304                            'like
305                            (clsql:sql-expression :table 'foo
306                                                 :attribute 'bar)
307                            "SU%"))))
308   "SELECT FOO.BAR,BAZ FROM FOO,QUUX WHERE ((BAZ > 3) OR (FOO.BAR LIKE 'SU%'))")
309   
310 (deftest :syntax/expression/2
311     (clsql:sql
312      (apply (clsql:sql-operator 'and)
313             (loop for table in '(thistime nexttime sometime never)
314                   for count from 42
315                   collect
316                   [function "BETWEEN"
317                             (clsql:sql-expression :table table
318                                                  :attribute 'bar)
319                             (clsql:sql-operation '* [hip] [hop])
320                             count]
321                   collect
322                   [like (clsql:sql-expression :table table
323                                              :attribute 'baz)
324                         (clsql:sql table)])))
325   "(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'))")
326
327 ))
328
329 #.(clsql:restore-sql-reader-syntax-state)