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_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)) t)
87 (deftest :fddl/attributes/4
88 (clsql:attribute-type [first-name] [employee])
91 (deftest :fddl/attributes/5
92 (and (member (clsql:attribute-type [birthday] [employee]) '(:datetime :timestamp)) t)
95 (deftest :fddl/attributes/6
96 (and (member (clsql:attribute-type [height] [employee]) '(:float :float8)) t)
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]
106 :where [= [managerid] 1]])
108 (clsql:view-exists-p [lenins-group] :owner *test-database-user*)
110 (clsql:drop-view [lenins-group] :if-does-not-exist :ignore)
111 (clsql:view-exists-p [lenins-group] :owner *test-database-user*))))
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]
120 :where [= [managerid] 1]])
122 (sort (mapcar #'string-downcase
123 (clsql:list-attributes [lenins-group]))
125 (clsql:drop-view [lenins-group] :if-does-not-exist :ignore)))
126 ("email" "first_name" "last_name")))
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]
133 :where [= [managerid] 1]])
137 (clsql:select [first-name] [last-name] [email]
139 :where [= [last-name] "Lenin"])
141 (car (clsql:select [first-name] [last-name] [email]
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"))
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]
153 :where [= [managerid] 1]])
157 (clsql:select [forename] [surname] [email]
159 :where [= [surname] "Lenin"])
161 (car (clsql:select [forename] [surname] [email]
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"))
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)
173 (clsql:index-exists-p [bar] :owner *test-database-user*)
175 (clsql:drop-index [bar] :on [employee]
176 :if-does-not-exist :ignore)
177 (clsql:index-exists-p [bar] :owner *test-database-user*))))
180 ;; create indexes with names as strings, symbols and in square brackets
181 (deftest :fddl/index/2
182 (let ((names '("foo" foo [foo]))
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))
191 ;; test list-table-indexes
192 (deftest :fddl/index/3
194 (clsql:create-table [i3test] '(([a] (string 10))
196 (clsql:create-index [foo] :on [i3test] :attributes
198 (clsql:create-index [bar] :on [i3test] :attributes
201 (clsql:table-exists-p [i3test])
202 (clsql:index-exists-p [foo])
203 (clsql:index-exists-p [bar])
207 (clsql:list-table-indexes [i3test] :owner *test-database-user*))
210 (clsql:drop-index [bar] :on [i3test])
211 (clsql:drop-index [foo] :on [i3test])
212 (clsql:drop-table [i3test])
214 t t t ("bar" "foo") t)
216 ;; create an sequence, test for existence, drop it and test again
217 (deftest :fddl/sequence/1
218 (progn (clsql:create-sequence [foo])
220 (clsql:sequence-exists-p [foo] :owner *test-database-user*)
222 (clsql:drop-sequence [foo] :if-does-not-exist :ignore)
223 (clsql:sequence-exists-p [foo] :owner *test-database-user*))))
226 ;; create and increment a sequence
227 (deftest :fddl/sequence/2
229 (clsql:create-sequence [foo])
230 (setf val1 (clsql:sequence-next [foo]))
232 (< val1 (clsql:sequence-next [foo]))
233 (clsql:drop-sequence [foo] :if-does-not-exist :ignore)))
236 ;; explicitly set the value of a sequence
237 (deftest :fddl/sequence/3
239 (clsql:create-sequence [foo])
240 (clsql:set-sequence-position [foo] 5)
242 (clsql:sequence-next [foo])
243 (clsql:drop-sequence [foo] :if-does-not-exist :ignore)))
248 #.(clsql:restore-sql-reader-syntax-state)