1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: Table creation tests in CLSQL
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 :default (user-homedir-pathname)
38 (defclass conn-specs ()
39 ((aodbc-spec :accessor aodbc-spec)
40 (mysql-spec :accessor mysql-spec)
41 (pgsql-spec :accessor pgsql-spec)
42 (pgsql-socket-spec :accessor pgsql-socket-spec))
43 (:documentation "Test fixture for CLSQL testing"))
46 (defun read-specs (&optional (path *config-pathname*))
48 (with-open-file (stream path :direction :input)
49 (let ((config (read stream))
50 (specs (make-instance 'conn-specs)))
51 (setf (aodbc-spec specs) (cadr (assoc :aodbc config)))
52 (setf (mysql-spec specs) (cadr (assoc :mysql config)))
53 (setf (pgsql-spec specs) (cadr (assoc :postgresql config)))
54 (setf (pgsql-socket-spec specs)
55 (cadr (assoc :postgresql-socket config)))
57 (warn "CLSQL tester config file ~S not found" path)))
59 (defvar *conn-specs* (read-specs))
61 (defmethod mysql-table-test ((test conn-specs))
62 (test-table (mysql-spec test) :mysql))
64 (defmethod aodbc-table-test ((test conn-specs))
65 (test-table (aodbc-spec test) :aodbc))
67 (defmethod pgsql-table-test ((test conn-specs))
68 (test-table (pgsql-spec test) :postgresql))
70 (defmethod pgsql-socket-table-test ((test conn-specs))
71 (test-table (pgsql-socket-spec test) :postgresql-socket))
73 (defmethod test-table (spec type)
75 (let ((db (clsql:connect spec :database-type type :if-exists :new)))
78 (create-test-table db)
79 (dolist (row (query "select * from test_clsql" :database db :types :auto))
80 (test-table-row row :auto type))
81 (dolist (row (query "select * from test_clsql" :database db :types nil))
82 (test-table-row row nil type))
83 (loop for row across (map-query 'vector #'list "select * from test_clsql"
84 :database db :types :auto)
85 do (test-table-row row :auto type))
86 (loop for row across (map-query 'vector #'list "select * from test_clsql"
87 :database db :types nil)
88 do (test-table-row row nil type))
89 (loop for row in (map-query 'list #'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 :auto)
94 do (test-table-row row :auto type))
95 (test (map-query nil #'list "select * from test_clsql"
96 :database db :types :auto)
98 :fail-info "Expected NIL result from map-query nil")
99 (do-query ((int float bigint str) "select * from test_clsql")
100 (test-table-row (list int float bigint str) nil type))
101 (do-query ((int float bigint str) "select * from test_clsql" :types :auto)
102 (test-table-row (list int float bigint str) :auto type))
105 (disconnect :database db)))))
108 ;;;; Testing functions
110 (defun transform-float-1 (i)
111 (coerce (* i (abs (/ i 2)) (expt 10 (* 2 i))) 'double-float))
113 (defun transform-bigint-1 (i)
114 (* i (expt 10 (* 3 (abs i)))))
116 (defun create-test-table (db)
118 (clsql:execute-command
119 "DROP TABLE test_clsql" :database db))
120 (clsql:execute-command
121 "CREATE TABLE test_clsql (t_int integer, t_float float, t_bigint BIGINT, t_str CHAR(30))"
124 (let* ((test-int (- i 5))
125 (test-flt (transform-float-1 test-int)))
126 (clsql:execute-command
127 (format nil "INSERT INTO test_clsql VALUES (~a,~a,~a,'~a')"
129 (number-to-sql-string test-flt)
130 (transform-bigint-1 test-int)
131 (number-to-sql-string test-flt)
135 (defun parse-double (num-str)
136 (let ((*read-default-float-format* 'double-float))
137 (coerce (read-from-string num-str) 'double-float)))
139 (defun test-table-row (row types db-type)
140 (test (and (listp row)
144 (format nil "Row ~S is incorrect format" row))
145 (destructuring-bind (int float bigint str) row
148 (test (and (integerp int)
149 (typep float 'double-float)
150 (or (eq db-type :aodbc) ;; aodbc doesn't handle bigint conversions
155 (format nil "Incorrect field type for row ~S (types :auto)" row)))
157 (test (and (stringp int)
163 (format nil "Incorrect field type for row ~S (types nil)" row))
164 (setq int (parse-integer int))
165 (setq bigint (parse-integer bigint))
166 (setq float (parse-double float)))
173 (format nil "Invalid types field (~S) passed to test-table-row" types))))
174 (test (transform-float-1 int)
178 (format nil "Wrong float value ~A for int ~A (row ~S)" float int row))
181 :test #'double-float-equal
182 :fail-info (format nil "Wrong string value ~A for double ~A~%Row: ~S"
186 (defun double-float-equal (a b)
191 (let ((diff (abs (/ (- a b) a))))
192 (if (> diff (* 10 double-float-epsilon))
196 (defun drop-test-table (db)
197 (clsql:execute-command "DROP TABLE test_clsql" :database db))
200 (deftest lowlevel.mysql.table.1
201 (let ((spec (mysql-spec *conn-specs*))
204 (let ((db (clsql-mysql::database-connect spec :mysql)))
205 (clsql-mysql::database-execute-command "DROP TABLE IF EXISTS test_clsql" db)
206 (clsql-mysql::database-execute-command
207 "CREATE TABLE test_clsql (i integer, sqrt double, sqrt_str CHAR(20))" db)
209 (clsql-mysql::database-execute-command
210 (format nil "INSERT INTO test_clsql VALUES (~d,~d,'~a')"
211 i (clsql:number-to-sql-string (sqrt i))
212 (clsql:number-to-sql-string (sqrt i)))
214 (let ((res (clsql-mysql::database-query-result-set "select * from test_clsql" db :full-set t :types nil)))
215 (setq result (mysql:mysql-num-rows
216 (clsql-mysql::mysql-result-set-res-ptr res)))
217 (clsql-mysql::database-dump-result-set res db))
218 (clsql-mysql::database-execute-command "DROP TABLE test_clsql" db)
219 (clsql-mysql::database-disconnect db))))
222 ;(mysql-table-test specs)
223 ;(pgsql-table-test specs)
224 ;(pgsql-socket-table-test specs)
225 ;(aodbc-table-test specs)
229 (defmacro def-test-table (name spec type)
232 (let ((db (clsql:connect ,spec :database-type ,type :if-exists :new)))
235 (create-test-table db)
236 (dolist (row (query "select * from test_clsql" :database db :types :auto))
237 (test-table-row row :auto type))
238 (dolist (row (query "select * from test_clsql" :database db :types nil))
239 (test-table-row row nil type))
240 (loop for row across (map-query 'vector #'list "select * from test_clsql"
241 :database db :types :auto)
242 do (test-table-row row :auto type))
243 (loop for row across (map-query 'vector #'list "select * from test_clsql"
244 :database db :types nil)
245 do (test-table-row row nil type))
246 (loop for row in (map-query 'list #'list "select * from test_clsql"
247 :database db :types nil)
248 do (test-table-row row nil type))
249 (loop for row in (map-query 'list #'list "select * from test_clsql"
250 :database db :types :auto)
251 do (test-table-row row :auto type))
252 (test (map-query nil #'list "select * from test_clsql"
253 :database db :types :auto)
255 :fail-info "Expected NIL result from map-query nil")
256 (do-query ((int float bigint str) "select * from test_clsql")
257 (test-table-row (list int float bigint str) nil type))
258 (do-query ((int float bigint str) "select * from test_clsql" :types :auto)
259 (test-table-row (list int float bigint str) :auto type))
262 (disconnect :database db)))))