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)))
102 (deftest :basic/map/2
104 (rows (map-query 'list #'identity "select * from TYPE_TABLE"
106 (dotimes (i (length rows) results)
110 (length (nth i rows))
112 (parse-integer (first (nth i rows))
115 (transform-float-1 (parse-integer (first (nth i rows))))
116 (parse-double (second (nth i rows)))))
118 ((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)))
120 (deftest :basic/map/3
122 (rows (map-query 'list #'identity "select * from TYPE_TABLE"
123 :result-types :auto)))
124 (dotimes (i (length rows) results)
128 (length (nth i rows))
130 (first (nth i rows)))
132 (transform-float-1 (first (nth i rows)))
133 (second (nth i rows))))
135 ((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)))
137 ;; confirm that a query on a single element returns a list of one element
138 (deftest :basic/map/4
139 (let ((rows (map-query 'list #'identity "select t_int from TYPE_TABLE"
143 (length (first rows))))
148 (do-query ((int float str) "select * from TYPE_TABLE" :result-types nil)
149 (let ((int-number (parse-integer int)))
151 (cons (list (double-float-equal (transform-float-1
153 (parse-double float))
154 (double-float-equal (parse-double str)
155 (parse-double float)))
158 ((t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t)))
162 (do-query ((int float str) "select * from TYPE_TABLE" :result-types :auto)
165 (list (double-float-equal
166 (transform-float-1 int)
173 ((t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t)))
176 (deftest :basic/bigint/1
178 (dolist (row (query "select * from TYPE_BIGINT" :result-types :auto)
180 (destructuring-bind (int bigint) row
181 (push (list (integerp int)
182 (if (and (eq :odbc *test-database-type*)
183 (eq :postgresql *test-database-underlying-type*))
184 ;; ODBC/Postgresql may return returns bigints as strings or integer
185 ;; depending upon the platform
189 ((t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t)))
194 (defun test-basic-initialize ()
196 (clsql:execute-command "DROP TABLE TYPE_TABLE")
197 (clsql:execute-command "DROP TABLE TYPE_BIGINT"))
199 (clsql:execute-command
200 "CREATE TABLE TYPE_TABLE (t_int integer, t_float double precision, t_str VARCHAR(30))")
202 (if (clsql-sys:db-type-has-bigint? *test-database-type*)
203 (clsql:execute-command "CREATE TABLE TYPE_BIGINT (t_int integer, t_bigint BIGINT)")
204 (clsql:execute-command "CREATE TABLE TYPE_BIGINT (t_int integer)"))
207 (let* ((test-int (- i 5))
208 (test-flt (transform-float-1 test-int)))
209 (clsql:execute-command
210 (format nil "INSERT INTO TYPE_TABLE VALUES (~a,~a,'~a')"
212 (clsql-sys:number-to-sql-string test-flt)
213 (clsql-sys:number-to-sql-string test-flt)
215 (when (clsql-sys:db-type-has-bigint? *test-database-type*)
216 (clsql:execute-command
217 (format nil "INSERT INTO TYPE_BIGINT VALUES (~a,~a)"
219 (transform-bigint-1 test-int)
222 ;;;; Testing functions
224 (defun transform-float-1 (i)
225 (coerce (* i (abs (/ i 2)) (expt 10 (* 2 i))) 'double-float))
227 (defun transform-bigint-1 (i)
228 (* i (expt 10 (* 3 (abs i)))))
230 (defun parse-double (num-str)
231 (let ((*read-default-float-format* 'double-float))
232 (coerce (read-from-string num-str) 'double-float)))
234 (defun double-float-equal (a b)
239 (let ((diff (abs (/ (- a b) a))))
240 (if (> diff (* 10 double-float-epsilon))