Following up on my 'this Q is going to be closed', I believe there's no single authoritative way of doing it, and you need to make a choice on how you want to mock things up.
Basically, SQLAlchemy is an abstraction of the DB design and the query. So that means, you can place your mockups at different position in the abstraction:
- real db, with test data
you can setup an sqlite instance. The advantage of this solution is that you don't need to tweak/monkey patch anything in the python stack because you're changing stuff on the frontier of your system, the downside is that it's more work to maintain, and thus more risk.
and store those data files in your code as resources. Then you load the right db for the test you're running. You'll need a tearDown()
method to restore the db in its original state after the tests.
The plus side is that it's easy to implement with little code, but it's a hell of work if you amend your model.
- with an in-memory database
There you populate your data in the setUp()
method, and have no db to store in your git or to take care of outside of your code.
Though, you still have to maintain a lot of setUp()
code.
cf that answer
That strategy would be the most commonly used and advised one.
- mock db access
Another way, that's being often used for mocking up HTTP connections, it's to serialise the output, and inputs of the communications between SA and the database. I'm not aware of a library doing that, but that would be a brilliant thing to implement.
Basically, just imagine a JSON file that contains all the requests sent to, and data returned from, the database. Each time you play your test, you're just replaying that JSON record.
When you make changes, you can record the I/O interactions and serialize them. A bit like the betamax library.
That strategy would be my favourite way of implementing it, but someone has to write the implementation for such a tool!
- mock/monkey patch individual elements of sqlalchemy
that would be painful and a lot of work, still many people try that approach. But I cannot think of a good reason to do that, as your goal is not to test sqlalchemy, but your own code, and you'd end up mocking up large parts of SQLAlchemy.
- abstraction of all db interactions in your code
if all db interaction is being abstracted by an interface in your own code, you can just replace the implementation of that interface with a mockup one and assert that you get the right parameters. The plus side is that it'd be easy to test and maintain. Though, I wouldn't advise to do that, because then you wouldn't check that you're using the SQLAlchemy library correctly.