Comments (3)
OK, I found initializer
and finalizer
pattern in the documentation. I think there should be "Testing" tab in the main page. It should have two levels. First explain basics for each main python test framework (pytest, unittest, etc.) with full working examples, how to write an empty test. Then second level on how to integrate with each individual framework (FastAPI, etc.). It should have very basic examples, also including FastAPI websockets.
Anyway, I got back to a place where I try to create an empty test case, this time armed with initialzier and finalizer methods. I have this:
from tortoise.contrib import test
from tortoise.contrib.test import initializer, finalizer
class MyTest(test.TestCase):
@classmethod
def setUpClass(cls):
initializer(
modules=["db.models"],
db_url="psycopg://my_user:[email protected]:5432/test_mydb",
)
@classmethod
def tearDownClass(cls):
finalizer()
async def test_nothing(self):
pass
On the DB I ran
CREATE USER 'my_user' with password 'my_pass'
ALTER USER 'my_user' superuser ---> this is so it can drop/create the DB
I also created an empty DB with name test_mydb
When I run the above, I'm getting
psycopg.errors.ObjectInUse: cannot drop the currently open database
from db_delete method in base_postgres.client()
from tortoise-orm.
I made it!
Apparently, one can't use postgres for the test database. When using default which apparently is sqlite://memory
I finally got my empty test to work.
The final version which is working:
from tortoise.contrib import test
from tortoise.contrib.test import initializer, finalizer
class MyTest(test.TestCase):
@classmethod
def setUpClass(cls):
initializer(
modules=["db.models"]
)
@classmethod
def tearDownClass(cls):
finalizer()
async def test_nothing(self):
pass
from tortoise-orm.
Hi!
Sad to see you struggling like that
Good that you made it work with sqlite, although I think it should work with postgres too.
You issue may be because tortoise initializer/finalizer designed in way, that create and destroy database, so it can fail in the moment of it's creation/deletion.
In your case seems like something else is connected to db in moment of delete, not sure what is it, but maybe you can troubleshoot that on your side, or at least narrow down why it's tortoise fault here
In our internal testcase we use this pytest fixture:
@pytest.fixture(scope="session", autouse=True)
def initialize_tests(request):
# Reduce the default timeout for psycopg because the tests become very slow otherwise
try:
from tortoise.backends.psycopg import PsycopgClient
PsycopgClient.default_timeout = float(os.environ.get("TORTOISE_POSTGRES_TIMEOUT", "15"))
except ImportError:
pass
db_url = os.environ.get("TORTOISE_TEST_DB", "sqlite://:memory:")
initializer(["tests.testmodels"], db_url=db_url)
request.addfinalizer(finalizer)
And it works with all databases that in our testcase
If you have ideas how to improve documentation - I will be glad to accept PR with improvements on this part
from tortoise-orm.
Related Issues (20)
- Support symmetrical ManyToMany relationships HOT 2
- Bulk Create without returning objects HOT 3
- OperationalError when attempting to filter by annotated field in m2m relation HOT 1
- Refactoring pydantic_model_creator for Enhanced Code Maintainability and Reliability HOT 2
- 0.21.0 throws "IntegrityError: NOT NULL constraint failed" due to usage of 'pk' to indicate primary key HOT 3
- max recursion bug
- Feature: expose `timezone` and `use_tz` params in RegisterTortoise class HOT 5
- OperationalError while bulk updating the tortoise model. HOT 1
- [BUG] get_or_create function raises "Object does not exist" HOT 1
- DoesNotExist can be more useful HOT 2
- There is an entry for table "table", but it cannot be referenced from this part of the query HOT 2
- Unable to run unit tests using tortoise-orm initializer() with a postgres database HOT 6
- Is there a django similar scheme to generate model files based on the structure of the existing database python manage.py inspectdb > models.py HOT 1
- unique indexes (for unique_together) on nullable columns need to be optionable for nulls not distinct HOT 3
- [Bug] Failed to set schema for models.Model
- Custom generated primary key (Snowflake) ignored, a serial is saved in the backend HOT 1
- Have you considered allowing Tortoise to be independently initialized multiple times in different packages? HOT 3
- Generic relation field HOT 1
- Race condition bug in ConnectionWrapper HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tortoise-orm.