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.6 2002/03/27 11:13:27 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 ;;;; *************************************************************************
22 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
24 (mk:load-system "XPTest")
26 (in-package :clsql-user)
29 (def-test-fixture clsql-fixture ()
30 ((aodbc-spec :accessor aodbc-spec)
31 (mysql-spec :accessor mysql-spec)
32 (pgsql-spec :accessor pgsql-spec)
33 (pgsql-socket-spec :accessor pgsql-socket-spec))
34 (:documentation "Test fixture for CLSQL testing"))
36 (defvar *config-pathname* (make-pathname :name "test"
38 :defaults *load-truename*))
39 (defmethod setup ((fix clsql-fixture))
40 (if (probe-file *config-pathname*)
42 (with-open-file (stream *config-pathname* :direction :input)
43 (setq config (read stream)))
44 (setf (aodbc-spec fix) (cadr (assoc :aodbc config)))
45 (setf (mysql-spec fix) (cadr (assoc :mysql config)))
46 (setf (pgsql-spec fix) (cadr (assoc :postgresql config)))
47 (setf (pgsql-socket-spec fix)
48 (cadr (assoc :postgresql-socket config))))
49 (error "XPTest Config file ~S not found" *config-pathname*)))
51 (defmethod teardown ((fix clsql-fixture))
54 (defmethod mysql-table-test ((test clsql-fixture))
55 (test-table (mysql-spec test) :mysql))
57 (defmethod aodbc-table-test ((test clsql-fixture))
58 (test-table (aodbc-spec test) :aodbc))
60 (defmethod pgsql-table-test ((test clsql-fixture))
61 (test-table (pgsql-spec test) :postgresql))
63 (defmethod pgsql-socket-table-test ((test clsql-fixture))
64 (test-table (pgsql-socket-spec test) :postgresql-socket))
67 (defmethod test-table (spec type)
69 (let ((db (clsql:connect spec :database-type type :if-exists :new)))
72 (create-test-table db)
73 (dolist (row (query "select * from test_clsql" :database db :types :auto))
74 (test-table-row row :auto))
75 (dolist (row (query "select * from test_clsql" :database db :types nil))
76 (test-table-row row nil))
77 (loop for row across (map-query 'vector #'list "select * from test_clsql"
78 :database db :types :auto)
79 do (test-table-row row :auto))
80 (loop for row across (map-query 'vector #'list "select * from test_clsql"
81 :database db :types nil)
82 do (test-table-row row nil))
83 (loop for row in (map-query 'list #'list "select * from test_clsql"
84 :database db :types nil)
85 do (test-table-row row nil))
86 (loop for row in (map-query 'list #'list "select * from test_clsql"
87 :database db :types :auto)
88 do (test-table-row row :auto))
89 (when (map-query nil #'list "select * from test_clsql"
90 :database db :types :auto)
91 (failure "Expected NIL result from map-query nil"))
92 (do-query ((int float str) "select * from test_clsql")
93 (test-table-row (list int float str) nil))
94 (do-query ((int float str) "select * from test_clsql" :types :auto)
95 (test-table-row (list int float str) :auto))
98 (disconnect :database db)))))
101 (defmethod mysql-low-level ((test clsql-fixture))
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 (sqrt i) (format nil "~d" (sqrt i)))
113 (let ((res (clsql-mysql::database-query-result-set "select * from test_clsql" db :full-set t :types nil)))
114 (unless (= 10 (mysql:mysql-num-rows (clsql-mysql::mysql-result-set-res-ptr res)))
115 (failure "Error calling mysql-num-rows"))
116 (clsql-mysql::database-dump-result-set res db))
117 (clsql-mysql::database-execute-command "DROP TABLE test_clsql" db)
118 (clsql-mysql::database-disconnect db)))))
120 (defparameter clsql-test-suite
123 "Basic test suite for database operations."
124 ("MySQL Low Level Interface Test" 'clsql-fixture
125 :test-thunk 'mysql-low-level
126 :description "A test of MySQL low-level interface")
127 ("MySQL Test" 'clsql-fixture
128 :test-thunk 'mysql-table-test
129 :description "A test of MySQL")
130 ("PostgreSQL Test" 'clsql-fixture
131 :test-thunk 'pgsql-table-test
132 :description "A test of PostgreSQL tables")
133 ("PostgreSQL Socket Table Test" 'clsql-fixture
134 :test-thunk 'pgsql-socket-table-test
135 :description "A test of PostgreSQL Socket tables")
139 (add-test (make-test-case "AODBC table test" 'clsql-fixture
140 :test-thunk 'aodbc-table-test
141 :description "Test AODBC table")
144 ;;;; Testing functions
146 (defun transform1 (i)
147 (* i (abs (/ i 2)) (expt 10 (* 2 i))))
149 (defun create-test-table (db)
151 (clsql:execute-command
152 "DROP TABLE test_clsql" :database db))
153 (clsql:execute-command
154 "CREATE TABLE test_clsql (t_int integer, t_float float, t_str CHAR(20))"
157 (let* ((test-int (- i 5))
158 (test-flt (transform1 test-int)))
159 (clsql:execute-command
160 (format nil "INSERT INTO test_clsql VALUES (~a,~a,'~a')"
162 (number-to-sql-string test-flt)
163 (number-to-sql-string test-flt))
166 (defun parse-double (num-str)
167 (let ((*read-default-float-format* 'double-float))
168 (read-from-string num-str)))
170 (defun test-table-row (row types)
171 (unless (and (listp row)
173 (failure "Row ~S is incorrect format" row))
174 (destructuring-bind (int float str) row
177 (unless (and (integerp int)
178 (typep float 'double-float)
180 (failure "Incorrect field type for row ~S" row)))
182 (unless (and (stringp int)
185 (failure "Incorrect field type for row ~S" row))
186 (setq int (parse-integer int))
187 (setq float (parse-double float)))
191 (failure "Invalid types field (~S) passed to test-table-row" types)))
193 (unless (= float (transform1 int))
194 (failure "Wrong float value ~A for int ~A (row ~S)" float int row))
196 (unless (= float (parse-double str))
197 (failure "Wrong string value ~A" str))))
200 (defun drop-test-table (db)
201 (clsql:execute-command "DROP TABLE test_clsql"))
203 (report-result (run-test clsql-test-suite :handle-errors nil) :verbose t)