r9082: fix sequence, error-string
[clsql.git] / tests / test-fddl.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; ======================================================================
3 ;;;; File:    test-fddl.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 Definition Language
9 ;;;; (FDDL).
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-fddl*
23       '(
24       
25 ;; list current tables 
26 (deftest :fddl/table/1
27     (apply #'values 
28            (sort (mapcar #'string-downcase
29                          (clsql:list-tables :owner *test-database-user*))
30                  #'string>))
31   "employee" "company")
32
33 ;; create a table, test for its existence, drop it and test again 
34 (deftest :fddl/table/2
35     (progn (clsql:create-table  [foo]
36                                '(([id] integer)
37                                  ([height] float)
38                                  ([name] (string 24))
39                                  ([comments] longchar)))
40            (values
41             (clsql:table-exists-p [foo] :owner *test-database-user*)
42             (progn
43               (clsql:drop-table [foo] :if-does-not-exist :ignore)
44               (clsql:table-exists-p [foo] :owner *test-database-user*))))
45   t nil)
46
47 ;; create a table, list its attributes and drop it 
48 (deftest :fddl/table/3
49     (apply #'values 
50            (progn (clsql:create-table  [foo]
51                                       '(([id] integer)
52                                         ([height] float)
53                                         ([name] (char 255))
54                                         ([comments] longchar)))
55                   (prog1
56                       (sort (mapcar #'string-downcase
57                                     (clsql:list-attributes [foo]))
58                             #'string<)
59                     (clsql:drop-table [foo] :if-does-not-exist :ignore))))
60   "comments" "height" "id" "name")
61
62 (deftest :fddl/attributes/1
63     (apply #'values
64            (sort 
65             (mapcar #'string-downcase
66                     (clsql:list-attributes [employee]
67                                           :owner *test-database-user*))
68             #'string<))
69   "birthday" "companyid" "email" "emplid" "first_name" "groupid" "height"
70   "last_name" "managerid" "married")
71
72 (deftest :fddl/attributes/2
73     (apply #'values 
74            (sort 
75             (mapcar #'(lambda (a) (string-downcase (car a)))
76                     (clsql:list-attribute-types [employee]
77                                                :owner *test-database-user*))
78             #'string<))
79   "birthday" "companyid" "email" "emplid" "first_name" "groupid" "height"
80   "last_name" "managerid" "married")
81
82 ;; create a view, test for existence, drop it and test again 
83 (deftest :fddl/view/1
84     (progn (clsql:create-view [lenins-group]
85                              ;;not in sqlite 
86                              ;;:column-list '([forename] [surname] [email])
87                              :as [select [first-name] [last-name] [email]
88                                          :from [employee]
89                                          :where [= [managerid] 1]])
90            (values  
91             (clsql:view-exists-p [lenins-group] :owner *test-database-user*)
92             (progn
93               (clsql:drop-view [lenins-group] :if-does-not-exist :ignore)
94               (clsql:view-exists-p [lenins-group] :owner *test-database-user*))))
95   t nil)
96
97 ;; create a view, list its attributes and drop it 
98 (deftest :fddl/view/2
99     (progn (clsql:create-view [lenins-group]
100                              ;;not in sqlite 
101                              ;;:column-list '([forename] [surname] [email])
102                               :as [select [first-name] [last-name] [email]
103                                           :from [employee]
104                                           :where [= [managerid] 1]])
105            (prog1
106                (sort (mapcar #'string-downcase
107                              (clsql:list-attributes [lenins-group]))
108                      #'string<)
109              (clsql:drop-view [lenins-group] :if-does-not-exist :ignore)))
110   ("email" "first_name" "last_name"))
111
112 ;; create a view, select stuff from it and drop it 
113 (deftest :fddl/view/3
114     (progn (clsql:create-view [lenins-group]
115                               :as [select [first-name] [last-name] [email]
116                                           :from [employee]
117                                           :where [= [managerid] 1]])
118            (let ((result 
119                   (list 
120                    ;; Shouldn't exist 
121                    (clsql:select [first-name] [last-name] [email]
122                                 :from [lenins-group]
123                                 :where [= [last-name] "Lenin"])
124                    ;; Should exist 
125                    (car (clsql:select [first-name] [last-name] [email]
126                                      :from [lenins-group]
127                                      :where [= [last-name] "Stalin"])))))
128              (clsql:drop-view [lenins-group] :if-does-not-exist :ignore)
129              (apply #'values result)))
130   nil ("Josef" "Stalin" "stalin@soviet.org"))
131
132 ;; not in sqlite 
133 (deftest :fddl/view/4
134     (progn (clsql:create-view [lenins-group]
135             :column-list '([forename] [surname] [email])
136             :as [select [first-name] [last-name] [email]
137             :from [employee]
138             :where [= [managerid] 1]])
139            (let ((result 
140                   (list
141                    ;; Shouldn't exist 
142                        (clsql:select [forename] [surname] [email]
143                                      :from [lenins-group]
144                                      :where [= [surname] "Lenin"])
145                        ;; Should exist 
146                        (car (clsql:select [forename] [surname] [email]
147                                           :from [lenins-group]
148                                           :where [= [surname] "Stalin"])))))
149              (clsql:drop-view [lenins-group] :if-does-not-exist :ignore)
150              (apply #'values result)))
151   nil ("Josef" "Stalin" "stalin@soviet.org"))
152
153 ;; create an index, test for existence, drop it and test again 
154 (deftest :fddl/index/1
155     (progn (clsql:create-index [bar] :on [employee] :attributes
156                               '([first-name] [last-name] [email]) :unique t)
157            (values
158             (clsql:index-exists-p [bar] :owner *test-database-user*)
159             (progn
160               (case *test-database-type*
161                 (:mysql 
162                  (clsql:drop-index [bar] :on [employee]
163                                   :if-does-not-exist :ignore))
164                 (t 
165                  (clsql:drop-index [bar]:if-does-not-exist :ignore)))
166               (clsql:index-exists-p [bar] :owner *test-database-user*))))
167   t nil)
168
169 ;; create indexes with names as strings, symbols and in square brackets 
170 (deftest :fddl/index/2
171     (let ((names '("foo" foo [foo]))
172           (result '()))
173       (dolist (name names)
174         (clsql:create-index name :on [employee] :attributes '([emplid]))
175         (push (clsql:index-exists-p name :owner *test-database-user*) result)
176         (case *test-database-type*
177           (:mysql 
178            (clsql:drop-index name :on [employee] :if-does-not-exist :ignore))
179           (t (clsql:drop-index name :if-does-not-exist :ignore))))
180       (apply #'values result))
181   t t t)
182
183 ;; create an sequence, test for existence, drop it and test again 
184 (deftest :fddl/sequence/1
185     (progn (clsql:create-sequence [foo])
186            (values
187             (clsql:sequence-exists-p [foo] :owner *test-database-user*)
188             (progn
189               (clsql:drop-sequence [foo] :if-does-not-exist :ignore)
190               (clsql:sequence-exists-p [foo] :owner *test-database-user*))))
191   t nil)
192
193 ;; create and increment a sequence
194 (deftest :fddl/sequence/2
195     (let ((val1 nil))
196       (clsql:create-sequence [foo])
197       (setf val1 (clsql:sequence-next [foo]))
198       (prog1
199           (< val1 (clsql:sequence-next [foo]))
200         (clsql:drop-sequence [foo] :if-does-not-exist :ignore)))
201   t)
202
203 ;; explicitly set the value of a sequence
204 (deftest :fddl/sequence/3
205     (progn
206       (clsql:create-sequence [foo])
207       (clsql:set-sequence-position [foo] 5)
208       (prog1
209           (clsql:sequence-next [foo])
210         (clsql:drop-sequence [foo] :if-does-not-exist :ignore)))
211   6)
212
213 ))
214
215 #.(clsql:restore-sql-reader-syntax-state)