r2913: *** empty log message ***
[clsql.git] / test-suite / tester-clsql.cl
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          tester-clsql.cl
6 ;;;; Purpose:       Automated test of CLSQL using ACL's tester
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Mar 2002
9 ;;;;
10 ;;;; $Id: tester-clsql.cl,v 1.9 2002/09/30 01:57:32 kevin Exp $
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 "test.config"
20 ;;; This file contains a single a-list that specifies the connection
21 ;;; specs for each database type to be tested. For example, to test all
22 ;;; platforms, a sample "test.config" may look like:
23 ;;;
24 ;;; ((:mysql ("localhost" "a-mysql-db" "user1" "secret"))
25 ;;;  (:aodbc ("my-dsn" "a-user" "pass"))
26 ;;;  (:postgresql ("localhost" "another-db" "user2" "dont-tell"))
27 ;;;  (:postgresql-socket ("pg-server" "a-db-name" "user" "secret-password")))
28
29
30 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
31 (in-package :cl-user)
32
33 (unless (find-package :util.test)
34   (load (make-pathname :name "acl-compat-tester" :type "cl"
35                        :defaults *load-truename*)))
36
37 (in-package :clsql-user)
38 (use-package :util.test)
39
40 (defvar *config-pathname* (make-pathname :name "test"
41                                          :type "config"
42                                          :defaults *load-truename*))
43
44 (defclass conn-specs ()
45   ((aodbc-spec :accessor aodbc-spec)
46    (mysql-spec :accessor mysql-spec)
47    (pgsql-spec :accessor pgsql-spec)
48    (pgsql-socket-spec :accessor pgsql-socket-spec))
49   (:documentation "Test fixture for CLSQL testing"))
50
51
52 (defun read-specs (&optional (path *config-pathname*))
53   (if (probe-file path)
54       (with-open-file (stream path :direction :input)
55         (let ((config (read stream))
56               (specs (make-instance 'conn-specs)))
57           (setf (aodbc-spec specs) (cadr (assoc :aodbc config)))
58           (setf (mysql-spec specs) (cadr (assoc :mysql config)))
59           (setf (pgsql-spec specs) (cadr (assoc :postgresql config)))
60           (setf (pgsql-socket-spec specs) 
61                 (cadr (assoc :postgresql-socket config)))
62           specs))
63       (error "CLSQL tester config file ~S not found" path)))
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 test-table (spec type)
78   (when spec
79     (let ((db (clsql:connect spec :database-type type :if-exists :new)))
80       (unwind-protect
81            (progn
82              (create-test-table db)
83              (dolist (row (query "select * from test_clsql" :database db :types :auto))
84                (test-table-row row :auto type))
85              (dolist (row (query "select * from test_clsql" :database db :types nil))
86                (test-table-row row nil type))
87              (loop for row across (map-query 'vector #'list "select * from test_clsql" 
88                                              :database db :types :auto)
89                    do (test-table-row row :auto type))
90              (loop for row across (map-query 'vector #'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 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 :auto)
98                  do (test-table-row row :auto type))
99              (test (map-query nil #'list "select * from test_clsql" 
100                               :database db :types :auto)
101                    nil
102                    :fail-info "Expected NIL result from map-query nil")
103              (do-query ((int float bigint str) "select * from test_clsql")
104                (test-table-row (list int float bigint str) nil type))
105              (do-query ((int float bigint str) "select * from test_clsql" :types :auto)
106                (test-table-row (list int float bigint str) :auto type))
107              (drop-test-table db)
108              )
109         (disconnect :database db)))))
110
111
112 (defmethod mysql-low-level ((test conn-specs))
113   (let ((spec (mysql-spec test)))
114     (when spec
115       (let ((db (clsql-mysql::database-connect spec :mysql)))
116         (clsql-mysql::database-execute-command "DROP TABLE IF EXISTS test_clsql" db)
117         (clsql-mysql::database-execute-command 
118          "CREATE TABLE test_clsql (i integer, sqrt double, sqrt_str CHAR(20))" db)
119         (dotimes (i 10)
120           (clsql-mysql::database-execute-command
121            (format nil "INSERT INTO test_clsql VALUES (~d,~d,'~a')"
122                    i (clsql:number-to-sql-string (sqrt i))
123                    (clsql:number-to-sql-string (sqrt i)))
124            db))
125         (let ((res (clsql-mysql::database-query-result-set "select * from test_clsql" db :full-set t :types nil)))
126           (test (mysql:mysql-num-rows
127                  (clsql-mysql::mysql-result-set-res-ptr res))
128                 10
129                 :test #'eql
130                 :fail-info "Error calling mysql-num-rows")
131           (clsql-mysql::database-dump-result-set res db))
132         (clsql-mysql::database-execute-command "DROP TABLE test_clsql" db)
133         (clsql-mysql::database-disconnect db)))))
134
135
136
137 ;;;; Testing functions
138
139 (defun transform-float-1 (i)
140   (coerce (* i (abs (/ i 2)) (expt 10 (* 2 i))) 'double-float))
141
142 (defun transform-bigint-1 (i)
143   (* i (expt 10 (* 3 (abs i)))))
144
145 (defun create-test-table (db)
146   (ignore-errors
147     (clsql:execute-command 
148      "DROP TABLE test_clsql" :database db))
149   (clsql:execute-command 
150    "CREATE TABLE test_clsql (t_int integer, t_float float, t_bigint BIGINT, t_str CHAR(30))" 
151    :database db)
152   (dotimes (i 11)
153     (let* ((test-int (- i 5))
154            (test-flt (transform-float-1 test-int)))
155       (clsql:execute-command
156        (format nil "INSERT INTO test_clsql VALUES (~a,~a,~a,'~a')"
157                test-int
158                (number-to-sql-string test-flt)
159                (transform-bigint-1 test-int)
160                (number-to-sql-string test-flt)
161                )
162        :database db))))
163
164 (defun parse-double (num-str)
165   (let ((*read-default-float-format* 'double-float))
166     (coerce (read-from-string num-str) 'double-float)))
167
168 (defun test-table-row (row types db-type)
169   (test (and (listp row)
170              (= 4 (length row)))
171         t
172         :fail-info 
173         (format nil "Row ~S is incorrect format" row))
174   (destructuring-bind (int float bigint str) row
175     (cond
176       ((eq types :auto)
177        (test (and (integerp int)
178                   (typep float 'double-float)
179                   (or (eq db-type :aodbc) ;; aodbc doesn't handle bigint conversions
180                       (integerp bigint)) 
181                   (stringp str))
182              t
183              :fail-info 
184              (format nil "Incorrect field type for row ~S (types :auto)" row)))
185        ((null types)
186         (test (and (stringp int)
187                      (stringp float)
188                      (stringp bigint)
189                      (stringp str))
190               t
191               :fail-info 
192               (format nil "Incorrect field type for row ~S (types nil)" row))
193         (setq int (parse-integer int))
194         (setq bigint (parse-integer bigint))
195         (setq float (parse-double float)))
196        ((listp types)
197         (error "NYI")
198         )
199        (t 
200         (test t nil
201               :fail-info
202               (format nil "Invalid types field (~S) passed to test-table-row" types))))
203     (test (transform-float-1 int)
204           float
205           :test #'eql
206           :fail-info 
207           (format nil "Wrong float value ~A for int ~A (row ~S)" float int row))
208     (test float
209           (parse-double str)
210           :test #'double-float-equal
211           :fail-info (format nil "Wrong string value ~A for double ~A~%Row: ~S"
212                              str float row))))
213
214
215 (defun double-float-equal (a b)
216   (if (zerop a)
217       (if (zerop b)
218           t
219           nil)
220       (let ((diff (abs (/ (- a b) a))))
221         (if (> diff (* 10 double-float-epsilon))
222             nil
223             t))))
224          
225 (defun drop-test-table (db)
226   (clsql:execute-command "DROP TABLE test_clsql" :database db))
227
228 (defun do-test ()
229     (let ((specs (read-specs)))
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
238
239 (do-test)