r8935: example dir
[clsql.git] / classic-tests / old-tests / xptest-clsql.lisp
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$
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
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:
27 ;;;
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")))
32
33 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
34 (in-package :cl-user)
35 (mk:load-system "XPTest")
36
37 (in-package :clsql-user)
38 (use-package :xptest)
39
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"))
46
47 (defvar *config-pathname* (make-pathname :name "test"
48                                          :type "config"
49                                          :defaults *load-truename*))
50 (defmethod setup ((fix clsql-fixture))
51   (if (probe-file *config-pathname*)
52       (let (config)
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*)))
61
62 (defmethod teardown ((fix clsql-fixture))
63   t)
64
65 (defmethod mysql-table-test ((test clsql-fixture))
66   (test-table (mysql-spec test) :mysql))
67
68 (defmethod aodbc-table-test ((test clsql-fixture))
69   (test-table (aodbc-spec test) :aodbc))
70
71 (defmethod pgsql-table-test ((test clsql-fixture))
72   (test-table (pgsql-spec test) :postgresql))
73
74 (defmethod pgsql-socket-table-test ((test clsql-fixture))
75   (test-table (pgsql-socket-spec test) :postgresql-socket))
76
77
78 (defmethod test-table (spec type)
79   (when spec
80     (let ((db (clsql:connect spec :database-type type :if-exists :new)))
81       (unwind-protect
82            (progn
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))
107              (drop-test-table db)
108              )
109         (disconnect :database db)))))
110
111
112 (defmethod mysql-low-level ((test clsql-fixture))
113   (let ((spec (mysql-spec test)))
114     (when spec
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)
119         (dotimes (i 10)
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)))
124            db))
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)))))
131
132 (defparameter clsql-test-suite 
133     (make-test-suite
134      "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")
148   ))
149
150 #+allegro 
151 (add-test (make-test-case "AODBC table test" 'clsql-fixture
152                           :test-thunk 'aodbc-table-test
153                           :description "Test AODBC table")
154           clsql-test-suite)
155
156 ;;;; Testing functions
157
158 (defun transform-float-1 (i)
159   (* i (abs (/ i 2)) (expt 10 (* 2 i))))
160
161 (defun transform-bigint-1 (i)
162   (* i (expt 10 (* 3 (abs i)))))
163
164 (defun create-test-table (db)
165   (ignore-errors
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))" 
170    :database db)
171   (dotimes (i 11)
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')"
176                test-int
177                (number-to-sql-string test-flt)
178                (transform-bigint-1 test-int)
179                (number-to-sql-string test-flt)
180                )
181        :database db))))
182
183 (defun parse-double (num-str)
184   (let ((*read-default-float-format* 'double-float))
185     (coerce (read-from-string num-str) 'double-float)))
186
187 (defun test-table-row (row types)
188   (unless (and (listp row)
189                (= 4 (length row)))
190     (failure "Row ~S is incorrect format" row))
191   (destructuring-bind (int float bigint str) row
192     (cond
193       ((eq types :auto)
194        (unless (and (integerp int)
195                     (typep float 'double-float)
196                     (integerp bigint)
197                     (stringp str))
198          (failure "Incorrect field type for row ~S" row)))
199        ((null types)
200         (unless (and (stringp int)
201                      (stringp float)
202                      (stringp bigint)
203                      (stringp str))
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)))
208        ((listp types)
209         (error "NYI")
210         )
211        (t 
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))))
217
218
219 (defun drop-test-table (db)
220   (clsql:execute-command "DROP TABLE test_clsql"))
221
222 (report-result (run-test clsql-test-suite :handle-errors nil) :verbose t)
223
224