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