Skip to content

Commit 968e9ba

Browse files
author
Arjun Guha
committed
minor new test case
1 parent b73917a commit 968e9ba

File tree

1 file changed

+72
-5
lines changed

1 file changed

+72
-5
lines changed

src/Database/CouchDB/Tests.hs

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,88 @@
1-
module Database.CouchDB.Tests where
1+
{-# OPTIONS_GHC -XFlexibleInstances #-}
2+
module Database.CouchDB.Tests ( main, allTests) where
23

34
import Test.HUnit
45
import Database.CouchDB
6+
import Database.CouchDB.JSON
7+
import Text.JSON
8+
9+
-- ----------------------------------------------------------------------------
10+
-- Helper functions
11+
--
512

613
assertDBEqual :: (Eq a, Show a) => String -> a -> CouchMonad a -> Assertion
714
assertDBEqual msg v m = do
815
v' <- runCouchDB' m
916
assertEqual msg v' v
1017

11-
testCreate = TestCase $ assertDBEqual "create/drop database" True $ do
12-
createDB "test1"
13-
dropDB "test1" -- returns True since the database exists.
18+
instance Assertable (Either String a) where
19+
assert (Left s) = assertFailure s
20+
assert (Right _) = return ()
21+
22+
assertRight :: (Either String a) -> IO a
23+
assertRight (Left s) = assertFailure s >> fail "assertion failed"
24+
assertRight (Right a) = return a
25+
26+
instance Assertable (Maybe a) where
27+
assert Nothing = assertFailure "expected (Just ...), got Nothing"
28+
assert (Just a) = return ()
1429

15-
allTests = TestList [ testCreate ]
30+
assertJust :: Maybe a -> IO a
31+
assertJust (Just v) = return v
32+
assertJust Nothing = do
33+
assertFailure "expected (Just ...), got Nothing"
34+
fail "assertion failed"
1635

1736
main = do
1837
putStrLn "Running CouchDB test suite..."
1938
runTestTT allTests
2039
putStrLn "Testing complete."
2140
return ()
41+
42+
-- -----------------------------------------------------------------------------
43+
-- Data definitions for testing
44+
--
45+
46+
data Age = Age
47+
{ ageName :: String
48+
, ageValue :: Int
49+
} deriving (Eq,Show)
50+
51+
instance JSON Age where
52+
53+
showJSON (Age name val) = JSObject $ toJSObject
54+
[ ("name", showJSON name)
55+
, ("age", showJSON val)
56+
]
57+
58+
readJSON val = do
59+
obj <- jsonObject val
60+
name <- jsonField "name" obj
61+
age <- jsonField "age" obj
62+
return (Age name age)
63+
64+
-- ----------------------------------------------------------------------------
65+
-- Test cases
66+
--
67+
68+
69+
testCreate = TestCase $ assertDBEqual "create/drop database" True $ do
70+
createDB "test1"
71+
dropDB "test1" -- returns True since the database exists.
72+
73+
people = [ Age "Arjun" 18, Age "Alex" 17 ]
74+
75+
testNamedDocs = TestCase $ assertDBEqual "add named documents" people $ do
76+
createDB "test1"
77+
let mydb = db "test1"
78+
newNamedDoc mydb (doc "arjun") (people !! 0)
79+
newNamedDoc mydb (doc "alex") (people !! 1)
80+
Just (_,_,v1) <- getDoc mydb (doc "arjun")
81+
Just (_,_,v2) <- getDoc mydb (doc "alex")
82+
dropDB "test1"
83+
return [v1, v2]
84+
85+
86+
87+
allTests = TestList [ testCreate, testNamedDocs ]
88+

0 commit comments

Comments
 (0)