r9019: odbc updates
[clsql.git] / tests / test-basic.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:    test-basic.lisp
6 ;;;; Purpose: Tests for clsql-base and result types
7 ;;;; Author:  Kevin M. Rosenberg
8 ;;;; Created: Mar 2002
9 ;;;;
10 ;;;; $Id: tests.lisp 8926 2004-04-10 21:12:52Z kevin $
11 ;;;;
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002-2004 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 (in-package #:clsql-tests)
20
21
22 (defun test-basic (spec type)
23   (let ((db (clsql:connect spec :database-type type :if-exists :new)))
24     (unwind-protect
25          (if (eq type :sqlite)
26              (%test-basic-untyped db type)
27              (%test-basic db type))
28       (disconnect :database db))))
29
30 (defun %test-basic (db type)
31   (create-test-table db)
32   (dolist (row (query "select * from test_clsql" :database db :result-types :auto))
33     (test-table-row row :auto type))
34   (dolist (row (query "select * from test_clsql" :database db :result-types nil))
35     (test-table-row row nil type))
36   (loop for row across (map-query 'vector #'list "select * from test_clsql" 
37                                   :database db :result-types :auto)
38         do (test-table-row row :auto type))
39   (loop for row across (map-query 'vector #'list "select * from test_clsql" 
40                                   :database db :result-types nil)
41         do (test-table-row row nil type))
42   (loop for row in (map-query 'list #'list "select * from test_clsql" 
43                               :database db :result-types nil)
44         do (test-table-row row nil type))
45   (loop for row in (map-query 'list #'list "select * from test_clsql" 
46                               :database db :result-types :auto)
47         do (test-table-row row :auto type))
48   (test (map-query nil #'list "select * from test_clsql" 
49                    :database db :result-types :auto)
50         nil
51         :fail-info "Expected NIL result from map-query nil")
52   (do-query ((int float bigint str) "select * from test_clsql")
53     (test-table-row (list int float bigint str) nil type))
54   (do-query ((int float bigint str) "select * from test_clsql" :result-types :auto)
55     (test-table-row (list int float bigint str) :auto type))
56   #+ignore (drop-test-table db))
57
58
59 (defun %test-basic-untyped (db type)
60   (create-test-table db)
61   (dolist (row (query "select * from test_clsql" :database db :result-types nil))
62     (test-table-row row nil type))
63   (loop for row across (map-query 'vector #'list "select * from test_clsql" 
64                                   :database db :result-types nil)
65         do (test-table-row row nil type))
66   (loop for row in (map-query 'list #'list "select * from test_clsql" 
67                               :database db :result-types nil)
68         do (test-table-row row nil type))
69   
70   (do-query ((int float bigint str) "select * from test_clsql")
71     (test-table-row (list int float bigint str) nil type))
72   (drop-test-table db))
73
74 ;;;; Testing functions
75
76 (defun transform-float-1 (i)
77   (coerce (* i (abs (/ i 2)) (expt 10 (* 2 i))) 'double-float))
78
79 (defun transform-bigint-1 (i)
80   (* i (expt 10 (* 3 (abs i)))))
81
82 (defun create-test-table (db)
83   (ignore-errors
84     (clsql:execute-command 
85      "DROP TABLE test_clsql" :database db))
86   (clsql:execute-command 
87    "CREATE TABLE test_clsql (t_int integer, t_float double, t_bigint BIGINT, t_str CHAR(30))" 
88    :database db)
89   (dotimes (i 11)
90     (let* ((test-int (- i 5))
91            (test-flt (transform-float-1 test-int)))
92       (clsql:execute-command
93        (format nil "INSERT INTO test_clsql VALUES (~a,~a,~a,'~a')"
94                test-int
95                (clsql-base:number-to-sql-string test-flt)
96                (transform-bigint-1 test-int)
97                (clsql-base:number-to-sql-string test-flt)
98                )
99        :database db))))
100
101 (defun parse-double (num-str)
102   (let ((*read-default-float-format* 'double-float))
103     (coerce (read-from-string num-str) 'double-float)))
104
105 (defun test-table-row (row types db-type)
106   (test (and (listp row)
107              (= 4 (length row)))
108         t
109         :fail-info 
110         (format nil "Row ~S is incorrect format" row))
111   (destructuring-bind (int float bigint str) row
112     (cond
113       ((eq types :auto)
114        (test (and (integerp int)
115                   (typep float 'double-float)
116                   (or (eq db-type :aodbc)  ;; aodbc considers bigints as strings
117                       (integerp bigint)) 
118                   (stringp str))
119              t
120              :fail-info 
121              (format nil "Incorrect field type for row ~S (types :auto)" row)))
122        ((null types)
123         (test (and (stringp int)
124                      (stringp float)
125                      (stringp bigint)
126                      (stringp str))
127               t
128               :fail-info 
129               (format nil "Incorrect field type for row ~S (types nil)" row))
130         (when (stringp int)
131           (setq int (parse-integer int)))
132         (setq bigint (parse-integer bigint))
133         (when (stringp float)
134           (setq float (parse-double float))))
135        ((listp types)
136         (error "NYI")
137         )
138        (t 
139         (test t nil
140               :fail-info
141               (format nil "Invalid types field (~S) passed to test-table-row" types))))
142     (unless (eq db-type :sqlite)                ; SQLite is typeless.
143       (test (transform-float-1 int)
144             float
145             :test #'double-float-equal
146             :fail-info 
147             (format nil "Wrong float value ~A for int ~A (row ~S)" float int row)))
148     (test float
149           (parse-double str)
150           :test #'double-float-equal
151           :fail-info (format nil "Wrong string value ~A for double ~A~%Row: ~S"
152                              str float row))))
153
154
155 (defun double-float-equal (a b)
156   (if (zerop a)
157       (if (zerop b)
158           t
159           nil)
160       (let ((diff (abs (/ (- a b) a))))
161         (if (> diff (* 10 double-float-epsilon))
162             nil
163             t))))
164          
165 (defun drop-test-table (db)
166   (clsql:execute-command "DROP TABLE test_clsql" :database db))