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 ()
40 (defun test-basic-forms-untyped ()
44 (defun %test-basic-forms ()
45 (dolist (row (query "select * from TYPE_TABLE" :result-types :auto))
46 (test-table-row row :auto))
47 (dolist (row (query "select * from TYPE_TABLE" :result-types nil))
48 (test-table-row row nil))
49 (loop for row across (map-query 'vector #'list "select * from TYPE_TABLE"
51 do (test-table-row row :auto))
52 (loop for row across (map-query 'vector #'list "select * from TYPE_TABLE"
54 do (test-table-row row nil))
55 (loop for row in (map-query 'list #'list "select * from TYPE_TABLE"
57 do (test-table-row row nil))
58 (loop for row in (map-query 'list #'list "select * from TYPE_TABLE"
60 do (test-table-row row :auto))
61 (test (map-query nil #'list "select * from TYPE_TABLE"
64 :fail-info "Expected NIL result from map-query nil")
65 (do-query ((int float bigint str) "select * from TYPE_TABLE")
66 (test-table-row (list int float bigint str) nil))
67 (do-query ((int float bigint str) "select * from TYPE_TABLE" :result-types :auto)
68 (test-table-row (list int float bigint str) :auto)))
71 (defun %test-basic-forms-untyped ()
72 (dolist (row (query "select * from TYPE_TABLE" :result-types nil))
73 (test-table-row row nil))
74 (loop for row across (map-query 'vector #'list "select * from TYPE_TABLE"
76 do (test-table-row row nil))
77 (loop for row in (map-query 'list #'list "select * from TYPE_TABLE"
79 do (test-table-row row nil))
81 (do-query ((int float bigint str) "select * from TYPE_TABLE")
82 (test-table-row (list int float bigint str) nil)))
85 ;;;; Testing functions
87 (defun transform-float-1 (i)
88 (coerce (* i (abs (/ i 2)) (expt 10 (* 2 i))) 'double-float))
90 (defun transform-bigint-1 (i)
91 (* i (expt 10 (* 3 (abs i)))))
95 (defun parse-double (num-str)
96 (let ((*read-default-float-format* 'double-float))
97 (coerce (read-from-string num-str) 'double-float)))
99 (defun test-table-row (row types)
100 (test (and (listp row)
104 (format nil "Row ~S is incorrect format" row))
105 (destructuring-bind (int float bigint str) row
108 (test (and (integerp int)
109 (typep float 'double-float)
110 (or (member *test-database-type*
111 '(:odbc :aodbc)) ;; aodbc considers bigints as strings
116 (format nil "Incorrect field type for row ~S (types :auto)" row)))
118 (test (and (stringp int)
124 (format nil "Incorrect field type for row ~S (types nil)" row))
126 (setq int (parse-integer int)))
127 (setq bigint (parse-integer bigint))
128 (when (stringp float)
129 (setq float (parse-double float))))
136 (format nil "Invalid types field (~S) passed to test-table-row" types))))
137 (unless (eq *test-database-type* :sqlite) ; SQLite is typeless.
138 (test (transform-float-1 int)
140 :test #'double-float-equal
142 (format nil "Wrong float value ~A for int ~A (row ~S)" float int row)))
145 :test #'double-float-equal
146 :fail-info (format nil "Wrong string value ~A for double ~A~%Row: ~S"
150 (defun double-float-equal (a b)
155 (let ((diff (abs (/ (- a b) a))))
156 (if (> diff (* 10 double-float-epsilon))