r9657: Cleanup and document the FDDL.
[clsql.git] / tests / test-fddl.lisp
index 2cb4b2b64046f0ff268ae062f3149921b195f316..c5c305b3828d3d4aa1ac694df734e8949fb5339a 100644 (file)
@@ -1,9 +1,9 @@
 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
 ;;;; ======================================================================
-;;;; File:    test-fddl.lisp
-;;;; Author:  Marcus Pearce <m.t.pearce@city.ac.uk>
-;;;; Created: 30/03/2004
-;;;; Updated: $Id$
+;;;; File:     test-fddl.lisp
+;;;; Authors:  Marcus Pearce <m.t.pearce@city.ac.uk> and Kevin Rosenberg
+;;;; Created:  30/03/2004
+;;;; Updated:  $Id$
 ;;;;
 ;;;; Tests for the CLSQL Functional Data Definition Language
 ;;;; (FDDL).
        
 ;; list current tables 
 (deftest :fddl/table/1
-    (apply #'values 
-           (sort (mapcar #'string-downcase
-                         (clsql:list-tables :owner *test-database-user*))
-                 #'string>))
-  "employee" "company")
+    (sort (mapcar #'string-downcase
+                 (clsql:list-tables :owner *test-database-user*))
+     #'string<)
+  ("addr" "big" "company" "ea_join" "employee" "type_bigint" "type_table"))
 
 ;; create a table, test for its existence, drop it and test again 
 (deftest :fddl/table/2
                     (clsql:drop-table [foo] :if-does-not-exist :ignore))))
   "comments" "height" "id" "name")
 
+(deftest :fddl/table/4
+    (values
+     (clsql:table-exists-p "MyMixedCase")
+     (progn
+       (clsql:create-table "MyMixedCase" '(([a] integer)))
+       (clsql:table-exists-p "MyMixedCase"))
+     (progn
+       (clsql:drop-table "MyMixedCase")
+       (clsql:table-exists-p "MyMixedCase")))
+  nil t nil)
+
+(deftest :fddl/table/5
+    (prog1
+       (progn
+         (clsql:create-table "MyMixedCase" '(([a] integer)))
+         (clsql:execute-command "insert into MyMixedCase values (5)")
+          (clsql:insert-records :into "MyMixedCase" :values '(6))
+          (clsql:select [a] :from "MyMixedCase" :order-by '((a :asc))))
+      (clsql:drop-table "MyMixedCase"))
+  ((5) (6)))
+
 (deftest :fddl/attributes/1
     (apply #'values
            (sort 
             (mapcar #'string-downcase
                     (clsql:list-attributes [employee]
-                                          :owner *test-database-user*))
+                                          :owner *test-database-user*))
             #'string<))
-  "birthday" "companyid" "email" "emplid" "first_name" "groupid" "height"
+  "bd_utime" "birthday" "ecompanyid" "email" "emplid" "first_name" "groupid" "height"
   "last_name" "managerid" "married")
 
 (deftest :fddl/attributes/2
                     (clsql:list-attribute-types [employee]
                                                :owner *test-database-user*))
             #'string<))
-  "birthday" "companyid" "email" "emplid" "first_name" "groupid" "height"
+  "bd_utime" "birthday" "ecompanyid" "email" "emplid" "first_name" "groupid" "height"
   "last_name" "managerid" "married")
 
+;; Attribute types are vendor specific so need to test a range
+(deftest :fddl/attributes/3
+    (and (member (clsql:attribute-type [emplid] [employee]) '(:int :integer :int4 :number)) t)
+  t)
+
+(deftest :fddl/attributes/4
+    (multiple-value-bind (type length scale nullable)
+       (clsql:attribute-type [first-name] [employee])
+      (values (clsql-sys:in type :varchar :varchar2) length scale nullable))
+  t 30 nil 1)
+
+(deftest :fddl/attributes/5
+    (and (member (clsql:attribute-type [birthday] [employee]) '(:datetime :timestamp :date)) t)
+  t)
+
+(deftest :fddl/attributes/6
+    (and (member (clsql:attribute-type [height] [employee]) '(:float :float8 :number)) t)
+  t)
+
+(deftest :fddl/attributes/7
+    (and (member (clsql:attribute-type [bd_utime] [employee]) '(:bigint :int8 :char)) t)
+  t)
+
+
 ;; create a view, test for existence, drop it and test again
 (deftest :fddl/view/1
     (progn (clsql:create-view [lenins-group]
   t nil)
   
   ;; create a view, list its attributes and drop it 
-(when (clsql-base-sys:db-type-has-views? *test-database-underlying-type*)
+(when (clsql-sys:db-type-has-views? *test-database-underlying-type*)
   (deftest :fddl/view/2
       (progn (clsql:create-view [lenins-group]
                                :as [select [first-name] [last-name] [email]
       (apply #'values result))
   t t t)
 
-;; test list-table-indexes
+;; test list-indexes with keyword :ON 
 (deftest :fddl/index/3
     (progn
-      (clsql:execute-command "CREATE TABLE I3TEST (a char(10), b integer)")
-      (clsql:create-index [bar] :on [i3test] :attributes
-                         '([a]) :unique t)
+      (clsql:create-table [i3test] '(([a] (string 10))
+                                    ([b] integer)))
       (clsql:create-index [foo] :on [i3test] :attributes
-                         '([b]) :unique nil)
+       '([b]) :unique nil)
+      (clsql:create-index [bar] :on [i3test] :attributes
+       '([a]) :unique t)
       (values
-       
+       (clsql:table-exists-p [i3test])
+       (clsql:index-exists-p [foo])
+       (clsql:index-exists-p [bar])
        (sort 
        (mapcar 
         #'string-downcase
-        (clsql:list-table-indexes [i3test] :owner *test-database-user*))
-           #'string-lessp)
-       (sort (clsql:list-table-indexes [company] :owner *test-database-user*)
-            #'string-lessp)
+        (clsql:list-indexes :on [i3test] :owner *test-database-user*))
+       #'string-lessp)
        (progn
         (clsql:drop-index [bar] :on [i3test])
         (clsql:drop-index [foo] :on [i3test])
-        (clsql:execute-command "DROP TABLE I3TEST")
+        (clsql:drop-table [i3test])
         t)))
-  ("bar" "foo") nil t)
+  t t t ("bar" "foo") t)
 
 ;; create an sequence, test for existence, drop it and test again 
 (deftest :fddl/sequence/1
         (clsql:drop-sequence [foo] :if-does-not-exist :ignore)))
   6)
 
+(deftest :fddl/big/1 
+    (let ((rows (clsql:select [*] :from [big] :field-names nil)))
+      (values
+       (length rows)
+       (do ((i 0 (1+ i))
+           (max (expt 2 60))
+           (rest rows (cdr rest)))
+          ((= i (length rows)) t)
+        (let ((index (1+ i))
+              (int (first (car rest)))
+              (bigint (second (car rest))))
+          (when (and (or (eq *test-database-type* :oracle)
+                         (and (eq *test-database-type* :odbc)
+                              (eq *test-database-underlying-type* :postgresql)))
+                     (stringp bigint))
+            (setf bigint (parse-integer bigint)))
+          (unless (and (eql int index)
+                       (eql bigint (truncate max index)))
+            (return nil))))))
+  555 t)
+
+          
 ))
 
 #.(clsql:restore-sql-reader-syntax-state)