;; Copyright 2000-2005 Ryan Culpepper
;; Released under the terms of the modified BSD license (see the file
;; COPYRIGHT for terms).

(module sql-types mzscheme
  (require (planet "test.ss" ("schematics" "schemeunit.plt" 1))
  (provide sql-types-test)
  (define sql-types-test
    (make-test-suite "SQL types"
      (make-test-suite "Parsing"
        (make-test-case "Parse boolean"
          (assert-eq? #t (sql-parse 'boolin "t"))
          (assert-eq? #f (sql-parse 'boolin "f"))
          (assert-exn (spgsql-error? exn:spgsql:user 'sql-parse)
                      (lambda () (sql-parse 'boolin "g"))))
        (make-test-case "Parse integer"
          (assert-equal? 0 (sql-parse 'int4in "0"))
          (assert-equal? 17 (sql-parse 'int4in "17"))
          (assert-exn (spgsql-error? exn:spgsql:user 'sql-parse)
                      (lambda () (sql-parse 'int4in "")))
          (assert-exn (spgsql-error? exn:spgsql:user 'sql-parse)
                      (lambda () (sql-parse 'int4in "alpha"))))
        (make-test-case "Parse fload"
          (assert-equal? 0.0 (sql-parse 'float4in "0.0"))
          (assert-equal? 17.123 (sql-parse 'float4in "17.123"))
          (assert-exn (spgsql-error? exn:spgsql:user 'sql-parse)
                      (lambda () (sql-parse 'float4in "")))
          (assert-exn (spgsql-error? exn:spgsql:user 'sql-parse)
                      (lambda () (sql-parse 'float4in "alpha"))))
        ;; Date parsing... when implemented
      (make-test-suite "Marshaling"
        (make-test-case "String formatting, unchanged"
          (assert-equal? "'this is the time'"
                         (sql-marshal 'string "this is the time"))
          (assert-equal? "'I am called... \"Tim\"'"
                         (sql-marshal 'text "I am called... \"Tim\"")))
        (make-test-case "String formatting, escaped quotes"
          (assert-equal? "'this is the \\'time\\''"
                         (sql-marshal 'string "this is the 'time'"))
          (assert-equal? "'nothing\\'s new under the sun'"
                         (sql-marshal 'string
                                      "nothing's new under the sun"))))))