r1773: Initial import, not yet complete
[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.1 2002/04/08 02:50:28 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 (error "Not yet implemented")
20
21 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
22 (in-package :cl-user)
23
24 (defvar *config-pathname* (make-pathname :name "test"
25                                          :type "config"
26                                          :defaults *load-truename*))
27 (unless (find-package :util.test)
28   (load (make-pathname :name "acl-compat-tester" :type "cl"
29                        :defaults *load-truename*)))
30
31 (in-package :clsql-user)
32 (use-package :util.test)
33
34 (def-test-fixture clsql-fixture ()
35   ((aodbc-spec :accessor aodbc-spec)
36    (mysql-spec :accessor mysql-spec)
37    (pgsql-spec :accessor pgsql-spec)
38    (pgsql-socket-spec :accessor pgsql-socket-spec))
39   (:documentation "Test fixture for CLSQL testing"))
40
41 (defvar *config-pathname* (make-pathname :name "test"
42                                          :type "config"
43                                          :defaults *load-truename*))
44 (defmethod setup ((fix clsql-fixture))
45   (if (probe-file *config-pathname*)
46       (let (config)
47         (with-open-file (stream *config-pathname* :direction :input)
48           (setq config (read stream)))
49         (setf (aodbc-spec fix) (cadr (assoc :aodbc config)))
50         (setf (mysql-spec fix) (cadr (assoc :mysql config)))
51         (setf (pgsql-spec fix) (cadr (assoc :postgresql config)))
52         (setf (pgsql-socket-spec fix) 
53               (cadr (assoc :postgresql-socket config))))
54       (error "XPTest Config file ~S not found" *config-pathname*)))
55
56 (defmethod teardown ((fix clsql-fixture))
57   t)
58
59 (defmethod mysql-table-test ((test clsql-fixture))
60   (test-table (mysql-spec test) :mysql))
61
62 (defmethod aodbc-table-test ((test clsql-fixture))
63   (test-table (aodbc-spec test) :aodbc))
64
65 (defmethod pgsql-table-test ((test clsql-fixture))
66   (test-table (pgsql-spec test) :postgresql))
67
68 (defmethod pgsql-socket-table-test ((test clsql-fixture))
69   (test-table (pgsql-socket-spec test) :postgresql-socket))
70
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))
80              (dolist (row (query "select * from test_clsql" :database db :types nil))
81                (test-table-row row nil))
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))
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))
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))
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))
94              (when (map-query nil #'list "select * from test_clsql" 
95                                          :database db :types :auto)
96                (failure "Expected NIL result from map-query nil"))
97              (do-query ((int float bigint str) "select * from test_clsql")
98                (test-table-row (list int float bigint str) nil))
99              (do-query ((int float bigint str) "select * from test_clsql" :types :auto)
100                (test-table-row (list int float bigint str) :auto))
101              (drop-test-table db)
102              )
103         (disconnect :database db)))))
104
105
106 (defmethod mysql-low-level ((test clsql-fixture))
107   (let ((spec (mysql-spec test)))
108     (when spec
109       (let ((db (clsql-mysql::database-connect spec :mysql)))
110         (clsql-mysql::database-execute-command "DROP TABLE IF EXISTS test_clsql" db)
111         (clsql-mysql::database-execute-command 
112          "CREATE TABLE test_clsql (i integer, sqrt double, sqrt_str CHAR(20))" db)
113         (dotimes (i 10)
114           (clsql-mysql::database-execute-command
115            (format nil "INSERT INTO test_clsql VALUES (~d,~d,'~a')"
116                    i (number-to-sql-string (sqrt i))
117                    (number-to-sql-string (sqrt i)))
118            db))
119         (let ((res (clsql-mysql::database-query-result-set "select * from test_clsql" db :full-set t :types nil)))
120           (unless (= 10 (mysql:mysql-num-rows (clsql-mysql::mysql-result-set-res-ptr res)))
121             (failure "Error calling mysql-num-rows"))
122           (clsql-mysql::database-dump-result-set res db))
123         (clsql-mysql::database-execute-command "DROP TABLE test_clsql" db)
124         (clsql-mysql::database-disconnect db)))))
125
126 (defparameter clsql-test-suite 
127     (make-test-suite
128      "CLSQL Test Suite"
129      "Basic test suite for database operations."
130      ("MySQL Low Level Interface" 'clsql-fixture
131                    :test-thunk 'mysql-low-level
132                    :description "A test of MySQL low-level interface")
133      ("MySQL Table" 'clsql-fixture
134                    :test-thunk 'mysql-table-test
135                    :description "A test of MySQL")
136      ("PostgreSQL Table" 'clsql-fixture
137                    :test-thunk 'pgsql-table-test
138                    :description "A test of PostgreSQL tables")     
139      ("PostgreSQL Socket Table" 'clsql-fixture
140                    :test-thunk 'pgsql-socket-table-test
141                    :description "A test of PostgreSQL Socket tables")
142   ))
143
144 #+allegro 
145 (add-test (make-test-case "AODBC table test" 'clsql-fixture
146                           :test-thunk 'aodbc-table-test
147                           :description "Test AODBC table")
148           clsql-test-suite)
149
150 ;;;; Testing functions
151
152 (defun transform-float-1 (i)
153   (* i (abs (/ i 2)) (expt 10 (* 2 i))))
154
155 (defun transform-bigint-1 (i)
156   (* i (expt 10 (* 3 (abs i)))))
157
158 (defun create-test-table (db)
159   (ignore-errors
160     (clsql:execute-command 
161      "DROP TABLE test_clsql" :database db))
162   (clsql:execute-command 
163    "CREATE TABLE test_clsql (t_int integer, t_float float, t_bigint BIGINT, t_str CHAR(30))" 
164    :database db)
165   (dotimes (i 11)
166     (let* ((test-int (- i 5))
167            (test-flt (transform-float-1 test-int)))
168       (clsql:execute-command
169        (format nil "INSERT INTO test_clsql VALUES (~a,~a,~a,'~a')"
170                test-int
171                (number-to-sql-string test-flt)
172                (transform-bigint-1 test-int)
173                (number-to-sql-string test-flt)
174                )
175        :database db))))
176
177 (defun parse-double (num-str)
178   (let ((*read-default-float-format* 'double-float))
179     (coerce (read-from-string num-str) 'double-float)))
180
181 (defun test-table-row (row types)
182   (unless (and (listp row)
183                (= 4 (length row)))
184     (failure "Row ~S is incorrect format" row))
185   (destructuring-bind (int float bigint str) row
186     (cond
187       ((eq types :auto)
188        (unless (and (integerp int)
189                     (typep float 'double-float)
190                     (integerp bigint)
191                     (stringp str))
192          (failure "Incorrect field type for row ~S" row)))
193        ((null types)
194         (unless (and (stringp int)
195                      (stringp float)
196                      (stringp bigint)
197                      (stringp str))
198           (failure "Incorrect field type for row ~S" row))
199         (setq int (parse-integer int))
200         (setq bigint (parse-integer bigint))
201         (setq float (parse-double float)))
202        ((listp types)
203         (error "NYI")
204         )
205        (t 
206         (failure "Invalid types field (~S) passed to test-table-row" types)))
207     (unless (= float (transform-float-1 int))
208       (failure "Wrong float value ~A for int ~A (row ~S)" float int row))
209     (unless (= float (parse-double str))
210       (failure "Wrong string value ~A" str))))
211
212
213 (defun drop-test-table (db)
214   (clsql:execute-command "DROP TABLE test_clsql"))
215
216 (report-result (run-test clsql-test-suite :handle-errors nil) :verbose t)
217
218