r8935: example dir
[clsql.git] / classic-tests / tables.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          tables.lisp
6 ;;;; Purpose:       Table creation tests in CLSQL
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Mar 2002
9 ;;;;
10 ;;;; $Id$
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 ;;; This test suite looks for a configuration file named ".clsql-test.config"
20 ;;; located in the users home directory.
21 ;;;
22 ;;; This file contains a single a-list that specifies the connection
23 ;;; specs for each database type to be tested. For example, to test all
24 ;;; platforms, a sample "test.config" may look like:
25 ;;;
26 ;;; ((:mysql ("localhost" "a-mysql-db" "user1" "secret"))
27 ;;;  (:aodbc ("my-dsn" "a-user" "pass"))
28 ;;;  (:postgresql ("localhost" "another-db" "user2" "dont-tell"))
29 ;;;  (:postgresql-socket ("pg-server" "a-db-name" "user" "secret-password")))
30
31 (in-package #:clsql-classic-tests)
32
33 (defvar *config-pathname*
34   (make-pathname :default (user-homedir-pathname)
35                  :name ".clsql-test"
36                  :type ".config"))
37
38 (defclass conn-specs ()
39   ((aodbc-spec :accessor aodbc-spec)
40    (mysql-spec :accessor mysql-spec)
41    (pgsql-spec :accessor pgsql-spec)
42    (pgsql-socket-spec :accessor pgsql-socket-spec))
43   (:documentation "Test fixture for CLSQL testing"))
44
45
46 (defun read-specs (&optional (path *config-pathname*))
47   (if (probe-file path)
48       (with-open-file (stream path :direction :input)
49         (let ((config (read stream))
50               (specs (make-instance 'conn-specs)))
51           (setf (aodbc-spec specs) (cadr (assoc :aodbc config)))
52           (setf (mysql-spec specs) (cadr (assoc :mysql config)))
53           (setf (pgsql-spec specs) (cadr (assoc :postgresql config)))
54           (setf (pgsql-socket-spec specs) 
55                 (cadr (assoc :postgresql-socket config)))
56           specs))
57       (warn "CLSQL tester config file ~S not found" path)))
58
59 (defvar *conn-specs* (read-specs))
60
61 (defmethod mysql-table-test ((test conn-specs))
62   (test-table (mysql-spec test) :mysql))
63
64 (defmethod aodbc-table-test ((test conn-specs))
65   (test-table (aodbc-spec test) :aodbc))
66
67 (defmethod pgsql-table-test ((test conn-specs))
68   (test-table (pgsql-spec test) :postgresql))
69
70 (defmethod pgsql-socket-table-test ((test conn-specs))
71   (test-table (pgsql-socket-spec test) :postgresql-socket))
72
73 (defmethod test-table (spec type)
74   (when spec
75     (let ((db (clsql:connect spec :database-type type :if-exists :new)))
76       (unwind-protect
77            (progn
78              (create-test-table db)
79              (dolist (row (query "select * from test_clsql" :database db :result-types :auto))
80                (test-table-row row :auto type))
81              (dolist (row (query "select * from test_clsql" :database db :result-types nil))
82                (test-table-row row nil type))
83              (loop for row across (map-query 'vector #'list "select * from test_clsql" 
84                                              :database db :result-types :auto)
85                    do (test-table-row row :auto type))
86              (loop for row across (map-query 'vector #'list "select * from test_clsql" 
87                                              :database db :result-types nil)
88                    do (test-table-row row nil type))
89              (loop for row in (map-query 'list #'list "select * from test_clsql" 
90                                          :database db :result-types nil)
91                    do (test-table-row row nil type))
92              (loop for row in (map-query 'list #'list "select * from test_clsql" 
93                                          :database db :result-types :auto)
94                  do (test-table-row row :auto type))
95              (test (map-query nil #'list "select * from test_clsql" 
96                               :database db :result-types :auto)
97                    nil
98                    :fail-info "Expected NIL result from map-query nil")
99              (do-query ((int float bigint str) "select * from test_clsql")
100                (test-table-row (list int float bigint str) nil type))
101              (do-query ((int float bigint str) "select * from test_clsql" :result-types :auto)
102                (test-table-row (list int float bigint str) :auto type))
103              (drop-test-table db)
104              )
105         (disconnect :database db)))))
106
107
108 ;;;; Testing functions
109
110 (defun transform-float-1 (i)
111   (coerce (* i (abs (/ i 2)) (expt 10 (* 2 i))) 'double-float))
112
113 (defun transform-bigint-1 (i)
114   (* i (expt 10 (* 3 (abs i)))))
115
116 (defun create-test-table (db)
117   (ignore-errors
118     (clsql:execute-command 
119      "DROP TABLE test_clsql" :database db))
120   (clsql:execute-command 
121    "CREATE TABLE test_clsql (t_int integer, t_float float, t_bigint BIGINT, t_str CHAR(30))" 
122    :database db)
123   (dotimes (i 11)
124     (let* ((test-int (- i 5))
125            (test-flt (transform-float-1 test-int)))
126       (clsql:execute-command
127        (format nil "INSERT INTO test_clsql VALUES (~a,~a,~a,'~a')"
128                test-int
129                (number-to-sql-string test-flt)
130                (transform-bigint-1 test-int)
131                (number-to-sql-string test-flt)
132                )
133        :database db))))
134
135 (defun parse-double (num-str)
136   (let ((*read-default-float-format* 'double-float))
137     (coerce (read-from-string num-str) 'double-float)))
138
139 (defun test-table-row (row types db-type)
140   (test (and (listp row)
141              (= 4 (length row)))
142         t
143         :fail-info 
144         (format nil "Row ~S is incorrect format" row))
145   (destructuring-bind (int float bigint str) row
146     (cond
147       ((eq types :auto)
148        (test (and (integerp int)
149                   (typep float 'double-float)
150                   (or (eq db-type :aodbc) ;; aodbc doesn't handle bigint conversions
151                       (integerp bigint)) 
152                   (stringp str))
153              t
154              :fail-info 
155              (format nil "Incorrect field type for row ~S (types :auto)" row)))
156        ((null types)
157         (test (and (stringp int)
158                      (stringp float)
159                      (stringp bigint)
160                      (stringp str))
161               t
162               :fail-info 
163               (format nil "Incorrect field type for row ~S (types nil)" row))
164         (setq int (parse-integer int))
165         (setq bigint (parse-integer bigint))
166         (setq float (parse-double float)))
167        ((listp types)
168         (error "NYI")
169         )
170        (t 
171         (test t nil
172               :fail-info
173               (format nil "Invalid types field (~S) passed to test-table-row" types))))
174     (test (transform-float-1 int)
175           float
176           :test #'eql
177           :fail-info 
178           (format nil "Wrong float value ~A for int ~A (row ~S)" float int row))
179     (test float
180           (parse-double str)
181           :test #'double-float-equal
182           :fail-info (format nil "Wrong string value ~A for double ~A~%Row: ~S"
183                              str float row))))
184
185
186 (defun double-float-equal (a b)
187   (if (zerop a)
188       (if (zerop b)
189           t
190           nil)
191       (let ((diff (abs (/ (- a b) a))))
192         (if (> diff (* 10 double-float-epsilon))
193             nil
194             t))))
195          
196 (defun drop-test-table (db)
197   (clsql:execute-command "DROP TABLE test_clsql" :database db))
198
199
200 (deftest lowlevel.mysql.table.1
201   (let ((spec (mysql-spec *conn-specs*))
202         (result))
203     (when spec
204       (let ((db (clsql-mysql::database-connect spec :mysql)))
205         (clsql-mysql::database-execute-command "DROP TABLE IF EXISTS test_clsql" db)
206         (clsql-mysql::database-execute-command 
207          "CREATE TABLE test_clsql (i integer, sqrt double, sqrt_str CHAR(20))" db)
208         (dotimes (i 10)
209           (clsql-mysql::database-execute-command
210            (format nil "INSERT INTO test_clsql VALUES (~d,~d,'~a')"
211                    i (clsql:number-to-sql-string (sqrt i))
212                    (clsql:number-to-sql-string (sqrt i)))
213            db))
214         (let ((res (clsql-mysql::database-query-result-set "select * from test_clsql" db :full-set t :result-types nil)))
215           (setq result (mysql:mysql-num-rows
216                         (clsql-mysql::mysql-result-set-res-ptr res)))
217           (clsql-mysql::database-dump-result-set res db))
218         (clsql-mysql::database-execute-command "DROP TABLE test_clsql" db)
219         (clsql-mysql::database-disconnect db))))
220   10)
221
222 ;(mysql-table-test specs)
223 ;(pgsql-table-test specs)
224 ;(pgsql-socket-table-test specs)
225 ;(aodbc-table-test specs)
226
227
228
229 (defmacro def-test-table (name spec type)
230   (deftest ,name
231       (when ,spec
232         (let ((db (clsql:connect ,spec :database-type ,type :if-exists :new)))
233           (unwind-protect
234            (progn
235              (create-test-table db)
236              (dolist (row (query "select * from test_clsql" :database db :result-types :auto))
237                (test-table-row row :auto type))
238              (dolist (row (query "select * from test_clsql" :database db :result-types nil))
239                (test-table-row row nil type))
240              (loop for row across (map-query 'vector #'list "select * from test_clsql" 
241                                              :database db :result-types :auto)
242                    do (test-table-row row :auto type))
243              (loop for row across (map-query 'vector #'list "select * from test_clsql" 
244                                              :database db :result-types nil)
245                    do (test-table-row row nil type))
246              (loop for row in (map-query 'list #'list "select * from test_clsql" 
247                                          :database db :result-types nil)
248                    do (test-table-row row nil type))
249              (loop for row in (map-query 'list #'list "select * from test_clsql" 
250                                          :database db :result-types :auto)
251                  do (test-table-row row :auto type))
252              (test (map-query nil #'list "select * from test_clsql" 
253                               :database db :result-types :auto)
254                    nil
255                    :fail-info "Expected NIL result from map-query nil")
256              (do-query ((int float bigint str) "select * from test_clsql")
257                (test-table-row (list int float bigint str) nil type))
258              (do-query ((int float bigint str) "select * from test_clsql" :result-types :auto)
259                (test-table-row (list int float bigint str) :auto type))
260              (drop-test-table db)
261              )
262         (disconnect :database db)))))