1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: xptest-clsql.cl
6 ;;;; Purpose: Test of CLSQL using XPTest package
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Mar 2002
10 ;;;; $Id: xptest-clsql.cl,v 1.1 2002/04/20 22:51:42 kevin Exp $
12 ;;;; The XPTest package can be downloaded from
13 ;;;; http://alpha.onshored.com/lisp-software/
15 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
17 ;;;; CLSQL users are granted the rights to distribute and use this software
18 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
19 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
20 ;;;; *************************************************************************
23 ;;; This test suite looks for a configuration file named "test.config"
24 ;;; This file contains a single a-list that specifies the connection
25 ;;; specs for each database type to be tested. For example, to test all
26 ;;; platforms, a sample "test.config" may look like:
28 ;;; ((:mysql ("localhost" "a-mysql-db" "user1" "secret"))
29 ;;; (:aodbc ("my-dsn" "a-user" "pass"))
30 ;;; (:paostgresql ("localhost" "another-db" "user2" "dont-tell"))
31 ;;; (:postgresql-socket ("pg-server" "a-db-name" "user" "secret-password")))
33 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
35 (mk:load-system "XPTest")
37 (in-package :clsql-user)
40 (def-test-fixture clsql-fixture ()
41 ((aodbc-spec :accessor aodbc-spec)
42 (mysql-spec :accessor mysql-spec)
43 (pgsql-spec :accessor pgsql-spec)
44 (pgsql-socket-spec :accessor pgsql-socket-spec))
45 (:documentation "Test fixture for CLSQL testing"))
47 (defvar *config-pathname* (make-pathname :name "test"
49 :defaults *load-truename*))
50 (defmethod setup ((fix clsql-fixture))
51 (if (probe-file *config-pathname*)
53 (with-open-file (stream *config-pathname* :direction :input)
54 (setq config (read stream)))
55 (setf (aodbc-spec fix) (cadr (assoc :aodbc config)))
56 (setf (mysql-spec fix) (cadr (assoc :mysql config)))
57 (setf (pgsql-spec fix) (cadr (assoc :postgresql config)))
58 (setf (pgsql-socket-spec fix)
59 (cadr (assoc :postgresql-socket config))))
60 (error "XPTest Config file ~S not found" *config-pathname*)))
62 (defmethod teardown ((fix clsql-fixture))
65 (defmethod mysql-table-test ((test clsql-fixture))
66 (test-table (mysql-spec test) :mysql))
68 (defmethod aodbc-table-test ((test clsql-fixture))
69 (test-table (aodbc-spec test) :aodbc))
71 (defmethod pgsql-table-test ((test clsql-fixture))
72 (test-table (pgsql-spec test) :postgresql))
74 (defmethod pgsql-socket-table-test ((test clsql-fixture))
75 (test-table (pgsql-socket-spec test) :postgresql-socket))
78 (defmethod test-table (spec type)
80 (let ((db (clsql:connect spec :database-type type :if-exists :new)))
83 (create-test-table db)
84 (dolist (row (query "select * from test_clsql" :database db :types :auto))
85 (test-table-row row :auto))
86 (dolist (row (query "select * from test_clsql" :database db :types nil))
87 (test-table-row row nil))
88 (loop for row across (map-query 'vector #'list "select * from test_clsql"
89 :database db :types :auto)
90 do (test-table-row row :auto))
91 (loop for row across (map-query 'vector #'list "select * from test_clsql"
92 :database db :types nil)
93 do (test-table-row row nil))
94 (loop for row in (map-query 'list #'list "select * from test_clsql"
95 :database db :types nil)
96 do (test-table-row row nil))
97 (loop for row in (map-query 'list #'list "select * from test_clsql"
98 :database db :types :auto)
99 do (test-table-row row :auto))
100 (when (map-query nil #'list "select * from test_clsql"
101 :database db :types :auto)
102 (failure "Expected NIL result from map-query nil"))
103 (do-query ((int float bigint str) "select * from test_clsql")
104 (test-table-row (list int float bigint str) nil))
105 (do-query ((int float bigint str) "select * from test_clsql" :types :auto)
106 (test-table-row (list int float bigint str) :auto))
109 (disconnect :database db)))))
112 (defmethod mysql-low-level ((test clsql-fixture))
113 (let ((spec (mysql-spec test)))
115 (let ((db (clsql-mysql::database-connect spec :mysql)))
116 (clsql-mysql::database-execute-command "DROP TABLE IF EXISTS test_clsql" db)
117 (clsql-mysql::database-execute-command
118 "CREATE TABLE test_clsql (i integer, sqrt double, sqrt_str CHAR(20))" db)
120 (clsql-mysql::database-execute-command
121 (format nil "INSERT INTO test_clsql VALUES (~d,~d,'~a')"
122 i (number-to-sql-string (sqrt i))
123 (number-to-sql-string (sqrt i)))
125 (let ((res (clsql-mysql::database-query-result-set "select * from test_clsql" db :full-set t :types nil)))
126 (unless (= 10 (mysql:mysql-num-rows (clsql-mysql::mysql-result-set-res-ptr res)))
127 (failure "Error calling mysql-num-rows"))
128 (clsql-mysql::database-dump-result-set res db))
129 (clsql-mysql::database-execute-command "DROP TABLE test_clsql" db)
130 (clsql-mysql::database-disconnect db)))))
132 (defparameter clsql-test-suite
135 "Basic test suite for database operations."
136 ("MySQL Low Level Interface" 'clsql-fixture
137 :test-thunk 'mysql-low-level
138 :description "A test of MySQL low-level interface")
139 ("MySQL Table" 'clsql-fixture
140 :test-thunk 'mysql-table-test
141 :description "A test of MySQL")
142 ("PostgreSQL Table" 'clsql-fixture
143 :test-thunk 'pgsql-table-test
144 :description "A test of PostgreSQL tables")
145 ("PostgreSQL Socket Table" 'clsql-fixture
146 :test-thunk 'pgsql-socket-table-test
147 :description "A test of PostgreSQL Socket tables")
151 (add-test (make-test-case "AODBC table test" 'clsql-fixture
152 :test-thunk 'aodbc-table-test
153 :description "Test AODBC table")
156 ;;;; Testing functions
158 (defun transform-float-1 (i)
159 (* i (abs (/ i 2)) (expt 10 (* 2 i))))
161 (defun transform-bigint-1 (i)
162 (* i (expt 10 (* 3 (abs i)))))
164 (defun create-test-table (db)
166 (clsql:execute-command
167 "DROP TABLE test_clsql" :database db))
168 (clsql:execute-command
169 "CREATE TABLE test_clsql (t_int integer, t_float float, t_bigint BIGINT, t_str CHAR(30))"
172 (let* ((test-int (- i 5))
173 (test-flt (transform-float-1 test-int)))
174 (clsql:execute-command
175 (format nil "INSERT INTO test_clsql VALUES (~a,~a,~a,'~a')"
177 (number-to-sql-string test-flt)
178 (transform-bigint-1 test-int)
179 (number-to-sql-string test-flt)
183 (defun parse-double (num-str)
184 (let ((*read-default-float-format* 'double-float))
185 (coerce (read-from-string num-str) 'double-float)))
187 (defun test-table-row (row types)
188 (unless (and (listp row)
190 (failure "Row ~S is incorrect format" row))
191 (destructuring-bind (int float bigint str) row
194 (unless (and (integerp int)
195 (typep float 'double-float)
198 (failure "Incorrect field type for row ~S" row)))
200 (unless (and (stringp int)
204 (failure "Incorrect field type for row ~S" row))
205 (setq int (parse-integer int))
206 (setq bigint (parse-integer bigint))
207 (setq float (parse-double float)))
212 (failure "Invalid types field (~S) passed to test-table-row" types)))
213 (unless (= float (transform-float-1 int))
214 (failure "Wrong float value ~A for int ~A (row ~S)" float int row))
215 (unless (= float (parse-double str))
216 (failure "Wrong string value ~A" str))))
219 (defun drop-test-table (db)
220 (clsql:execute-command "DROP TABLE test_clsql"))
222 (report-result (run-test clsql-test-suite :handle-errors nil) :verbose t)