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