1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: classic-tests.lisp
6 ;;;; Purpose: Automated test of CLSQL using ACL's tester
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Mar 2002
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 ".clsql-test.config"
20 ;;; located in the users home directory.
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:
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 ;;; (:sqlite ("path-to-sqlite-db")))
32 (in-package #:clsql-classic-tests)
34 (defvar *config-pathname*
35 (make-pathname :defaults (user-homedir-pathname)
40 (defclass conn-specs ()
41 ((aodbc-spec :accessor aodbc-spec)
42 (mysql-spec :accessor mysql-spec)
43 (pgsql-spec :accessor pgsql-spec)
44 (pgsql-socket-spec :accessor pgsql-socket-spec)
45 (sqlite-spec :accessor sqlite-spec))
46 (:documentation "Test fixture for CLSQL testing"))
49 (defun read-specs (&optional (path *config-pathname*))
51 (with-open-file (stream path :direction :input)
52 (let ((config (read stream))
53 (specs (make-instance 'conn-specs)))
54 (setf (aodbc-spec specs) (cadr (assoc :aodbc config)))
55 (setf (mysql-spec specs) (cadr (assoc :mysql config)))
56 (setf (pgsql-spec specs) (cadr (assoc :postgresql config)))
57 (setf (pgsql-socket-spec specs)
58 (cadr (assoc :postgresql-socket config)))
59 (setf (sqlite-spec specs) (cadr (assoc :sqlite config)))
62 (warn "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 sqlite-table-test ((test conn-specs))
78 (test-table (sqlite-spec test) :sqlite))
80 (defmethod test-table (spec type)
82 (let ((db (clsql:connect spec :database-type type :if-exists :new)))
85 (create-test-table db)
86 (dolist (row (query "select * from test_clsql" :database db :result-types :auto))
87 (test-table-row row :auto type))
88 (dolist (row (query "select * from test_clsql" :database db :result-types nil))
89 (test-table-row row nil type))
90 (loop for row across (map-query 'vector #'list "select * from test_clsql"
91 :database db :result-types :auto)
92 do (test-table-row row :auto type))
93 (loop for row across (map-query 'vector #'list "select * from test_clsql"
94 :database db :result-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 :result-types nil)
98 do (test-table-row row nil type))
99 (loop for row in (map-query 'list #'list "select * from test_clsql"
100 :database db :result-types :auto)
101 do (test-table-row row :auto type))
102 (test (map-query nil #'list "select * from test_clsql"
103 :database db :result-types :auto)
105 :fail-info "Expected NIL result from map-query nil")
106 (do-query ((int float bigint str) "select * from test_clsql")
107 (test-table-row (list int float bigint str) nil type))
108 (do-query ((int float bigint str) "select * from test_clsql" :result-types :auto)
109 (test-table-row (list int float bigint str) :auto type))
112 (disconnect :database db)))))
115 ;;; SQLite is typeless: execute untyped tests only.
117 (defmethod test-table (spec (type (eql :sqlite)))
119 (let ((db (clsql:connect spec :database-type type :if-exists :new)))
122 (create-test-table db)
123 (dolist (row (query "select * from test_clsql" :database db :result-types nil))
124 (test-table-row row nil type))
125 (loop for row across (map-query 'vector #'list "select * from test_clsql"
126 :database db :result-types nil)
127 do (test-table-row row nil type))
128 (loop for row in (map-query 'list #'list "select * from test_clsql"
129 :database db :result-types nil)
130 do (test-table-row row nil type))
132 (do-query ((int float bigint str) "select * from test_clsql")
133 (test-table-row (list int float bigint str) nil type))
136 (disconnect :database db)))))
138 (defmethod mysql-low-level ((test conn-specs))
140 (let ((spec (mysql-spec test)))
142 (let ((db (clsql-mysql::database-connect spec :mysql)))
143 (clsql-mysql::database-execute-command "DROP TABLE IF EXISTS test_clsql" db)
144 (clsql-mysql::database-execute-command
145 "CREATE TABLE test_clsql (i integer, sqrt double, sqrt_str CHAR(20))" db)
147 (clsql-mysql::database-execute-command
148 (format nil "INSERT INTO test_clsql VALUES (~d,~d,'~a')"
149 i (clsql-base:number-to-sql-string (sqrt i))
150 (clsql-base:number-to-sql-string (sqrt i)))
152 (let ((res (clsql-mysql::database-query-result-set "select * from test_clsql" db :full-set t :result-types nil)))
153 (test (mysql:mysql-num-rows
154 (clsql-mysql::mysql-result-set-res-ptr res))
157 :fail-info "Error calling mysql-num-rows")
158 (clsql-mysql::database-dump-result-set res db))
159 (clsql-mysql::database-execute-command "DROP TABLE test_clsql" db)
160 (clsql-mysql::database-disconnect db)))))
164 ;;;; Testing functions
166 (defun transform-float-1 (i)
167 (coerce (* i (abs (/ i 2)) (expt 10 (* 2 i))) 'double-float))
169 (defun transform-bigint-1 (i)
170 (* i (expt 10 (* 3 (abs i)))))
172 (defun create-test-table (db)
174 (clsql:execute-command
175 "DROP TABLE test_clsql" :database db))
176 (clsql:execute-command
177 "CREATE TABLE test_clsql (t_int integer, t_float float, t_bigint BIGINT, t_str CHAR(30))"
180 (let* ((test-int (- i 5))
181 (test-flt (transform-float-1 test-int)))
182 (clsql:execute-command
183 (format nil "INSERT INTO test_clsql VALUES (~a,~a,~a,'~a')"
185 (clsql-base:number-to-sql-string test-flt)
186 (transform-bigint-1 test-int)
187 (clsql-base:number-to-sql-string test-flt)
191 (defun parse-double (num-str)
192 (let ((*read-default-float-format* 'double-float))
193 (coerce (read-from-string num-str) 'double-float)))
195 (defun test-table-row (row types db-type)
196 (test (and (listp row)
200 (format nil "Row ~S is incorrect format" row))
201 (destructuring-bind (int float bigint str) row
204 (test (and (integerp int)
205 (typep float 'double-float)
206 (or (eq db-type :aodbc) ;; aodbc doesn't handle bigint conversions
211 (format nil "Incorrect field type for row ~S (types :auto)" row)))
213 (test (and (stringp int)
219 (format nil "Incorrect field type for row ~S (types nil)" row))
220 (setq int (parse-integer int))
221 (setq bigint (parse-integer bigint))
222 (setq float (parse-double float)))
229 (format nil "Invalid types field (~S) passed to test-table-row" types))))
230 (unless (eq db-type :sqlite) ; SQLite is typeless.
231 (test (transform-float-1 int)
235 (format nil "Wrong float value ~A for int ~A (row ~S)" float int row)))
238 :test #'double-float-equal
239 :fail-info (format nil "Wrong string value ~A for double ~A~%Row: ~S"
243 (defun double-float-equal (a b)
248 (let ((diff (abs (/ (- a b) a))))
249 (if (> diff (* 10 double-float-epsilon))
253 (defun drop-test-table (db)
254 (clsql:execute-command "DROP TABLE test_clsql" :database db))
257 (let ((specs (read-specs)))
259 (warn "Not running test because test configuration file is missing")
260 (return-from run-tests :skipped))
261 (with-tests (:name "CLSQL")
262 (mysql-low-level specs)
263 (mysql-table-test specs)
264 (pgsql-table-test specs)
265 (pgsql-socket-table-test specs)
266 (aodbc-table-test specs)
267 (sqlite-table-test specs)