X-Git-Url: http://git.kpe.io/?p=clsql.git;a=blobdiff_plain;f=tests%2Ftest-fdml.lisp;h=0286b2d807a0034efcb6831b7f1a9d98f291f00c;hp=541b4d4e28e16bd40b2d91f687b24669250aa59c;hb=8a8ee2d7d791b7a3efaed06420802a925d16fca3;hpb=73cf858d596ad1d51c745b478292433617cf9d72 diff --git a/tests/test-fdml.lisp b/tests/test-fdml.lisp index 541b4d4..0286b2d 100644 --- a/tests/test-fdml.lisp +++ b/tests/test-fdml.lisp @@ -4,26 +4,30 @@ ;;;; Author: Marcus Pearce ;;;; Created: 30/03/2004 ;;;; Updated: $Id$ -;;;; ====================================================================== -;;;; -;;;; Description ========================================================== -;;;; ====================================================================== ;;;; ;;;; Tests for the CLSQL Functional Data Manipulation Language ;;;; (FDML). -;;;; +;;;; +;;;; This file is part of CLSQL. +;;;; +;;;; 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) #.(clsql:locally-enable-sql-reader-syntax) +(setq *rt-fdml* + '( + ;; inserts a record using all values only and then deletes it (deftest :fdml/insert/1 (progn (clsql:insert-records :into [employee] :values `(11 1 "Yuri" "Gagarin" "gagarin@soviet.org" - 1 1 1.85 t ,(clsql-base:get-time))) + 1 1 1.85 t ,(clsql:get-time))) (values (clsql:select [first-name] [last-name] [email] :from [employee] :where [= [emplid] 11]) @@ -36,7 +40,7 @@ (progn (clsql:insert-records :into [employee] :attributes '(emplid groupid first_name last_name - email companyid managerid) + email ecompanyid managerid) :values '(11 1 "Yuri" "Gagarin" "gagarin@soviet.org" 1 1)) (values @@ -54,7 +58,7 @@ (first_name "Yuri") (last_name "Gagarin") (email "gagarin@soviet.org") - (companyid 1) (managerid 1))) + (ecompanyid 1) (managerid 1))) (values (clsql:select [first-name] [last-name] [email] :from [employee] :where [= [emplid] 11]) @@ -124,17 +128,57 @@ (("Vladamir" "Lenin" "lenin@soviet.org"))) +;; Computed values are not always classified as numeric by psqlodbc (deftest :fdml/query/1 - (clsql:query "SELECT COUNT(*) FROM EMPLOYEE WHERE (EMAIL LIKE '%org')") - (("10"))) + (let ((count (caar (clsql:query "SELECT COUNT(*) FROM EMPLOYEE WHERE (EMAIL LIKE '%org')" :field-names nil)))) + (if (stringp count) + (nth-value 0 (parse-integer count)) + count)) + 10) (deftest :fdml/query/2 - (clsql:query - "SELECT FIRST_NAME,LAST_NAME FROM EMPLOYEE WHERE (EMPLID <= 5) ORDER BY LAST_NAME") + (multiple-value-bind (rows field-names) + (clsql:query + "SELECT FIRST_NAME,LAST_NAME FROM EMPLOYEE WHERE (EMPLID <= 5) ORDER BY LAST_NAME") + (values rows (mapcar 'string-upcase field-names))) (("Leonid" "Brezhnev") ("Nikita" "Kruschev") ("Vladamir" "Lenin") - ("Josef" "Stalin") ("Leon" "Trotsky"))) + ("Josef" "Stalin") ("Leon" "Trotsky")) + ("FIRST_NAME" "LAST_NAME")) +(deftest :fdml/query/3 + (caar (clsql:query "SELECT EMPLID FROM EMPLOYEE WHERE LAST_NAME = 'Andropov'" :field-names nil)) + 6) +(deftest :fdml/query/4 + (typep (caar (clsql:query "SELECT HEIGHT FROM EMPLOYEE WHERE LAST_NAME = 'Andropov'" :field-names nil)) + 'float) + t) + +(deftest :fdml/query/5 + (clsql:query (clsql:sql [select [first-name] [sum [emplid]] :from [employee]] + [group-by [first-name]] [order-by [sum [emplid]]]) + :field-names nil :result-types nil) + (("Josef" "2") ("Leon" "3") ("Nikita" "4") ("Leonid" "5") ("Yuri" "6") + ("Konstantin" "7") ("Mikhail" "8") ("Boris" "9") ("Vladamir" "11"))) + +(deftest :fdml/query/6 + (clsql:query (clsql:sql [union [select [emplid] :from [employee]] + [select [groupid] :from [company]]]) + :field-names nil :result-types nil :flatp t) + ("1" "2" "3" "4" "5" "6" "7" "8" "9" "10")) + +(deftest :fdml/query/7 + (clsql:query (clsql:sql [intersect [select [emplid] :from [employee]] + [select [groupid] :from [company]]]) + :field-names nil :result-types nil :flatp t) + ("1")) + +(deftest :fdml/query/8 + (clsql:query (clsql:sql [except [select [emplid] :from [employee]] + [select [groupid] :from [company]]]) + :field-names nil :result-types nil :flatp t) + ("2" "3" "4" "5" "6" "7" "8" "9" "10")) + (deftest :fdml/execute-command/1 (values (clsql:table-exists-p [foo] :owner *test-database-user*) @@ -148,93 +192,190 @@ ;; compare min, max and average hieghts in inches (they're quite short -;; these guys!) -- only works with pgsql +;; these guys!) (deftest :fdml/select/1 - (if (member *test-database-type* '(:postgresql-socket :postgresql)) - (let ((max (clsql:select [function "floor" - [/ [* [max [height]] 100] 2.54]] - :from [employee] - :flatp t)) - (min (clsql:select [function "floor" - [/ [* [min [height]] 100] 2.54]] - :from [employee] - :flatp t)) - (avg (clsql:select [function "floor" - [avg [/ [* [height] 100] 2.54]]] - :from [employee] - :flatp t))) - (apply #'< (mapcar #'parse-integer (append min avg max)))) - t) - t) + (let ((max (clsql:select [function "floor" + [/ [* [max [height]] 100] 2.54]] + :from [employee] + :result-types nil + :flatp t)) + (min (clsql:select [function "floor" + [/ [* [min [height]] 100] 2.54]] + :from [employee] + :result-types nil + :flatp t)) + (avg (clsql:select [function "floor" + [avg [/ [* [height] 100] 2.54]]] + :from [employee] + :result-types nil + :flatp t))) + (apply #'< (mapcar #'(lambda (s) (parse-integer s :junk-allowed t)) + (append min avg max)))) + t) (deftest :fdml/select/2 - (clsql:select [first-name] :from [employee] :flatp t :distinct t - :order-by [first-name]) - ("Boris" "Josef" "Konstantin" "Leon" "Leonid" "Mikhail" "Nikita" "Vladamir" - "Yuri")) + (clsql:select [first-name] :from [employee] :flatp t :distinct t + :field-names nil + :result-types nil + :order-by [first-name]) + ("Boris" "Josef" "Konstantin" "Leon" "Leonid" "Mikhail" "Nikita" "Vladamir" + "Yuri")) (deftest :fdml/select/3 (clsql:select [first-name] [count [*]] :from [employee] - :group-by [first-name] - :order-by [first-name]) - (("Boris" "1") ("Josef" "1") ("Konstantin" "1") ("Leon" "1") ("Leonid" "1") - ("Mikhail" "1") ("Nikita" "1") ("Vladamir" "2") ("Yuri" "1"))) + :result-types nil + :group-by [first-name] + :order-by [first-name] + :field-names nil) + (("Boris" "1") ("Josef" "1") ("Konstantin" "1") ("Leon" "1") ("Leonid" "1") + ("Mikhail" "1") ("Nikita" "1") ("Vladamir" "2") ("Yuri" "1"))) (deftest :fdml/select/4 - (clsql:select [last-name] :from [employee] :where [like [email] "%org"] - :order-by [last-name] - :flatp t) - ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin" - "Stalin" "Trotsky" "Yeltsin")) + (clsql:select [last-name] :from [employee] + :where [like [email] "%org"] + :order-by [last-name] + :field-names nil + :result-types nil + :flatp t) + ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin" + "Stalin" "Trotsky" "Yeltsin")) (deftest :fdml/select/5 - (clsql:select [email] :from [employee] :flatp t - :where [in [employee emplid] - [select [managerid] :from [employee]]]) + (clsql:select [email] :from [employee] :flatp t :result-types nil + :where [in [employee emplid] + [select [managerid] :from [employee]]] + :field-names nil) ("lenin@soviet.org")) (deftest :fdml/select/6 - (if (member *test-database-type* '(:postgresql-socket :postgresql)) - (mapcar #'parse-integer - (clsql:select [function "trunc" [height]] :from [employee] - :flatp t)) - (mapcar #'(lambda (s) (truncate (parse-integer s :junk-allowed t))) - (clsql:select [height] :from [employee] :flatp t))) - (1 1 1 1 1 1 1 1 1 1)) + (if (clsql-sys:db-type-has-fancy-math? *test-database-underlying-type*) + (mapcar #'(lambda (s) (parse-integer s :junk-allowed t)) + (clsql:select [function "trunc" [height]] :from [employee] + :result-types nil + :field-names nil + :flatp t)) + (mapcar #'(lambda (s) (truncate (parse-integer s :junk-allowed t))) + (clsql:select [height] :from [employee] :flatp t + :field-names nil :result-types nil))) + (1 1 1 1 1 1 1 1 1 1)) (deftest :fdml/select/7 - (sql:select [max [emplid]] :from [employee] :flatp t) + (clsql:select [max [emplid]] :from [employee] :flatp t + :field-names nil :result-types nil) ("10")) (deftest :fdml/select/8 - (sql:select [min [emplid]] :from [employee] :flatp t) + (clsql:select [min [emplid]] :from [employee] :flatp t + :field-names nil :result-types nil) ("1")) (deftest :fdml/select/9 - (subseq (car (sql:select [avg [emplid]] :from [employee] :flatp t)) 0 3) + (subseq + (car + (clsql:select [avg [emplid]] :from [employee] :flatp t + :field-names nil :result-types nil)) + 0 3) "5.5") (deftest :fdml/select/10 - (sql:select [last-name] :from [employee] - :where [not [in [emplid] - [select [managerid] :from [company]]]] - :flatp t - :order-by [last-name]) - ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Putin" "Stalin" - "Trotsky" "Yeltsin")) + (clsql:select [last-name] :from [employee] + :where [not [in [emplid] + [select [managerid] :from [company]]]] + :result-types nil + :field-names nil + :flatp t + :order-by [last-name]) + ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Putin" "Stalin" + "Trotsky" "Yeltsin")) (deftest :fdml/select/11 (clsql:select [last-name] :from [employee] :where [married] :flatp t - :order-by [emplid]) + :field-names nil :order-by [emplid] :result-types nil) ("Lenin" "Stalin" "Trotsky")) (deftest :fdml/select/12 (let ((v 1)) - (clsql:select [last-name] :from [employee] :where [= [emplid] v])) + (clsql:select [last-name] :from [employee] :where [= [emplid] v] + :field-names nil :result-types nil)) (("Lenin"))) +(deftest :fdml/select/13 + (multiple-value-bind (results field-names) + (clsql:select [emplid] [last-name] :from [employee] + :where [= [emplid] 1]) + (values results (mapcar #'string-downcase field-names))) + ((1 "Lenin")) + ("emplid" "last_name")) + +(deftest :fdml/select/14 + (floatp (car (clsql:select [height] :from [employee] :where [= [emplid] 1] + :flatp t))) + t) + +(deftest :fdml/select/15 + (multiple-value-bind (rows field-names) + (clsql:select [addressid] [street-number] [street-name] [city_field] [zip] + :from [addr] + :where [= 1 [addressid]]) + (values + rows + (mapcar #'string-downcase field-names))) + ((1 10 "Park Place" "Leningrad" 123)) + ("addressid" "street_number" "street_name" "city_field" "zip")) + +(deftest :fdml/select/16 + (clsql:select [emplid] :from [employee] :where [= 1 [emplid]] + :field-names nil) + ((1))) + +(deftest :fdml/select/17 + (clsql:select [emplid] [last-name] :from [employee] :where [= 1 [emplid]] + :field-names nil) + ((1 "Lenin"))) + +(deftest :fdml/select/18 + (clsql:select [emplid :string] [last-name] :from [employee] :where [= 1 [emplid]] + :field-names nil) + (("1" "Lenin"))) + +(deftest :fdml/select/19 + (clsql:select [emplid] :from [employee] :order-by [emplid] + :where [between [* [emplid] 10] [* 5 10] [* 10 10]] + :field-names nil :result-types nil :flatp t) + ("5" "6" "7" "8" "9" "10")) + +(deftest :fdml/select/20 + (clsql:select [emplid] :from [employee] :order-by [emplid] + :where [not [between [* [emplid] 10] [* 5 10] [* 10 10]]] + :field-names nil :result-types nil :flatp t) + ("1" "2" "3" "4")) + +(deftest :fdml/select/21 + (clsql:select [substr [first-name] 1 4] :from [employee] + :flatp t :order-by [emplid] :field-names nil) + ("Vlad" "Jose" "Leon" "Niki" "Leon" "Yuri" "Kons" "Mikh" "Bori" "Vlad")) + +(deftest :fdml/select/22 + (clsql:select [\|\| [first-name] " " [last-name]] :from [employee] + :flatp t :order-by [emplid] :field-names nil) + ("Vladamir Lenin" "Josef Stalin" "Leon Trotsky" "Nikita Kruschev" + "Leonid Brezhnev" "Yuri Andropov" "Konstantin Chernenko" "Mikhail Gorbachev" + "Boris Yeltsin" "Vladamir Putin")) + +(deftest :fdml/select/23 + (clsql:select [emplid] :from [employee] :where [in [emplid] '(1 2 3 4)] + :flatp t :order-by [emplid] :field-names nil + :result-types nil) + ("1" "2" "3" "4")) + +(deftest :fdml/select/24 + (clsql:select [distinct [first-name]] :from [employee] :flatp t + :order-by [first-name] :field-names nil :result-types nil) + ("Boris" "Josef" "Konstantin" "Leon" "Leonid" "Mikhail" "Nikita" "Vladamir" + "Yuri")) + ;(deftest :fdml/select/11 -; (sql:select [emplid] :from [employee] +; (clsql:select [emplid] :from [employee] ; :where [= [emplid] [any [select [companyid] :from [company]]]] ; :flatp t) ; ("1")) @@ -269,7 +410,21 @@ collect (concatenate 'string forename " " surname)) ("Yuri Andropov" "Leonid Brezhnev" "Konstantin Chernenko" "Mikhail Gorbachev" "Nikita Kruschev" "Vladamir Lenin" "Vladamir Putin" - "Josef Stalin" "Leon Trotsky" "Boris Yeltsin")) + "Josef Stalin" "Leon Trotsky" "Boris Yeltsin")) + +(deftest :fdml/loop/2 + (loop for (addressid) + being each tuple in + [select [addressid] :from [addr] :order-by [addressid]] + collect addressid) + (1 2)) + +(deftest :fdml/loop/3 + (loop for addressid + being each tuple in + [select [addressid] :from [addr] :order-by [addressid]] + collect addressid) + (1 2)) ;; starts a transaction deletes a record and then rolls back the deletion (deftest :fdml/transaction/1 @@ -281,8 +436,7 @@ ;; test if we are in a transaction (push (clsql:in-transaction-p) results) ;;Putin has got to go - (unless (eql *test-database-type* :mysql) - (clsql:delete-records :from [employee] :where [= [last-name] "Putin"])) + (clsql:delete-records :from [employee] :where [= [last-name] "Putin"]) ;;Should be nil (push (clsql:select [*] :from [employee] :where [= [last-name] "Putin"]) @@ -308,10 +462,9 @@ ;; test if we are in a transaction (push (clsql:in-transaction-p) results) ;;Putin has got to go - (unless (eql *test-database-type* :mysql) - (clsql:update-records [employee] - :av-pairs '((email "putin-nospam@soviet.org")) - :where [= [last-name] "Putin"])) + (clsql:update-records [employee] + :av-pairs '((email "putin-nospam@soviet.org")) + :where [= [last-name] "Putin"]) ;;Should be new value (push (clsql:select [email] :from [employee] :where [= [last-name] "Putin"] @@ -360,7 +513,7 @@ :flatp t) results) (apply #'values (nreverse results))) - nil :COMMITTED nil ("lenin-nospam@soviet.org") :COMMITTED + nil :committed nil ("lenin-nospam@soviet.org") :committed nil ("lenin@soviet.org")) ;; runs a valid update and an invalid one within a transaction and checks @@ -369,19 +522,18 @@ (let ((results '())) ;; check status (push (clsql:in-transaction-p) results) - (unless (eql *test-database-type* :mysql) - (handler-case - (clsql:with-transaction () - ;; valid update - (clsql:update-records [employee] - :av-pairs '((email "lenin-nospam@soviet.org")) - :where [= [emplid] 1]) - ;; invalid update which generates an error + (handler-case + (clsql:with-transaction () + ;; valid update + (clsql:update-records [employee] + :av-pairs '((email "lenin-nospam@soviet.org")) + :where [= [emplid] 1]) + ;; invalid update which generates an error (clsql:update-records [employee] - :av-pairs - '((emale "lenin-nospam@soviet.org")) - :where [= [emplid] 1])) - (clsql:clsql-sql-error () + :av-pairs + '((emale "lenin-nospam@soviet.org")) + :where [= [emplid] 1])) + (clsql:clsql-error () (progn ;; check status (push (clsql:in-transaction-p) results) @@ -389,7 +541,9 @@ (push (clsql:select [email] :from [employee] :where [= [emplid] 1] :flatp t) results) - (apply #'values (nreverse results))))))) + (apply #'values (nreverse results)))))) nil nil ("lenin@soviet.org")) +)) + #.(clsql:restore-sql-reader-syntax-state)