d94ed475a314184c56cf26988806c13097598393
[clsql.git] / classic-tests / tests.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          classic-tests.lisp
6 ;;;; Purpose:       Automated test of CLSQL using ACL's tester
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Mar 2002
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; CLSQL users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
18
19 ;;; This test suite looks for a configuration file named ".clsql-test.config"
20 ;;; located in the users home directory.
21 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
22 ;;;; *************************************************************************
23 ;;;; FILE IDENTIFICATION
24 ;;;;
25 ;;;; File:    tests.lisp
26 ;;;; Author: Kevin Rosenberg
27 ;;;; $Id$
28 ;;;;
29 ;;;; This file is part of CLSQL.
30 ;;;;
31 ;;;; CLSQL users are granted the rights to distribute and use this software
32 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
33 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
34 ;;;; *************************************************************************
35
36 ;;; You need a file named "~/.clsql-tests.config"
37
38 ;;; This file contains a single a-list that specifies the connection
39 ;;; specs for each database type to be tested. For example, to test all
40 ;;; platforms, a sample "test.config" may look like:
41 ;;;
42 ;;; ((:mysql ("localhost" "a-mysql-db" "user1" "secret"))
43 ;;;  (:aodbc ("my-dsn" "a-user" "pass"))
44 ;;;  (:postgresql ("localhost" "another-db" "user2" "dont-tell"))
45 ;;;  (:postgresql-socket ("pg-server" "a-db-name" "user" "secret-password"))
46 ;;;  (:sqlite ("path-to-sqlite-db")))
47
48 (in-package #:clsql-classic-tests)
49
50 (defvar *config-pathname*
51   (make-pathname :defaults (user-homedir-pathname)
52                  :name ".clsql-test"
53                  :type "config"))
54
55 (defvar +all-db-types+
56   #-clisp '(:postgresql :postgresql-socket :sqlite :aodbc :mysql)
57   #+clisp '(:sqlite))
58
59 (defclass conn-specs ()
60   ((aodbc-spec :accessor aodbc-spec :initform nil)
61    (mysql-spec :accessor mysql-spec :initform nil)
62    (pgsql-spec :accessor postgresql-spec :initform nil)
63    (pgsql-socket-spec :accessor postgresql-socket-spec :initform nil)
64    (sqlite-spec :accessor sqlite-spec :initform nil))
65   (:documentation "Connection specs for CLSQL testing"))
66
67
68 (defun read-specs (&optional (path *config-pathname*))
69   (if (probe-file path)
70       (with-open-file (stream path :direction :input)
71         (let ((config (read stream))
72               (specs (make-instance 'conn-specs)))
73           (setf (aodbc-spec specs) (cadr (assoc :aodbc config)))
74           (setf (mysql-spec specs) (cadr (assoc :mysql config)))
75           (setf (postgresql-spec specs) (cadr (assoc :postgresql config)))
76           (setf (postgresql-socket-spec specs) 
77                 (cadr (assoc :postgresql-socket config)))
78           (setf (sqlite-spec specs) (cadr (assoc :sqlite config)))
79           specs))
80       (progn
81         (warn "CLSQL test config file ~S not found" path)
82         nil)))
83
84 (defgeneric test-table (spec type))
85
86 (defmethod test-table (spec type)
87   (when spec
88     (let ((db (clsql:connect spec :database-type type :if-exists :new)))
89       (unwind-protect
90            (progn
91              (create-test-table db)
92              (dolist (row (query "select * from test_clsql" :database db :result-types :auto))
93                (test-table-row row :auto type))
94              (dolist (row (query "select * from test_clsql" :database db :result-types nil))
95                (test-table-row row nil type))
96              (loop for row across (map-query 'vector #'list "select * from test_clsql" 
97                                              :database db :result-types :auto)
98                    do (test-table-row row :auto type))
99              (loop for row across (map-query 'vector #'list "select * from test_clsql" 
100                                              :database db :result-types nil)
101                    do (test-table-row row nil type))
102              (loop for row in (map-query 'list #'list "select * from test_clsql" 
103                                          :database db :result-types nil)
104                    do (test-table-row row nil type))
105              (loop for row in (map-query 'list #'list "select * from test_clsql" 
106                                          :database db :result-types :auto)
107                  do (test-table-row row :auto type))
108              (test (map-query nil #'list "select * from test_clsql" 
109                               :database db :result-types :auto)
110                    nil
111                    :fail-info "Expected NIL result from map-query nil")
112              (do-query ((int float bigint str) "select * from test_clsql")
113                (test-table-row (list int float bigint str) nil type))
114              (do-query ((int float bigint str) "select * from test_clsql" :result-types :auto)
115                (test-table-row (list int float bigint str) :auto type))
116              (drop-test-table db)
117              )
118         (disconnect :database db)))))
119
120 ;;;
121 ;;; SQLite is typeless: execute untyped tests only.
122 ;;;
123 (defmethod test-table (spec (type (eql :sqlite)))
124   (when spec
125     (let ((db (clsql:connect spec :database-type type :if-exists :new)))
126       (unwind-protect
127            (progn
128              (create-test-table db)
129              (dolist (row (query "select * from test_clsql" :database db :result-types nil))
130                (test-table-row row nil type))
131              (loop for row across (map-query 'vector #'list "select * from test_clsql" 
132                                              :database db :result-types nil)
133                    do (test-table-row row nil type))
134              (loop for row in (map-query 'list #'list "select * from test_clsql" 
135                                          :database db :result-types nil)
136                    do (test-table-row row nil type))
137
138              (do-query ((int float bigint str) "select * from test_clsql")
139                (test-table-row (list int float bigint str) nil type))
140              (drop-test-table db)
141              )
142         (disconnect :database db)))))
143
144 (defun mysql-low-level (specs)
145   #-clisp
146   (let ((spec (mysql-spec specs)))
147     (when spec
148       (let ((db (clsql-mysql::database-connect spec :mysql)))
149         (clsql-mysql::database-execute-command "DROP TABLE IF EXISTS test_clsql" db)
150         (clsql-mysql::database-execute-command 
151          "CREATE TABLE test_clsql (i integer, sqrt double, sqrt_str CHAR(20))" db)
152         (dotimes (i 10)
153           (clsql-mysql::database-execute-command
154            (format nil "INSERT INTO test_clsql VALUES (~d,~d,'~a')"
155                    i (clsql-base:number-to-sql-string (sqrt i))
156                    (clsql-base:number-to-sql-string (sqrt i)))
157            db))
158         (let ((res (clsql-mysql::database-query-result-set "select * from test_clsql" db :full-set t :result-types nil)))
159           (test (mysql:mysql-num-rows
160                  (clsql-mysql::mysql-result-set-res-ptr res))
161                 10
162                 :test #'eql
163                 :fail-info "Error calling mysql-num-rows")
164           (clsql-mysql::database-dump-result-set res db))
165         (clsql-mysql::database-execute-command "DROP TABLE test_clsql" db)
166         (clsql-mysql::database-disconnect db)))))
167
168
169
170 ;;;; Testing functions
171
172 (defun transform-float-1 (i)
173   (coerce (* i (abs (/ i 2)) (expt 10 (* 2 i))) 'double-float))
174
175 (defun transform-bigint-1 (i)
176   (* i (expt 10 (* 3 (abs i)))))
177
178 (defun create-test-table (db)
179   (ignore-errors
180     (clsql:execute-command 
181      "DROP TABLE test_clsql" :database db))
182   (clsql:execute-command 
183    "CREATE TABLE test_clsql (t_int integer, t_float float, t_bigint BIGINT, t_str CHAR(30))" 
184    :database db)
185   (dotimes (i 11)
186     (let* ((test-int (- i 5))
187            (test-flt (transform-float-1 test-int)))
188       (clsql:execute-command
189        (format nil "INSERT INTO test_clsql VALUES (~a,~a,~a,'~a')"
190                test-int
191                (clsql-base:number-to-sql-string test-flt)
192                (transform-bigint-1 test-int)
193                (clsql-base:number-to-sql-string test-flt)
194                )
195        :database db))))
196
197 (defun parse-double (num-str)
198   (let ((*read-default-float-format* 'double-float))
199     (coerce (read-from-string num-str) 'double-float)))
200
201 (defun test-table-row (row types db-type)
202   (test (and (listp row)
203              (= 4 (length row)))
204         t
205         :fail-info 
206         (format nil "Row ~S is incorrect format" row))
207   (destructuring-bind (int float bigint str) row
208     (cond
209       ((eq types :auto)
210        (test (and (integerp int)
211                   (typep float 'double-float)
212                   (or (eq db-type :aodbc) ;; aodbc doesn't handle bigint conversions
213                       (integerp bigint)) 
214                   (stringp str))
215              t
216              :fail-info 
217              (format nil "Incorrect field type for row ~S (types :auto)" row)))
218        ((null types)
219         (test (and (stringp int)
220                      (stringp float)
221                      (stringp bigint)
222                      (stringp str))
223               t
224               :fail-info 
225               (format nil "Incorrect field type for row ~S (types nil)" row))
226         (setq int (parse-integer int))
227         (setq bigint (parse-integer bigint))
228         (setq float (parse-double float)))
229        ((listp types)
230         (error "NYI")
231         )
232        (t 
233         (test t nil
234               :fail-info
235               (format nil "Invalid types field (~S) passed to test-table-row" types))))
236     (unless (eq db-type :sqlite)                ; SQLite is typeless.
237       (test (transform-float-1 int)
238             float
239             :test #'eql
240             :fail-info 
241             (format nil "Wrong float value ~A for int ~A (row ~S)" float int row)))
242     (test float
243           (parse-double str)
244           :test #'double-float-equal
245           :fail-info (format nil "Wrong string value ~A for double ~A~%Row: ~S"
246                              str float row))))
247
248
249 (defun double-float-equal (a b)
250   (if (zerop a)
251       (if (zerop b)
252           t
253           nil)
254       (let ((diff (abs (/ (- a b) a))))
255         (if (> diff (* 10 double-float-epsilon))
256             nil
257             t))))
258          
259 (defun drop-test-table (db)
260   (clsql:execute-command "DROP TABLE test_clsql" :database db))
261
262 (defun db-type-spec (db-type specs)
263   (let ((accessor (intern (concatenate 'string (symbol-name db-type)
264                                        (symbol-name '#:-spec))
265                           (find-package '#:clsql-classic-tests))))
266     (funcall accessor specs)))
267
268 (defun db-type-ensure-system (db-type)
269   (unless (find-package (symbol-name db-type))
270     (asdf:operate 'asdf:load-op
271                   (intern (concatenate 'string
272                                        (symbol-name '#:clsql-)
273                                        (symbol-name db-type))))))
274
275 (defun run-tests ()
276   (let ((specs (read-specs)))
277     (unless specs
278       (warn "Not running test because test configuration file is missing")
279       (return-from run-tests :skipped))
280     (mysql-low-level specs)
281     (with-tests (:name "CLSQL")
282       (dolist (db-type +all-db-types+)
283         (let ((spec (db-type-spec db-type specs)))
284           (when spec
285             (db-type-ensure-system db-type)
286             (ignore-errors (destroy-database spec db-type))
287             (ignore-errors (create-database spec db-type))
288             (test-table spec db-type))))))
289   t)