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
8 ;;;; Tests for the CLSQL Functional Data Definition Language
11 ;;;; This file is part of CLSQL.
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 ;;;; ======================================================================
18 (in-package #:clsql-tests)
20 #.(clsql:locally-enable-sql-reader-syntax)
25 ;; list current tables
26 (deftest :fddl/table/1
28 (sort (mapcar #'string-downcase
29 (clsql:list-tables :owner *test-database-user*))
31 "addr" "company" "ea_join" "employee" "type_bigint" "type_table")
33 ;; create a table, test for its existence, drop it and test again
34 (deftest :fddl/table/2
35 (progn (clsql:create-table [foo]
39 ([comments] longchar)))
41 (clsql:table-exists-p [foo] :owner *test-database-user*)
43 (clsql:drop-table [foo] :if-does-not-exist :ignore)
44 (clsql:table-exists-p [foo] :owner *test-database-user*))))
47 ;; create a table, list its attributes and drop it
48 (deftest :fddl/table/3
50 (progn (clsql:create-table [foo]
54 ([comments] longchar)))
56 (sort (mapcar #'string-downcase
57 (clsql:list-attributes [foo]))
59 (clsql:drop-table [foo] :if-does-not-exist :ignore))))
60 "comments" "height" "id" "name")
62 (deftest :fddl/attributes/1
65 (mapcar #'string-downcase
66 (clsql:list-attributes [employee]
67 :owner *test-database-user*))
69 "birthday" "ecompanyid" "email" "emplid" "first_name" "groupid" "height"
70 "last_name" "managerid" "married")
72 (deftest :fddl/attributes/2
75 (mapcar #'(lambda (a) (string-downcase (car a)))
76 (clsql:list-attribute-types [employee]
77 :owner *test-database-user*))
79 "birthday" "ecompanyid" "email" "emplid" "first_name" "groupid" "height"
80 "last_name" "managerid" "married")
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 :number)) t)
87 (deftest :fddl/attributes/4
88 (multiple-value-bind (type length scale nullable)
89 (clsql:attribute-type [first-name] [employee])
90 (values (clsql-sys:in type :varchar :varchar2) length scale nullable))
93 (deftest :fddl/attributes/5
94 (and (member (clsql:attribute-type [birthday] [employee]) '(:datetime :timestamp :date)) t)
97 (deftest :fddl/attributes/6
98 (and (member (clsql:attribute-type [height] [employee]) '(:float :float8 :number)) t)
103 ;; create a view, test for existence, drop it and test again
104 (deftest :fddl/view/1
105 (progn (clsql:create-view [lenins-group]
106 :as [select [first-name] [last-name] [email]
108 :where [= [managerid] 1]])
110 (clsql:view-exists-p [lenins-group] :owner *test-database-user*)
112 (clsql:drop-view [lenins-group] :if-does-not-exist :ignore)
113 (clsql:view-exists-p [lenins-group] :owner *test-database-user*))))
116 ;; create a view, list its attributes and drop it
117 (when (clsql-sys:db-type-has-views? *test-database-underlying-type*)
118 (deftest :fddl/view/2
119 (progn (clsql:create-view [lenins-group]
120 :as [select [first-name] [last-name] [email]
122 :where [= [managerid] 1]])
124 (sort (mapcar #'string-downcase
125 (clsql:list-attributes [lenins-group]))
127 (clsql:drop-view [lenins-group] :if-does-not-exist :ignore)))
128 ("email" "first_name" "last_name")))
130 ;; create a view, select stuff from it and drop it
131 (deftest :fddl/view/3
132 (progn (clsql:create-view [lenins-group]
133 :as [select [first-name] [last-name] [email]
135 :where [= [managerid] 1]])
139 (clsql:select [first-name] [last-name] [email]
141 :where [= [last-name] "Lenin"])
143 (car (clsql:select [first-name] [last-name] [email]
145 :where [= [last-name] "Stalin"])))))
146 (clsql:drop-view [lenins-group] :if-does-not-exist :ignore)
147 (apply #'values result)))
148 nil ("Josef" "Stalin" "stalin@soviet.org"))
150 (deftest :fddl/view/4
151 (progn (clsql:create-view [lenins-group]
152 :column-list '([forename] [surname] [email])
153 :as [select [first-name] [last-name] [email]
155 :where [= [managerid] 1]])
159 (clsql:select [forename] [surname] [email]
161 :where [= [surname] "Lenin"])
163 (car (clsql:select [forename] [surname] [email]
165 :where [= [surname] "Stalin"])))))
166 (clsql:drop-view [lenins-group] :if-does-not-exist :ignore)
167 (apply #'values result)))
168 nil ("Josef" "Stalin" "stalin@soviet.org"))
170 ;; create an index, test for existence, drop it and test again
171 (deftest :fddl/index/1
172 (progn (clsql:create-index [bar] :on [employee] :attributes
173 '([first-name] [last-name] [email]) :unique t)
175 (clsql:index-exists-p [bar] :owner *test-database-user*)
177 (clsql:drop-index [bar] :on [employee]
178 :if-does-not-exist :ignore)
179 (clsql:index-exists-p [bar] :owner *test-database-user*))))
182 ;; create indexes with names as strings, symbols and in square brackets
183 (deftest :fddl/index/2
184 (let ((names '("foo" foo [foo]))
187 (clsql:create-index name :on [employee] :attributes '([emplid]))
188 (push (clsql:index-exists-p name :owner *test-database-user*) result)
189 (clsql:drop-index name :on [employee] :if-does-not-exist :ignore))
190 (apply #'values result))
193 ;; test list-table-indexes
194 (deftest :fddl/index/3
196 (clsql:create-table [i3test] '(([a] (string 10))
198 (clsql:create-index [foo] :on [i3test] :attributes
200 (clsql:create-index [bar] :on [i3test] :attributes
203 (clsql:table-exists-p [i3test])
204 (clsql:index-exists-p [foo])
205 (clsql:index-exists-p [bar])
209 (clsql:list-table-indexes [i3test] :owner *test-database-user*))
212 (clsql:drop-index [bar] :on [i3test])
213 (clsql:drop-index [foo] :on [i3test])
214 (clsql:drop-table [i3test])
216 t t t ("bar" "foo") t)
218 ;; create an sequence, test for existence, drop it and test again
219 (deftest :fddl/sequence/1
220 (progn (clsql:create-sequence [foo])
222 (clsql:sequence-exists-p [foo] :owner *test-database-user*)
224 (clsql:drop-sequence [foo] :if-does-not-exist :ignore)
225 (clsql:sequence-exists-p [foo] :owner *test-database-user*))))
228 ;; create and increment a sequence
229 (deftest :fddl/sequence/2
231 (clsql:create-sequence [foo])
232 (setf val1 (clsql:sequence-next [foo]))
234 (< val1 (clsql:sequence-next [foo]))
235 (clsql:drop-sequence [foo] :if-does-not-exist :ignore)))
238 ;; explicitly set the value of a sequence
239 (deftest :fddl/sequence/3
241 (clsql:create-sequence [foo])
242 (clsql:set-sequence-position [foo] 5)
244 (clsql:sequence-next [foo])
245 (clsql:drop-sequence [foo] :if-does-not-exist :ignore)))
250 #.(clsql:restore-sql-reader-syntax-state)