Easiest way to interact with database in unit tests?

What I’m trying to do:
I’m more or less trying to replicate the testing flow that factory_boy and django provide, where you can use factories to churn out new database row objects at will into a test database. At the end of test execution, the test database should be automatically destroyed.
For example, let’s say I want to test that a function’s behavior in the case of a user having or not having some permissions. Per my own implementation, I would need to create rows in my User, Roles, and Permissions tables to test the functionality, and then I’d need those rows to be deleted.

What I’ve tried and what’s not working:
I honestly don’t even know where to start. I’ve tried checking the forums for any answers, found none. The only strategy I could think of so far, which is a bit messy and painful, is to manually spin up test tables and then delete them when done testing. The flow I described in the section above is far simpler and more robust.

1 Like

This still requires some manual work to initialize everything (one time only), but you could create a test environment that is tied to your unit test and easily delete all rows after your test

1 Like

Being able to do this sort of thing was one of the reasons I created amoni

I do that sort of testing locally using amoni and its docker containers - dropping a db just means dropping the container.

1 Like