Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Effectful Property Tests #19

Open
parsonsmatt opened this issue Apr 15, 2022 · 0 comments
Open

Effectful Property Tests #19

parsonsmatt opened this issue Apr 15, 2022 · 0 comments

Comments

@parsonsmatt
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant