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.
21 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
22 ;;;; *************************************************************************
23 ;;;; FILE IDENTIFICATION
26 ;;;; Author: Kevin Rosenberg
29 ;;;; This file is part of CLSQL.
31 ;;;; CLSQL users are granted the rights to distribute and use this software
32 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
33 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
34 ;;;; *************************************************************************
36 ;;; You need a file named "~/.clsql-tests.config"
38 ;;; This file contains a single a-list that specifies the connection
39 ;;; specs for each database type to be tested. For example, to test all
40 ;;; platforms, a sample "test.config" may look like:
42 ;;; ((:mysql ("localhost" "a-mysql-db" "user1" "secret"))
43 ;;; (:aodbc ("my-dsn" "a-user" "pass"))
44 ;;; (:postgresql ("localhost" "another-db" "user2" "dont-tell"))
45 ;;; (:postgresql-socket ("pg-server" "a-db-name" "user" "secret-password"))
46 ;;; (:sqlite ("path-to-sqlite-db")))
48 (in-package #:clsql-classic-tests)
50 (defvar *config-pathname*
51 (make-pathname :defaults (user-homedir-pathname)
55 (defvar +all-db-types+
56 #-clisp '(:postgresql :postgresql-socket :sqlite :aodbc :mysql)
59 (defclass conn-specs ()
60 ((aodbc-spec :accessor aodbc-spec :initform nil)
61 (mysql-spec :accessor mysql-spec :initform nil)
62 (pgsql-spec :accessor postgresql-spec :initform nil)
63 (pgsql-socket-spec :accessor postgresql-socket-spec :initform nil)
64 (sqlite-spec :accessor sqlite-spec :initform nil))
65 (:documentation "Connection specs for CLSQL testing"))
68 (defun read-specs (&optional (path *config-pathname*))
70 (with-open-file (stream path :direction :input)
71 (let ((config (read stream))
72 (specs (make-instance 'conn-specs)))
73 (setf (aodbc-spec specs) (cadr (assoc :aodbc config)))
74 (setf (mysql-spec specs) (cadr (assoc :mysql config)))
75 (setf (postgresql-spec specs) (cadr (assoc :postgresql config)))
76 (setf (postgresql-socket-spec specs)
77 (cadr (assoc :postgresql-socket config)))
78 (setf (sqlite-spec specs) (cadr (assoc :sqlite config)))
81 (warn "CLSQL test config file ~S not found" path)
84 (defmethod test-table (spec type)
86 (let ((db (clsql:connect spec :database-type type :if-exists :new)))
89 (create-test-table db)
90 (dolist (row (query "select * from test_clsql" :database db :result-types :auto))
91 (test-table-row row :auto type))
92 (dolist (row (query "select * from test_clsql" :database db :result-types nil))
93 (test-table-row row nil type))
94 (loop for row across (map-query 'vector #'list "select * from test_clsql"
95 :database db :result-types :auto)
96 do (test-table-row row :auto type))
97 (loop for row across (map-query 'vector #'list "select * from test_clsql"
98 :database db :result-types nil)
99 do (test-table-row row nil type))
100 (loop for row in (map-query 'list #'list "select * from test_clsql"
101 :database db :result-types nil)
102 do (test-table-row row nil type))
103 (loop for row in (map-query 'list #'list "select * from test_clsql"
104 :database db :result-types :auto)
105 do (test-table-row row :auto type))
106 (test (map-query nil #'list "select * from test_clsql"
107 :database db :result-types :auto)
109 :fail-info "Expected NIL result from map-query nil")
110 (do-query ((int float bigint str) "select * from test_clsql")
111 (test-table-row (list int float bigint str) nil type))
112 (do-query ((int float bigint str) "select * from test_clsql" :result-types :auto)
113 (test-table-row (list int float bigint str) :auto type))
116 (disconnect :database db)))))
119 ;;; SQLite is typeless: execute untyped tests only.
121 (defmethod test-table (spec (type (eql :sqlite)))
123 (let ((db (clsql:connect spec :database-type type :if-exists :new)))
126 (create-test-table db)
127 (dolist (row (query "select * from test_clsql" :database db :result-types nil))
128 (test-table-row row nil type))
129 (loop for row across (map-query 'vector #'list "select * from test_clsql"
130 :database db :result-types nil)
131 do (test-table-row row nil type))
132 (loop for row in (map-query 'list #'list "select * from test_clsql"
133 :database db :result-types nil)
134 do (test-table-row row nil type))
136 (do-query ((int float bigint str) "select * from test_clsql")
137 (test-table-row (list int float bigint str) nil type))
140 (disconnect :database db)))))
142 (defmethod mysql-low-level ((test conn-specs))
144 (let ((spec (mysql-spec test)))
146 (let ((db (clsql-mysql::database-connect spec :mysql)))
147 (clsql-mysql::database-execute-command "DROP TABLE IF EXISTS test_clsql" db)
148 (clsql-mysql::database-execute-command
149 "CREATE TABLE test_clsql (i integer, sqrt double, sqrt_str CHAR(20))" db)
151 (clsql-mysql::database-execute-command
152 (format nil "INSERT INTO test_clsql VALUES (~d,~d,'~a')"
153 i (clsql-base:number-to-sql-string (sqrt i))
154 (clsql-base:number-to-sql-string (sqrt i)))
156 (let ((res (clsql-mysql::database-query-result-set "select * from test_clsql" db :full-set t :result-types nil)))
157 (test (mysql:mysql-num-rows
158 (clsql-mysql::mysql-result-set-res-ptr res))
161 :fail-info "Error calling mysql-num-rows")
162 (clsql-mysql::database-dump-result-set res db))
163 (clsql-mysql::database-execute-command "DROP TABLE test_clsql" db)
164 (clsql-mysql::database-disconnect db)))))
168 ;;;; Testing functions
170 (defun transform-float-1 (i)
171 (coerce (* i (abs (/ i 2)) (expt 10 (* 2 i))) 'double-float))
173 (defun transform-bigint-1 (i)
174 (* i (expt 10 (* 3 (abs i)))))
176 (defun create-test-table (db)
178 (clsql:execute-command
179 "DROP TABLE test_clsql" :database db))
180 (clsql:execute-command
181 "CREATE TABLE test_clsql (t_int integer, t_float float, t_bigint BIGINT, t_str CHAR(30))"
184 (let* ((test-int (- i 5))
185 (test-flt (transform-float-1 test-int)))
186 (clsql:execute-command
187 (format nil "INSERT INTO test_clsql VALUES (~a,~a,~a,'~a')"
189 (clsql-base:number-to-sql-string test-flt)
190 (transform-bigint-1 test-int)
191 (clsql-base:number-to-sql-string test-flt)
195 (defun parse-double (num-str)
196 (let ((*read-default-float-format* 'double-float))
197 (coerce (read-from-string num-str) 'double-float)))
199 (defun test-table-row (row types db-type)
200 (test (and (listp row)
204 (format nil "Row ~S is incorrect format" row))
205 (destructuring-bind (int float bigint str) row
208 (test (and (integerp int)
209 (typep float 'double-float)
210 (or (eq db-type :aodbc) ;; aodbc doesn't handle bigint conversions
215 (format nil "Incorrect field type for row ~S (types :auto)" row)))
217 (test (and (stringp int)
223 (format nil "Incorrect field type for row ~S (types nil)" row))
224 (setq int (parse-integer int))
225 (setq bigint (parse-integer bigint))
226 (setq float (parse-double float)))
233 (format nil "Invalid types field (~S) passed to test-table-row" types))))
234 (unless (eq db-type :sqlite) ; SQLite is typeless.
235 (test (transform-float-1 int)
239 (format nil "Wrong float value ~A for int ~A (row ~S)" float int row)))
242 :test #'double-float-equal
243 :fail-info (format nil "Wrong string value ~A for double ~A~%Row: ~S"
247 (defun double-float-equal (a b)
252 (let ((diff (abs (/ (- a b) a))))
253 (if (> diff (* 10 double-float-epsilon))
257 (defun drop-test-table (db)
258 (clsql:execute-command "DROP TABLE test_clsql" :database db))
260 (defun db-type-spec (db-type specs)
261 (let ((accessor (intern (concatenate 'string (symbol-name db-type)
262 (symbol-name '#:-spec))
263 (find-package '#:clsql-classic-tests))))
264 (funcall accessor specs)))
266 (defun db-type-ensure-system (db-type)
267 (unless (find-package (symbol-name db-type))
268 (asdf:operate 'asdf:load-op
269 (intern (concatenate 'string
270 (symbol-name '#:clsql-)
271 (symbol-name db-type))))))
274 (let ((specs (read-specs)))
276 (warn "Not running test because test configuration file is missing")
277 (return-from run-tests :skipped))
278 (mysql-low-level specs)
279 (with-tests (:name "CLSQL")
280 (dolist (db-type +all-db-types+)
281 (let ((spec (db-type-spec db-type specs)))
283 (db-type-ensure-system db-type)
284 (test-table spec db-type))))))