hspec / hspec-hedgehog

A library to integrate hedgehog tests into your hspec test suite.
https://hackage.haskell.org/package/hspec-hedgehog
Other
28 stars 6 forks source link

Effectful Property Tests #19

Open parsonsmatt opened 2 years ago

parsonsmatt commented 2 years ago

I wrote Effectful Property Tests, which solved the issue nicely for me.

@avieth has notified me that I could make it even better by doing Gen (m (Test ()) instead of PropertyT IO (m (PropertyT IO ()) to get a more clear separation and intent on what's going on.

This suggests the following instance:

instance Example (Gen (SqlPersistT IO (Test ())) where
    type Arg (Gen (SqlPersistT IO (Test ()))) = SqlBackend

    evaluateExample example = 
        evaluateExample $ \conn -> do
            sqlAction <- forAll example
            testAction <- runReaderT sqlAction conn -- using runReaderT here because the transaction is rolled back in around
            test testAction -- delegate to `PropertyT IO ()` instance

And it also kinda makes sense to have an instance for functions, that uses a generator.

instance Example (a -> SqlPersistT IO (Test ()) where
    type Arg (a -> SqlPersistT IO (Test ())) = (SqlBackend, Gen a)

    evaluateExample mkSqlMkTest = 
        evaluateExample $ \(sqlBackend, gen) -> do
            a <- forAll gen
            testAction <- runReaderT (mkSqlMkTest a) sqlBackend
            test testAction

Which lets you share generators among effectful test cases.

But, you probably don't want all of the test cases to look like that, so it's probably better for there to also be an instance:

instance Example (a -> Gen (Sql (Test ()))) where
    type Arg (a -> Gen (Sql (Test ())) = (SqlBackend, Gen a)

    evaluateExample genSqlTest = 
        evaluateExample $ \(sqlBackend, gen) -> do
            a <- forAll gen
            sqlTest <- forAll $ genSqlTest a
            testAction <- runReaderT sqlTest sqlBackend
            test testAction

which would let you generate more stuff in the thing itself.