(load-relative "../couchdb.scm") (use test http-client intarweb uri-common) (import couchdb) (define (simple-request method uri) (condition-case (with-input-from-request (make-request method: method uri: uri) values values) (exn (client-error) #f))) (server (uri-reference "http://localhost:5984/")) (database "chicken-test-couchdb") ;; (Re)create the test database (let ([uri (update-uri (server) path: `(/ ,(database)))]) (simple-request 'DELETE uri) (simple-request 'PUT uri)) (test-group "server API" (test-assert "reporting version" (json-ref 'version (get-server-info)))) (test-group "database API" (test-assert "creating" (begin (simple-request 'DELETE (update-uri (server) path: '(/ "chicken-test-couchdb-foo"))) (create-database "chicken-test-couchdb-foo"))) (test-assert "deleting" (begin (simple-request 'PUT (update-uri (server) path: '(/ "chicken-test-couchdb-foo"))) (delete-database "chicken-test-couchdb-foo"))) (test "getting information about it" "chicken-test-couchdb" (json-ref 'db_name (get-database-info)))) (test-group "document API" (let ([body (alist->hash-table '((foo . "bar")))]) (test "accessing attributes" "bar" (document-attribute (make-document body: body) 'foo)) (test-assert "creating" (document-id (save-document (make-document body: body)))) (test "creating with given id" "foo" (document-id (save-document (make-document id: "foo")))) (test "retrieving" "bar" (let ([id (document-id (save-document (make-document body: body)))]) (document-attribute (get-document id) 'foo))) (test-assert "deleting" (let* ([doc (save-document (make-document))]) (delete-document doc) (not (get-document (document-id doc))))) (test "updating" "baz!" (let* ([doc (save-document (make-document body: body))] [doc (save-document (update-document doc body: (alist->hash-table '((foo . "baz!")))))]) (document-attribute (get-document (document-id doc)) 'foo)))) (test-group "errors" (parameterize ([last-error #f]) (test-assert "retrieving non-existant document" (and (not (get-document "bogus")) (last-error)))) (parameterize ([last-error #f]) (test-assert "deleting non-existant document" (and (not (delete-document "something")) (last-error))))))