;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
-;;;; ======================================================================
-;;;; File: test-syntax.lisp
-;;;; Author: Marcus Pearce <m.t.pearce@city.ac.uk>
-;;;; Created: 30/03/2004
-;;;; Updated: $Id$
-;;;; ======================================================================
+;;;; *************************************************************************
+;;;; FILE IDENTIFICATION
;;;;
-;;;; Description ==========================================================
-;;;; ======================================================================
+;;;; Name: clsql.asd
+;;;; Purpose: Tests for the CLSQL Symbolic SQL syntax.
+;;;; Authors: Marcus Pearce and Kevin M. Rosenberg
+;;;; Created: March 2004
;;;;
-;;;; Tests for the CLSQL Symbolic SQL syntax.
-;;;;
-;;;; ======================================================================
+;;;; CLSQL users are granted the rights to distribute and use this software
+;;;; as governed by the terms of the Lisp Lesser GNU Public License
+;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
+;;;; *************************************************************************
(in-package #:clsql-tests)
#.(clsql:locally-enable-sql-reader-syntax)
+
(setq *rt-syntax*
'(
-
+
(deftest :syntax/generic/1
(clsql:sql "foo")
"'foo'")
(clsql:sql ["SELECT FOO,BAR FROM BAZ"])
"SELECT FOO,BAR FROM BAZ")
-(deftest :syntax/generic/6
- (clsql:sql "What's up Doc?")
+(deftest :syntax/generic/6
+ (clsql:sql "What's up Doc?")
"'What''s up Doc?'")
(deftest :syntax/ident/1
(deftest :syntax/ident/5
(clsql:sql [foo "bar"])
- "FOO \"bar\"")
+ "FOO \"bar\"")
(deftest :syntax/ident/6
(clsql:sql ["foo" bar])
"\"foo\".BAR")
+(deftest :syntax/attribute/1
+ (clsql:sql (clsql:sql-expression :table 'foo :attribute 'bar))
+ "FOO.BAR")
+
+(deftest :syntax/attribute/2
+ (clsql:sql (clsql:sql-expression :table 'foo :attribute "bar"))
+ "FOO.\"bar\"")
+
+(deftest :syntax/attribute/3
+ (clsql:sql (clsql:sql-expression :table "foo" :attribute 'bar))
+ "\"foo\".BAR")
+
+(deftest :syntax/attribute/4
+ (clsql:sql (clsql:sql-expression :table "foo" :attribute "bar"))
+ "\"foo\".\"bar\"")
+
(deftest :syntax/subquery/1
(clsql:sql [any '(3 4)])
"SOME((FOO,BAR,BAZ))")
-(deftest :syntax/aggregate/1
+(deftest :syntax/aggregate/1
(clsql:sql [max [+ [foo] [* 1000 [bar]]]])
"MAX((FOO + (1000 * BAR)))")
"COUNT(FOO)")
-(deftest :syntax/logical/1
- (clsql:sql [and [foo] [bar]])
- "(FOO AND BAR)")
-
-(deftest :syntax/logical/2
- (clsql:sql [or [foo] [bar]])
+(deftest :syntax/logical/1
+ (values (clsql:sql [and [foo] [bar]])
+ (clsql:sql [or [foo] [bar]]))
+ "(FOO AND BAR)"
"(FOO OR BAR)")
-(deftest :syntax/logical/3
+(deftest :syntax/logical/2
(clsql:sql [not [foo]])
"(NOT (FOO))")
+;;; Test how we apply logical operators when we have different numbers of children
+;;; This is useful if we wish to (apply #'sql-and some-list) without having to do
+;;; alot of length checking
+(deftest :syntax/logical/3
+ (values (clsql:sql [and ])
+ (clsql:sql [and [foo]])
+ (clsql:sql [and [not [foo]]])
+ (clsql:sql [and [foo] [bar] [baz]]))
+ ""
+ "FOO"
+ "(NOT (FOO))"
+ "(FOO AND BAR AND BAZ)")
+
+(deftest :syntax/logical/4
+ (clsql:sql [and [= [foo] [bar]]])
+ "(FOO = BAR)")
+
+(deftest :syntax/logical/5
+ (clsql:sql [and [= [foo] [bar]]
+ [= [bar] [bast]]
+ [= [block] [blech]]])
+ "((FOO = BAR) AND (BAR = BAST) AND (BLOCK = BLECH))")
+
+(deftest :syntax/logical/6
+ (clsql:sql
+ (apply #'sql-and
+ (list [= [foo] [bar]]
+ [and ]
+ [and [= [bar] [bast]]])))
+ "((FOO = BAR) AND (BAR = BAST))")
+
-(deftest :syntax/null/1
+(deftest :syntax/null/1
(clsql:sql [null [foo]])
"(FOO IS NULL)")
(deftest :syntax/null/2
(clsql:sql [not [null [foo]]])
"(NOT ((FOO IS NULL)))")
-
+
(deftest :syntax/null/3
(clsql:sql [null])
"NULL")
"(BAZ <> BEEP)")
-(deftest :syntax/between/1
+(deftest :syntax/between/1
(clsql:sql [between [- [foo] 1] [* [bar] 5] [/ [baz] 9]])
"(FOO - 1) BETWEEN (BAR * 5) AND (BAZ / 9)")
-(deftest :syntax/between/2
+(deftest :syntax/between/2
(clsql:sql [not [between [- [foo] 1] [* [bar] 5] [/ [baz] 9]]])
"(NOT ((FOO - 1) BETWEEN (BAR * 5) AND (BAZ / 9)))")
-(deftest :syntax/arithmetic/1
+(deftest :syntax/arithmetic/1
(clsql:sql [+ [foo bar] [baz]])
"(FOO.BAR + BAZ)")
"(2 * 3)")
-(deftest :syntax/substr/1
+(deftest :syntax/substr/1
(clsql:sql [substr [hello] 1 4])
"SUBSTR(HELLO,1,4)")
-(deftest :syntax/substring/1
+(deftest :syntax/substring/1
(clsql:sql [substring [hello] 1 4])
"SUBSTRING(HELLO,1,4)")
-(deftest :syntax/concat/1
+(deftest :syntax/concat/1
(clsql:sql [|| [foo] [bar] [baz]])
"(FOO || BAR || BAZ)")
"CONCAT(FOO,BAR)")
-(deftest :syntax/pattern/1
+(deftest :syntax/pattern/1
(clsql:sql [like [foo] "%v"])
"(FOO LIKE '%v')")
"(NOT ((FOO LIKE '%v')))")
-(deftest :syntax/distinct/1
+(deftest :syntax/distinct/1
(clsql:sql [distinct [foo bar :string]])
"DISTINCT FOO.BAR")
"DISTINCT FOO, BAR")
-(deftest :syntax/order-by/1
+(deftest :syntax/order-by/1
(clsql:sql [order-by [foo]])
"ORDER BY FOO")
-(deftest :syntax/group-by/1
+(deftest :syntax/group-by/1
(clsql:sql [group-by [foo]])
"GROUP BY FOO")
+(deftest :syntax/group-by/2
+ (clsql:sql
+ (clsql-sys::make-query [foo] [bar] [count [foo]]
+ :from [table]
+ :group-by '([foo] [bar])
+ :order-by '([foo] [bar])))
+ "SELECT FOO,BAR,COUNT(FOO) FROM TABLE GROUP BY FOO,BAR ORDER BY FOO,BAR")
-(deftest :syntax/coalesce/1
+
+(deftest :syntax/coalesce/1
(clsql:sql [coalesce [foo] [bar] "not specified"])
"COALESCE(FOO,BAR,'not specified')")
(clsql:sql [nvl [foo] "not specified"])
"COALESCE(FOO,'not specified')")
-(deftest :syntax/nvl/1
+(deftest :syntax/nvl/1
(clsql:sql [nvl [foo] "not specified"])
"COALESCE(FOO,'not specified')")
-(deftest :syntax/sets/1
+(deftest :syntax/sets/1
(clsql:sql [union [select [foo] :from [bar]] [select [baz] :from [bar]]])
"SELECT FOO FROM BAR UNION SELECT BAZ FROM BAR")
-(deftest :syntax/sets/2
+(deftest :syntax/sets/2
(clsql:sql [intersect [select [foo] :from [bar]] [select [baz] :from [bar]]])
"SELECT FOO FROM BAR INTERSECT SELECT BAZ FROM BAR")
(clsql:sql [select [person_id] [surname] :from [person]])
"SELECT PERSON_ID,SURNAME FROM PERSON")
-(deftest :syntax/query/2
+(deftest :syntax/query/2
(clsql:sql [select [foo] [bar *]
:from '([baz] [bar])
:where [or [= [foo] 3]
(deftest :syntax/query/4
(clsql:sql [select [count [*]] :from [emp]])
"SELECT COUNT(*) FROM EMP")
-
-(deftest :syntax/expression1
+
+(deftest :syntax/expression/1
(clsql:sql
(clsql:sql-operation
'select
(clsql:sql-expression :table 'foo :attribute 'bar)
(clsql:sql-expression :attribute 'baz)
- :from (list
+ :from (list
(clsql:sql-expression :table 'foo)
(clsql:sql-expression :table 'quux))
:where
- (clsql:sql-operation 'or
+ (clsql:sql-operation 'or
(clsql:sql-operation
'>
(clsql:sql-expression :attribute 'baz)
:attribute 'bar)
"SU%"))))
"SELECT FOO.BAR,BAZ FROM FOO,QUUX WHERE ((BAZ > 3) OR (FOO.BAR LIKE 'SU%'))")
-
+
(deftest :syntax/expression/2
(clsql:sql
(apply (clsql:sql-operator 'and)
))
+(defun test-output-sql/sql-ident-table ()
+ (let ((tests `((,(make-instance 'sql-ident-table :name :foo) "FOO")
+ (,(make-instance 'sql-ident-table :name :foo-bar) "FOO_BAR")
+ (,(make-instance 'sql-ident-table :name "foo") "\"foo\"")
+ (,(make-instance 'sql-ident-table :name '|foo bar|) "\"foo bar\"")
+ (,(make-instance 'sql-ident-table :name :foo :table-alias :bar) "FOO BAR" )
+ (,(make-instance 'sql-ident-table :name :foo_bar :table-alias :bar-bast) "FOO_BAR BAR_BAST")
+ (,(make-instance 'sql-ident-table :name "foo" :table-alias "Bar") "\"foo\" \"Bar\"")
+ (,(make-instance 'sql-ident-table :name '|foo bar| :table-alias :bast) "\"foo bar\" BAST"))))
+ (loop for (test expected-result) in tests
+ for test-out = (with-output-to-string (*sql-stream*) (output-sql test nil))
+ do (assert (string-equal test-out expected-result)
+ (test test-out expected-result)
+ "Test:~s didnt match ~S"
+ test-out expected-result))))
+
#.(clsql:restore-sql-reader-syntax-state)