1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: test-basic.lisp
6 ;;;; Purpose: Tests for clsql string-based queries and result types
7 ;;;; Author: Kevin M. Rosenberg
10 ;;;; $Id: tests.lisp 8926 2004-04-10 21:12:52Z kevin $
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002-2004 by Kevin M. Rosenberg
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 ;;;; *************************************************************************
19 (in-package #:clsql-tests)
21 (defun test-basic-initialize ()
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))")
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')"
32 (clsql-sys:number-to-sql-string test-flt)
33 (transform-bigint-1 test-int)
34 (clsql-sys:number-to-sql-string test-flt)
37 (defun test-basic-forms ()
39 (test-basic-forms-untyped)
41 (deftest :BASIC/TYPE/1
43 (dolist (row (query "select * from TYPE_TABLE" :result-types :auto)
45 (destructuring-bind (int float bigint str) row
46 (push (list (integerp int)
47 (typep float 'double-float)
48 (if (and (eq :odbc *test-database-type*)
49 (eq :postgresql *test-database-underlying-type*))
50 ;; ODBC/Postgresql may return returns bigints as strings or integer
51 ;; depending upon the platform
56 ((t t t t) (t t t t) (t t t t) (t t t t) (t t t t) (t t t t) (t t t t) (t t t t) (t t t t) (t t t t) (t t t t)))
58 (deftest :BASIC/TYPE/2
60 (dolist (row (query "select * from TYPE_TABLE" :result-types :auto)
62 (destructuring-bind (int float bigint str) row
64 (cons (list (double-float-equal
65 (transform-float-1 int)
72 ((t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t)))
75 (defun test-basic-forms-untyped ()
76 '((deftest :BASIC/SELECT/1
77 (let ((rows (query "select * from TYPE_TABLE" :result-types :auto)))
83 (deftest :BASIC/SELECT/2
85 (dolist (row (query "select * from TYPE_TABLE" :result-types nil)
87 (destructuring-bind (int float bigint str) row
88 (push (list (stringp int)
93 ((t t t t) (t t t t) (t t t t) (t t t t) (t t t t) (t t t t) (t t t t) (t t t t) (t t t t) (t t t t) (t t t t)))
95 (deftest :BASIC/SELECT/3
97 (dolist (row (query "select * from TYPE_TABLE" :result-types nil)
99 (destructuring-bind (int float bigint str) row
100 (declare (ignore bigint))
101 (push (list (double-float-equal
102 (transform-float-1 (parse-integer int))
103 (parse-double float))
106 (parse-double float)))
108 ((t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t)))
110 (deftest :BASIC/MAP/1
112 (rows (map-query 'vector #'identity "select * from TYPE_TABLE"
114 (declare (type (simple-array list (*)) rows))
115 (dotimes (i (length rows) results)
118 (listp (aref rows i))
119 (length (aref rows i))
121 (parse-integer (first (aref rows i))
124 (transform-float-1 (parse-integer (first (aref rows i))))
125 (parse-double (second (aref rows i)))))
127 ((t 4 t t) (t 4 t t) (t 4 t t) (t 4 t t) (t 4 t t) (t 4 t t) (t 4 t t) (t 4 t t) (t 4 t t) (t 4 t t) (t 4 t t)))
129 (deftest :BASIC/MAP/2
131 (rows (map-query 'list #'identity "select * from TYPE_TABLE"
133 (dotimes (i (length rows) results)
137 (length (nth i rows))
139 (parse-integer (first (nth i rows))
142 (transform-float-1 (parse-integer (first (nth i rows))))
143 (parse-double (second (nth i rows)))))
145 ((t 4 t t) (t 4 t t) (t 4 t t) (t 4 t t) (t 4 t t) (t 4 t t) (t 4 t t) (t 4 t t) (t 4 t t) (t 4 t t) (t 4 t t)))
147 (deftest :BASIC/MAP/3
149 (rows (map-query 'list #'identity "select * from TYPE_TABLE"
150 :result-types :auto)))
151 (dotimes (i (length rows) results)
155 (length (nth i rows))
157 (first (nth i rows)))
159 (transform-float-1 (first (nth i rows)))
160 (second (nth i rows))))
162 ((t 4 t t) (t 4 t t) (t 4 t t) (t 4 t t) (t 4 t t) (t 4 t t) (t 4 t t) (t 4 t t) (t 4 t t) (t 4 t t) (t 4 t t)))
166 (do-query ((int float bigint str) "select * from TYPE_TABLE" :result-types nil)
167 (declare (ignore bigint))
168 (let ((int-number (parse-integer int)))
170 (cons (list (double-float-equal (transform-float-1
172 (parse-double float))
173 (double-float-equal (parse-double str)
174 (parse-double float)))
177 ((t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t)))
181 (do-query ((int float bigint str) "select * from TYPE_TABLE" :result-types :auto)
182 (declare (ignore bigint))
185 (list (double-float-equal
186 (transform-float-1 int)
193 ((t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t)))
197 ;;;; Testing functions
199 (defun transform-float-1 (i)
200 (coerce (* i (abs (/ i 2)) (expt 10 (* 2 i))) 'double-float))
202 (defun transform-bigint-1 (i)
203 (* i (expt 10 (* 3 (abs i)))))
205 (defun parse-double (num-str)
206 (let ((*read-default-float-format* 'double-float))
207 (coerce (read-from-string num-str) 'double-float)))
209 (defun double-float-equal (a b)
214 (let ((diff (abs (/ (- a b) a))))
215 (if (> diff (* 10 double-float-epsilon))