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