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