|
77 | 77 | InheritedRestaurant, |
78 | 78 | Library, |
79 | 79 | ManyToManyModelOther, |
| 80 | + ModelWithCustomAttrOneToOneField, |
80 | 81 | ModelWithExcludedManyToMany, |
81 | 82 | ModelWithFkToModelWithHistoryUsingBaseModelDb, |
82 | 83 | ModelWithHistoryInDifferentDb, |
|
93 | 94 | PollChildBookWithManyToMany, |
94 | 95 | PollChildRestaurantWithManyToMany, |
95 | 96 | PollInfo, |
| 97 | + PollWithAlternativeManager, |
96 | 98 | PollWithExcludedFieldsWithDefaults, |
97 | 99 | PollWithExcludedFKField, |
98 | 100 | PollWithExcludeFields, |
@@ -894,33 +896,83 @@ def test_get_next_record_with_excluded_field(self): |
894 | 896 |
|
895 | 897 |
|
896 | 898 | class CreateHistoryModelTests(unittest.TestCase): |
| 899 | + @staticmethod |
| 900 | + def create_history_model(model, inherited): |
| 901 | + custom_model_name_prefix = f"Mock{HistoricalRecords.DEFAULT_MODEL_NAME_PREFIX}" |
| 902 | + records = HistoricalRecords( |
| 903 | + # Provide a custom history model name, to prevent name collisions |
| 904 | + # with existing historical models |
| 905 | + custom_model_name=lambda name: f"{custom_model_name_prefix}{name}", |
| 906 | + ) |
| 907 | + records.module = model.__module__ |
| 908 | + return records.create_history_model(model, inherited) |
| 909 | + |
| 910 | + def test_create_history_model_has_expected_tracked_files_attr(self): |
| 911 | + def assert_tracked_fields_equal(model, expected_field_names): |
| 912 | + from .. import models |
| 913 | + |
| 914 | + history_model = getattr( |
| 915 | + models, f"{HistoricalRecords.DEFAULT_MODEL_NAME_PREFIX}{model.__name__}" |
| 916 | + ) |
| 917 | + self.assertListEqual( |
| 918 | + [field.name for field in history_model.tracked_fields], |
| 919 | + expected_field_names, |
| 920 | + ) |
| 921 | + |
| 922 | + assert_tracked_fields_equal( |
| 923 | + Poll, |
| 924 | + ["id", "question", "pub_date"], |
| 925 | + ) |
| 926 | + assert_tracked_fields_equal( |
| 927 | + PollWithNonEditableField, |
| 928 | + ["id", "question", "pub_date", "modified"], |
| 929 | + ) |
| 930 | + assert_tracked_fields_equal( |
| 931 | + PollWithExcludeFields, |
| 932 | + ["id", "question", "place"], |
| 933 | + ) |
| 934 | + assert_tracked_fields_equal( |
| 935 | + PollWithExcludedFieldsWithDefaults, |
| 936 | + ["id", "question"], |
| 937 | + ) |
| 938 | + assert_tracked_fields_equal( |
| 939 | + PollWithExcludedFKField, |
| 940 | + ["id", "question", "pub_date"], |
| 941 | + ) |
| 942 | + assert_tracked_fields_equal( |
| 943 | + PollWithAlternativeManager, |
| 944 | + ["id", "question", "pub_date"], |
| 945 | + ) |
| 946 | + assert_tracked_fields_equal( |
| 947 | + PollWithHistoricalIPAddress, |
| 948 | + ["id", "question", "pub_date"], |
| 949 | + ) |
| 950 | + assert_tracked_fields_equal( |
| 951 | + ModelWithCustomAttrOneToOneField, |
| 952 | + ["id", "poll"], |
| 953 | + ) |
| 954 | + |
897 | 955 | def test_create_history_model_with_one_to_one_field_to_integer_field(self): |
898 | | - records = HistoricalRecords() |
899 | | - records.module = AdminProfile.__module__ |
900 | 956 | try: |
901 | | - records.create_history_model(AdminProfile, False) |
| 957 | + self.create_history_model(AdminProfile, False) |
902 | 958 | except Exception: |
903 | 959 | self.fail( |
904 | 960 | "SimpleHistory should handle foreign keys to one to one" |
905 | 961 | "fields to integer fields without throwing an exception" |
906 | 962 | ) |
907 | 963 |
|
908 | 964 | def test_create_history_model_with_one_to_one_field_to_char_field(self): |
909 | | - records = HistoricalRecords() |
910 | | - records.module = Bookcase.__module__ |
911 | 965 | try: |
912 | | - records.create_history_model(Bookcase, False) |
| 966 | + self.create_history_model(Bookcase, False) |
913 | 967 | except Exception: |
914 | 968 | self.fail( |
915 | 969 | "SimpleHistory should handle foreign keys to one to one" |
916 | 970 | "fields to char fields without throwing an exception." |
917 | 971 | ) |
918 | 972 |
|
919 | 973 | def test_create_history_model_with_multiple_one_to_ones(self): |
920 | | - records = HistoricalRecords() |
921 | | - records.module = MultiOneToOne.__module__ |
922 | 974 | try: |
923 | | - records.create_history_model(MultiOneToOne, False) |
| 975 | + self.create_history_model(MultiOneToOne, False) |
924 | 976 | except Exception: |
925 | 977 | self.fail( |
926 | 978 | "SimpleHistory should handle foreign keys to one to one" |
|
0 commit comments