1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: tester-clsql.cl
6 ;;;; Purpose: Automated test of CLSQL using ACL's tester
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Mar 2002
10 ;;;; $Id: tester-clsql.cl,v 1.3 2002/04/08 03:50:00 kevin Exp $
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 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
22 (unless (find-package :util.test)
23 (load (make-pathname :name "acl-compat-tester" :type "cl"
24 :defaults *load-truename*)))
26 (in-package :clsql-user)
27 (use-package :util.test)
29 (defvar *config-pathname* (make-pathname :name "test"
31 :defaults *load-truename*))
33 (defclass conn-specs ()
34 ((aodbc-spec :accessor aodbc-spec)
35 (mysql-spec :accessor mysql-spec)
36 (pgsql-spec :accessor pgsql-spec)
37 (pgsql-socket-spec :accessor pgsql-socket-spec))
38 (:documentation "Test fixture for CLSQL testing"))
41 (defun read-specs (&optional (path *config-pathname*))
43 (with-open-file (stream path :direction :input)
44 (let ((config (read stream))
45 (specs (make-instance 'conn-specs)))
46 (setf (aodbc-spec specs) (cadr (assoc :aodbc config)))
47 (setf (mysql-spec specs) (cadr (assoc :mysql config)))
48 (setf (pgsql-spec specs) (cadr (assoc :postgresql config)))
49 (setf (pgsql-socket-spec specs)
50 (cadr (assoc :postgresql-socket config)))
52 (error "CLSQL tester config file ~S not found" path)))
54 (defmethod mysql-table-test ((test conn-specs))
55 (test-table (mysql-spec test) :mysql))
57 (defmethod aodbc-table-test ((test conn-specs))
58 (test-table (aodbc-spec test) :aodbc))
60 (defmethod pgsql-table-test ((test conn-specs))
61 (test-table (pgsql-spec test) :postgresql))
63 (defmethod pgsql-socket-table-test ((test conn-specs))
64 (test-table (pgsql-socket-spec test) :postgresql-socket))
66 (defmethod test-table (spec type)
68 (let ((db (clsql:connect spec :database-type type :if-exists :new)))
71 (create-test-table db)
72 (dolist (row (query "select * from test_clsql" :database db :types :auto))
73 (test-table-row row :auto type))
74 (dolist (row (query "select * from test_clsql" :database db :types nil))
75 (test-table-row row nil type))
76 (loop for row across (map-query 'vector #'list "select * from test_clsql"
77 :database db :types :auto)
78 do (test-table-row row :auto type))
79 (loop for row across (map-query 'vector #'list "select * from test_clsql"
80 :database db :types nil)
81 do (test-table-row row nil type))
82 (loop for row in (map-query 'list #'list "select * from test_clsql"
83 :database db :types nil)
84 do (test-table-row row nil type))
85 (loop for row in (map-query 'list #'list "select * from test_clsql"
86 :database db :types :auto)
87 do (test-table-row row :auto type))
88 (test (map-query nil #'list "select * from test_clsql"
89 :database db :types :auto)
91 :fail-info "Expected NIL result from map-query nil")
92 (do-query ((int float bigint str) "select * from test_clsql")
93 (test-table-row (list int float bigint str) nil type))
94 (do-query ((int float bigint str) "select * from test_clsql" :types :auto)
95 (test-table-row (list int float bigint str) :auto type))
98 (disconnect :database db)))))
101 (defmethod mysql-low-level ((test conn-specs))
102 (let ((spec (mysql-spec test)))
104 (let ((db (clsql-mysql::database-connect spec :mysql)))
105 (clsql-mysql::database-execute-command "DROP TABLE IF EXISTS test_clsql" db)
106 (clsql-mysql::database-execute-command
107 "CREATE TABLE test_clsql (i integer, sqrt double, sqrt_str CHAR(20))" db)
109 (clsql-mysql::database-execute-command
110 (format nil "INSERT INTO test_clsql VALUES (~d,~d,'~a')"
111 i (number-to-sql-string (sqrt i))
112 (number-to-sql-string (sqrt i)))
114 (let ((res (clsql-mysql::database-query-result-set "select * from test_clsql" db :full-set t :types nil)))
115 (test (mysql:mysql-num-rows
116 (clsql-mysql::mysql-result-set-res-ptr res))
119 :fail-info "Error calling mysql-num-rows")
120 (clsql-mysql::database-dump-result-set res db))
121 (clsql-mysql::database-execute-command "DROP TABLE test_clsql" db)
122 (clsql-mysql::database-disconnect db)))))
126 ;;;; Testing functions
128 (defun transform-float-1 (i)
129 (* i (abs (/ i 2)) (expt 10 (* 2 i))))
131 (defun transform-bigint-1 (i)
132 (* i (expt 10 (* 3 (abs i)))))
134 (defun create-test-table (db)
136 (clsql:execute-command
137 "DROP TABLE test_clsql" :database db))
138 (clsql:execute-command
139 "CREATE TABLE test_clsql (t_int integer, t_float float, t_bigint BIGINT, t_str CHAR(30))"
142 (let* ((test-int (- i 5))
143 (test-flt (transform-float-1 test-int)))
144 (clsql:execute-command
145 (format nil "INSERT INTO test_clsql VALUES (~a,~a,~a,'~a')"
147 (number-to-sql-string test-flt)
148 (transform-bigint-1 test-int)
149 (number-to-sql-string test-flt)
153 (defun parse-double (num-str)
154 (let ((*read-default-float-format* 'double-float))
155 (coerce (read-from-string num-str) 'double-float)))
157 (defun test-table-row (row types db-type)
158 (test (and (listp row)
162 (format nil "Row ~S is incorrect format" row))
163 (destructuring-bind (int float bigint str) row
166 (test (and (integerp int)
167 (typep float 'double-float)
168 (or (eq db-type :aodbc) ;; aodbc doesn't handle bigint conversions
173 (format nil "Incorrect field type for row ~S (types :auto)" row)))
175 (test (and (stringp int)
181 (format nil "Incorrect field type for row ~S (types nil)" row))
182 (setq int (parse-integer int))
183 (setq bigint (parse-integer bigint))
184 (setq float (parse-double float)))
191 (format nil "Invalid types field (~S) passed to test-table-row" types))))
192 (test (transform-float-1 int)
196 (format nil "Wrong float value ~A for int ~A (row ~S)" float int row))
197 (test (parse-double str)
200 :fail-info (format nil "Wrong string value ~A" str))))
203 (defun drop-test-table (db)
204 (clsql:execute-command "DROP TABLE test_clsql"))
209 (let ((specs (read-specs)))
210 (mysql-low-level specs)
211 (mysql-table-test specs)
212 (pgsql-table-test specs)
213 (pgsql-socket-table-test specs)
214 (aodbc-table-test specs)