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.lisp,v 1.2 2002/10/16 11:51:04 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" :defaults *load-truename*)))
36 (in-package :clsql-user)
37 (use-package :util.test)
39 (defvar *config-pathname* (make-pathname :name "test"
41 :defaults *load-truename*))
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"))
51 (defun read-specs (&optional (path *config-pathname*))
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)))
62 (error "CLSQL tester config file ~S not found" path)))
64 (defmethod mysql-table-test ((test conn-specs))
65 (test-table (mysql-spec test) :mysql))
67 (defmethod aodbc-table-test ((test conn-specs))
68 (test-table (aodbc-spec test) :aodbc))
70 (defmethod pgsql-table-test ((test conn-specs))
71 (test-table (pgsql-spec test) :postgresql))
73 (defmethod pgsql-socket-table-test ((test conn-specs))
74 (test-table (pgsql-socket-spec test) :postgresql-socket))
76 (defmethod test-table (spec type)
78 (let ((db (clsql:connect spec :database-type type :if-exists :new)))
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)
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))
108 (disconnect :database db)))))
111 (defmethod mysql-low-level ((test conn-specs))
112 (let ((spec (mysql-spec test)))
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)
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)))
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))
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)))))
136 ;;;; Testing functions
138 (defun transform-float-1 (i)
139 (coerce (* i (abs (/ i 2)) (expt 10 (* 2 i))) 'double-float))
141 (defun transform-bigint-1 (i)
142 (* i (expt 10 (* 3 (abs i)))))
144 (defun create-test-table (db)
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))"
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')"
157 (number-to-sql-string test-flt)
158 (transform-bigint-1 test-int)
159 (number-to-sql-string test-flt)
163 (defun parse-double (num-str)
164 (let ((*read-default-float-format* 'double-float))
165 (coerce (read-from-string num-str) 'double-float)))
167 (defun test-table-row (row types db-type)
168 (test (and (listp row)
172 (format nil "Row ~S is incorrect format" row))
173 (destructuring-bind (int float bigint str) row
176 (test (and (integerp int)
177 (typep float 'double-float)
178 (or (eq db-type :aodbc) ;; aodbc doesn't handle bigint conversions
183 (format nil "Incorrect field type for row ~S (types :auto)" row)))
185 (test (and (stringp int)
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)))
201 (format nil "Invalid types field (~S) passed to test-table-row" types))))
202 (test (transform-float-1 int)
206 (format nil "Wrong float value ~A for int ~A (row ~S)" float int row))
209 :test #'double-float-equal
210 :fail-info (format nil "Wrong string value ~A for double ~A~%Row: ~S"
214 (defun double-float-equal (a b)
219 (let ((diff (abs (/ (- a b) a))))
220 (if (> diff (* 10 double-float-epsilon))
224 (defun drop-test-table (db)
225 (clsql:execute-command "DROP TABLE test_clsql" :database db))
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)