r8952: remove unused schema version table
[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     (if (eql *test-database-type* :sqlite)
135         (values nil '(("Josef" "Stalin" "stalin@soviet.org")))
136         (progn (clsql:create-view [lenins-group]
137                                  :column-list '([forename] [surname] [email])
138                                  :as [select [first-name] [last-name] [email]
139                                              :from [employee]
140                                              :where [= [managerid] 1]])
141                (let ((result 
142                       (list
143                        ;; Shouldn't exist 
144                        (clsql:select [forename] [surname] [email]
145                                     :from [lenins-group]
146                                     :where [= [surname] "Lenin"])
147                        ;; Should exist 
148                        (car (clsql:select [forename] [surname] [email]
149                                          :from [lenins-group]
150                                          :where [= [surname] "Stalin"])))))
151                  (clsql:drop-view [lenins-group] :if-does-not-exist :ignore)
152                  (apply #'values result))))
153   nil ("Josef" "Stalin" "stalin@soviet.org"))
154
155 ;; create an index, test for existence, drop it and test again 
156 (deftest :fddl/index/1
157     (progn (clsql:create-index [bar] :on [employee] :attributes
158                               '([first-name] [last-name] [email]) :unique t)
159            (values
160             (clsql:index-exists-p [bar] :owner *test-database-user*)
161             (progn
162               (case *test-database-type*
163                 (:mysql 
164                  (clsql:drop-index [bar] :on [employee]
165                                   :if-does-not-exist :ignore))
166                 (t 
167                  (clsql:drop-index [bar]:if-does-not-exist :ignore)))
168               (clsql:view-exists-p [bar] :owner *test-database-user*))))
169   t nil)
170
171 ;; create indexes with names as strings, symbols and in square brackets 
172 (deftest :fddl/index/2
173     (let ((names '("foo" foo [foo]))
174           (result '()))
175       (dolist (name names)
176         (clsql:create-index name :on [employee] :attributes '([emplid]))
177         (push (clsql:index-exists-p name :owner *test-database-user*) result)
178         (case *test-database-type*
179           (:mysql 
180            (clsql:drop-index name :on [employee] :if-does-not-exist :ignore))
181           (t (clsql:drop-index name :if-does-not-exist :ignore))))
182       (apply #'values result))
183   t t t)
184
185 ;; create an sequence, test for existence, drop it and test again 
186 (deftest :fddl/sequence/1
187     (progn (clsql:create-sequence [foo])
188            (values
189             (clsql:sequence-exists-p [foo] :owner *test-database-user*)
190             (progn
191               (clsql:drop-sequence [foo] :if-does-not-exist :ignore)
192               (clsql:sequence-exists-p [foo] :owner *test-database-user*))))
193   t nil)
194
195 ;; create and increment a sequence
196 (deftest :fddl/sequence/2
197     (let ((val1 nil))
198       (clsql:create-sequence [foo])
199       (setf val1 (clsql:sequence-next [foo]))
200       (prog1
201           (< val1 (clsql:sequence-next [foo]))
202         (clsql:drop-sequence [foo] :if-does-not-exist :ignore)))
203   t)
204
205 ;; explicitly set the value of a sequence
206 (deftest :fddl/sequence/3
207     (progn
208       (clsql:create-sequence [foo])
209       (clsql:set-sequence-position [foo] 5)
210       (prog1
211           (clsql:sequence-next [foo])
212         (clsql:drop-sequence [foo] :if-does-not-exist :ignore)))
213   6)
214
215 ))
216
217 #.(clsql:restore-sql-reader-syntax-state)