r8914: rework test suites
[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 (defmethod test-table (spec type)
85   (when spec
86     (let ((db (clsql:connect spec :database-type type :if-exists :new)))
87       (unwind-protect
88            (progn
89              (create-test-table db)
90              (dolist (row (query "select * from test_clsql" :database db :result-types :auto))
91                (test-table-row row :auto type))
92              (dolist (row (query "select * from test_clsql" :database db :result-types nil))
93                (test-table-row row nil type))
94              (loop for row across (map-query 'vector #'list "select * from test_clsql" 
95                                              :database db :result-types :auto)
96                    do (test-table-row row :auto type))
97              (loop for row across (map-query 'vector #'list "select * from test_clsql" 
98                                              :database db :result-types nil)
99                    do (test-table-row row nil type))
100              (loop for row in (map-query 'list #'list "select * from test_clsql" 
101                                          :database db :result-types nil)
102                    do (test-table-row row nil type))
103              (loop for row in (map-query 'list #'list "select * from test_clsql" 
104                                          :database db :result-types :auto)
105                  do (test-table-row row :auto type))
106              (test (map-query nil #'list "select * from test_clsql" 
107                               :database db :result-types :auto)
108                    nil
109                    :fail-info "Expected NIL result from map-query nil")
110              (do-query ((int float bigint str) "select * from test_clsql")
111                (test-table-row (list int float bigint str) nil type))
112              (do-query ((int float bigint str) "select * from test_clsql" :result-types :auto)
113                (test-table-row (list int float bigint str) :auto type))
114              (drop-test-table db)
115              )
116         (disconnect :database db)))))
117
118 ;;;
119 ;;; SQLite is typeless: execute untyped tests only.
120 ;;;
121 (defmethod test-table (spec (type (eql :sqlite)))
122   (when spec
123     (let ((db (clsql:connect spec :database-type type :if-exists :new)))
124       (unwind-protect
125            (progn
126              (create-test-table db)
127              (dolist (row (query "select * from test_clsql" :database db :result-types nil))
128                (test-table-row row nil type))
129              (loop for row across (map-query 'vector #'list "select * from test_clsql" 
130                                              :database db :result-types nil)
131                    do (test-table-row row nil type))
132              (loop for row in (map-query 'list #'list "select * from test_clsql" 
133                                          :database db :result-types nil)
134                    do (test-table-row row nil type))
135
136              (do-query ((int float bigint str) "select * from test_clsql")
137                (test-table-row (list int float bigint str) nil type))
138              (drop-test-table db)
139              )
140         (disconnect :database db)))))
141
142 (defmethod mysql-low-level ((test conn-specs))
143   #-clisp
144   (let ((spec (mysql-spec test)))
145     (when spec
146       (let ((db (clsql-mysql::database-connect spec :mysql)))
147         (clsql-mysql::database-execute-command "DROP TABLE IF EXISTS test_clsql" db)
148         (clsql-mysql::database-execute-command 
149          "CREATE TABLE test_clsql (i integer, sqrt double, sqrt_str CHAR(20))" db)
150         (dotimes (i 10)
151           (clsql-mysql::database-execute-command
152            (format nil "INSERT INTO test_clsql VALUES (~d,~d,'~a')"
153                    i (clsql-base:number-to-sql-string (sqrt i))
154                    (clsql-base:number-to-sql-string (sqrt i)))
155            db))
156         (let ((res (clsql-mysql::database-query-result-set "select * from test_clsql" db :full-set t :result-types nil)))
157           (test (mysql:mysql-num-rows
158                  (clsql-mysql::mysql-result-set-res-ptr res))
159                 10
160                 :test #'eql
161                 :fail-info "Error calling mysql-num-rows")
162           (clsql-mysql::database-dump-result-set res db))
163         (clsql-mysql::database-execute-command "DROP TABLE test_clsql" db)
164         (clsql-mysql::database-disconnect db)))))
165
166
167
168 ;;;; Testing functions
169
170 (defun transform-float-1 (i)
171   (coerce (* i (abs (/ i 2)) (expt 10 (* 2 i))) 'double-float))
172
173 (defun transform-bigint-1 (i)
174   (* i (expt 10 (* 3 (abs i)))))
175
176 (defun create-test-table (db)
177   (ignore-errors
178     (clsql:execute-command 
179      "DROP TABLE test_clsql" :database db))
180   (clsql:execute-command 
181    "CREATE TABLE test_clsql (t_int integer, t_float float, t_bigint BIGINT, t_str CHAR(30))" 
182    :database db)
183   (dotimes (i 11)
184     (let* ((test-int (- i 5))
185            (test-flt (transform-float-1 test-int)))
186       (clsql:execute-command
187        (format nil "INSERT INTO test_clsql VALUES (~a,~a,~a,'~a')"
188                test-int
189                (clsql-base:number-to-sql-string test-flt)
190                (transform-bigint-1 test-int)
191                (clsql-base:number-to-sql-string test-flt)
192                )
193        :database db))))
194
195 (defun parse-double (num-str)
196   (let ((*read-default-float-format* 'double-float))
197     (coerce (read-from-string num-str) 'double-float)))
198
199 (defun test-table-row (row types db-type)
200   (test (and (listp row)
201              (= 4 (length row)))
202         t
203         :fail-info 
204         (format nil "Row ~S is incorrect format" row))
205   (destructuring-bind (int float bigint str) row
206     (cond
207       ((eq types :auto)
208        (test (and (integerp int)
209                   (typep float 'double-float)
210                   (or (eq db-type :aodbc) ;; aodbc doesn't handle bigint conversions
211                       (integerp bigint)) 
212                   (stringp str))
213              t
214              :fail-info 
215              (format nil "Incorrect field type for row ~S (types :auto)" row)))
216        ((null types)
217         (test (and (stringp int)
218                      (stringp float)
219                      (stringp bigint)
220                      (stringp str))
221               t
222               :fail-info 
223               (format nil "Incorrect field type for row ~S (types nil)" row))
224         (setq int (parse-integer int))
225         (setq bigint (parse-integer bigint))
226         (setq float (parse-double float)))
227        ((listp types)
228         (error "NYI")
229         )
230        (t 
231         (test t nil
232               :fail-info
233               (format nil "Invalid types field (~S) passed to test-table-row" types))))
234     (unless (eq db-type :sqlite)                ; SQLite is typeless.
235       (test (transform-float-1 int)
236             float
237             :test #'eql
238             :fail-info 
239             (format nil "Wrong float value ~A for int ~A (row ~S)" float int row)))
240     (test float
241           (parse-double str)
242           :test #'double-float-equal
243           :fail-info (format nil "Wrong string value ~A for double ~A~%Row: ~S"
244                              str float row))))
245
246
247 (defun double-float-equal (a b)
248   (if (zerop a)
249       (if (zerop b)
250           t
251           nil)
252       (let ((diff (abs (/ (- a b) a))))
253         (if (> diff (* 10 double-float-epsilon))
254             nil
255             t))))
256          
257 (defun drop-test-table (db)
258   (clsql:execute-command "DROP TABLE test_clsql" :database db))
259
260 (defun db-type-spec (db-type specs)
261   (let ((accessor (intern (concatenate 'string (symbol-name db-type)
262                                        (symbol-name '#:-spec))
263                           (find-package '#:clsql-classic-tests))))
264     (funcall accessor specs)))
265
266 (defun db-type-ensure-system (db-type)
267   (unless (find-package (symbol-name db-type))
268     (asdf:operate 'asdf:load-op
269                   (intern (concatenate 'string
270                                        (symbol-name '#:clsql-)
271                                        (symbol-name db-type))))))
272
273 (defun run-tests ()
274   (let ((specs (read-specs)))
275     (unless specs
276       (warn "Not running test because test configuration file is missing")
277       (return-from run-tests :skipped))
278     (mysql-low-level specs)
279     (with-tests (:name "CLSQL")
280       (dolist (db-type +all-db-types+)
281         (let ((spec (db-type-spec db-type specs)))
282           (when spec
283             (db-type-ensure-system db-type)
284             (test-table spec db-type))))))
285   t)