2589fa0d027919b8e184b47bf379c2f58054de59
[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.4 2002/03/27 10:48:21 kevin Exp $
11 ;;;;
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
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 ;;;; *************************************************************************
18
19 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
20 (in-package :cl-user)
21 (mk:load-system "XPTest")
22
23 (in-package :clsql-user)
24 (use-package :xptest)
25
26 (def-test-fixture clsql-fixture ()
27   ((aodbc-spec :accessor aodbc-spec)
28    (mysql-spec :accessor mysql-spec)
29    (pgsql-spec :accessor pgsql-spec)
30    (pgsql-socket-spec :accessor pgsql-socket-spec))
31   (:documentation "Test fixture for CLSQL testing"))
32
33 (defvar *config-pathname* (make-pathname :name "test"
34                                          :type "config"
35                                          :defaults *load-truename*))
36 (defmethod setup ((fix clsql-fixture))
37   (if (probe-file *config-pathname*)
38       (let (config)
39         (with-open-file (stream *config-pathname* :direction :input)
40           (setq config (read stream)))
41         (setf (aodbc-spec fix) (cadr (assoc :aodbc config)))
42         (setf (mysql-spec fix) (cadr (assoc :mysql config)))
43         (setf (pgsql-spec fix) (cadr (assoc :postgresql config)))
44         (setf (pgsql-socket-spec fix) 
45               (cadr (assoc :postgresql-socket config))))
46       (error "XPTest Config file ~S not found" *config-pathname*)))
47
48 (defmethod teardown ((fix clsql-fixture))
49   t)
50
51 (defmethod mysql-table-test ((test clsql-fixture))
52   (test-table (mysql-spec test) :mysql))
53
54 (defmethod aodbc-table-test ((test clsql-fixture))
55   (test-table (aodbc-spec test) :aodbc))
56
57 (defmethod pgsql-table-test ((test clsql-fixture))
58   (test-table (pgsql-spec test) :postgresql))
59
60 (defmethod pgsql-socket-table-test ((test clsql-fixture))
61   (test-table (pgsql-socket-spec test) :postgresql-socket))
62
63
64 (defmethod test-table (spec type)
65   (when spec
66     (let ((db (clsql:connect spec :database-type type :if-exists :new)))
67       (unwind-protect
68            (progn
69              (create-test-table db)
70              (dolist (row (query "select * from test_clsql" :database db :types :auto))
71                (test-table-row row :auto))
72              (dolist (row (query "select * from test_clsql" :database db :types nil))
73                (test-table-row row nil))
74              (loop for row across (map-query 'vector #'list "select * from test_clsql" 
75                                              :database db :types :auto)
76                    do (test-table-row row :auto))
77              (loop for row across (map-query 'vector #'list "select * from test_clsql" 
78                                              :database db :types nil)
79                    do (test-table-row row nil))
80              (drop-test-table db)
81              )
82         (disconnect :database db)))))
83
84
85 (defmethod mysql-low-level ((test clsql-fixture))
86   (let ((spec (mysql-spec test)))
87     (when spec
88       (let ((db (clsql-mysql::database-connect spec :mysql)))
89         (clsql-mysql::database-execute-command "DROP TABLE IF EXISTS test_clsql" db)
90         (clsql-mysql::database-execute-command 
91          "CREATE TABLE test_clsql (i integer, sqrt double, sqrt_str CHAR(20))" db)
92         (dotimes (i 10)
93           (clsql-mysql::database-execute-command
94            (format nil "INSERT INTO test_clsql VALUES (~d,~d,'~a')"
95                    i (sqrt i) (format nil "~d" (sqrt i)))
96            db))
97         (let ((res (clsql-mysql::database-query-result-set "select * from test_clsql" db :full-set t :types nil)))
98           (unless (= 10 (mysql:mysql-num-rows (clsql-mysql::mysql-result-set-res-ptr res)))
99             (failure "Error calling mysql-num-rows"))
100           (clsql-mysql::database-dump-result-set res db))
101         (clsql-mysql::database-execute-command "DROP TABLE test_clsql" db)
102         (clsql-mysql::database-disconnect db)))))
103
104 (defparameter clsql-test-suite 
105     (make-test-suite
106      "CLSQL Test Suite"
107      "Basic test suite for database operations."
108      ("MySQL Low Level Interface Test" 'clsql-fixture
109                    :test-thunk 'mysql-low-level
110                    :description "A test of MySQL low-level interface")
111      ("MySQL Test" 'clsql-fixture
112                    :test-thunk 'mysql-table-test
113                    :description "A test of MySQL")
114      ("PostgreSQL Test" 'clsql-fixture
115                    :test-thunk 'pgsql-table-test
116                    :description "A test of PostgreSQL tables")     
117      ("PostgreSQL Socket Table Test" 'clsql-fixture
118                    :test-thunk 'pgsql-socket-table-test
119                    :description "A test of PostgreSQL Socket tables")
120   ))
121
122 #+allegro 
123 (add-test (make-test-case "AODBC table test" 'clsql-fixture
124                           :test-thunk 'aodbc-table-test
125                           :description "Test AODBC table")
126           clsql-test-suite)
127
128 ;;;; Testing functions
129
130 (defun transform1 (i)
131   (* i (abs (/ i 2)) (expt 10 (* 2 i))))
132
133 (defun create-test-table (db)
134   (ignore-errors
135     (clsql:execute-command 
136      "DROP TABLE test_clsql" :database db))
137   (clsql:execute-command 
138    "CREATE TABLE test_clsql (t_int integer, t_float float, t_str CHAR(20))" 
139    :database db)
140   (dotimes (i 11)
141     (let* ((test-int (- i 5))
142            (test-flt (transform1 test-int)))
143       (clsql:execute-command
144        (format nil "INSERT INTO test_clsql VALUES (~a,~a,'~a')"
145                test-int
146                (number-to-sql-string test-flt)
147                (number-to-sql-string test-flt))
148        :database db))))
149
150 (defun parse-double (num-str)
151   (let ((*read-default-float-format* 'double-float))
152     (read-from-string num-str)))
153
154 (defun test-table-row (row types)
155   (unless (and (listp row)
156                (= 3 (length row)))
157     (failure "Row ~S is incorrect format" row))
158   (destructuring-bind (int float str) row
159     (cond
160       ((eq types :auto)
161        (unless (and (integerp int)
162                     (typep float 'double-float)
163                     (stringp str))
164          (failure "Incorrect field type for row ~S" row)))
165        ((null types)
166         (unless (and (stringp int)
167                      (stringp float)
168                      (stringp str))
169           (failure "Incorrect field type for row ~S" row))
170           (setq int (parse-integer int))
171           (setq float (parse-double float)))
172        ((listp types)
173         )
174        (t 
175         (failure "Invalid types field (~S) passed to test-table-row" types)))
176 #+ignore
177     (unless (= float (transform1 int))
178       (failure "Wrong float value ~A for int ~A (row ~S)" float int row))
179 #+ignore
180     (unless (= float (parse-double str))
181       (failure "Wrong string value ~A" str))))
182
183
184 (defun drop-test-table (db)
185   (clsql:execute-command "DROP TABLE test_clsql"))
186
187 (report-result (run-test clsql-test-suite :handle-errors nil) :verbose t)
188
189