#lang scheme
(require (planet schematics/schemeunit))
(require "main.ss")
(define
tests
(test-suite
"wrapper"
(let ([c (connect #:dbname "test" #:user "www" #:port 5433)])
(send c initialize)
(test-case
"does exec work?"
(send c exec "SELECT 42"))
(send c exec "DROP TABLE IF EXISTS test_wrapper")
(test-case
"create table"
(send c exec "CREATE TABLE test_wrapper (foo INTEGER, bar TEXT)"))
(test-case
"prepared statements"
(check-equal? (send (send c p-exec "SELECT $1" 42) get-matrix) '((42))))
(test-case
"results finalize properly"
(collect-garbage))
(test-case
"inserts"
(with-transaction
c
(let ([prepared (send c prepare "INSERT INTO test_wrapper (foo,bar) VALUES ($1,$2)" '(256 ""))])
(send prepared exec 1 "synx") (send prepared exec 4 "ferret")
(send prepared exec 2 "rat")
(send prepared exec 2 "glider")
(send prepared exec 23 "ringtail"))))
(test-case
"selects"
(send (send c p-exec "SELECT foo,bar FROM test_wrapper WHERE foo=$1" 23) for-each
(λ (foo bar)
(check-equal? foo 23)
(check-equal? bar "ringtail")))
(send (send c p-exec "SELECT foo,bar FROM test_wrapper WHERE bar=$1" "ferret") for-each
(λ (foo bar)
(check-equal? foo 4)
(check-equal? bar "ferret")))
(let ([values null])
(send (send c p-exec "SELECT foo FROM test_wrapper ORDER BY foo") for-each
(λ (foo) (set! values (cons foo values))))
(check-equal? values '(23 4 2 2 1))))
(test-case
"cursors"
(with-transaction
c
(let ([cursor (send c select "SELECT bar FROM test_wrapper ORDER BY foo")])
(let ([results null])
(send cursor for-each
(λ (bar)
(set! results (cons bar results))
(sleep 0.1)))
(check-equal? results '("ringtail" "ferret" "glider" "rat" "synx"))))))
(test-case
"updates"
(let ([prepared (send c prepare "UPDATE test_wrapper SET bar=$2 WHERE foo=$1" '(0 ""))])
(send prepared exec 2 "zebra")
(send prepared exec 23 "lemur")))
(test-case
"verify updates"
(check-equal?
(send (send c p-exec "SELECT foo,bar FROM test_wrapper ORDER BY bar") get-matrix)
'((4 "ferret") (23 "lemur") (1 "synx") (2 "zebra") (2 "zebra")))))))
(provide tests)