summaryrefslogtreecommitdiff
path: root/tests/test-basic.lisp
blob: 4d277e358d14ba2c722940c36e02df888371048d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
;;;; *************************************************************************
;;;; FILE IDENTIFICATION
;;;;
;;;; Name:    test-basic.lisp
;;;; Purpose: Tests for clsql string-based queries and result types
;;;; Author:  Kevin M. Rosenberg
;;;; Created: Mar 2002
;;;;
;;;; This file, part of CLSQL, is Copyright (c) 2002-2010 by Kevin M. Rosenberg
;;;;
;;;; CLSQL users are granted the rights to distribute and use this software
;;;; as governed by the terms of the Lisp Lesser GNU Public License
;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
;;;; *************************************************************************

(in-package #:clsql-tests)


(setq *rt-basic*
  '(
    (deftest :basic/type/1
	(with-dataset *ds-basic*
	  (let ((results '()))
	    (dolist (row (query "select * from TYPE_TABLE" :result-types :auto)
		     results)
	      (destructuring-bind (int float str) row
		(push (list (integerp int)
			    (typep float 'double-float)
			    (stringp str))
		      results)))))
      ((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)))

    (deftest :basic/type/2
	(with-dataset *ds-basic*
	  (let ((results '()))
	    (dolist (row (query "select * from TYPE_TABLE" :result-types :auto)
		     results)
	      (destructuring-bind (int float str) row
		(setq results
		      (cons (list (double-float-equal
				   (transform-float-1 int)
				   float)
				  (double-float-equal
				   (parse-double str)
				   float))
			    results))))
	    results))
      ((t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t)))

    (deftest :basic/select/1
	(with-dataset *ds-basic*
	  (let ((rows (query "select * from TYPE_TABLE" :result-types :auto)))
	    (values
	      (length rows)
	      (length (car rows)))))
      11 3)

    (deftest :BASIC/SELECT/2
	(with-dataset *ds-basic*
	  (let ((results '()))
	    (dolist (row (query "select * from TYPE_TABLE" :result-types nil)
		     results)
	      (destructuring-bind (int float str) row
		(push (list (stringp int)
			    (stringp float)
			    (stringp str))
		      results)))))
      ((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)))

    (deftest :basic/select/3
	(with-dataset *ds-basic*
	  (let ((results '()))
	    (dolist (row (query "select * from TYPE_TABLE" :result-types nil)
		     results)
	      (destructuring-bind (int float str) row
		(push (list (double-float-equal
			     (transform-float-1 (parse-integer int))
			     (parse-double float))
			    (double-float-equal
			     (parse-double str)
			     (parse-double float)))
		      results)))))
      ((t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t)))

    (deftest :basic/map/1
	(with-dataset *ds-basic*
	  (let ((results '())
		(rows (map-query 'vector #'identity "select * from TYPE_TABLE"
				 :result-types nil)))
	    (declare (type (simple-array list (*)) rows))
	    (dotimes (i (length rows) results)
	      (push
	       (list
		(listp (aref rows i))
		(length (aref rows i))
		(eql (- i 5)
		     (parse-integer (first (aref rows i))
				    :junk-allowed nil))
		(double-float-equal
		 (transform-float-1 (parse-integer (first (aref rows i))))
		 (parse-double (second (aref rows i)))))
	       results))))
      ((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)))


    (deftest :basic/map/2
	(with-dataset *ds-basic*
	  (let ((results '())
		(rows (map-query 'list #'identity "select * from TYPE_TABLE"
				 :result-types nil)))
	    (dotimes (i (length rows) results)
	      (push
	       (list
		(listp (nth i rows))
		(length (nth i rows))
		(eql (- i 5)
		     (parse-integer (first (nth i rows))
				    :junk-allowed nil))
		(double-float-equal
		 (transform-float-1 (parse-integer (first (nth i rows))))
		 (parse-double (second (nth i rows)))))
	       results))))
      ((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)))

    (deftest :basic/map/3
	(with-dataset *ds-basic*
	  (let ((results '())
		(rows (map-query 'list #'identity "select * from TYPE_TABLE"
				 :result-types :auto)))
	    (dotimes (i (length rows) results)
	      (push
	       (list
		(listp (nth i rows))
		(length (nth i rows))
		(eql (- i 5)
		     (first (nth i rows)))
		(double-float-equal
		 (transform-float-1 (first (nth i rows)))
		 (second (nth i rows))))
	       results))))
      ((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)))

    ;; confirm that a query on a single element returns a list of one element
    (deftest :basic/map/4
	(with-dataset *ds-basic*
	  (let ((rows (map-query 'list #'identity "select t_int from TYPE_TABLE"
				 :result-types nil)))
	    (values
	      (consp (first rows))
	      (length (first rows)))))
      t 1)

    (deftest :basic/do/1
	(with-dataset *ds-basic*
	  (let ((results '()))
	    (do-query ((int float str) "select * from TYPE_TABLE" :result-types nil)
	      (let ((int-number (parse-integer int)))
		(setq results
		      (cons (list (double-float-equal (transform-float-1
						       int-number)
						      (parse-double float))
				  (double-float-equal (parse-double str)
						      (parse-double float)))
			    results))))
	    results))
      ((t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t)))

    (deftest :basic/do/2
	(with-dataset *ds-basic*
	  (let ((results '()))
	    (do-query ((int float str) "select * from TYPE_TABLE" :result-types :auto)
	      (setq results
		    (cons
		     (list (double-float-equal
			    (transform-float-1 int)
			    float)
			   (double-float-equal
			    (parse-double str)
			    float))
		     results)))
	    results))
      ((t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t)))


    (deftest :basic/bigint/1
	(with-dataset *ds-bigint*
	  (let ((results '()))
	    (dolist (row (query "select * from TYPE_BIGINT" :result-types :auto)
		     results)
	      (destructuring-bind (int bigint) row
		(push (list (integerp int)
                            (integerp bigint))
		      results)))))
      ((t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t) (t t)))


    (deftest :basic/bigtext/1
	(with-dataset *ds-bigtext*
	  (let* ((len 7499)
		 (str (make-string len :initial-element #\a))
		 (cmd (format nil "INSERT INTO testbigtext (a) VALUES ('~a')" str)))
	    (execute-command cmd)
	    (let ((a (first (query "SELECT a from testbigtext"
				   :flatp t :field-names nil))))
	      (assert (string= str a) (str a)
		      "mismatch on a. inserted: ~a returned: ~a" len (length a)))
	    ))
      nil)
    (deftest :basic/bigtext/2
     (flet ((random-char ()
              (let ((alphabet "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
                    (idx (random 52)))
                (elt alphabet idx))))
       (dotimes (n 10)
         (with-dataset *ds-bigtext*
           (let* ((len (random 7500))
                  (str (coerce (make-array len
                                           :initial-contents (loop repeat len collect (random-char)))
                               'string))
                  (cmd (format nil "INSERT INTO testbigtext (a) VALUES ('~a')" str)))
             (execute-command cmd)
             (let ((a (first (query "SELECT a from testbigtext"
                                    :flatp t :field-names nil))))
               (assert (string= str a) (str a)
                       "mismatch on randomized bigtext(~a) inserted: ~s returned: ~s" len str a))
             ))))
     nil)

    (deftest :basic/reallybigintegers/1
        (with-dataset *ds-reallybigintegers*
          (let* ((a (1- (expt 2 64)))
                 (b (- (expt 2 64) 2))
                 (c (expt 2 63))
                 (d (expt 2 62))
                 (sql (format nil "INSERT INTO testreallybigintegers
                              VALUES (~A, ~A, ~A, ~A)"
                              a b c d)))
            (query sql)
            (let ((results
                    (query
                     (format nil "SELECT * FROM testreallybigintegers"))))
              (equal `(,a ,b ,c ,d) (car results)))))
      t)
    ))


(def-dataset *ds-basic*
  (:setup (lambda ()
	    (ignore-errors
	      (clsql:execute-command "DROP TABLE TYPE_TABLE")
	      (clsql:execute-command "DROP TABLE TYPE_BIGINT"))

	    (clsql:execute-command
	     "CREATE TABLE TYPE_TABLE (t_int integer, t_float double precision, t_str VARCHAR(30))")

	    (dotimes (i 11)
	      (let* ((test-int (- i 5))
		     (test-flt (transform-float-1 test-int)))
		(clsql:execute-command
		 (format nil "INSERT INTO TYPE_TABLE VALUES (~a,~a,'~a')"
			 test-int
			 (clsql-sys:number-to-sql-string test-flt)
			 (clsql-sys:number-to-sql-string test-flt)
			 ))))))
  (:cleanup "DROP TABLE TYPE_TABLE"))

(def-dataset *ds-bigint*
  (:setup (lambda ()
	    (ignore-errors (clsql:execute-command "DROP TABLE TYPE_BIGINT"))
	    (clsql:execute-command "CREATE TABLE TYPE_BIGINT (T_INT integer, T_BIGINT BIGINT)")
	    (dotimes (i 11)
	      (clsql:execute-command
	       (format nil "INSERT INTO TYPE_BIGINT VALUES (~a,~a)"
		       (- i 5)
		       (transform-bigint-1 (- i 5)))))))
  (:cleanup "DROP TABLE TYPE_BIGINT"))

;;;; Testing functions

(defun transform-float-1 (i)
  (coerce (* i (abs (/ i 2)) (expt 10 (* 2 i))) 'double-float))

(defun transform-bigint-1 (i)
  (* i (expt 10 (* 3 (abs i)))))

(defun parse-double (num-str)
  (let ((*read-default-float-format* 'double-float))
    (coerce (read-from-string num-str) 'double-float)))

(defun double-float-equal (a b)
  (if (zerop a)
      (if (zerop b)
	  t
	  nil)
      (let ((diff (abs (/ (- a b) a))))
	(if (> diff (* 10 double-float-epsilon))
	    nil
	    t))))

(def-dataset *ds-bigtext*
  (:setup "CREATE TABLE testbigtext(a varchar(7500))")
  (:cleanup "DROP TABLE testbigtext"))

(def-dataset *ds-reallybigintegers*
  (:setup (lambda ()
            (ignore-errors
             (clsql:execute-command "DROP TABLE testreallybigintegers"))
            (clsql:execute-command
             "CREATE TABLE testreallybigintegers( a BIGINT UNSIGNED,
                                                  b BIGINT UNSIGNED,
                                                  c BIGINT UNSIGNED,
                                                  d BIGINT UNSIGNED )")))
  (:cleanup "DROP TABLE testreallybigintegers"))