1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
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
10 ;;;; $Id: tester-clsql.cl,v 1.8 2002/06/12 17:47:13 kevin Exp $
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
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 ;;;; *************************************************************************
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:
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")))
30 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
33 (unless (find-package :util.test)
34 (load (make-pathname :name "acl-compat-tester" :type "cl"
35 :defaults *load-truename*)))
37 (in-package :clsql-user)
38 (use-package :util.test)
40 (defvar *config-pathname* (make-pathname :name "test"
42 :defaults *load-truename*))
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"))
52 (defun read-specs (&optional (path *config-pathname*))
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)))
63 (error "CLSQL tester config file ~S not found" path)))
65 (defmethod mysql-table-test ((test conn-specs))
66 (test-table (mysql-spec test) :mysql))
68 (defmethod aodbc-table-test ((test conn-specs))
69 (test-table (aodbc-spec test) :aodbc))
71 (defmethod pgsql-table-test ((test conn-specs))
72 (test-table (pgsql-spec test) :postgresql))
74 (defmethod pgsql-socket-table-test ((test conn-specs))
75 (test-table (pgsql-socket-spec test) :postgresql-socket))
77 (defmethod test-table (spec type)
79 (let ((db (clsql:connect spec :database-type type :if-exists :new)))
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)
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))
109 (disconnect :database db)))))
112 (defmethod mysql-low-level ((test conn-specs))
113 (let ((spec (mysql-spec test)))
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)
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)))
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))
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)))))
137 ;;;; Testing functions
139 (defun transform-float-1 (i)
140 (coerce (* i (abs (/ i 2)) (expt 10 (* 2 i))) 'double-float))
142 (defun transform-bigint-1 (i)
143 (* i (expt 10 (* 3 (abs i)))))
145 (defun create-test-table (db)
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))"
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')"
158 (number-to-sql-string test-flt)
159 (transform-bigint-1 test-int)
160 (number-to-sql-string test-flt)
164 (defun parse-double (num-str)
165 (let ((*read-default-float-format* 'double-float))
166 (coerce (read-from-string num-str) 'double-float)))
168 (defun test-table-row (row types db-type)
169 (test (and (listp row)
173 (format nil "Row ~S is incorrect format" row))
174 (destructuring-bind (int float bigint str) row
177 (test (and (integerp int)
178 (typep float 'double-float)
179 (or (eq db-type :aodbc) ;; aodbc doesn't handle bigint conversions
184 (format nil "Incorrect field type for row ~S (types :auto)" row)))
186 (test (and (stringp int)
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)))
202 (format nil "Invalid types field (~S) passed to test-table-row" types))))
203 (test (transform-float-1 int)
207 (format nil "Wrong float value ~A for int ~A (row ~S)" float int row))
210 :test #'double-float-equal
211 :fail-info (format nil "Wrong string value ~A for double ~A~%Row: ~S"
215 (defun double-float-equal (a b)
220 (let ((diff (abs (/ (- a b) a))))
221 (if (> diff (* 10 double-float-epsilon))
225 (defun drop-test-table (db)
226 (clsql:execute-command "DROP TABLE test_clsql"))
231 (let ((specs (read-specs)))
232 (with-tests (:name "CLSQL")
233 (mysql-low-level specs)
234 (mysql-table-test specs)
235 (pgsql-table-test specs)
236 (pgsql-socket-table-test specs)
237 (aodbc-table-test specs)