6d082274fd024e97022b197ec0a0d6f643e1ff1d
[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.6 2002/03/27 11:13:27 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              (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))
96              (drop-test-table db)
97              )
98         (disconnect :database db)))))
99
100
101 (defmethod mysql-low-level ((test clsql-fixture))
102   (let ((spec (mysql-spec test)))
103     (when spec
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)
108         (dotimes (i 10)
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)))
112            db))
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)))))
119
120 (defparameter clsql-test-suite 
121     (make-test-suite
122      "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")
136   ))
137
138 #+allegro 
139 (add-test (make-test-case "AODBC table test" 'clsql-fixture
140                           :test-thunk 'aodbc-table-test
141                           :description "Test AODBC table")
142           clsql-test-suite)
143
144 ;;;; Testing functions
145
146 (defun transform1 (i)
147   (* i (abs (/ i 2)) (expt 10 (* 2 i))))
148
149 (defun create-test-table (db)
150   (ignore-errors
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))" 
155    :database db)
156   (dotimes (i 11)
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')"
161                test-int
162                (number-to-sql-string test-flt)
163                (number-to-sql-string test-flt))
164        :database db))))
165
166 (defun parse-double (num-str)
167   (let ((*read-default-float-format* 'double-float))
168     (read-from-string num-str)))
169
170 (defun test-table-row (row types)
171   (unless (and (listp row)
172                (= 3 (length row)))
173     (failure "Row ~S is incorrect format" row))
174   (destructuring-bind (int float str) row
175     (cond
176       ((eq types :auto)
177        (unless (and (integerp int)
178                     (typep float 'double-float)
179                     (stringp str))
180          (failure "Incorrect field type for row ~S" row)))
181        ((null types)
182         (unless (and (stringp int)
183                      (stringp float)
184                      (stringp str))
185           (failure "Incorrect field type for row ~S" row))
186           (setq int (parse-integer int))
187           (setq float (parse-double float)))
188        ((listp types)
189         )
190        (t 
191         (failure "Invalid types field (~S) passed to test-table-row" types)))
192 #+ignore
193     (unless (= float (transform1 int))
194       (failure "Wrong float value ~A for int ~A (row ~S)" float int row))
195 #+ignore
196     (unless (= float (parse-double str))
197       (failure "Wrong string value ~A" str))))
198
199
200 (defun drop-test-table (db)
201   (clsql:execute-command "DROP TABLE test_clsql"))
202
203 (report-result (run-test clsql-test-suite :handle-errors nil) :verbose t)
204
205