|
| 1 | +from __future__ import unicode_literals |
| 2 | + |
| 3 | +from datetime import datetime, timedelta |
| 4 | +from six.moves import cStringIO as StringIO |
| 5 | +import unittest |
| 6 | + |
| 7 | +import django |
| 8 | +from django.contrib.auth import get_user_model |
| 9 | +from django.core import management |
| 10 | +from django.test import TestCase |
| 11 | + |
| 12 | +from simple_history import exceptions, register |
| 13 | +from ..tests.models import ( |
| 14 | + Poll, Choice, Voter, Restaurant, HistoricalPoll, HistoricalChoice, |
| 15 | + HistoricalState, HistoricalCustomFKError, |
| 16 | + UserAccessorDefault, UserAccessorOverride, |
| 17 | + TrackedAbstractBaseA, TrackedAbstractBaseB, |
| 18 | + TrackedWithAbstractBase, TrackedWithConcreteBase, |
| 19 | + InheritTracking1, InheritTracking2, InheritTracking3, InheritTracking4, |
| 20 | +) |
| 21 | + |
| 22 | +try: |
| 23 | + from django.apps import apps |
| 24 | +except ImportError: # Django < 1.7 |
| 25 | + from django.db.models import get_model |
| 26 | +else: |
| 27 | + get_model = apps.get_model |
| 28 | + |
| 29 | +User = get_user_model() |
| 30 | +today = datetime(2021, 1, 1, 10, 0) |
| 31 | +tomorrow = today + timedelta(days=1) |
| 32 | +yesterday = today - timedelta(days=1) |
| 33 | + |
| 34 | + |
| 35 | +class RegisterTest(TestCase): |
| 36 | + def test_register_no_args(self): |
| 37 | + self.assertEqual(len(Choice.history.all()), 0) |
| 38 | + poll = Poll.objects.create(pub_date=today) |
| 39 | + choice = Choice.objects.create(poll=poll, votes=0) |
| 40 | + self.assertEqual(len(choice.history.all()), 1) |
| 41 | + |
| 42 | + def test_register_separate_app(self): |
| 43 | + def get_history(model): |
| 44 | + return model.history |
| 45 | + |
| 46 | + self.assertRaises(AttributeError, get_history, User) |
| 47 | + self.assertEqual(len(User.histories.all()), 0) |
| 48 | + user = User.objects.create(username='bob', password='pass') |
| 49 | + self.assertEqual(len(User.histories.all()), 1) |
| 50 | + self.assertEqual(len(user.histories.all()), 1) |
| 51 | + |
| 52 | + def test_reregister(self): |
| 53 | + with self.assertRaises(exceptions.MultipleRegistrationsError): |
| 54 | + register(Restaurant, manager_name='again') |
| 55 | + |
| 56 | + def test_register_custome_records(self): |
| 57 | + self.assertEqual(len(Voter.history.all()), 0) |
| 58 | + poll = Poll.objects.create(pub_date=today) |
| 59 | + choice = Choice.objects.create(poll=poll, votes=0) |
| 60 | + user = User.objects.create(username='voter') |
| 61 | + voter = Voter.objects.create(choice=choice, user=user) |
| 62 | + self.assertEqual(len(voter.history.all()), 1) |
| 63 | + expected = 'Voter object changed by None as of ' |
| 64 | + self.assertEqual(expected, |
| 65 | + str(voter.history.all()[0])[:len(expected)]) |
| 66 | + |
| 67 | + |
| 68 | +class TestUserAccessor(unittest.TestCase): |
| 69 | + |
| 70 | + def test_accessor_default(self): |
| 71 | + register(UserAccessorDefault) |
| 72 | + assert not hasattr(User, 'historicaluseraccessordefault_set') |
| 73 | + |
| 74 | + def test_accessor_override(self): |
| 75 | + register(UserAccessorOverride, |
| 76 | + user_related_name='my_history_model_accessor') |
| 77 | + assert hasattr(User, 'my_history_model_accessor') |
| 78 | + |
| 79 | + |
| 80 | +class TestTrackingInheritance(TestCase): |
| 81 | + |
| 82 | + def test_tracked_abstract_base(self): |
| 83 | + self.assertEqual( |
| 84 | + [ |
| 85 | + f.attname |
| 86 | + for f in TrackedWithAbstractBase.history.model._meta.fields |
| 87 | + ], |
| 88 | + [ |
| 89 | + 'id', 'history_id', 'history_date', 'history_user_id', |
| 90 | + 'history_type', |
| 91 | + ], |
| 92 | + ) |
| 93 | + |
| 94 | + def test_tracked_concrete_base(self): |
| 95 | + self.assertEqual( |
| 96 | + [ |
| 97 | + f.attname |
| 98 | + for f in TrackedWithConcreteBase.history.model._meta.fields |
| 99 | + ], |
| 100 | + [ |
| 101 | + 'id', 'trackedconcretebase_ptr_id', 'history_id', |
| 102 | + 'history_date', 'history_user_id', 'history_type', |
| 103 | + ], |
| 104 | + ) |
| 105 | + |
| 106 | + def test_multiple_tracked_bases(self): |
| 107 | + with self.assertRaises(exceptions.MultipleRegistrationsError): |
| 108 | + class TrackedWithMultipleAbstractBases( |
| 109 | + TrackedAbstractBaseA, TrackedAbstractBaseB): |
| 110 | + pass |
| 111 | + |
| 112 | + def test_tracked_abstract_and_untracked_concrete_base(self): |
| 113 | + self.assertEqual( |
| 114 | + [f.attname for f in InheritTracking1.history.model._meta.fields], |
| 115 | + [ |
| 116 | + 'id', 'untrackedconcretebase_ptr_id', 'history_id', |
| 117 | + 'history_date', 'history_user_id', 'history_type', |
| 118 | + ], |
| 119 | + ) |
| 120 | + |
| 121 | + def test_indirect_tracked_abstract_base(self): |
| 122 | + self.assertEqual( |
| 123 | + [f.attname for f in InheritTracking2.history.model._meta.fields], |
| 124 | + [ |
| 125 | + 'id', 'baseinherittracking2_ptr_id', 'history_id', |
| 126 | + 'history_date', 'history_user_id', 'history_type', |
| 127 | + ], |
| 128 | + ) |
| 129 | + |
| 130 | + def test_indirect_tracked_concrete_base(self): |
| 131 | + self.assertEqual( |
| 132 | + [f.attname for f in InheritTracking3.history.model._meta.fields], |
| 133 | + [ |
| 134 | + 'id', 'baseinherittracking3_ptr_id', 'history_id', |
| 135 | + 'history_date', 'history_user_id', 'history_type', |
| 136 | + ], |
| 137 | + ) |
| 138 | + |
| 139 | + def test_registering_with_tracked_abstract_base(self): |
| 140 | + with self.assertRaises(exceptions.MultipleRegistrationsError): |
| 141 | + register(InheritTracking4) |
| 142 | + |
| 143 | + |
| 144 | +@unittest.skipUnless(django.get_version() >= "1.7", "Requires 1.7 migrations") |
| 145 | +class TestMigrate(TestCase): |
| 146 | + |
| 147 | + def test_makemigration_command(self): |
| 148 | + management.call_command( |
| 149 | + 'makemigrations', 'migration_test_app', stdout=StringIO()) |
| 150 | + |
| 151 | + def test_migrate_command(self): |
| 152 | + management.call_command( |
| 153 | + 'migrate', 'migration_test_app', fake=True, stdout=StringIO()) |
0 commit comments