r9123: test & capability 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 (defun test-basic-initialize ()
22   (ignore-errors
23    (clsql:execute-command "DROP TABLE TYPE_TABLE"))
24   (clsql:execute-command 
25    "CREATE TABLE TYPE_TABLE (t_int integer, t_float double precision, t_bigint BIGINT, t_str VARCHAR(30))")
26   (dotimes (i 11)
27     (let* ((test-int (- i 5))
28            (test-flt (transform-float-1 test-int)))
29       (clsql:execute-command
30        (format nil "INSERT INTO TYPE_TABLE VALUES (~a,~a,~a,'~a')"
31                test-int
32                (clsql-base:number-to-sql-string test-flt)
33                (transform-bigint-1 test-int)
34                (clsql-base:number-to-sql-string test-flt)
35                )))))
36
37 (defun test-basic-forms ()
38   nil)
39
40 (defun test-basic-forms-untyped ()
41   nil)
42
43
44 (defun %test-basic-forms ()
45   (dolist (row (query "select * from TYPE_TABLE" :result-types :auto))
46     (test-table-row row :auto))
47   (dolist (row (query "select * from TYPE_TABLE" :result-types nil))
48     (test-table-row row nil))
49   (loop for row across (map-query 'vector #'list "select * from TYPE_TABLE" 
50                                   :result-types :auto)
51         do (test-table-row row :auto))
52   (loop for row across (map-query 'vector #'list "select * from TYPE_TABLE" 
53                                   :result-types nil)
54         do (test-table-row row nil))
55   (loop for row in (map-query 'list #'list "select * from TYPE_TABLE" 
56                               :result-types nil)
57         do (test-table-row row nil))
58   (loop for row in (map-query 'list #'list "select * from TYPE_TABLE" 
59                               :result-types :auto)
60         do (test-table-row row :auto))
61   (test (map-query nil #'list "select * from TYPE_TABLE" 
62                    :result-types :auto)
63         nil
64         :fail-info "Expected NIL result from map-query nil")
65   (do-query ((int float bigint str) "select * from TYPE_TABLE")
66     (test-table-row (list int float bigint str) nil))
67   (do-query ((int float bigint str) "select * from TYPE_TABLE" :result-types :auto)
68     (test-table-row (list int float bigint str) :auto)))
69
70
71 (defun %test-basic-forms-untyped ()
72   (dolist (row (query "select * from TYPE_TABLE" :result-types nil))
73     (test-table-row row nil))
74   (loop for row across (map-query 'vector #'list "select * from TYPE_TABLE" 
75                                   :result-types nil)
76         do (test-table-row row nil))
77   (loop for row in (map-query 'list #'list "select * from TYPE_TABLE" 
78                               :result-types nil)
79         do (test-table-row row nil))
80   
81   (do-query ((int float bigint str) "select * from TYPE_TABLE")
82     (test-table-row (list int float bigint str) nil)))
83
84
85 ;;;; Testing functions
86
87 (defun transform-float-1 (i)
88   (coerce (* i (abs (/ i 2)) (expt 10 (* 2 i))) 'double-float))
89
90 (defun transform-bigint-1 (i)
91   (* i (expt 10 (* 3 (abs i)))))
92
93
94
95 (defun parse-double (num-str)
96   (let ((*read-default-float-format* 'double-float))
97     (coerce (read-from-string num-str) 'double-float)))
98
99 (defun test-table-row (row types)
100   (test (and (listp row)
101              (= 4 (length row)))
102         t
103         :fail-info 
104         (format nil "Row ~S is incorrect format" row))
105   (destructuring-bind (int float bigint str) row
106     (cond
107       ((eq types :auto)
108        (test (and (integerp int)
109                   (typep float 'double-float)
110                   (or (member *test-database-type* 
111                               '(:odbc :aodbc))  ;; aodbc considers bigints as strings
112                       (integerp bigint)) 
113                   (stringp str))
114              t
115              :fail-info 
116              (format nil "Incorrect field type for row ~S (types :auto)" row)))
117        ((null types)
118         (test (and (stringp int)
119                      (stringp float)
120                      (stringp bigint)
121                      (stringp str))
122               t
123               :fail-info 
124               (format nil "Incorrect field type for row ~S (types nil)" row))
125         (when (stringp int)
126           (setq int (parse-integer int)))
127         (setq bigint (parse-integer bigint))
128         (when (stringp float)
129           (setq float (parse-double float))))
130        ((listp types)
131         (error "NYI")
132         )
133        (t 
134         (test t nil
135               :fail-info
136               (format nil "Invalid types field (~S) passed to test-table-row" types))))
137     (unless (eq *test-database-type* :sqlite)           ; SQLite is typeless.
138       (test (transform-float-1 int)
139             float
140             :test #'double-float-equal
141             :fail-info 
142             (format nil "Wrong float value ~A for int ~A (row ~S)" float int row)))
143     (test float
144           (parse-double str)
145           :test #'double-float-equal
146           :fail-info (format nil "Wrong string value ~A for double ~A~%Row: ~S"
147                              str float row))))
148
149
150 (defun double-float-equal (a b)
151   (if (zerop a)
152       (if (zerop b)
153           t
154           nil)
155       (let ((diff (abs (/ (- a b) a))))
156         (if (> diff (* 10 double-float-epsilon))
157             nil
158             t))))