sanic-org / sanic-testing

Test clients for Sanic

Home Page:https://sanic.dev/en/plugins/sanic-testing/getting-started.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to create test databases and connect them to the test Sanic app?

Maksim-Burtsev opened this issue · comments

commented

I am trying to test app with TortoiseORM and i dont understand what is the right way to setup my test application in fixture.

I was tried to make with listeners and fixtures but i am always how problems with event loop, or tortoise can't connect to the database, or fixture doesnt work and etc. And i can't find any example on stackoverflow/github with testing Sanic with tortoise and test databases.

Can you show me the best way to setup tests for testing Sanic & TortoiseORM on the test db (e.g. sqlite) ?

p.s I think i can use sqlite:///:memory for

I cannot advise on any best practices with an ORM, you will have to turn to them for that.

GENERALLY, I would say your testing should NOT hit any database. Instead you should use mocking to retrieve fake data in your tests. When you have to start incorporating networking calls and state management into tests it gets pretty complex quickly.

I cover some testing strategies specifically dealing with DB interactions in my book. Generally, I just point people to the companion code in GH, but I think the concepts you are aiming for are much more complex and you really should look deeper into the topic of testing. IMO the problem is in strategy not implementation.

Need to understand the working of sanic-testing, specifically app running part, when we provide an app fixture which typically has our routes/blueprints and middlewares and maybe listeners, are listeners not executed when it "runs" the app?