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