r9336: 12 May 2004 Kevin Rosenberg (kevin@rosenberg.net)
[clsql.git] / tests / test-fddl.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; ======================================================================
3 ;;;; File:     test-fddl.lisp
4 ;;;; Authors:  Marcus Pearce <m.t.pearce@city.ac.uk> and Kevin Rosenberg
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   "addr" "company" "ea_join" "employee" "type_table")
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" "ecompanyid" "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" "ecompanyid" "email" "emplid" "first_name" "groupid" "height"
80   "last_name" "managerid" "married")
81
82 ;; Attribute types are vendor specific so need to test a range
83 (deftest :fddl/attributes/3
84     (and (member (clsql:attribute-type [emplid] [employee]) '(:int :integer :int4)) t)
85   t)
86
87 (deftest :fddl/attributes/4
88     (clsql:attribute-type [first-name] [employee]) 
89   :varchar 30 nil 1)
90
91 (deftest :fddl/attributes/5
92     (and (member (clsql:attribute-type [birthday] [employee]) '(:datetime :timestamp)) t)
93   t)
94
95 (deftest :fddl/attributes/6
96     (and (member (clsql:attribute-type [height] [employee]) '(:float :float8)) t)
97   t)
98
99
100
101 ;; create a view, test for existence, drop it and test again
102 (deftest :fddl/view/1
103     (progn (clsql:create-view [lenins-group]
104                               :as [select [first-name] [last-name] [email]
105                                           :from [employee]
106                                           :where [= [managerid] 1]])
107            (values  
108             (clsql:view-exists-p [lenins-group] :owner *test-database-user*)
109             (progn
110               (clsql:drop-view [lenins-group] :if-does-not-exist :ignore)
111               (clsql:view-exists-p [lenins-group] :owner *test-database-user*))))
112   t nil)
113   
114   ;; create a view, list its attributes and drop it 
115 (when (clsql-sys:db-type-has-views? *test-database-underlying-type*)
116   (deftest :fddl/view/2
117       (progn (clsql:create-view [lenins-group]
118                                 :as [select [first-name] [last-name] [email]
119                                             :from [employee]
120                                             :where [= [managerid] 1]])
121              (prog1
122                  (sort (mapcar #'string-downcase
123                                (clsql:list-attributes [lenins-group]))
124                        #'string<)
125                (clsql:drop-view [lenins-group] :if-does-not-exist :ignore)))
126     ("email" "first_name" "last_name")))
127   
128   ;; create a view, select stuff from it and drop it 
129 (deftest :fddl/view/3
130     (progn (clsql:create-view [lenins-group]
131                               :as [select [first-name] [last-name] [email]
132                                           :from [employee]
133                                           :where [= [managerid] 1]])
134            (let ((result 
135                   (list 
136                    ;; Shouldn't exist 
137                    (clsql:select [first-name] [last-name] [email]
138                                  :from [lenins-group]
139                                  :where [= [last-name] "Lenin"])
140                    ;; Should exist 
141                    (car (clsql:select [first-name] [last-name] [email]
142                                       :from [lenins-group]
143                                       :where [= [last-name] "Stalin"])))))
144              (clsql:drop-view [lenins-group] :if-does-not-exist :ignore)
145              (apply #'values result)))
146   nil ("Josef" "Stalin" "stalin@soviet.org"))
147   
148 (deftest :fddl/view/4
149     (progn (clsql:create-view [lenins-group]
150                               :column-list '([forename] [surname] [email])
151                               :as [select [first-name] [last-name] [email]
152                                           :from [employee]
153                                           :where [= [managerid] 1]])
154            (let ((result 
155                   (list
156                    ;; Shouldn't exist 
157                    (clsql:select [forename] [surname] [email]
158                                  :from [lenins-group]
159                                  :where [= [surname] "Lenin"])
160                    ;; Should exist 
161                    (car (clsql:select [forename] [surname] [email]
162                                       :from [lenins-group]
163                                       :where [= [surname] "Stalin"])))))
164              (clsql:drop-view [lenins-group] :if-does-not-exist :ignore)
165              (apply #'values result)))
166   nil ("Josef" "Stalin" "stalin@soviet.org"))
167
168 ;; create an index, test for existence, drop it and test again 
169 (deftest :fddl/index/1
170     (progn (clsql:create-index [bar] :on [employee] :attributes
171                               '([first-name] [last-name] [email]) :unique t)
172            (values
173             (clsql:index-exists-p [bar] :owner *test-database-user*)
174             (progn
175               (clsql:drop-index [bar] :on [employee]
176                                 :if-does-not-exist :ignore)
177               (clsql:index-exists-p [bar] :owner *test-database-user*))))
178   t nil)
179
180 ;; create indexes with names as strings, symbols and in square brackets 
181 (deftest :fddl/index/2
182     (let ((names '("foo" foo [foo]))
183           (result '()))
184       (dolist (name names)
185         (clsql:create-index name :on [employee] :attributes '([emplid]))
186         (push (clsql:index-exists-p name :owner *test-database-user*) result)
187         (clsql:drop-index name :on [employee] :if-does-not-exist :ignore))
188       (apply #'values result))
189   t t t)
190
191 ;; test list-table-indexes
192 (deftest :fddl/index/3
193     (progn
194       (clsql:create-table [i3test] '(([a] (string 10))
195                                      ([b] integer)))
196       (clsql:create-index [foo] :on [i3test] :attributes
197        '([b]) :unique nil)
198       (clsql:create-index [bar] :on [i3test] :attributes
199        '([a]) :unique t)
200       (values
201        (clsql:table-exists-p [i3test])
202        (clsql:index-exists-p [foo])
203        (clsql:index-exists-p [bar])
204        (sort 
205         (mapcar 
206          #'string-downcase
207          (clsql:list-table-indexes [i3test] :owner *test-database-user*))
208         #'string-lessp)
209        (progn
210          (clsql:drop-index [bar] :on [i3test])
211          (clsql:drop-index [foo] :on [i3test])
212          (clsql:drop-table [i3test])
213          t)))
214   t t t ("bar" "foo") t)
215
216 ;; create an sequence, test for existence, drop it and test again 
217 (deftest :fddl/sequence/1
218     (progn (clsql:create-sequence [foo])
219            (values
220             (clsql:sequence-exists-p [foo] :owner *test-database-user*)
221             (progn
222               (clsql:drop-sequence [foo] :if-does-not-exist :ignore)
223               (clsql:sequence-exists-p [foo] :owner *test-database-user*))))
224   t nil)
225
226 ;; create and increment a sequence
227 (deftest :fddl/sequence/2
228     (let ((val1 nil))
229       (clsql:create-sequence [foo])
230       (setf val1 (clsql:sequence-next [foo]))
231       (prog1
232           (< val1 (clsql:sequence-next [foo]))
233         (clsql:drop-sequence [foo] :if-does-not-exist :ignore)))
234   t)
235
236 ;; explicitly set the value of a sequence
237 (deftest :fddl/sequence/3
238     (progn
239       (clsql:create-sequence [foo])
240       (clsql:set-sequence-position [foo] 5)
241       (prog1
242           (clsql:sequence-next [foo])
243         (clsql:drop-sequence [foo] :if-does-not-exist :ignore)))
244   6)
245
246 ))
247
248 #.(clsql:restore-sql-reader-syntax-state)