|
1 | 1 | import pytest |
2 | 2 | from cms.models import Page, Sitemap |
| 3 | +from django.core.exceptions import ValidationError |
3 | 4 |
|
4 | 5 |
|
5 | 6 | @pytest.mark.django_db |
@@ -31,3 +32,121 @@ def test_route_calculation(): |
31 | 32 | assert root_sitemap.route == "root" |
32 | 33 | assert child_sitemap.route == "root/child" |
33 | 34 | assert grandchild_sitemap.route == "root/child/grandchild" |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.django_db |
| 38 | +def test_get_all_routes(): |
| 39 | + # Given: nested한 사이트맵 구조 생성 |
| 40 | + data = { |
| 41 | + "root_1": { |
| 42 | + "child_1_1": {"child_1_1_1": {}, "child_1_1_2": {}}, |
| 43 | + "child_1_2": {"child_1_2_1": {}}, |
| 44 | + "child_1_3": {}, |
| 45 | + }, |
| 46 | + "root_2": { |
| 47 | + "child_2_1": {"child_2_1_1": {}}, |
| 48 | + "child_2_2": {}, |
| 49 | + }, |
| 50 | + } |
| 51 | + |
| 52 | + def create_sitemaps(data: dict[str, dict], parent: Sitemap = None) -> None: |
| 53 | + for name, children in data.items(): |
| 54 | + sitemap = Sitemap.objects.create( |
| 55 | + route_code=name, |
| 56 | + name=name, |
| 57 | + page=Page.objects.create(title=name, subtitle=f"{name} Subtitle"), |
| 58 | + parent_sitemap=parent, |
| 59 | + ) |
| 60 | + create_sitemaps(children, sitemap) |
| 61 | + |
| 62 | + create_sitemaps(data) |
| 63 | + |
| 64 | + # When: Sitemap.objects.get_all_routes() 메서드를 호출할 시 |
| 65 | + all_routes = Sitemap.objects.get_all_routes() |
| 66 | + |
| 67 | + # Then: 예상한 모든 route가 나와야 한다. |
| 68 | + assert all_routes == { |
| 69 | + "root_1", |
| 70 | + "root_1/child_1_1", |
| 71 | + "root_1/child_1_1/child_1_1_1", |
| 72 | + "root_1/child_1_1/child_1_1_2", |
| 73 | + "root_1/child_1_2", |
| 74 | + "root_1/child_1_2/child_1_2_1", |
| 75 | + "root_1/child_1_3", |
| 76 | + "root_2", |
| 77 | + "root_2/child_2_1", |
| 78 | + "root_2/child_2_1/child_2_1_1", |
| 79 | + "root_2/child_2_2", |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.django_db |
| 84 | +def test_clean_should_check_for_self_reference(): |
| 85 | + # Given: Sitemap 객체 생성 |
| 86 | + sitemap = Sitemap.objects.create( |
| 87 | + route_code="self", |
| 88 | + name="Self Sitemap", |
| 89 | + page=Page.objects.create(title="Self Page", subtitle="Self Subtitle"), |
| 90 | + ) |
| 91 | + |
| 92 | + # When: Self-reference를 만들기 위해 parent_sitemap을 자기 자신으로 설정 |
| 93 | + sitemap.parent_sitemap = sitemap |
| 94 | + |
| 95 | + # Then: ValidationError가 발생해야 한다. |
| 96 | + with pytest.raises(ValidationError) as excinfo: |
| 97 | + sitemap.clean() |
| 98 | + assert str(excinfo.value) == "자기 자신을 부모로 설정할 수 없습니다." |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.django_db |
| 102 | +def test_clean_should_check_for_circular_reference(): |
| 103 | + # Given: Circular reference가 있는 Sitemap 객체 생성 |
| 104 | + root_sitemap = Sitemap.objects.create( |
| 105 | + route_code="root", |
| 106 | + name="Root Sitemap", |
| 107 | + page=Page.objects.create(title="Root Page", subtitle="Root Subtitle"), |
| 108 | + ) |
| 109 | + |
| 110 | + child_sitemap = Sitemap.objects.create( |
| 111 | + route_code="child", |
| 112 | + name="Child Sitemap", |
| 113 | + parent_sitemap=root_sitemap, |
| 114 | + page=Page.objects.create(title="Child Page", subtitle="Child Subtitle"), |
| 115 | + ) |
| 116 | + |
| 117 | + grandchild_sitemap = Sitemap.objects.create( |
| 118 | + route_code="grandchild", |
| 119 | + name="Grandchild Sitemap", |
| 120 | + parent_sitemap=child_sitemap, |
| 121 | + page=Page.objects.create(title="Grandchild Page", subtitle="Grandchild Subtitle"), |
| 122 | + ) |
| 123 | + |
| 124 | + # When: Circular reference를 만들기 위해 child_sitemap을 root_sitemap의 parent로 설정 |
| 125 | + root_sitemap.parent_sitemap = grandchild_sitemap |
| 126 | + |
| 127 | + # Then: ValidationError가 발생해야 한다. |
| 128 | + with pytest.raises(ValidationError) as excinfo: |
| 129 | + root_sitemap.clean() |
| 130 | + assert str(excinfo.value) == "Parent Sitemap이 자식 Sitemap을 가리킬 수 없습니다." |
| 131 | + |
| 132 | + |
| 133 | +@pytest.mark.django_db |
| 134 | +def test_clean_should_check_for_existing_route(): |
| 135 | + # Given: 이미 존재하는 route를 가진 Sitemap 객체 생성 |
| 136 | + Sitemap.objects.create( |
| 137 | + route_code="existing", |
| 138 | + name="Existing Sitemap", |
| 139 | + page=Page.objects.create(title="Existing Page", subtitle="Existing Subtitle"), |
| 140 | + ) |
| 141 | + |
| 142 | + # When: 새로운 Sitemap 객체를 생성하고, 기존의 route와 같은 route_code를 설정 |
| 143 | + new_sitemap = Sitemap( |
| 144 | + route_code="existing", |
| 145 | + name="New Sitemap", |
| 146 | + page=Page.objects.create(title="New Page", subtitle="New Subtitle"), |
| 147 | + ) |
| 148 | + |
| 149 | + # Then: ValidationError가 발생해야 한다. |
| 150 | + with pytest.raises(ValidationError) as excinfo: |
| 151 | + new_sitemap.clean() |
| 152 | + assert str(excinfo.value) == "`existing`라우트는 이미 존재하는 route입니다." |
0 commit comments