add exclude to save_related method, switch to same relation_map from iter
This commit is contained in:
@ -91,6 +91,14 @@ async def test_saving_related_fk_rel():
|
||||
assert count == 1
|
||||
assert comp.hq.saved
|
||||
|
||||
comp.hq.name = "Suburbs 2"
|
||||
assert not comp.hq.saved
|
||||
assert comp.saved
|
||||
|
||||
count = await comp.save_related(exclude={"hq"})
|
||||
assert count == 0
|
||||
assert not comp.hq.saved
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_saving_many_to_many():
|
||||
@ -123,6 +131,16 @@ async def test_saving_many_to_many():
|
||||
assert hq.nicks[0].saved
|
||||
assert hq.nicks[1].saved
|
||||
|
||||
hq.nicks[0].name = "Kabucha a"
|
||||
hq.nicks[1].name = "Kabucha2 a"
|
||||
assert not hq.nicks[0].saved
|
||||
assert not hq.nicks[1].saved
|
||||
|
||||
count = await hq.save_related(exclude={"nicks": ...})
|
||||
assert count == 0
|
||||
assert not hq.nicks[0].saved
|
||||
assert not hq.nicks[1].saved
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_saving_reversed_relation():
|
||||
@ -211,3 +229,16 @@ async def test_saving_nested():
|
||||
assert hq.nicks[0].level.saved
|
||||
assert hq.nicks[1].saved
|
||||
assert hq.nicks[1].level.saved
|
||||
|
||||
hq.nicks[0].level.name = "Low 2"
|
||||
hq.nicks[1].level.name = "Medium 2"
|
||||
assert not hq.nicks[0].level.saved
|
||||
assert not hq.nicks[1].level.saved
|
||||
assert hq.nicks[0].saved
|
||||
assert hq.nicks[1].saved
|
||||
count = await hq.save_related(follow=True, exclude={"nicks": {"level"}})
|
||||
assert count == 0
|
||||
assert hq.nicks[0].saved
|
||||
assert not hq.nicks[0].level.saved
|
||||
assert hq.nicks[1].saved
|
||||
assert not hq.nicks[1].level.saved
|
||||
|
||||
@ -1,76 +0,0 @@
|
||||
from typing import Union
|
||||
|
||||
import databases
|
||||
import pytest
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy import create_engine
|
||||
|
||||
import ormar
|
||||
from ormar.exceptions import ModelPersistenceError
|
||||
from tests.settings import DATABASE_URL
|
||||
|
||||
metadata = sa.MetaData()
|
||||
db = databases.Database(DATABASE_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(autouse=True, scope="module")
|
||||
def create_test_database():
|
||||
engine = create_engine(DATABASE_URL)
|
||||
metadata.create_all(engine)
|
||||
yield
|
||||
metadata.drop_all(engine)
|
||||
|
||||
|
||||
@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"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_model_relationship_with_not_saved():
|
||||
async with db:
|
||||
async with db.transaction(force_rollback=True):
|
||||
cat = Category(name="Foo", code=123)
|
||||
with pytest.raises(ModelPersistenceError):
|
||||
await Workshop(topic="Topic 1", category=cat).save()
|
||||
|
||||
with pytest.raises(ModelPersistenceError):
|
||||
await Workshop.objects.create(topic="Topic 1", category=cat)
|
||||
Reference in New Issue
Block a user