X-Git-Url: http://git.kpe.io/?p=clsql.git;a=blobdiff_plain;f=tests%2Ftest-basic.lisp;h=6a27fdd7de0d5f1f7883bfbf2d2044235dd184d0;hp=b4cec793479b444078cc0afba61d594c05170656;hb=8a8ee2d7d791b7a3efaed06420802a925d16fca3;hpb=d107be8f0cad113b96b6cfe443cc4d7c08126db4 diff --git a/tests/test-basic.lisp b/tests/test-basic.lisp index b4cec79..6a27fdd 100644 --- a/tests/test-basic.lisp +++ b/tests/test-basic.lisp @@ -3,7 +3,7 @@ ;;;; FILE IDENTIFICATION ;;;; ;;;; Name: test-basic.lisp -;;;; Purpose: Tests for clsql-base and result types +;;;; Purpose: Tests for clsql string-based queries and result types ;;;; Author: Kevin M. Rosenberg ;;;; Created: Mar 2002 ;;;; @@ -18,58 +18,181 @@ (in-package #:clsql-tests) +(defun test-basic-initialize () + (ignore-errors + (clsql:execute-command "DROP TABLE TYPE_TABLE")) + (clsql:execute-command + "CREATE TABLE TYPE_TABLE (t_int integer, t_float double precision, t_bigint BIGINT, 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,'~a')" + test-int + (clsql-sys:number-to-sql-string test-flt) + (transform-bigint-1 test-int) + (clsql-sys:number-to-sql-string test-flt) + ))))) + +(defun test-basic-forms () + (append + (test-basic-forms-untyped) + '( + (deftest :BASIC/TYPE/1 + (let ((results '())) + (dolist (row (query "select * from TYPE_TABLE" :result-types :auto) + results) + (destructuring-bind (int float bigint str) row + (push (list (integerp int) + (typep float 'double-float) + (if (and (eq :odbc *test-database-type*) + (eq :postgresql *test-database-underlying-type*)) + ;; ODBC/Postgresql may return returns bigints as strings or integer + ;; depending upon the platform + t + (integerp bigint)) + (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 t t t) (t t t t) (t t t t))) + + (deftest :BASIC/TYPE/2 + (let ((results '())) + (dolist (row (query "select * from TYPE_TABLE" :result-types :auto) + results) + (destructuring-bind (int float bigint 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))) + ))) + +(defun test-basic-forms-untyped () + '((deftest :BASIC/SELECT/1 + (let ((rows (query "select * from TYPE_TABLE" :result-types :auto))) + (values + (length rows) + (length (car rows)))) + 11 4) + + (deftest :BASIC/SELECT/2 + (let ((results '())) + (dolist (row (query "select * from TYPE_TABLE" :result-types nil) + results) + (destructuring-bind (int float bigint str) row + (push (list (stringp int) + (stringp float) + (stringp bigint) + (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 t t t) (t t t t) (t t t t))) + + (deftest :BASIC/SELECT/3 + (let ((results '())) + (dolist (row (query "select * from TYPE_TABLE" :result-types nil) + results) + (destructuring-bind (int float bigint str) row + (declare (ignore bigint)) + (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))) -(defun test-basic (spec type) - (let ((db (clsql:connect spec :database-type type :if-exists :new))) - (unwind-protect - (if (eq type :sqlite) - (%test-basic-untyped db type) - (%test-basic db type)) - (disconnect :database db)))) + (deftest :BASIC/MAP/1 + (let ((results '()) + (rows (map-query 'vector #'list "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 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))) + + (deftest :BASIC/MAP/2 + (let ((results '()) + (rows (map-query 'list #'list "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 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))) + + (deftest :BASIC/MAP/3 + (let ((results '()) + (rows (map-query 'list #'list "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 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))) -(defun %test-basic (db type) - (create-test-table db) - (dolist (row (query "select * from test_clsql" :database db :result-types :auto)) - (test-table-row row :auto type)) - (dolist (row (query "select * from test_clsql" :database db :result-types nil)) - (test-table-row row nil type)) - (loop for row across (map-query 'vector #'list "select * from test_clsql" - :database db :result-types :auto) - do (test-table-row row :auto type)) - (loop for row across (map-query 'vector #'list "select * from test_clsql" - :database db :result-types nil) - do (test-table-row row nil type)) - (loop for row in (map-query 'list #'list "select * from test_clsql" - :database db :result-types nil) - do (test-table-row row nil type)) - (loop for row in (map-query 'list #'list "select * from test_clsql" - :database db :result-types :auto) - do (test-table-row row :auto type)) - (test (map-query nil #'list "select * from test_clsql" - :database db :result-types :auto) - nil - :fail-info "Expected NIL result from map-query nil") - (do-query ((int float bigint str) "select * from test_clsql") - (test-table-row (list int float bigint str) nil type)) - (do-query ((int float bigint str) "select * from test_clsql" :result-types :auto) - (test-table-row (list int float bigint str) :auto type)) - #+ignore (drop-test-table db)) + (deftest :BASIC/DO/1 + (let ((results '())) + (do-query ((int float bigint str) "select * from TYPE_TABLE" :result-types nil) + (declare (ignore bigint)) + (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 + (let ((results '())) + (do-query ((int float bigint str) "select * from TYPE_TABLE" :result-types :auto) + (declare (ignore bigint)) + (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))) + )) -(defun %test-basic-untyped (db type) - (create-test-table db) - (dolist (row (query "select * from test_clsql" :database db :result-types nil)) - (test-table-row row nil type)) - (loop for row across (map-query 'vector #'list "select * from test_clsql" - :database db :result-types nil) - do (test-table-row row nil type)) - (loop for row in (map-query 'list #'list "select * from test_clsql" - :database db :result-types nil) - do (test-table-row row nil type)) - - (do-query ((int float bigint str) "select * from test_clsql") - (test-table-row (list int float bigint str) nil type)) - (drop-test-table db)) ;;;; Testing functions @@ -79,79 +202,10 @@ (defun transform-bigint-1 (i) (* i (expt 10 (* 3 (abs i))))) -(defun create-test-table (db) - (ignore-errors - (clsql:execute-command - "DROP TABLE test_clsql" :database db)) - (clsql:execute-command - "CREATE TABLE test_clsql (t_int integer, t_float double precision, t_bigint BIGINT, t_str CHAR(30))" - :database db) - (dotimes (i 11) - (let* ((test-int (- i 5)) - (test-flt (transform-float-1 test-int))) - (clsql:execute-command - (format nil "INSERT INTO test_clsql VALUES (~a,~a,~a,'~a')" - test-int - (clsql-base:number-to-sql-string test-flt) - (transform-bigint-1 test-int) - (clsql-base:number-to-sql-string test-flt) - ) - :database db)))) - (defun parse-double (num-str) (let ((*read-default-float-format* 'double-float)) (coerce (read-from-string num-str) 'double-float))) -(defun test-table-row (row types db-type) - (test (and (listp row) - (= 4 (length row))) - t - :fail-info - (format nil "Row ~S is incorrect format" row)) - (destructuring-bind (int float bigint str) row - (cond - ((eq types :auto) - (test (and (integerp int) - (typep float 'double-float) - (or (eq db-type :aodbc) ;; aodbc considers bigints as strings - (integerp bigint)) - (stringp str)) - t - :fail-info - (format nil "Incorrect field type for row ~S (types :auto)" row))) - ((null types) - (test (and (stringp int) - (stringp float) - (stringp bigint) - (stringp str)) - t - :fail-info - (format nil "Incorrect field type for row ~S (types nil)" row)) - (when (stringp int) - (setq int (parse-integer int))) - (setq bigint (parse-integer bigint)) - (when (stringp float) - (setq float (parse-double float)))) - ((listp types) - (error "NYI") - ) - (t - (test t nil - :fail-info - (format nil "Invalid types field (~S) passed to test-table-row" types)))) - (unless (eq db-type :sqlite) ; SQLite is typeless. - (test (transform-float-1 int) - float - :test #'double-float-equal - :fail-info - (format nil "Wrong float value ~A for int ~A (row ~S)" float int row))) - (test float - (parse-double str) - :test #'double-float-equal - :fail-info (format nil "Wrong string value ~A for double ~A~%Row: ~S" - str float row)))) - - (defun double-float-equal (a b) (if (zerop a) (if (zerop b) @@ -161,6 +215,3 @@ (if (> diff (* 10 double-float-epsilon)) nil t)))) - -(defun drop-test-table (db) - (clsql:execute-command "DROP TABLE test_clsql" :database db))