r8936: merged classic-tests into tests
[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   (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 float, 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 doesn't handle bigint conversions
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         (setq int (parse-integer int))
131         (setq bigint (parse-integer bigint))
132         (setq float (parse-double float)))
133        ((listp types)
134         (error "NYI")
135         )
136        (t 
137         (test t nil
138               :fail-info
139               (format nil "Invalid types field (~S) passed to test-table-row" types))))
140     (unless (eq db-type :sqlite)                ; SQLite is typeless.
141       (test (transform-float-1 int)
142             float
143             :test #'eql
144             :fail-info 
145             (format nil "Wrong float value ~A for int ~A (row ~S)" float int row)))
146     (test float
147           (parse-double str)
148           :test #'double-float-equal
149           :fail-info (format nil "Wrong string value ~A for double ~A~%Row: ~S"
150                              str float row))))
151
152
153 (defun double-float-equal (a b)
154   (if (zerop a)
155       (if (zerop b)
156           t
157           nil)
158       (let ((diff (abs (/ (- a b) a))))
159         (if (> diff (* 10 double-float-epsilon))
160             nil
161             t))))
162          
163 (defun drop-test-table (db)
164   (clsql:execute-command "DROP TABLE test_clsql" :database db))