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
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*))
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" "companyid" "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" "companyid" "email" "emplid" "first_name" "groupid" "height"
80 "last_name" "managerid" "married")
82 ;; create a view, test for existence, drop it and test again
84 (progn (clsql:create-view [lenins-group]
86 ;;:column-list '([forename] [surname] [email])
87 :as [select [first-name] [last-name] [email]
89 :where [= [managerid] 1]])
91 (clsql:view-exists-p [lenins-group] :owner *test-database-user*)
93 (clsql:drop-view [lenins-group] :if-does-not-exist :ignore)
94 (clsql:view-exists-p [lenins-group] :owner *test-database-user*))))
97 ;; create a view, list its attributes and drop it
99 (progn (clsql:create-view [lenins-group]
101 ;;:column-list '([forename] [surname] [email])
102 :as [select [first-name] [last-name] [email]
104 :where [= [managerid] 1]])
106 (sort (mapcar #'string-downcase
107 (clsql:list-attributes [lenins-group]))
109 (clsql:drop-view [lenins-group] :if-does-not-exist :ignore)))
110 ("email" "first_name" "last_name"))
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]
117 :where [= [managerid] 1]])
121 (clsql:select [first-name] [last-name] [email]
123 :where [= [last-name] "Lenin"])
125 (car (clsql:select [first-name] [last-name] [email]
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"))
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]
140 :where [= [managerid] 1]])
144 (clsql:select [forename] [surname] [email]
146 :where [= [surname] "Lenin"])
148 (car (clsql:select [forename] [surname] [email]
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"))
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)
160 (clsql:index-exists-p [bar] :owner *test-database-user*)
162 (case *test-database-type*
164 (clsql:drop-index [bar] :on [employee]
165 :if-does-not-exist :ignore))
167 (clsql:drop-index [bar]:if-does-not-exist :ignore)))
168 (clsql:index-exists-p [bar] :owner *test-database-user*))))
171 ;; create indexes with names as strings, symbols and in square brackets
172 (deftest :fddl/index/2
173 (let ((names '("foo" foo [foo]))
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*
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))
185 ;; create an sequence, test for existence, drop it and test again
186 (deftest :fddl/sequence/1
187 (progn (clsql:create-sequence [foo])
189 (clsql:sequence-exists-p [foo] :owner *test-database-user*)
191 (clsql:drop-sequence [foo] :if-does-not-exist :ignore)
192 (clsql:sequence-exists-p [foo] :owner *test-database-user*))))
195 ;; create and increment a sequence
196 (deftest :fddl/sequence/2
198 (clsql:create-sequence [foo])
199 (setf val1 (clsql:sequence-next [foo]))
201 (< val1 (clsql:sequence-next [foo]))
202 (clsql:drop-sequence [foo] :if-does-not-exist :ignore)))
205 ;; explicitly set the value of a sequence
206 (deftest :fddl/sequence/3
208 (clsql:create-sequence [foo])
209 (clsql:set-sequence-position [foo] 5)
211 (clsql:sequence-next [foo])
212 (clsql:drop-sequence [foo] :if-does-not-exist :ignore)))
217 #.(clsql:restore-sql-reader-syntax-state)