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)))
205 (deftest :basic/bigtext/1
206 (with-dataset *ds-bigtext*
208 (str (make-string len :initial-element #\a))
209 (cmd (format nil "INSERT INTO testbigtext (a) VALUES ('~a')" str)))
210 (execute-command cmd)
211 (let ((a (first (query "SELECT a from testbigtext"
212 :flatp t :field-names nil))))
213 (assert (string= str a) (str a)
214 "mismatch on a. inserted: ~a returned: ~a" len (length a)))
217 (deftest :basic/bigtext/2
219 (with-dataset *ds-bigtext*
220 (let* ((len (random 7500))
221 (str (make-string len :initial-element #\a))
222 (cmd (format nil "INSERT INTO testbigtext (a) VALUES ('~a')" str)))
223 (execute-command cmd)
224 (let ((a (first (query "SELECT a from testbigtext"
225 :flatp t :field-names nil))))
226 (assert (string= str a) (str a)
227 "mismatch on a. inserted: ~a returned: ~a" len (length a)))
233 (def-dataset *ds-basic*
236 (clsql:execute-command "DROP TABLE TYPE_TABLE")
237 (clsql:execute-command "DROP TABLE TYPE_BIGINT"))
239 (clsql:execute-command
240 "CREATE TABLE TYPE_TABLE (t_int integer, t_float double precision, t_str VARCHAR(30))")
243 (let* ((test-int (- i 5))
244 (test-flt (transform-float-1 test-int)))
245 (clsql:execute-command
246 (format nil "INSERT INTO TYPE_TABLE VALUES (~a,~a,'~a')"
248 (clsql-sys:number-to-sql-string test-flt)
249 (clsql-sys:number-to-sql-string test-flt)
251 (:cleanup "DROP TABLE TYPE_TABLE"))
253 (def-dataset *ds-bigint*
255 (ignore-errors (clsql:execute-command "DROP TABLE TYPE_BIGINT"))
256 (clsql:execute-command "CREATE TABLE TYPE_BIGINT (T_INT integer, T_BIGINT BIGINT)")
258 (clsql:execute-command
259 (format nil "INSERT INTO TYPE_BIGINT VALUES (~a,~a)"
261 (transform-bigint-1 (- i 5)))))))
262 (:cleanup "DROP TABLE TYPE_BIGINT"))
264 ;;;; Testing functions
266 (defun transform-float-1 (i)
267 (coerce (* i (abs (/ i 2)) (expt 10 (* 2 i))) 'double-float))
269 (defun transform-bigint-1 (i)
270 (* i (expt 10 (* 3 (abs i)))))
272 (defun parse-double (num-str)
273 (let ((*read-default-float-format* 'double-float))
274 (coerce (read-from-string num-str) 'double-float)))
276 (defun double-float-equal (a b)
281 (let ((diff (abs (/ (- a b) a))))
282 (if (> diff (* 10 double-float-epsilon))
286 (def-dataset *ds-bigtext*
287 (:setup "CREATE TABLE testbigtext(a varchar(7500))")
288 (:cleanup "DROP TABLE testbigtext"))