1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
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")))
31 (in-package :clsql-tests)
33 (defvar *config-pathname*
34 (make-pathname :defaults (user-homedir-pathname)
39 (defclass conn-specs ()
40 ((aodbc-spec :accessor aodbc-spec)
41 (mysql-spec :accessor mysql-spec)
42 (pgsql-spec :accessor pgsql-spec)
43 (pgsql-socket-spec :accessor pgsql-socket-spec))
44 (:documentation "Test fixture for CLSQL testing"))
47 (defun read-specs (&optional (path *config-pathname*))
49 (with-open-file (stream path :direction :input)
50 (let ((config (read stream))
51 (specs (make-instance 'conn-specs)))
52 (setf (aodbc-spec specs) (cadr (assoc :aodbc config)))
53 (setf (mysql-spec specs) (cadr (assoc :mysql config)))
54 (setf (pgsql-spec specs) (cadr (assoc :postgresql config)))
55 (setf (pgsql-socket-spec specs)
56 (cadr (assoc :postgresql-socket config)))
58 (error "CLSQL tester config file ~S not found" path)))
60 (defmethod mysql-table-test ((test conn-specs))
61 (test-table (mysql-spec test) :mysql))
63 (defmethod aodbc-table-test ((test conn-specs))
64 (test-table (aodbc-spec test) :aodbc))
66 (defmethod pgsql-table-test ((test conn-specs))
67 (test-table (pgsql-spec test) :postgresql))
69 (defmethod pgsql-socket-table-test ((test conn-specs))
70 (test-table (pgsql-socket-spec test) :postgresql-socket))
72 (defmethod test-table (spec type)
74 (let ((db (clsql:connect spec :database-type type :if-exists :new)))
77 (create-test-table db)
78 (dolist (row (query "select * from test_clsql" :database db :types :auto))
79 (test-table-row row :auto type))
80 (dolist (row (query "select * from test_clsql" :database db :types nil))
81 (test-table-row row nil type))
82 (loop for row across (map-query 'vector #'list "select * from test_clsql"
83 :database db :types :auto)
84 do (test-table-row row :auto type))
85 (loop for row across (map-query 'vector #'list "select * from test_clsql"
86 :database db :types nil)
87 do (test-table-row row nil type))
88 (loop for row in (map-query 'list #'list "select * from test_clsql"
89 :database db :types nil)
90 do (test-table-row row nil type))
91 (loop for row in (map-query 'list #'list "select * from test_clsql"
92 :database db :types :auto)
93 do (test-table-row row :auto type))
94 (test (map-query nil #'list "select * from test_clsql"
95 :database db :types :auto)
97 :fail-info "Expected NIL result from map-query nil")
98 (do-query ((int float bigint str) "select * from test_clsql")
99 (test-table-row (list int float bigint str) nil type))
100 (do-query ((int float bigint str) "select * from test_clsql" :types :auto)
101 (test-table-row (list int float bigint str) :auto type))
104 (disconnect :database db)))))
107 (defmethod mysql-low-level ((test conn-specs))
108 (let ((spec (mysql-spec test)))
110 (let ((db (clsql-mysql::database-connect spec :mysql)))
111 (clsql-mysql::database-execute-command "DROP TABLE IF EXISTS test_clsql" db)
112 (clsql-mysql::database-execute-command
113 "CREATE TABLE test_clsql (i integer, sqrt double, sqrt_str CHAR(20))" db)
115 (clsql-mysql::database-execute-command
116 (format nil "INSERT INTO test_clsql VALUES (~d,~d,'~a')"
117 i (clsql:number-to-sql-string (sqrt i))
118 (clsql:number-to-sql-string (sqrt i)))
120 (let ((res (clsql-mysql::database-query-result-set "select * from test_clsql" db :full-set t :types nil)))
121 (test (mysql:mysql-num-rows
122 (clsql-mysql::mysql-result-set-res-ptr res))
125 :fail-info "Error calling mysql-num-rows")
126 (clsql-mysql::database-dump-result-set res db))
127 (clsql-mysql::database-execute-command "DROP TABLE test_clsql" db)
128 (clsql-mysql::database-disconnect db)))))
132 ;;;; Testing functions
134 (defun transform-float-1 (i)
135 (coerce (* i (abs (/ i 2)) (expt 10 (* 2 i))) 'double-float))
137 (defun transform-bigint-1 (i)
138 (* i (expt 10 (* 3 (abs i)))))
140 (defun create-test-table (db)
142 (clsql:execute-command
143 "DROP TABLE test_clsql" :database db))
144 (clsql:execute-command
145 "CREATE TABLE test_clsql (t_int integer, t_float float, t_bigint BIGINT, t_str CHAR(30))"
148 (let* ((test-int (- i 5))
149 (test-flt (transform-float-1 test-int)))
150 (clsql:execute-command
151 (format nil "INSERT INTO test_clsql VALUES (~a,~a,~a,'~a')"
153 (number-to-sql-string test-flt)
154 (transform-bigint-1 test-int)
155 (number-to-sql-string test-flt)
159 (defun parse-double (num-str)
160 (let ((*read-default-float-format* 'double-float))
161 (coerce (read-from-string num-str) 'double-float)))
163 (defun test-table-row (row types db-type)
164 (test (and (listp row)
168 (format nil "Row ~S is incorrect format" row))
169 (destructuring-bind (int float bigint str) row
172 (test (and (integerp int)
173 (typep float 'double-float)
174 (or (eq db-type :aodbc) ;; aodbc doesn't handle bigint conversions
179 (format nil "Incorrect field type for row ~S (types :auto)" row)))
181 (test (and (stringp int)
187 (format nil "Incorrect field type for row ~S (types nil)" row))
188 (setq int (parse-integer int))
189 (setq bigint (parse-integer bigint))
190 (setq float (parse-double float)))
197 (format nil "Invalid types field (~S) passed to test-table-row" types))))
198 (test (transform-float-1 int)
202 (format nil "Wrong float value ~A for int ~A (row ~S)" float int row))
205 :test #'double-float-equal
206 :fail-info (format nil "Wrong string value ~A for double ~A~%Row: ~S"
210 (defun double-float-equal (a b)
215 (let ((diff (abs (/ (- a b) a))))
216 (if (> diff (* 10 double-float-epsilon))
220 (defun drop-test-table (db)
221 (clsql:execute-command "DROP TABLE test_clsql" :database db))
223 (defun clsql-tests ()
224 (let ((specs (read-specs)))
225 (with-tests (:name "CLSQL")
226 (mysql-low-level specs)
227 (mysql-table-test specs)
228 (pgsql-table-test specs)
229 (pgsql-socket-table-test specs)
230 (aodbc-table-test specs)
234 ;(deftest clsql.all (clsql-tests) t)