Giter Club home page Giter Club logo

Comments (3)

PawelRoman avatar PawelRoman commented on June 24, 2024

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.

PawelRoman avatar PawelRoman commented on June 24, 2024

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.

abondar avatar abondar commented on June 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.