|
| 1 | +import copy |
| 2 | + |
| 3 | +from unittest import mock |
| 4 | + |
| 5 | +from django.test import TestCase |
| 6 | +from testapp.constants import COMPLEX_MODIFIED_BODY_VALUE, COMPLEX_ORIGINAL_BODY_VALUE |
| 7 | +from testapp.models import TestPage, TestSnippet |
| 8 | + |
| 9 | + |
| 10 | +# StreamField will have generated random 'id' values for blocks in some cases, |
| 11 | +# so the following constants allow us to successfully compare raw field values |
| 12 | +# against hardcoded ones, without caring about the 'id' value |
| 13 | + |
| 14 | +COMPARABLE_MODIFIED_BODY_VALUE = copy.deepcopy(COMPLEX_MODIFIED_BODY_VALUE) |
| 15 | +for item in COMPARABLE_MODIFIED_BODY_VALUE: |
| 16 | + item["id"] = mock.ANY |
| 17 | + |
| 18 | + |
| 19 | +COMPARABLE_ORIGINAL_BODY_VALUE = copy.deepcopy(COMPLEX_ORIGINAL_BODY_VALUE) |
| 20 | +for item in COMPARABLE_ORIGINAL_BODY_VALUE: |
| 21 | + item["id"] = mock.ANY |
| 22 | + |
| 23 | + |
| 24 | +class TestMigrationOutcomes(TestCase): |
| 25 | + @classmethod |
| 26 | + def setUpTestData(cls): |
| 27 | + for i, page in enumerate(TestPage.objects.all().order_by("id"), start=1): |
| 28 | + setattr(cls, f"page_{i}", page) |
| 29 | + for i, snippet in enumerate(TestSnippet.objects.all().order_by("id"), start=1): |
| 30 | + setattr(cls, f"snippet_{i}", snippet) |
| 31 | + |
| 32 | + def test_original_pages_and_snippets_have_retained_their_original_body_values(self): |
| 33 | + """ |
| 34 | + Test the objects created in `0002_create_test_objects` and DID NOT have their `body` value |
| 35 | + updated in later migrations have retained their original values after: |
| 36 | +
|
| 37 | + - Swapping the native StreamField for a custom one in `0003_swap_native_streamfield_for_mlstreamfield` |
| 38 | + - Updating their title in `0007_modify_title_values` and resaving |
| 39 | + - Updating their title in `0008_modify_title_values_with_bulk_update` via a bulk update |
| 40 | + """ |
| 41 | + self.assertEqual( |
| 42 | + self.page_1.body.raw_data, |
| 43 | + COMPARABLE_ORIGINAL_BODY_VALUE, |
| 44 | + ) |
| 45 | + self.assertEqual( |
| 46 | + self.snippet_1.body.raw_data, |
| 47 | + COMPARABLE_ORIGINAL_BODY_VALUE, |
| 48 | + ) |
| 49 | + |
| 50 | + def test_modified_original_pages_and_snippets_have_retained_their_modified_body_values(self): |
| 51 | + """ |
| 52 | + Test that the objects created in `0002_create_test_objects` and had their `body` value |
| 53 | + later modified in `0004_modify_body_values` have retained that updated body value after: |
| 54 | +
|
| 55 | + - Updating their `title` value in `0007_modify_title_values` and resaving |
| 56 | + """ |
| 57 | + self.assertEqual( |
| 58 | + self.page_2.body.raw_data, |
| 59 | + COMPARABLE_MODIFIED_BODY_VALUE, |
| 60 | + ) |
| 61 | + self.assertEqual( |
| 62 | + self.snippet_2.body.raw_data, |
| 63 | + COMPARABLE_MODIFIED_BODY_VALUE, |
| 64 | + ) |
| 65 | + |
| 66 | + def test_newer_pages_and_snippets_have_retained_their_original_body_values(self): |
| 67 | + """ |
| 68 | + Test that the objects created in `0005_create_more_test_objects` and DID NOT have their body |
| 69 | + value updated in later migrations have retained their original values after: |
| 70 | +
|
| 71 | + - Updating their `title` value in `0007_modify_title_values` and resaving |
| 72 | + - Updating their `title` value again in `0008_modify_title_values_with_bulk_update` via a bulk update |
| 73 | + """ |
| 74 | + self.assertEqual( |
| 75 | + self.page_3.body.raw_data, |
| 76 | + COMPARABLE_ORIGINAL_BODY_VALUE, |
| 77 | + ) |
| 78 | + self.assertEqual( |
| 79 | + self.snippet_3.body.raw_data, |
| 80 | + COMPARABLE_ORIGINAL_BODY_VALUE, |
| 81 | + ) |
| 82 | + |
| 83 | + def test_modified_newer_pages_and_snippets_have_retained_their_modified_body_values(self): |
| 84 | + """ |
| 85 | + Tests that the objects created in `0005_create_more_test_objects` and had their `body` value |
| 86 | + later modified in `0006_modify_body_values_with_bulk_update` have retained that updated `body` |
| 87 | + value after: |
| 88 | +
|
| 89 | + - Updating their `title` value in `0007_modify_title_values` and resaving |
| 90 | + - Updating their `title` value again in `0008_modify_title_values_with_bulk_update` via a bulk update |
| 91 | + """ |
| 92 | + self.assertEqual( |
| 93 | + self.page_4.body.raw_data, |
| 94 | + COMPARABLE_MODIFIED_BODY_VALUE, |
| 95 | + ) |
| 96 | + self.assertEqual( |
| 97 | + self.snippet_4.body.raw_data, |
| 98 | + COMPARABLE_MODIFIED_BODY_VALUE, |
| 99 | + ) |
0 commit comments