r1681: *** empty log message ***
[clsql.git] / test-suite / xptest-clsql.cl
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          xptest-clsql.cl
6 ;;;; Purpose:       Test of CLSQL using XPTest package
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Mar 2002
9 ;;;;
10 ;;;; $Id: xptest-clsql.cl,v 1.5 2002/03/27 10:56:02 kevin Exp $
11 ;;;;
12 ;;;; The XPTest package can be downloaded from
13 ;;;; http://alpha.onshored.com/lisp-software/
14 ;;;;
15 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
16 ;;;;
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 ;;;; *************************************************************************
21
22 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
23 (in-package :cl-user)
24 (mk:load-system "XPTest")
25
26 (in-package :clsql-user)
27 (use-package :xptest)
28
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"))
35
36 (defvar *config-pathname* (make-pathname :name "test"
37                                          :type "config"
38                                          :defaults *load-truename*))
39 (defmethod setup ((fix clsql-fixture))
40   (if (probe-file *config-pathname*)
41       (let (config)
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*)))
50
51 (defmethod teardown ((fix clsql-fixture))
52   t)
53
54 (defmethod mysql-table-test ((test clsql-fixture))
55   (test-table (mysql-spec test) :mysql))
56
57 (defmethod aodbc-table-test ((test clsql-fixture))
58   (test-table (aodbc-spec test) :aodbc))
59
60 (defmethod pgsql-table-test ((test clsql-fixture))
61   (test-table (pgsql-spec test) :postgresql))
62
63 (defmethod pgsql-socket-table-test ((test clsql-fixture))
64   (test-table (pgsql-socket-spec test) :postgresql-socket))
65
66
67 (defmethod test-table (spec type)
68   (when spec
69     (let ((db (clsql:connect spec :database-type type :if-exists :new)))
70       (unwind-protect
71            (progn
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              (drop-test-table db)
84              )
85         (disconnect :database db)))))
86
87
88 (defmethod mysql-low-level ((test clsql-fixture))
89   (let ((spec (mysql-spec test)))
90     (when spec
91       (let ((db (clsql-mysql::database-connect spec :mysql)))
92         (clsql-mysql::database-execute-command "DROP TABLE IF EXISTS test_clsql" db)
93         (clsql-mysql::database-execute-command 
94          "CREATE TABLE test_clsql (i integer, sqrt double, sqrt_str CHAR(20))" db)
95         (dotimes (i 10)
96           (clsql-mysql::database-execute-command
97            (format nil "INSERT INTO test_clsql VALUES (~d,~d,'~a')"
98                    i (sqrt i) (format nil "~d" (sqrt i)))
99            db))
100         (let ((res (clsql-mysql::database-query-result-set "select * from test_clsql" db :full-set t :types nil)))
101           (unless (= 10 (mysql:mysql-num-rows (clsql-mysql::mysql-result-set-res-ptr res)))
102             (failure "Error calling mysql-num-rows"))
103           (clsql-mysql::database-dump-result-set res db))
104         (clsql-mysql::database-execute-command "DROP TABLE test_clsql" db)
105         (clsql-mysql::database-disconnect db)))))
106
107 (defparameter clsql-test-suite 
108     (make-test-suite
109      "CLSQL Test Suite"
110      "Basic test suite for database operations."
111      ("MySQL Low Level Interface Test" 'clsql-fixture
112                    :test-thunk 'mysql-low-level
113                    :description "A test of MySQL low-level interface")
114      ("MySQL Test" 'clsql-fixture
115                    :test-thunk 'mysql-table-test
116                    :description "A test of MySQL")
117      ("PostgreSQL Test" 'clsql-fixture
118                    :test-thunk 'pgsql-table-test
119                    :description "A test of PostgreSQL tables")     
120      ("PostgreSQL Socket Table Test" 'clsql-fixture
121                    :test-thunk 'pgsql-socket-table-test
122                    :description "A test of PostgreSQL Socket tables")
123   ))
124
125 #+allegro 
126 (add-test (make-test-case "AODBC table test" 'clsql-fixture
127                           :test-thunk 'aodbc-table-test
128                           :description "Test AODBC table")
129           clsql-test-suite)
130
131 ;;;; Testing functions
132
133 (defun transform1 (i)
134   (* i (abs (/ i 2)) (expt 10 (* 2 i))))
135
136 (defun create-test-table (db)
137   (ignore-errors
138     (clsql:execute-command 
139      "DROP TABLE test_clsql" :database db))
140   (clsql:execute-command 
141    "CREATE TABLE test_clsql (t_int integer, t_float float, t_str CHAR(20))" 
142    :database db)
143   (dotimes (i 11)
144     (let* ((test-int (- i 5))
145            (test-flt (transform1 test-int)))
146       (clsql:execute-command
147        (format nil "INSERT INTO test_clsql VALUES (~a,~a,'~a')"
148                test-int
149                (number-to-sql-string test-flt)
150                (number-to-sql-string test-flt))
151        :database db))))
152
153 (defun parse-double (num-str)
154   (let ((*read-default-float-format* 'double-float))
155     (read-from-string num-str)))
156
157 (defun test-table-row (row types)
158   (unless (and (listp row)
159                (= 3 (length row)))
160     (failure "Row ~S is incorrect format" row))
161   (destructuring-bind (int float str) row
162     (cond
163       ((eq types :auto)
164        (unless (and (integerp int)
165                     (typep float 'double-float)
166                     (stringp str))
167          (failure "Incorrect field type for row ~S" row)))
168        ((null types)
169         (unless (and (stringp int)
170                      (stringp float)
171                      (stringp str))
172           (failure "Incorrect field type for row ~S" row))
173           (setq int (parse-integer int))
174           (setq float (parse-double float)))
175        ((listp types)
176         )
177        (t 
178         (failure "Invalid types field (~S) passed to test-table-row" types)))
179 #+ignore
180     (unless (= float (transform1 int))
181       (failure "Wrong float value ~A for int ~A (row ~S)" float int row))
182 #+ignore
183     (unless (= float (parse-double str))
184       (failure "Wrong string value ~A" str))))
185
186
187 (defun drop-test-table (db)
188   (clsql:execute-command "DROP TABLE test_clsql"))
189
190 (report-result (run-test clsql-test-suite :handle-errors nil) :verbose t)
191
192