|
| 1 | +""" |
| 2 | +Tests for truncate_all_models() and _topological_sort_models(). |
| 3 | +
|
| 4 | +Verifies FK-aware truncation ordering and the PostgreSQL TRUNCATE CASCADE path. |
| 5 | +""" |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from tests.testmodels import Employee, Event, MinRelation, Reporter, Team, Tournament |
| 10 | +from tortoise import Tortoise |
| 11 | +from tortoise.contrib.test import _topological_sort_models, truncate_all_models |
| 12 | + |
| 13 | +# --------------------------------------------------------------------------- |
| 14 | +# _topological_sort_models — unit tests on real model metadata |
| 15 | +# --------------------------------------------------------------------------- |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.asyncio |
| 19 | +async def test_topological_sort_children_before_parents(db): |
| 20 | + """Event (FK→Tournament) must come before Tournament in delete order.""" |
| 21 | + sorted_models = _topological_sort_models([Tournament, Event]) |
| 22 | + assert sorted_models.index(Event) < sorted_models.index(Tournament) |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.asyncio |
| 26 | +async def test_topological_sort_input_order_independent(db): |
| 27 | + """Result must be the same regardless of input order.""" |
| 28 | + order_a = _topological_sort_models([Tournament, Event]) |
| 29 | + order_b = _topological_sort_models([Event, Tournament]) |
| 30 | + assert order_a == order_b |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.asyncio |
| 34 | +async def test_topological_sort_self_referential_fk(db): |
| 35 | + """Self-referential FK (Employee→Employee) must not cause infinite loop.""" |
| 36 | + result = _topological_sort_models([Employee]) |
| 37 | + assert result == [Employee] |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.asyncio |
| 41 | +async def test_topological_sort_no_fk_models(db): |
| 42 | + """Models without FK relationships are still included.""" |
| 43 | + result = _topological_sort_models([Team]) |
| 44 | + assert result == [Team] |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.asyncio |
| 48 | +async def test_topological_sort_all_models(db): |
| 49 | + """Sorting all registered models succeeds and includes every model.""" |
| 50 | + all_models = list(Tortoise.apps.get_models_iterable()) |
| 51 | + sorted_models = _topological_sort_models(all_models) |
| 52 | + assert set(sorted_models) == set(all_models) |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.asyncio |
| 56 | +async def test_topological_sort_multi_level_chain(db): |
| 57 | + """MinRelation→Tournament and MinRelation→Team: MinRelation before both parents.""" |
| 58 | + sorted_models = _topological_sort_models([Tournament, Team, MinRelation]) |
| 59 | + assert sorted_models.index(MinRelation) < sorted_models.index(Tournament) |
| 60 | + assert sorted_models.index(MinRelation) < sorted_models.index(Team) |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.asyncio |
| 64 | +async def test_topological_sort_multiple_fks_on_one_model(db): |
| 65 | + """Event has FKs to both Tournament and Reporter — must come before both.""" |
| 66 | + sorted_models = _topological_sort_models([Tournament, Reporter, Event]) |
| 67 | + assert sorted_models.index(Event) < sorted_models.index(Tournament) |
| 68 | + assert sorted_models.index(Event) < sorted_models.index(Reporter) |
| 69 | + |
| 70 | + |
| 71 | +# --------------------------------------------------------------------------- |
| 72 | +# truncate_all_models — integration tests against real DB |
| 73 | +# --------------------------------------------------------------------------- |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.asyncio |
| 77 | +async def test_truncate_empty_db(db): |
| 78 | + """Truncating when tables are empty should succeed without error.""" |
| 79 | + await truncate_all_models() |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.asyncio |
| 83 | +async def test_truncate_clears_data(db): |
| 84 | + """Data created before truncation is gone after truncation.""" |
| 85 | + tournament = await Tournament.create(name="Test Tournament") |
| 86 | + await Event.create(name="Test Event", tournament=tournament) |
| 87 | + |
| 88 | + await truncate_all_models() |
| 89 | + |
| 90 | + assert await Tournament.all().count() == 0 |
| 91 | + assert await Event.all().count() == 0 |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.asyncio |
| 95 | +async def test_truncate_with_fk_constraints(db): |
| 96 | + """Truncation succeeds even with FK constraints (child→parent).""" |
| 97 | + t = await Tournament.create(name="T1") |
| 98 | + await Event.create(name="E1", tournament=t) |
| 99 | + await Event.create(name="E2", tournament=t) |
| 100 | + |
| 101 | + # This would fail with arbitrary order on strict FK enforcement |
| 102 | + await truncate_all_models() |
| 103 | + |
| 104 | + assert await Event.all().count() == 0 |
| 105 | + assert await Tournament.all().count() == 0 |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.asyncio |
| 109 | +async def test_truncate_with_self_referential_fk(db): |
| 110 | + """Self-referential FK (Employee→Employee) doesn't break truncation.""" |
| 111 | + boss = await Employee.create(name="Boss") |
| 112 | + await Employee.create(name="Worker", manager=boss) |
| 113 | + |
| 114 | + await truncate_all_models() |
| 115 | + |
| 116 | + assert await Employee.all().count() == 0 |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.asyncio |
| 120 | +async def test_truncate_raises_when_apps_not_loaded(db_simple): |
| 121 | + """truncate_all_models raises ValueError when apps aren't loaded.""" |
| 122 | + from tortoise.context import get_current_context |
| 123 | + |
| 124 | + ctx = get_current_context() |
| 125 | + saved_apps = ctx._apps |
| 126 | + ctx._apps = {} |
| 127 | + try: |
| 128 | + with pytest.raises(ValueError, match="apps are not loaded"): |
| 129 | + await truncate_all_models() |
| 130 | + finally: |
| 131 | + ctx._apps = saved_apps |
0 commit comments