When writing tests for my FastAPI endpoints I am not sure which is the best way to setup the test (store data in the DB, prepare data for the body of a POST call, etc.).
I can either use:
- direct calls to the DB to set it up (SQL)
- methods from my Repositories (Repository Pattern)
- other endpoints to get tokens, prepare the DB, and so on.
After doing the above cases, I can finally call the endpoint I'm testing and see if I get the error codes, response messages, data, that I expect.
Using SLQ (or an ORM) to setup the test feels problematic, since I have to replicate complex queries, effectively 'copying' code from the Repositories. Using other endpoints has the drawback that my tests are dependent on code that they aren't testing (it does however make some tests shorter). Or should I mix high level and low level code to do the test setup?
Picture below summarizes the question: