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.2 2002/04/08 02:52:39 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*))
34 (defclass conn-specs ()
35 ((aodbc-spec :accessor aodbc-spec)
36 (mysql-spec :accessor mysql-spec)
37 (pgsql-spec :accessor pgsql-spec)
38 (pgsql-socket-spec :accessor pgsql-socket-spec))
39 (:documentation "Test fixture for CLSQL testing"))
42 (error "Not yet implemented")
44 (defmethod setup ((fix conn-specs))
45 (if (probe-file *config-pathname*)
47 (with-open-file (stream *config-pathname* :direction :input)
48 (setq config (read stream)))
49 (setf (aodbc-spec fix) (cadr (assoc :aodbc config)))
50 (setf (mysql-spec fix) (cadr (assoc :mysql config)))
51 (setf (pgsql-spec fix) (cadr (assoc :postgresql config)))
52 (setf (pgsql-socket-spec fix)
53 (cadr (assoc :postgresql-socket config))))
54 (error "XPTest Config file ~S not found" *config-pathname*)))
56 (defmethod teardown ((fix conn-specs))
59 (defmethod mysql-table-test ((test conn-specs))
60 (test-table (mysql-spec test) :mysql))
62 (defmethod aodbc-table-test ((test conn-specs))
63 (test-table (aodbc-spec test) :aodbc))
65 (defmethod pgsql-table-test ((test conn-specs))
66 (test-table (pgsql-spec test) :postgresql))
68 (defmethod pgsql-socket-table-test ((test conn-specs))
69 (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))
80 (dolist (row (query "select * from test_clsql" :database db :types nil))
81 (test-table-row row nil))
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))
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))
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))
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))
94 (when (map-query nil #'list "select * from test_clsql"
95 :database db :types :auto)
96 (failure "Expected NIL result from map-query nil"))
97 (do-query ((int float bigint str) "select * from test_clsql")
98 (test-table-row (list int float bigint str) nil))
99 (do-query ((int float bigint str) "select * from test_clsql" :types :auto)
100 (test-table-row (list int float bigint str) :auto))
103 (disconnect :database db)))))
106 (defmethod mysql-low-level ((test conn-specs))
107 (let ((spec (mysql-spec test)))
109 (let ((db (clsql-mysql::database-connect spec :mysql)))
110 (clsql-mysql::database-execute-command "DROP TABLE IF EXISTS test_clsql" db)
111 (clsql-mysql::database-execute-command
112 "CREATE TABLE test_clsql (i integer, sqrt double, sqrt_str CHAR(20))" db)
114 (clsql-mysql::database-execute-command
115 (format nil "INSERT INTO test_clsql VALUES (~d,~d,'~a')"
116 i (number-to-sql-string (sqrt i))
117 (number-to-sql-string (sqrt i)))
119 (let ((res (clsql-mysql::database-query-result-set "select * from test_clsql" db :full-set t :types nil)))
120 (unless (= 10 (mysql:mysql-num-rows (clsql-mysql::mysql-result-set-res-ptr res)))
121 (failure "Error calling mysql-num-rows"))
122 (clsql-mysql::database-dump-result-set res db))
123 (clsql-mysql::database-execute-command "DROP TABLE test_clsql" db)
124 (clsql-mysql::database-disconnect db)))))
126 (defparameter clsql-test-suite
129 "Basic test suite for database operations."
130 ("MySQL Low Level Interface" 'conn-specs
131 :test-thunk 'mysql-low-level
132 :description "A test of MySQL low-level interface")
133 ("MySQL Table" 'conn-specs
134 :test-thunk 'mysql-table-test
135 :description "A test of MySQL")
136 ("PostgreSQL Table" 'conn-specs
137 :test-thunk 'pgsql-table-test
138 :description "A test of PostgreSQL tables")
139 ("PostgreSQL Socket Table" 'conn-specs
140 :test-thunk 'pgsql-socket-table-test
141 :description "A test of PostgreSQL Socket tables")
145 (add-test (make-test-case "AODBC table test" 'conn-specs
146 :test-thunk 'aodbc-table-test
147 :description "Test AODBC table")
150 ;;;; Testing functions
152 (defun transform-float-1 (i)
153 (* i (abs (/ i 2)) (expt 10 (* 2 i))))
155 (defun transform-bigint-1 (i)
156 (* i (expt 10 (* 3 (abs i)))))
158 (defun create-test-table (db)
160 (clsql:execute-command
161 "DROP TABLE test_clsql" :database db))
162 (clsql:execute-command
163 "CREATE TABLE test_clsql (t_int integer, t_float float, t_bigint BIGINT, t_str CHAR(30))"
166 (let* ((test-int (- i 5))
167 (test-flt (transform-float-1 test-int)))
168 (clsql:execute-command
169 (format nil "INSERT INTO test_clsql VALUES (~a,~a,~a,'~a')"
171 (number-to-sql-string test-flt)
172 (transform-bigint-1 test-int)
173 (number-to-sql-string test-flt)
177 (defun parse-double (num-str)
178 (let ((*read-default-float-format* 'double-float))
179 (coerce (read-from-string num-str) 'double-float)))
181 (defun test-table-row (row types)
182 (unless (and (listp row)
184 (failure "Row ~S is incorrect format" row))
185 (destructuring-bind (int float bigint str) row
188 (unless (and (integerp int)
189 (typep float 'double-float)
192 (failure "Incorrect field type for row ~S" row)))
194 (unless (and (stringp int)
198 (failure "Incorrect field type for row ~S" row))
199 (setq int (parse-integer int))
200 (setq bigint (parse-integer bigint))
201 (setq float (parse-double float)))
206 (failure "Invalid types field (~S) passed to test-table-row" types)))
207 (unless (= float (transform-float-1 int))
208 (failure "Wrong float value ~A for int ~A (row ~S)" float int row))
209 (unless (= float (parse-double str))
210 (failure "Wrong string value ~A" str))))
213 (defun drop-test-table (db)
214 (clsql:execute-command "DROP TABLE test_clsql"))
216 (report-result (run-test clsql-test-suite :handle-errors nil) :verbose t)