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)
24 (deftest :basic/type/1
25 (with-dataset *ds-basic*
27 (dolist (row (query "select * from TYPE_TABLE" :result-types :auto)
29 (destructuring-bind (int float str) row
30 (push (list (integerp int)
31 (typep float 'double-float)
34 ((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)))
36 (deftest :basic/type/2
37 (with-dataset *ds-basic*
39 (dolist (row (query "select * from TYPE_TABLE" :result-types :auto)
41 (destructuring-bind (int float str) row
43 (cons (list (double-float-equal
44 (transform-float-1 int)
51 ((t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t)))
53 (deftest :basic/select/1
54 (with-dataset *ds-basic*
55 (let ((rows (query "select * from TYPE_TABLE" :result-types :auto)))
58 (length (car rows)))))
61 (deftest :BASIC/SELECT/2
62 (with-dataset *ds-basic*
64 (dolist (row (query "select * from TYPE_TABLE" :result-types nil)
66 (destructuring-bind (int float str) row
67 (push (list (stringp int)
71 ((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)))
73 (deftest :basic/select/3
74 (with-dataset *ds-basic*
76 (dolist (row (query "select * from TYPE_TABLE" :result-types nil)
78 (destructuring-bind (int float str) row
79 (push (list (double-float-equal
80 (transform-float-1 (parse-integer int))
84 (parse-double float)))
86 ((t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t)))
89 (with-dataset *ds-basic*
91 (rows (map-query 'vector #'identity "select * from TYPE_TABLE"
93 (declare (type (simple-array list (*)) rows))
94 (dotimes (i (length rows) results)
98 (length (aref rows i))
100 (parse-integer (first (aref rows i))
103 (transform-float-1 (parse-integer (first (aref rows i))))
104 (parse-double (second (aref rows i)))))
106 ((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)))
109 (deftest :basic/map/2
110 (with-dataset *ds-basic*
112 (rows (map-query 'list #'identity "select * from TYPE_TABLE"
114 (dotimes (i (length rows) results)
118 (length (nth i rows))
120 (parse-integer (first (nth i rows))
123 (transform-float-1 (parse-integer (first (nth i rows))))
124 (parse-double (second (nth i rows)))))
126 ((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)))
128 (deftest :basic/map/3
129 (with-dataset *ds-basic*
131 (rows (map-query 'list #'identity "select * from TYPE_TABLE"
132 :result-types :auto)))
133 (dotimes (i (length rows) results)
137 (length (nth i rows))
139 (first (nth i rows)))
141 (transform-float-1 (first (nth i rows)))
142 (second (nth i rows))))
144 ((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)))
146 ;; confirm that a query on a single element returns a list of one element
147 (deftest :basic/map/4
148 (with-dataset *ds-basic*
149 (let ((rows (map-query 'list #'identity "select t_int from TYPE_TABLE"
153 (length (first rows)))))
157 (with-dataset *ds-basic*
159 (do-query ((int float str) "select * from TYPE_TABLE" :result-types nil)
160 (let ((int-number (parse-integer int)))
162 (cons (list (double-float-equal (transform-float-1
164 (parse-double float))
165 (double-float-equal (parse-double str)
166 (parse-double float)))
169 ((t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t)))
172 (with-dataset *ds-basic*
174 (do-query ((int float str) "select * from TYPE_TABLE" :result-types :auto)
177 (list (double-float-equal
178 (transform-float-1 int)
185 ((t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t)))
188 (deftest :basic/bigint/1
189 (with-dataset *ds-bigint*
191 (dolist (row (query "select * from TYPE_BIGINT" :result-types :auto)
193 (destructuring-bind (int bigint) row
194 (push (list (integerp int)
195 (if (and (eq :odbc *test-database-type*)
196 (eq :postgresql *test-database-underlying-type*))
197 ;; ODBC/Postgresql may return returns bigints as strings or integer
198 ;; depending upon the platform
202 ((t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t)))
207 (def-dataset *ds-basic*
210 (clsql:execute-command "DROP TABLE TYPE_TABLE")
211 (clsql:execute-command "DROP TABLE TYPE_BIGINT"))
213 (clsql:execute-command
214 "CREATE TABLE TYPE_TABLE (t_int integer, t_float double precision, t_str VARCHAR(30))")
217 (let* ((test-int (- i 5))
218 (test-flt (transform-float-1 test-int)))
219 (clsql:execute-command
220 (format nil "INSERT INTO TYPE_TABLE VALUES (~a,~a,'~a')"
222 (clsql-sys:number-to-sql-string test-flt)
223 (clsql-sys:number-to-sql-string test-flt)
225 (:cleanup "DROP TABLE TYPE_TABLE"))
227 (def-dataset *ds-bigint*
229 (ignore-errors (clsql:execute-command "DROP TABLE TYPE_BIGINT"))
230 (clsql:execute-command "CREATE TABLE TYPE_BIGINT (T_INT integer, T_BIGINT BIGINT)")
232 (clsql:execute-command
233 (format nil "INSERT INTO TYPE_BIGINT VALUES (~a,~a)"
235 (transform-bigint-1 (- i 5)))))))
236 (:cleanup "DROP TABLE TYPE_BIGINT"))
238 ;;;; Testing functions
240 (defun transform-float-1 (i)
241 (coerce (* i (abs (/ i 2)) (expt 10 (* 2 i))) 'double-float))
243 (defun transform-bigint-1 (i)
244 (* i (expt 10 (* 3 (abs i)))))
246 (defun parse-double (num-str)
247 (let ((*read-default-float-format* 'double-float))
248 (coerce (read-from-string num-str) 'double-float)))
250 (defun double-float-equal (a b)
255 (let ((diff (abs (/ (- a b) a))))
256 (if (> diff (* 10 double-float-epsilon))