r8963: pre 2.6.4
[clsql.git] / tests / test-init.lisp
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
6 ;;;; Updated: $Id$
7 ;;;;
8 ;;;; Initialisation utilities for running regression tests on CLSQL. 
9 ;;;;
10 ;;;; This file is part of CLSQL.
11 ;;;;
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 ;;;; ======================================================================
16
17 (in-package #:clsql-tests)
18
19 (defvar *rt-connection*)
20 (defvar *rt-fddl*)
21 (defvar *rt-fdml*)
22 (defvar *rt-ooddl*)
23 (defvar *rt-oodml*)
24 (defvar *rt-syntax*)
25
26 (defvar *test-database-type* nil)
27 (defvar *test-database-user* nil)
28
29 (defclass thing ()
30   ((extraterrestrial :initform nil :initarg :extraterrestrial)))
31
32 (def-view-class person (thing)
33   ((height :db-kind :base :accessor height :type float :nulls-ok t
34            :initarg :height)
35    (married :db-kind :base :accessor married :type boolean :nulls-ok t
36             :initarg :married)
37    (birthday :nulls-ok t :type clsql-base:wall-time :initarg :birthday)
38    (hobby :db-kind :virtual :initarg :hobby :initform nil)))
39   
40 (def-view-class employee (person)
41   ((emplid
42     :db-kind :key
43     :db-constraints :not-null
44     :nulls-ok nil
45     :type integer
46     :initarg :emplid)
47    (groupid
48     :db-kind :key
49     :db-constraints :not-null
50     :nulls-ok nil
51     :type integer
52     :initarg :groupid)
53    (first-name
54     :accessor first-name
55     :type (string 30)
56     :initarg :first-name)
57    (last-name
58     :accessor last-name
59     :type (string 30)
60     :initarg :last-name)
61    (email
62     :accessor employee-email
63     :type (string 100)
64     :nulls-ok t
65     :initarg :email)
66    (companyid
67     :type integer)
68    (company
69     :accessor employee-company
70     :db-kind :join
71     :db-info (:join-class company
72                           :home-key companyid
73                           :foreign-key companyid
74                           :set nil))
75    (managerid
76     :type integer
77     :nulls-ok t)
78    (manager
79     :accessor employee-manager
80     :db-kind :join
81     :db-info (:join-class employee
82                           :home-key managerid
83                           :foreign-key emplid
84                           :set nil)))
85   (:base-table employee))
86
87 (def-view-class company ()
88   ((companyid
89     :db-type :key
90     :db-constraints :not-null
91     :type integer
92     :initarg :companyid)
93    (groupid
94     :db-type :key
95     :db-constraints :not-null
96     :type integer
97     :initarg :groupid)
98    (name
99     :type (string 100)
100     :initarg :name)
101    (presidentid
102     :type integer)
103    (president
104     :reader president
105     :db-kind :join
106     :db-info (:join-class employee
107                           :home-key presidentid
108                           :foreign-key emplid
109                           :set nil))
110    (employees
111     :reader company-employees
112     :db-kind :join
113     :db-info (:join-class employee
114                           :home-key (companyid groupid)
115                           :foreign-key (companyid groupid)
116                           :set t)))
117   (:base-table company))
118
119
120
121 (defun test-connect-to-database (database-type spec)
122   (setf *test-database-type* database-type)
123   (when (>= (length spec) 3)
124     (setq *test-database-user* (third spec)))
125
126   ;; Connect to the database
127   (clsql:connect spec
128                  :database-type database-type
129                  :make-default t
130                  :if-exists :old))
131
132 (defparameter company1 nil)
133 (defparameter employee1 nil)
134 (defparameter employee2 nil)
135 (defparameter employee3 nil)
136 (defparameter employee4 nil)
137 (defparameter employee5 nil)
138 (defparameter employee6 nil)
139 (defparameter employee7 nil)
140 (defparameter employee8 nil)
141 (defparameter employee9 nil)
142 (defparameter employee10 nil)
143
144 (defun test-initialise-database ()
145   ;; Create the tables for our view classes
146   (ignore-errors (clsql:drop-view-from-class 'employee))
147   (ignore-errors (clsql:drop-view-from-class 'company))
148   (clsql:create-view-from-class 'employee)
149   (clsql:create-view-from-class 'company)
150
151   (setf company1 (make-instance 'company
152                    :companyid 1
153                    :groupid 1
154                    :name "Widgets Inc.")
155         employee1 (make-instance 'employee
156                     :emplid 1
157                     :groupid 1
158                     :married t 
159                     :height (1+ (random 1.00))
160                     :birthday (clsql-base:get-time)
161                     :first-name "Vladamir"
162                     :last-name "Lenin"
163                     :email "lenin@soviet.org")
164         employee2 (make-instance 'employee
165                     :emplid 2
166                     :groupid 1
167                     :height (1+ (random 1.00))
168                     :married t 
169                     :birthday (clsql-base:get-time)
170                     :first-name "Josef"
171                     :last-name "Stalin"
172                     :email "stalin@soviet.org")
173         employee3 (make-instance 'employee
174                     :emplid 3
175                     :groupid 1
176                     :height (1+ (random 1.00))
177                     :married t 
178                     :birthday (clsql-base:get-time)
179                     :first-name "Leon"
180                     :last-name "Trotsky"
181                     :email "trotsky@soviet.org")
182         employee4 (make-instance 'employee
183                     :emplid 4
184                     :groupid 1
185                     :height (1+ (random 1.00))
186                     :married nil
187                     :birthday (clsql-base:get-time)
188                     :first-name "Nikita"
189                     :last-name "Kruschev"
190                     :email "kruschev@soviet.org")
191         
192         employee5 (make-instance 'employee
193                     :emplid 5
194                     :groupid 1
195                     :married nil
196                     :height (1+ (random 1.00))
197                     :birthday (clsql-base:get-time)
198                     :first-name "Leonid"
199                     :last-name "Brezhnev"
200                     :email "brezhnev@soviet.org")
201
202         employee6 (make-instance 'employee
203                     :emplid 6
204                     :groupid 1
205                     :married nil
206                     :height (1+ (random 1.00))
207                     :birthday (clsql-base:get-time)
208                     :first-name "Yuri"
209                     :last-name "Andropov"
210                     :email "andropov@soviet.org")
211         employee7 (make-instance 'employee
212                     :emplid 7
213                     :groupid 1
214                     :height (1+ (random 1.00))
215                     :married nil
216                     :birthday (clsql-base:get-time)
217                     :first-name "Konstantin"
218                     :last-name "Chernenko"
219                     :email "chernenko@soviet.org")
220         employee8 (make-instance 'employee
221                     :emplid 8
222                     :groupid 1
223                     :height (1+ (random 1.00))
224                     :married nil
225                     :birthday (clsql-base:get-time)
226                     :first-name "Mikhail"
227                     :last-name "Gorbachev"
228                     :email "gorbachev@soviet.org")
229         employee9 (make-instance 'employee
230                     :emplid 9
231                     :groupid 1 
232                     :married nil
233                     :height (1+ (random 1.00))
234                     :birthday (clsql-base:get-time)
235                     :first-name "Boris"
236                     :last-name "Yeltsin"
237                     :email "yeltsin@soviet.org")
238         employee10 (make-instance 'employee
239                      :emplid 10
240                      :groupid 1
241                      :married nil
242                      :height (1+ (random 1.00))
243                      :birthday (clsql-base:get-time)
244                      :first-name "Vladamir"
245                      :last-name "Putin"
246                      :email "putin@soviet.org"))
247   
248   ;; Lenin manages everyone
249   (clsql:add-to-relation employee2 'manager employee1)
250   (clsql:add-to-relation employee3 'manager employee1)
251   (clsql:add-to-relation employee4 'manager employee1)
252   (clsql:add-to-relation employee5 'manager employee1)
253   (clsql:add-to-relation employee6 'manager employee1)
254   (clsql:add-to-relation employee7 'manager employee1)
255   (clsql:add-to-relation employee8 'manager employee1)
256   (clsql:add-to-relation employee9 'manager employee1)
257   (clsql:add-to-relation employee10 'manager employee1)
258   ;; Everyone works for Widgets Inc.
259   (clsql:add-to-relation company1 'employees employee1)
260   (clsql:add-to-relation company1 'employees employee2)
261   (clsql:add-to-relation company1 'employees employee3)
262   (clsql:add-to-relation company1 'employees employee4)
263   (clsql:add-to-relation company1 'employees employee5)
264   (clsql:add-to-relation company1 'employees employee6)
265   (clsql:add-to-relation company1 'employees employee7)
266   (clsql:add-to-relation company1 'employees employee8)
267   (clsql:add-to-relation company1 'employees employee9)
268   (clsql:add-to-relation company1 'employees employee10)
269   ;; Lenin is president of Widgets Inc.
270   (clsql:add-to-relation company1 'president employee1)
271   ;; store these instances 
272   (clsql:update-records-from-instance employee1)
273   (clsql:update-records-from-instance employee2)
274   (clsql:update-records-from-instance employee3)
275   (clsql:update-records-from-instance employee4)
276   (clsql:update-records-from-instance employee5)
277   (clsql:update-records-from-instance employee6)
278   (clsql:update-records-from-instance employee7)
279   (clsql:update-records-from-instance employee8)
280   (clsql:update-records-from-instance employee9)
281   (clsql:update-records-from-instance employee10)
282   (clsql:update-records-from-instance company1))
283
284 (defvar *error-count* 0)
285
286 (defun run-tests ()
287   (let ((specs (read-specs))
288         (*error-count* 0))
289     (unless specs
290       (warn "Not running tests because test configuration file is missing")
291       (return-from run-tests :skipped))
292     (load-necessary-systems specs)
293     (dolist (db-type +all-db-types+)
294       (let ((spec (db-type-spec db-type specs)))
295         (when spec
296           (do-tests-for-backend spec db-type))))
297     (zerop *error-count*)))
298
299 (defun load-necessary-systems (specs)
300   (dolist (db-type +all-db-types+)
301     (when (db-type-spec db-type specs)
302       (db-type-ensure-system db-type))))
303
304 (defun do-tests-for-backend (spec db-type)
305   (format t 
306           "~&
307 *******************************************************************
308 ***     Running CLSQL tests with ~A backend.
309 *******************************************************************
310 " db-type)
311   (regression-test:rem-all-tests)
312   
313   ;; Tests of clsql-base
314   (ignore-errors (destroy-database spec :database-type db-type))
315   (ignore-errors (create-database spec :database-type db-type))
316   (with-tests (:name "CLSQL")
317     (test-basic spec db-type))
318   (incf *error-count* *test-errors*)
319
320   (ignore-errors (destroy-database spec :database-type db-type))
321   (ignore-errors (create-database spec :database-type db-type))
322   (dolist (test (append *rt-connection* *rt-fddl* *rt-fdml*
323                         *rt-ooddl* *rt-oodml* *rt-syntax*))
324     (eval test))
325   (test-connect-to-database db-type spec)
326   (test-initialise-database)
327   (let ((remaining (rtest:do-tests)))
328     (when (consp remaining)
329       (incf *error-count* (length remaining))))
330   (disconnect))
331