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 (defgeneric test-table (spec type))
86 (defmethod test-table (spec type)
88 (let ((db (clsql:connect spec :database-type type :if-exists :new)))
91 (create-test-table db)
92 (dolist (row (query "select * from test_clsql" :database db :result-types :auto))
93 (test-table-row row :auto type))
94 (dolist (row (query "select * from test_clsql" :database db :result-types nil))
95 (test-table-row row nil type))
96 (loop for row across (map-query 'vector #'list "select * from test_clsql"
97 :database db :result-types :auto)
98 do (test-table-row row :auto type))
99 (loop for row across (map-query 'vector #'list "select * from test_clsql"
100 :database db :result-types nil)
101 do (test-table-row row nil type))
102 (loop for row in (map-query 'list #'list "select * from test_clsql"
103 :database db :result-types nil)
104 do (test-table-row row nil type))
105 (loop for row in (map-query 'list #'list "select * from test_clsql"
106 :database db :result-types :auto)
107 do (test-table-row row :auto type))
108 (test (map-query nil #'list "select * from test_clsql"
109 :database db :result-types :auto)
111 :fail-info "Expected NIL result from map-query nil")
112 (do-query ((int float bigint str) "select * from test_clsql")
113 (test-table-row (list int float bigint str) nil type))
114 (do-query ((int float bigint str) "select * from test_clsql" :result-types :auto)
115 (test-table-row (list int float bigint str) :auto type))
118 (disconnect :database db)))))
121 ;;; SQLite is typeless: execute untyped tests only.
123 (defmethod test-table (spec (type (eql :sqlite)))
125 (let ((db (clsql:connect spec :database-type type :if-exists :new)))
128 (create-test-table db)
129 (dolist (row (query "select * from test_clsql" :database db :result-types nil))
130 (test-table-row row nil type))
131 (loop for row across (map-query 'vector #'list "select * from test_clsql"
132 :database db :result-types nil)
133 do (test-table-row row nil type))
134 (loop for row in (map-query 'list #'list "select * from test_clsql"
135 :database db :result-types nil)
136 do (test-table-row row nil type))
138 (do-query ((int float bigint str) "select * from test_clsql")
139 (test-table-row (list int float bigint str) nil type))
142 (disconnect :database db)))))
144 (defun mysql-low-level (specs)
146 (let ((spec (mysql-spec specs)))
148 (let ((db (clsql-mysql::database-connect spec :mysql)))
149 (clsql-mysql::database-execute-command "DROP TABLE IF EXISTS test_clsql" db)
150 (clsql-mysql::database-execute-command
151 "CREATE TABLE test_clsql (i integer, sqrt double, sqrt_str CHAR(20))" db)
153 (clsql-mysql::database-execute-command
154 (format nil "INSERT INTO test_clsql VALUES (~d,~d,'~a')"
155 i (clsql-base:number-to-sql-string (sqrt i))
156 (clsql-base:number-to-sql-string (sqrt i)))
158 (let ((res (clsql-mysql::database-query-result-set "select * from test_clsql" db :full-set t :result-types nil)))
159 (test (mysql:mysql-num-rows
160 (clsql-mysql::mysql-result-set-res-ptr res))
163 :fail-info "Error calling mysql-num-rows")
164 (clsql-mysql::database-dump-result-set res db))
165 (clsql-mysql::database-execute-command "DROP TABLE test_clsql" db)
166 (clsql-mysql::database-disconnect db)))))
170 ;;;; Testing functions
172 (defun transform-float-1 (i)
173 (coerce (* i (abs (/ i 2)) (expt 10 (* 2 i))) 'double-float))
175 (defun transform-bigint-1 (i)
176 (* i (expt 10 (* 3 (abs i)))))
178 (defun create-test-table (db)
180 (clsql:execute-command
181 "DROP TABLE test_clsql" :database db))
182 (clsql:execute-command
183 "CREATE TABLE test_clsql (t_int integer, t_float float, t_bigint BIGINT, t_str CHAR(30))"
186 (let* ((test-int (- i 5))
187 (test-flt (transform-float-1 test-int)))
188 (clsql:execute-command
189 (format nil "INSERT INTO test_clsql VALUES (~a,~a,~a,'~a')"
191 (clsql-base:number-to-sql-string test-flt)
192 (transform-bigint-1 test-int)
193 (clsql-base:number-to-sql-string test-flt)
197 (defun parse-double (num-str)
198 (let ((*read-default-float-format* 'double-float))
199 (coerce (read-from-string num-str) 'double-float)))
201 (defun test-table-row (row types db-type)
202 (test (and (listp row)
206 (format nil "Row ~S is incorrect format" row))
207 (destructuring-bind (int float bigint str) row
210 (test (and (integerp int)
211 (typep float 'double-float)
212 (or (eq db-type :aodbc) ;; aodbc doesn't handle bigint conversions
217 (format nil "Incorrect field type for row ~S (types :auto)" row)))
219 (test (and (stringp int)
225 (format nil "Incorrect field type for row ~S (types nil)" row))
226 (setq int (parse-integer int))
227 (setq bigint (parse-integer bigint))
228 (setq float (parse-double float)))
235 (format nil "Invalid types field (~S) passed to test-table-row" types))))
236 (unless (eq db-type :sqlite) ; SQLite is typeless.
237 (test (transform-float-1 int)
241 (format nil "Wrong float value ~A for int ~A (row ~S)" float int row)))
244 :test #'double-float-equal
245 :fail-info (format nil "Wrong string value ~A for double ~A~%Row: ~S"
249 (defun double-float-equal (a b)
254 (let ((diff (abs (/ (- a b) a))))
255 (if (> diff (* 10 double-float-epsilon))
259 (defun drop-test-table (db)
260 (clsql:execute-command "DROP TABLE test_clsql" :database db))
262 (defun db-type-spec (db-type specs)
263 (let ((accessor (intern (concatenate 'string (symbol-name db-type)
264 (symbol-name '#:-spec))
265 (find-package '#:clsql-classic-tests))))
266 (funcall accessor specs)))
268 (defun db-type-ensure-system (db-type)
269 (unless (find-package (symbol-name db-type))
270 (asdf:operate 'asdf:load-op
271 (intern (concatenate 'string
272 (symbol-name '#:clsql-)
273 (symbol-name db-type))))))
276 (let ((specs (read-specs)))
278 (warn "Not running test because test configuration file is missing")
279 (return-from run-tests :skipped))
280 (mysql-low-level specs)
281 (with-tests (:name "CLSQL")
282 (dolist (db-type +all-db-types+)
283 (let ((spec (db-type-spec db-type specs)))
285 (db-type-ensure-system db-type)
286 (ignore-errors (destroy-database spec db-type))
287 (ignore-errors (create-database spec db-type))
288 (test-table spec db-type))))))