1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: test-basic.lisp
6 ;;;; Purpose: Tests for clsql-base 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-base:number-to-sql-string test-flt)
33 (transform-bigint-1 test-int)
34 (clsql-base:number-to-sql-string test-flt)
37 (defun test-basic-forms ()
39 (test-basic-forms-untyped)
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)
51 ((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)))
56 (dolist (row (query "select * from TYPE_TABLE" :result-types :auto)
58 (destructuring-bind (int float bigint str) row
59 (push (list (double-float-equal
60 (transform-float-1 int)
66 ((t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t)))
69 (defun test-basic-forms-untyped ()
70 '((deftest BASIC/SELECT/1
71 (let ((rows (query "select * from TYPE_TABLE" :result-types :auto)))
77 (deftest BASIC/SELECT/2
79 (dolist (row (query "select * from TYPE_TABLE" :result-types nil)
81 (destructuring-bind (int float bigint str) row
82 (push (list (stringp int)
87 ((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)))
89 (deftest BASIC/SELECT/3
91 (dolist (row (query "select * from TYPE_TABLE" :result-types nil)
93 (destructuring-bind (int float bigint str) row
94 (push (list (double-float-equal
95 (transform-float-1 (parse-integer int))
99 (parse-double float)))
101 ((t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t)))
105 (rows (map-query 'vector #'list "select * from TYPE_TABLE"
107 (dotimes (i (length rows) results)
110 (listp (aref rows i))
111 (length (aref rows i))
113 (parse-integer (first (aref rows i))
116 (transform-float-1 (parse-integer (first (aref rows i))))
117 (parse-double (second (aref rows i)))))
119 ((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)))
123 (rows (map-query 'list #'list "select * from TYPE_TABLE"
125 (dotimes (i (length rows) results)
129 (length (nth i rows))
131 (parse-integer (first (nth i rows))
134 (transform-float-1 (parse-integer (first (nth i rows))))
135 (parse-double (second (nth i rows)))))
137 ((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)))
141 (do-query ((int float bigint str) "select * from TYPE_TABLE")
142 (push (list (double-float-equal
143 (transform-float-1 (parse-integer int))
144 (parse-double float))
147 (parse-double float)))
150 ((t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t)))
154 ;;;; Testing functions
156 (defun transform-float-1 (i)
157 (coerce (* i (abs (/ i 2)) (expt 10 (* 2 i))) 'double-float))
159 (defun transform-bigint-1 (i)
160 (* i (expt 10 (* 3 (abs i)))))
162 (defun parse-double (num-str)
163 (let ((*read-default-float-format* 'double-float))
164 (coerce (read-from-string num-str) 'double-float)))
166 (defun double-float-equal (a b)
171 (let ((diff (abs (/ (- a b) a))))
172 (if (> diff (* 10 double-float-epsilon))