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
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)
23 (deftest :basic/type/1
25 (dolist (row (query "select * from TYPE_TABLE" :result-types :auto)
27 (destructuring-bind (int float str) row
28 (push (list (integerp int)
29 (typep float 'double-float)
32 ((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)))
34 (deftest :basic/type/2
36 (dolist (row (query "select * from TYPE_TABLE" :result-types :auto)
38 (destructuring-bind (int float str) row
40 (cons (list (double-float-equal
41 (transform-float-1 int)
48 ((t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t)))
50 (deftest :basic/select/1
51 (let ((rows (query "select * from TYPE_TABLE" :result-types :auto)))
57 (deftest :BASIC/SELECT/2
59 (dolist (row (query "select * from TYPE_TABLE" :result-types nil)
61 (destructuring-bind (int float str) row
62 (push (list (stringp int)
66 ((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)))
68 (deftest :basic/select/3
70 (dolist (row (query "select * from TYPE_TABLE" :result-types nil)
72 (destructuring-bind (int float str) row
73 (push (list (double-float-equal
74 (transform-float-1 (parse-integer int))
78 (parse-double float)))
80 ((t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t)))
84 (rows (map-query 'vector #'identity "select * from TYPE_TABLE"
86 (declare (type (simple-array list (*)) rows))
87 (dotimes (i (length rows) results)
91 (length (aref rows i))
93 (parse-integer (first (aref rows i))
96 (transform-float-1 (parse-integer (first (aref rows i))))
97 (parse-double (second (aref rows i)))))
99 ((t 3 t t) (t 3 t t) (t 3 t t) (t 3 t t) (t 3 t t) (t 3 t t) (t 3 t t) (t 3 t t) (t 3 t t) (t 3 t t) (t 3 t t)))
101 (deftest :basic/map/2
103 (rows (map-query 'list #'identity "select * from TYPE_TABLE"
105 (dotimes (i (length rows) results)
109 (length (nth i rows))
111 (parse-integer (first (nth i rows))
114 (transform-float-1 (parse-integer (first (nth i rows))))
115 (parse-double (second (nth i rows)))))
117 ((t 3 t t) (t 3 t t) (t 3 t t) (t 3 t t) (t 3 t t) (t 3 t t) (t 3 t t) (t 3 t t) (t 3 t t) (t 3 t t) (t 3 t t)))
119 (deftest :basic/map/3
121 (rows (map-query 'list #'identity "select * from TYPE_TABLE"
122 :result-types :auto)))
123 (dotimes (i (length rows) results)
127 (length (nth i rows))
129 (first (nth i rows)))
131 (transform-float-1 (first (nth i rows)))
132 (second (nth i rows))))
134 ((t 3 t t) (t 3 t t) (t 3 t t) (t 3 t t) (t 3 t t) (t 3 t t) (t 3 t t) (t 3 t t) (t 3 t t) (t 3 t t) (t 3 t t)))
138 (do-query ((int float str) "select * from TYPE_TABLE" :result-types nil)
139 (let ((int-number (parse-integer int)))
141 (cons (list (double-float-equal (transform-float-1
143 (parse-double float))
144 (double-float-equal (parse-double str)
145 (parse-double float)))
148 ((t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t)))
152 (do-query ((int float str) "select * from TYPE_TABLE" :result-types :auto)
155 (list (double-float-equal
156 (transform-float-1 int)
163 ((t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t)))
166 (deftest :basic/bigint/1
168 (dolist (row (query "select * from TYPE_BIGINT" :result-types :auto)
170 (destructuring-bind (int bigint) row
171 (push (list (integerp int)
172 (if (and (eq :odbc *test-database-type*)
173 (eq :postgresql *test-database-underlying-type*))
174 ;; ODBC/Postgresql may return returns bigints as strings or integer
175 ;; depending upon the platform
179 ((t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t)))
184 (defun test-basic-initialize ()
186 (clsql:execute-command "DROP TABLE TYPE_TABLE")
187 (clsql:execute-command "DROP TABLE TYPE_BIGINT"))
189 (clsql:execute-command
190 "CREATE TABLE TYPE_TABLE (t_int integer, t_float double precision, t_str VARCHAR(30))")
192 (if (clsql-sys:db-type-has-bigint? *test-database-type*)
193 (clsql:execute-command "CREATE TABLE TYPE_BIGINT (t_int integer, t_bigint BIGINT)")
194 (clsql:execute-command "CREATE TABLE TYPE_BIGINT (t_int integer)"))
197 (let* ((test-int (- i 5))
198 (test-flt (transform-float-1 test-int)))
199 (clsql:execute-command
200 (format nil "INSERT INTO TYPE_TABLE VALUES (~a,~a,'~a')"
202 (clsql-sys:number-to-sql-string test-flt)
203 (clsql-sys:number-to-sql-string test-flt)
205 (when (clsql-sys:db-type-has-bigint? *test-database-type*)
206 (clsql:execute-command
207 (format nil "INSERT INTO TYPE_BIGINT VALUES (~a,~a)"
209 (transform-bigint-1 test-int)
212 ;;;; Testing functions
214 (defun transform-float-1 (i)
215 (coerce (* i (abs (/ i 2)) (expt 10 (* 2 i))) 'double-float))
217 (defun transform-bigint-1 (i)
218 (* i (expt 10 (* 3 (abs i)))))
220 (defun parse-double (num-str)
221 (let ((*read-default-float-format* 'double-float))
222 (coerce (read-from-string num-str) 'double-float)))
224 (defun double-float-equal (a b)
229 (let ((diff (abs (/ (- a b) a))))
230 (if (> diff (* 10 double-float-epsilon))