|
10 | 10 | from django.test import TestCase
|
11 | 11 | from django.core.files.base import ContentFile
|
12 | 12 |
|
| 13 | +from simple_history import exceptions, register |
13 | 14 | from simple_history.models import HistoricalRecords, convert_auto_field
|
14 |
| -from simple_history import register |
15 | 15 | from ..models import (
|
16 | 16 | AdminProfile, Bookcase, MultiOneToOne, Poll, Choice, Voter, Restaurant,
|
17 | 17 | Person, FileModel, Document, Book, HistoricalPoll, Library, State,
|
18 | 18 | AbstractBase, ConcreteAttr, ConcreteUtil, SelfFK, Temperature, WaterLevel,
|
19 | 19 | ExternalModel1, ExternalModel3, UnicodeVerboseName, HistoricalChoice,
|
20 | 20 | HistoricalState, HistoricalCustomFKError, Series, SeriesWork, PollInfo,
|
21 | 21 | UserAccessorDefault, UserAccessorOverride, Employee, Country, Province,
|
22 |
| - City, Contact, ContactRegister |
| 22 | + City, Contact, ContactRegister, |
| 23 | + TrackedAbstractBaseA, TrackedAbstractBaseB, UntrackedAbstractBase, |
| 24 | + TrackedConcreteBase, UntrackedConcreteBase, |
23 | 25 | )
|
24 | 26 | from ..external.models import ExternalModel2, ExternalModel4
|
25 | 27 |
|
@@ -332,12 +334,8 @@ def test_register_separate_app(self):
|
332 | 334 | self.assertEqual(len(user.histories.all()), 1)
|
333 | 335 |
|
334 | 336 | def test_reregister(self):
|
335 |
| - register(Restaurant, manager_name='again') |
336 |
| - register(User, manager_name='again') |
337 |
| - self.assertTrue(hasattr(Restaurant, 'updates')) |
338 |
| - self.assertFalse(hasattr(Restaurant, 'again')) |
339 |
| - self.assertTrue(hasattr(User, 'histories')) |
340 |
| - self.assertFalse(hasattr(User, 'again')) |
| 337 | + with self.assertRaises(exceptions.MultipleRegistrationsError): |
| 338 | + register(Restaurant, manager_name='again') |
341 | 339 |
|
342 | 340 | def test_register_custome_records(self):
|
343 | 341 | self.assertEqual(len(Voter.history.all()), 0)
|
@@ -783,3 +781,73 @@ def test_custom_table_name_from_register(self):
|
783 | 781 | self.get_table_name(ContactRegister.history),
|
784 | 782 | 'contacts_register_history',
|
785 | 783 | )
|
| 784 | + |
| 785 | + |
| 786 | +class TestTrackingInheritance(TestCase): |
| 787 | + |
| 788 | + def test_tracked_abstract_base(self): |
| 789 | + class TrackedWithAbstractBase(TrackedAbstractBaseA): |
| 790 | + pass |
| 791 | + |
| 792 | + self.assertEqual( |
| 793 | + [f.attname for f in TrackedWithAbstractBase.history.model._meta.fields], |
| 794 | + ['id', 'history_id', 'history_date', 'history_user_id', 'history_type'], |
| 795 | + ) |
| 796 | + |
| 797 | + def test_tracked_concrete_base(self): |
| 798 | + class TrackedWithConcreteBase(TrackedConcreteBase): |
| 799 | + pass |
| 800 | + |
| 801 | + self.assertEqual( |
| 802 | + [f.attname for f in TrackedWithConcreteBase.history.model._meta.fields], |
| 803 | + ['id', 'trackedconcretebase_ptr_id', 'history_id', 'history_date', 'history_user_id', 'history_type'], |
| 804 | + ) |
| 805 | + |
| 806 | + def test_multiple_tracked_bases(self): |
| 807 | + with self.assertRaises(exceptions.MultipleRegistrationsError): |
| 808 | + class TrackedWithMultipleAbstractBases(TrackedAbstractBaseA, TrackedAbstractBaseB): |
| 809 | + pass |
| 810 | + |
| 811 | + def test_tracked_abstract_and_untracked_concrete_base(self): |
| 812 | + class TrackedWithTrackedAbstractAndUntrackedConcreteBase(TrackedAbstractBaseA, UntrackedConcreteBase): |
| 813 | + pass |
| 814 | + |
| 815 | + self.assertEqual( |
| 816 | + [f.attname for f in TrackedWithTrackedAbstractAndUntrackedConcreteBase.history.model._meta.fields], |
| 817 | + ['id', 'untrackedconcretebase_ptr_id', 'history_id', 'history_date', 'history_user_id', 'history_type'], |
| 818 | + ) |
| 819 | + |
| 820 | + def test_indirect_tracked_abstract_base(self): |
| 821 | + class BaseTrackedWithIndirectTrackedAbstractBase(TrackedAbstractBaseA): |
| 822 | + pass |
| 823 | + |
| 824 | + class TrackedWithIndirectTrackedAbstractBase(BaseTrackedWithIndirectTrackedAbstractBase): |
| 825 | + pass |
| 826 | + |
| 827 | + self.assertEqual( |
| 828 | + [f.attname for f in TrackedWithIndirectTrackedAbstractBase.history.model._meta.fields], |
| 829 | + [ |
| 830 | + 'id', 'basetrackedwithindirecttrackedabstractbase_ptr_id', |
| 831 | + 'history_id', 'history_date', 'history_user_id', 'history_type'], |
| 832 | + ) |
| 833 | + |
| 834 | + def test_indirect_tracked_concrete_base(self): |
| 835 | + class BaseTrackedWithIndirectTrackedConcreteBase(TrackedAbstractBaseA): |
| 836 | + pass |
| 837 | + |
| 838 | + class TrackedWithIndirectTrackedConcreteBase(BaseTrackedWithIndirectTrackedConcreteBase): |
| 839 | + pass |
| 840 | + |
| 841 | + self.assertEqual( |
| 842 | + [f.attname for f in TrackedWithIndirectTrackedConcreteBase.history.model._meta.fields], |
| 843 | + [ |
| 844 | + 'id', 'basetrackedwithindirecttrackedconcretebase_ptr_id', |
| 845 | + 'history_id', 'history_date', 'history_user_id', 'history_type'], |
| 846 | + ) |
| 847 | + |
| 848 | + def test_registering_with_tracked_abstract_base(self): |
| 849 | + class TrackedWithAbstractBaseToRegister(TrackedAbstractBaseA): |
| 850 | + pass |
| 851 | + |
| 852 | + with self.assertRaises(exceptions.MultipleRegistrationsError): |
| 853 | + register(TrackedWithAbstractBaseToRegister) |
0 commit comments