1

i'm a haskell noob and have problems with testing functions with acid-states.This ist my Datastructure

data UserState = UserState { name :: String }
    deriving (Eq, Ord, Read, Show, Data, Typeable)

and this is the function i want to test:

setName :: String -> Update UserState String                  
setName n =      
    do c@UserState{..} <- get 
       let newName = n 
       put $ c { name = newName } 
       return newName
$(makeAcidic ''UserState ['setName ])

This is my test:

spec :: Spec
spec = do
  describe "test" $
    it "test" $ do
            setName "Mike" `shouldBe` UserState{ name = "Mike"}

I have no idea how to model my expected values. UserState{ name = "Mike"} doesn't work

alex.b
  • 184
  • 1
  • 2
  • 15
  • I don't know what you want to test. If I wanted to test the `setName` behaviour, I would apply the update (using the `update` function), then query the new `UserState` and compare it with my expected `UserState` – Jean-Baptiste Potonnier Feb 22 '17 at 22:23
  • @Jean-BaptistePotonnier But how do i modell my expected UserState? Something like this? "UserState{ name = "Mike"}" – alex.b Feb 27 '17 at 18:18

1 Answers1

1

I don't think you can access the database state without querying for it. So you need to add a query to ask for your database state, for example like this:

getUserState :: Query UserState UserState
getUserState = ask

Then it is possible to write a test like this :

withDatabaseConnection :: (AcidState UserState -> IO ()) -> IO ()
withDatabaseConnection = 
    bracket (openLocalState UserState{name = "initial name"}) 
            closeAcidState

spec :: Spec
spec = do
    around withDatabaseConnection $ do
        describe "test" $
            it "test" $ \c -> do
                _ <- update c (SetName "Mike") 
                userState <- query c GetUserState
                userState `shouldBe` UserState{ name = "Mike"}
  • Thank you. I have one more comment: "openLocalState" takes the UserState from harddisk and not the passed Userstate. Data.acid documentation describe in detail: "Initial state value. This value is only used if no checkpoint is found." – alex.b Mar 09 '17 at 20:48
  • @boddAh234 I guess you could delete the file before or after each test, to better control the database state during tests. – Jean-Baptiste Potonnier Mar 11 '17 at 15:35