Sunday, 30 January 2022

How to write clean tests on model with database access

I'm using SQLAlchemy + Ormar and I want to write clean tests as it possible to write with pytest-django:

import pytest
@pytest.mark.django_db
def test_user_count():
    assert User.objects.count() == 0

I'm using FastAPI and not using Django at all so decorator as above isn't possible to use.

How to write clean tests on model with Database access as above but not with Django. It would be great to have that infrastructure for SQLAlchemy + Ormar but changing ORM is an option too.

Example of model to test:

class User(ormar.Model):
    class Meta:
        metadata = metadata
        database = database

    id: int = ormar.BigInteger(primary_key=True)
    phone: str = ormar.String(max_length=100)
    account: str = ormar.String(max_length=100)


from How to write clean tests on model with database access

No comments:

Post a Comment