1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; ======================================================================
3 ;;;; File: test-init.lisp
4 ;;;; Authors: Marcus Pearce <m.t.pearce@city.ac.uk>, Kevin Rosenberg
5 ;;;; Created: 30/03/2004
8 ;;;; Initialisation utilities for running regression tests on CLSQL.
10 ;;;; This file is part of CLSQL.
12 ;;;; CLSQL users are granted the rights to distribute and use this software
13 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
14 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
15 ;;;; ======================================================================
17 (in-package #:clsql-tests)
19 (defvar *rt-connection*)
27 (defvar *test-database-type* nil)
28 (defvar *test-database-underlying-type* nil)
29 (defvar *test-database-user* nil)
32 ((extraterrestrial :initform nil :initarg :extraterrestrial)))
34 (def-view-class person (thing)
35 ((height :db-kind :base :accessor height :type float :nulls-ok t
37 (married :db-kind :base :accessor married :type boolean :nulls-ok t
39 (birthday :nulls-ok t :type clsql-base:wall-time :initarg :birthday)
40 (hobby :db-kind :virtual :initarg :hobby :initform nil)))
42 (def-view-class employee (person)
45 :db-constraints :not-null
51 :db-constraints :not-null
64 :accessor employee-email
71 :accessor employee-company
73 :db-info (:join-class company
75 :foreign-key companyid
81 :accessor employee-manager
83 :db-info (:join-class employee
87 (:base-table employee))
89 (def-view-class company ()
92 :db-constraints :not-null
97 :db-constraints :not-null
108 :db-info (:join-class employee
109 :home-key presidentid
113 :reader company-employees
115 :db-info (:join-class employee
116 :home-key (companyid groupid)
117 :foreign-key (companyid groupid)
119 (:base-table company))
123 (defun test-connect-to-database (database-type spec)
124 (setf *test-database-type* database-type)
125 (when (>= (length spec) 3)
126 (setq *test-database-user* (third spec)))
128 ;; Connect to the database
130 :database-type database-type
134 (setf *test-database-underlying-type*
135 (clsql-sys:database-underlying-type *default-database*))
139 (defparameter company1 nil)
140 (defparameter employee1 nil)
141 (defparameter employee2 nil)
142 (defparameter employee3 nil)
143 (defparameter employee4 nil)
144 (defparameter employee5 nil)
145 (defparameter employee6 nil)
146 (defparameter employee7 nil)
147 (defparameter employee8 nil)
148 (defparameter employee9 nil)
149 (defparameter employee10 nil)
151 (defun test-initialise-database ()
152 ;; Remove the tables to support cases when destroy-database isn't supported, like odbc
153 (ignore-errors (clsql:drop-table "EMPLOYEE"))
154 (ignore-errors (clsql:drop-table "COMPANY"))
155 (ignore-errors (clsql:drop-table "FOO"))
156 (clsql:create-view-from-class 'employee)
157 (clsql:create-view-from-class 'company)
159 (setf company1 (make-instance 'company
162 :name "Widgets Inc.")
163 employee1 (make-instance 'employee
167 :height (1+ (random 1.00))
168 :birthday (clsql-base:get-time)
169 :first-name "Vladamir"
171 :email "lenin@soviet.org")
172 employee2 (make-instance 'employee
175 :height (1+ (random 1.00))
177 :birthday (clsql-base:get-time)
180 :email "stalin@soviet.org")
181 employee3 (make-instance 'employee
184 :height (1+ (random 1.00))
186 :birthday (clsql-base:get-time)
189 :email "trotsky@soviet.org")
190 employee4 (make-instance 'employee
193 :height (1+ (random 1.00))
195 :birthday (clsql-base:get-time)
197 :last-name "Kruschev"
198 :email "kruschev@soviet.org")
200 employee5 (make-instance 'employee
204 :height (1+ (random 1.00))
205 :birthday (clsql-base:get-time)
207 :last-name "Brezhnev"
208 :email "brezhnev@soviet.org")
210 employee6 (make-instance 'employee
214 :height (1+ (random 1.00))
215 :birthday (clsql-base:get-time)
217 :last-name "Andropov"
218 :email "andropov@soviet.org")
219 employee7 (make-instance 'employee
222 :height (1+ (random 1.00))
224 :birthday (clsql-base:get-time)
225 :first-name "Konstantin"
226 :last-name "Chernenko"
227 :email "chernenko@soviet.org")
228 employee8 (make-instance 'employee
231 :height (1+ (random 1.00))
233 :birthday (clsql-base:get-time)
234 :first-name "Mikhail"
235 :last-name "Gorbachev"
236 :email "gorbachev@soviet.org")
237 employee9 (make-instance 'employee
241 :height (1+ (random 1.00))
242 :birthday (clsql-base:get-time)
245 :email "yeltsin@soviet.org")
246 employee10 (make-instance 'employee
250 :height (1+ (random 1.00))
251 :birthday (clsql-base:get-time)
252 :first-name "Vladamir"
254 :email "putin@soviet.org"))
256 ;; Lenin manages everyone
257 (clsql:add-to-relation employee2 'manager employee1)
258 (clsql:add-to-relation employee3 'manager employee1)
259 (clsql:add-to-relation employee4 'manager employee1)
260 (clsql:add-to-relation employee5 'manager employee1)
261 (clsql:add-to-relation employee6 'manager employee1)
262 (clsql:add-to-relation employee7 'manager employee1)
263 (clsql:add-to-relation employee8 'manager employee1)
264 (clsql:add-to-relation employee9 'manager employee1)
265 (clsql:add-to-relation employee10 'manager employee1)
266 ;; Everyone works for Widgets Inc.
267 (clsql:add-to-relation company1 'employees employee1)
268 (clsql:add-to-relation company1 'employees employee2)
269 (clsql:add-to-relation company1 'employees employee3)
270 (clsql:add-to-relation company1 'employees employee4)
271 (clsql:add-to-relation company1 'employees employee5)
272 (clsql:add-to-relation company1 'employees employee6)
273 (clsql:add-to-relation company1 'employees employee7)
274 (clsql:add-to-relation company1 'employees employee8)
275 (clsql:add-to-relation company1 'employees employee9)
276 (clsql:add-to-relation company1 'employees employee10)
277 ;; Lenin is president of Widgets Inc.
278 (clsql:add-to-relation company1 'president employee1)
279 ;; store these instances
280 (clsql:update-records-from-instance employee1)
281 (clsql:update-records-from-instance employee2)
282 (clsql:update-records-from-instance employee3)
283 (clsql:update-records-from-instance employee4)
284 (clsql:update-records-from-instance employee5)
285 (clsql:update-records-from-instance employee6)
286 (clsql:update-records-from-instance employee7)
287 (clsql:update-records-from-instance employee8)
288 (clsql:update-records-from-instance employee9)
289 (clsql:update-records-from-instance employee10)
290 (clsql:update-records-from-instance company1))
292 (defvar *error-count* 0)
295 (let ((specs (read-specs))
298 (warn "Not running tests because test configuration file is missing")
299 (return-from run-tests :skipped))
300 (load-necessary-systems specs)
301 (dolist (db-type +all-db-types+)
302 (let ((spec (db-type-spec db-type specs)))
304 (do-tests-for-backend spec db-type))))
305 (zerop *error-count*)))
307 (defun load-necessary-systems (specs)
308 (dolist (db-type +all-db-types+)
309 (when (db-type-spec db-type specs)
310 (db-type-ensure-system db-type))))
312 (defun do-tests-for-backend (spec db-type)
315 *******************************************************************
316 *** Running CLSQL tests with ~A backend.
317 *******************************************************************
319 (regression-test:rem-all-tests)
321 ;; Tests of clsql-base
322 (ignore-errors (destroy-database spec :database-type db-type))
323 (ignore-errors (create-database spec :database-type db-type))
324 (with-tests (:name "CLSQL")
325 (test-basic spec db-type))
326 (incf *error-count* *test-errors*)
328 (when (db-backend-has-create/destroy-db? db-type)
329 (ignore-errors (destroy-database spec :database-type db-type))
330 (ignore-errors (create-database spec :database-type db-type)))
332 (test-connect-to-database db-type spec)
334 (dolist (test-form (append *rt-connection* *rt-fddl* *rt-fdml*
335 *rt-ooddl* *rt-oodml* *rt-syntax*))
336 (let ((test (second test-form)))
338 ((and (null (db-type-has-views? *test-database-underlying-type*))
339 (clsql-base-sys::in test :fddl/view/1 :fddl/view/2 :fddl/view/3 :fddl/view/4))
342 ((and (null (db-type-has-boolean-where? *test-database-underlying-type*))
343 (clsql-base-sys::in test :fdml/select/11 :oodml/select/5))
346 ((and (null (db-type-has-subqueries? *test-database-underlying-type*))
347 (clsql-base-sys::in test :fdml/select/5 :fdml/select/10))
350 ((and (null (db-type-transaction-capable? *test-database-underlying-type* *default-database*))
351 (clsql-base-sys::in test :fdml/transaction/1 :fdml/transaction/2 :fdml/transaction/3 :fdml/transaction/4))
354 ((and (eql *test-database-type* :sqlite)
355 (clsql-base-sys::in test :fddl/view/4 :fdml/select/10))
361 (test-initialise-database)
362 (let ((remaining (rtest:do-tests)))
363 (when (consp remaining)
364 (incf *error-count* (length remaining))))