Giter Site home page Giter Site logo

Comments (5)

collerek avatar collerek commented on May 18, 2024 1

OK,

I wanted to make sure that pre-save signal receive the model with populated defaults and overdid it, sorry :)

Should be fixed in 0.7.1.

Btw. When you want pytest fixture to run only once for whole module (like db creation) you cen set scope=module option in fixture decorator

@pytest.fixture(autouse=True, scope="module")
def create_test_database():
    engine = create_engine(DATABASE_URL)
    metadata.create_all(engine)
    yield
    metadata.drop_all(engine)

That way you don't have to pass the fixture around to test functions.

Also now you run against sqlite but if you intend to run against mysql or postgress you need to actually explicitly connect in test - best as a context manager with nomen omen with ;)

@pytest.mark.asyncio
async def test_model_relationship():
    async with db:
        async with db.transaction(force_rollback=True):
            # rest of the test

It does not affect sqlite buf fixes test for other backends.

from ormar.

collerek avatar collerek commented on May 18, 2024

Can you share whole sample so I can reproduce?

from ormar.

soderluk avatar soderluk commented on May 18, 2024
import databases
import ormar
import pytest
import sqlalchemy as sa


db_url = "sqlite:///./test.db"
engine = sa.create_engine(db_url, connect_args={"check_same_thread": False})
metadata = sa.MetaData(engine)
db = databases.Database(db_url)


class Category(ormar.Model):
    class Meta:
        tablename = "categories"
        metadata = metadata
        database = db

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=50, unique=True, index=True)
    code: int = ormar.Integer()
    
class Workshop(ormar.Model):
    class Meta:
        tablename = "workshops"
        metadata = metadata
        database = db

    id: int = ormar.Integer(primary_key=True)
    topic: str = ormar.String(max_length=255, index=True)
    category: Union[ormar.Model, Category] = ormar.ForeignKey(
        Category, related_name="workshops", nullable=False
    )

@pytest.fixture
def create_test_database():
    metadata.create_all(engine)
    yield
    metadata.drop_all(engine)

@pytest.mark.asyncio
async def test_model_relationship(create_test_database):
    c = await Category(name="Foo", code=123).save()
    ws = await Workshop(topic="Topic 1", category=c).save()
    
    assert ws.id == 1
    assert ws.topic == "Topic 1"
    assert ws.category.name == "Foo"

I found out that adding await ws.category.load() fixes the issue, but I think that's a bit redundant to have to do a separate load right after saving the model.

from ormar.

soderluk avatar soderluk commented on May 18, 2024

Does this fix the same issue with the .update() as well? It seems like it's also there. When running model.update() I have to "reload" the related models as well.

from ormar.

collerek avatar collerek commented on May 18, 2024

This passes for me:

@pytest.mark.asyncio
async def test_model_relationship():
    async with db:
        async with db.transaction(force_rollback=True):
            cat = await Category(name="Foo", code=123).save()
            ws = await Workshop(topic="Topic 1", category=cat).save()

            assert ws.id == 1
            assert ws.topic == "Topic 1"
            assert ws.category.name == "Foo"

            ws.topic = 'Topic 2'
            await ws.update()

            assert ws.id == 1
            assert ws.topic == "Topic 2"
            assert ws.category.name == "Foo"

So let me know if you still have the same problem.

from ormar.

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.