|
1 | | -module Database.CouchDB.Tests where |
| 1 | +{-# OPTIONS_GHC -XFlexibleInstances #-} |
| 2 | +module Database.CouchDB.Tests ( main, allTests) where |
2 | 3 |
|
3 | 4 | import Test.HUnit |
4 | 5 | import Database.CouchDB |
| 6 | +import Database.CouchDB.JSON |
| 7 | +import Text.JSON |
| 8 | + |
| 9 | +-- ---------------------------------------------------------------------------- |
| 10 | +-- Helper functions |
| 11 | +-- |
5 | 12 |
|
6 | 13 | assertDBEqual :: (Eq a, Show a) => String -> a -> CouchMonad a -> Assertion |
7 | 14 | assertDBEqual msg v m = do |
8 | 15 | v' <- runCouchDB' m |
9 | 16 | assertEqual msg v' v |
10 | 17 |
|
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 () |
14 | 29 |
|
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" |
16 | 35 |
|
17 | 36 | main = do |
18 | 37 | putStrLn "Running CouchDB test suite..." |
19 | 38 | runTestTT allTests |
20 | 39 | putStrLn "Testing complete." |
21 | 40 | 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