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)
22 (defun test-basic (spec type)
23 (let ((db (clsql:connect spec :database-type type :if-exists :new)))
26 (%test-basic-untyped db type)
27 (%test-basic db type))
28 (disconnect :database db))))
30 (defun %test-basic (db type)
31 (create-test-table db)
32 (dolist (row (query "select * from test_clsql" :database db :result-types :auto))
33 (test-table-row row :auto type))
34 (dolist (row (query "select * from test_clsql" :database db :result-types nil))
35 (test-table-row row nil type))
36 (loop for row across (map-query 'vector #'list "select * from test_clsql"
37 :database db :result-types :auto)
38 do (test-table-row row :auto type))
39 (loop for row across (map-query 'vector #'list "select * from test_clsql"
40 :database db :result-types nil)
41 do (test-table-row row nil type))
42 (loop for row in (map-query 'list #'list "select * from test_clsql"
43 :database db :result-types nil)
44 do (test-table-row row nil type))
45 (loop for row in (map-query 'list #'list "select * from test_clsql"
46 :database db :result-types :auto)
47 do (test-table-row row :auto type))
48 (test (map-query nil #'list "select * from test_clsql"
49 :database db :result-types :auto)
51 :fail-info "Expected NIL result from map-query nil")
52 (do-query ((int float bigint str) "select * from test_clsql")
53 (test-table-row (list int float bigint str) nil type))
54 (do-query ((int float bigint str) "select * from test_clsql" :result-types :auto)
55 (test-table-row (list int float bigint str) :auto type))
59 (defun %test-basic-untyped (db type)
60 (create-test-table db)
61 (dolist (row (query "select * from test_clsql" :database db :result-types nil))
62 (test-table-row row nil type))
63 (loop for row across (map-query 'vector #'list "select * from test_clsql"
64 :database db :result-types nil)
65 do (test-table-row row nil type))
66 (loop for row in (map-query 'list #'list "select * from test_clsql"
67 :database db :result-types nil)
68 do (test-table-row row nil type))
70 (do-query ((int float bigint str) "select * from test_clsql")
71 (test-table-row (list int float bigint str) nil type))
74 ;;;; Testing functions
76 (defun transform-float-1 (i)
77 (coerce (* i (abs (/ i 2)) (expt 10 (* 2 i))) 'double-float))
79 (defun transform-bigint-1 (i)
80 (* i (expt 10 (* 3 (abs i)))))
82 (defun create-test-table (db)
84 (clsql:execute-command
85 "DROP TABLE test_clsql" :database db))
86 (clsql:execute-command
87 "CREATE TABLE test_clsql (t_int integer, t_float float, t_bigint BIGINT, t_str CHAR(30))"
90 (let* ((test-int (- i 5))
91 (test-flt (transform-float-1 test-int)))
92 (clsql:execute-command
93 (format nil "INSERT INTO test_clsql VALUES (~a,~a,~a,'~a')"
95 (clsql-base:number-to-sql-string test-flt)
96 (transform-bigint-1 test-int)
97 (clsql-base:number-to-sql-string test-flt)
101 (defun parse-double (num-str)
102 (let ((*read-default-float-format* 'double-float))
103 (coerce (read-from-string num-str) 'double-float)))
105 (defun test-table-row (row types db-type)
106 (test (and (listp row)
110 (format nil "Row ~S is incorrect format" row))
111 (destructuring-bind (int float bigint str) row
114 (test (and (integerp int)
115 (typep float 'double-float)
116 (or (eq db-type :aodbc) ;; aodbc doesn't handle bigint conversions
121 (format nil "Incorrect field type for row ~S (types :auto)" row)))
123 (test (and (stringp int)
129 (format nil "Incorrect field type for row ~S (types nil)" row))
130 (setq int (parse-integer int))
131 (setq bigint (parse-integer bigint))
132 (setq float (parse-double float)))
139 (format nil "Invalid types field (~S) passed to test-table-row" types))))
140 (unless (eq db-type :sqlite) ; SQLite is typeless.
141 (test (transform-float-1 int)
145 (format nil "Wrong float value ~A for int ~A (row ~S)" float int row)))
148 :test #'double-float-equal
149 :fail-info (format nil "Wrong string value ~A for double ~A~%Row: ~S"
153 (defun double-float-equal (a b)
158 (let ((diff (abs (/ (- a b) a))))
159 (if (> diff (* 10 double-float-epsilon))
163 (defun drop-test-table (db)
164 (clsql:execute-command "DROP TABLE test_clsql" :database db))