Skip to content

Commit 9203349

Browse files
author
Ross Mechanic
authored
Fixed bug that prevented next_record and prev_record from using custo… (#501)
* Fixed bug that prevented next_record and prev_record from using custom manager names * Raise threshold for similar codeblocks. Doesn't make sense to refactor here
1 parent 5863d95 commit 9203349

File tree

5 files changed

+104
-91
lines changed

5 files changed

+104
-91
lines changed

.codeclimate.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ checks:
2626
threshold: 4
2727
similar-code:
2828
config:
29-
threshold: # language-specific defaults. an override will affect all languages.
29+
threshold: 50 # language-specific defaults. an override will affect all languages.
3030
identical-code:
3131
config:
3232
threshold: # language-specific defaults. an override will affect all languages.

CHANGES.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ Unreleased
99
- Modify `pre_create_historircal_record` to pass `history_instance` for ease of customization (gh-421)
1010
- Raise warning if HistoricalRecords(inherit=False) is in an abstract model (gh-341)
1111
- Ensure custom arguments for fields are included in historical models' fields (gh-431)
12-
- Add german translations
12+
- Add german translations (gh-484)
1313
- Add `extra_context` parameter to history_form_view (gh-467)
14+
- Fixed bug that prevented `next_record` and `prev_record` to work with custom manager names (gh-501)
1415

1516
2.5.1 (2018-10-19)
1617
------------------

simple_history/models.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from django.utils.timezone import now
2020
from django.utils.translation import ugettext_lazy as _
2121

22+
from simple_history import utils
2223
from . import exceptions
2324
from .manager import HistoryDescriptor
2425
from .signals import post_create_historical_record, pre_create_historical_record
@@ -262,8 +263,9 @@ def get_next_record(self):
262263
"""
263264
Get the next history record for the instance. `None` if last.
264265
"""
266+
history = utils.get_history_manager_for_model(self.instance)
265267
return (
266-
self.instance.history.filter(Q(history_date__gt=self.history_date))
268+
history.filter(Q(history_date__gt=self.history_date))
267269
.order_by("history_date")
268270
.first()
269271
)
@@ -272,8 +274,9 @@ def get_prev_record(self):
272274
"""
273275
Get the previous history record for the instance. `None` if first.
274276
"""
277+
history = utils.get_history_manager_for_model(self.instance)
275278
return (
276-
self.instance.history.filter(Q(history_date__lt=self.history_date))
279+
history.filter(Q(history_date__lt=self.history_date))
277280
.order_by("history_date")
278281
.last()
279282
)

simple_history/tests/models.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
from __future__ import unicode_literals
22

33
import uuid
4-
54
from django.apps import apps
65
from django.conf import settings
76
from django.db import models
8-
from django.dispatch import receiver
97
from django.urls import reverse
108

119
from simple_history import register
1210
from simple_history.models import HistoricalRecords
13-
from simple_history.signals import pre_create_historical_record
1411
from .custom_user.models import CustomUser as User
1512
from .external.models.model1 import AbstractExternal
1613

@@ -551,3 +548,8 @@ class CharFieldChangeReasonModel(models.Model):
551548
class CustomNameModel(models.Model):
552549
name = models.CharField(max_length=15, unique=True)
553550
history = HistoricalRecords(custom_model_name="MyHistoricalCustomNameModel")
551+
552+
553+
class CustomManagerNameModel(models.Model):
554+
name = models.CharField(max_length=15)
555+
log = HistoricalRecords()

simple_history/tests/tests/test_models.py

Lines changed: 91 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
import uuid
55
import warnings
66
from datetime import datetime, timedelta
7-
87
from django.apps import apps
98
from django.contrib.auth import get_user_model
109
from django.core.files.base import ContentFile
1110
from django.db import models
1211
from django.db.models.fields.proxy import OrderWrt
13-
from django.test import override_settings, TestCase
12+
from django.test import TestCase, override_settings
1413
from django.urls import reverse
1514

1615
from simple_history.models import HistoricalRecords, ModelChange
@@ -26,6 +25,7 @@
2625
BucketData,
2726
BucketDataRegisterChangedBy,
2827
BucketMember,
28+
CharFieldChangeReasonModel,
2929
Choice,
3030
City,
3131
ConcreteAttr,
@@ -34,7 +34,8 @@
3434
Contact,
3535
ContactRegister,
3636
Country,
37-
CustomNameModel,
37+
CustomManagerNameModel,
38+
DefaultTextFieldChangeReasonModel,
3839
Document,
3940
Employee,
4041
ExternalModel1,
@@ -61,13 +62,11 @@
6162
SeriesWork,
6263
State,
6364
Temperature,
64-
UnicodeVerboseName,
65-
UUIDModel,
6665
UUIDDefaultModel,
67-
WaterLevel,
68-
DefaultTextFieldChangeReasonModel,
66+
UUIDModel,
67+
UnicodeVerboseName,
6968
UserTextFieldChangeReasonModel,
70-
CharFieldChangeReasonModel,
69+
WaterLevel,
7170
)
7271

7372
get_model = apps.get_model
@@ -537,82 +536,6 @@ def test_user_textfield_history_change_reason(self):
537536
self.assertTrue(isinstance(field, models.TextField))
538537
self.assertEqual(history.history_change_reason, reason)
539538

540-
def test_get_prev_record(self):
541-
poll = Poll(question="what's up?", pub_date=today)
542-
poll.save()
543-
poll.question = "ask questions?"
544-
poll.save()
545-
poll.question = "eh?"
546-
poll.save()
547-
poll.question = "one more?"
548-
poll.save()
549-
first_record = poll.history.filter(question="what's up?").get()
550-
second_record = poll.history.filter(question="ask questions?").get()
551-
third_record = poll.history.filter(question="eh?").get()
552-
fourth_record = poll.history.filter(question="one more?").get()
553-
self.assertIsNone(first_record.prev_record)
554-
555-
def assertRecordsMatch(record_a, record_b):
556-
self.assertEqual(record_a, record_b)
557-
self.assertEqual(record_a.question, record_b.question)
558-
559-
assertRecordsMatch(second_record.prev_record, first_record)
560-
assertRecordsMatch(third_record.prev_record, second_record)
561-
assertRecordsMatch(fourth_record.prev_record, third_record)
562-
563-
def test_get_prev_record_none_if_only(self):
564-
poll = Poll(question="what's up?", pub_date=today)
565-
poll.save()
566-
self.assertEqual(poll.history.count(), 1)
567-
record = poll.history.get()
568-
self.assertIsNone(record.prev_record)
569-
570-
def test_get_prev_record_none_if_earliest(self):
571-
poll = Poll(question="what's up?", pub_date=today)
572-
poll.save()
573-
poll.question = "ask questions?"
574-
poll.save()
575-
first_record = poll.history.filter(question="what's up?").get()
576-
self.assertIsNone(first_record.prev_record)
577-
578-
def test_get_next_record(self):
579-
poll = Poll(question="what's up?", pub_date=today)
580-
poll.save()
581-
poll.question = "ask questions?"
582-
poll.save()
583-
poll.question = "eh?"
584-
poll.save()
585-
poll.question = "one more?"
586-
poll.save()
587-
first_record = poll.history.filter(question="what's up?").get()
588-
second_record = poll.history.filter(question="ask questions?").get()
589-
third_record = poll.history.filter(question="eh?").get()
590-
fourth_record = poll.history.filter(question="one more?").get()
591-
self.assertIsNone(fourth_record.next_record)
592-
593-
def assertRecordsMatch(record_a, record_b):
594-
self.assertEqual(record_a, record_b)
595-
self.assertEqual(record_a.question, record_b.question)
596-
597-
assertRecordsMatch(first_record.next_record, second_record)
598-
assertRecordsMatch(second_record.next_record, third_record)
599-
assertRecordsMatch(third_record.next_record, fourth_record)
600-
601-
def test_get_next_record_none_if_only(self):
602-
poll = Poll(question="what's up?", pub_date=today)
603-
poll.save()
604-
self.assertEqual(poll.history.count(), 1)
605-
record = poll.history.get()
606-
self.assertIsNone(record.next_record)
607-
608-
def test_get_next_record_none_if_most_recent(self):
609-
poll = Poll(question="what's up?", pub_date=today)
610-
poll.save()
611-
poll.question = "ask questions?"
612-
poll.save()
613-
recent_record = poll.history.filter(question="ask questions?").get()
614-
self.assertIsNone(recent_record.next_record)
615-
616539
def test_history_diff_includes_changed_fields(self):
617540
p = Poll.objects.create(question="what's up?", pub_date=today)
618541
p.question = "what's up, man?"
@@ -642,6 +565,90 @@ def test_history_diff_with_incorrect_type(self):
642565
new_record.diff_against("something")
643566

644567

568+
class GetPrevRecordAndNextRecordTestCase(TestCase):
569+
def assertRecordsMatch(self, record_a, record_b):
570+
self.assertEqual(record_a, record_b)
571+
self.assertEqual(record_a.question, record_b.question)
572+
573+
def setUp(self):
574+
self.poll = Poll(question="what's up?", pub_date=today)
575+
self.poll.save()
576+
577+
def test_get_prev_record(self):
578+
579+
self.poll.question = "ask questions?"
580+
self.poll.save()
581+
self.poll.question = "eh?"
582+
self.poll.save()
583+
self.poll.question = "one more?"
584+
self.poll.save()
585+
first_record = self.poll.history.filter(question="what's up?").get()
586+
second_record = self.poll.history.filter(question="ask questions?").get()
587+
third_record = self.poll.history.filter(question="eh?").get()
588+
fourth_record = self.poll.history.filter(question="one more?").get()
589+
590+
self.assertRecordsMatch(second_record.prev_record, first_record)
591+
self.assertRecordsMatch(third_record.prev_record, second_record)
592+
self.assertRecordsMatch(fourth_record.prev_record, third_record)
593+
594+
def test_get_prev_record_none_if_only(self):
595+
self.assertEqual(self.poll.history.count(), 1)
596+
record = self.poll.history.get()
597+
self.assertIsNone(record.prev_record)
598+
599+
def test_get_prev_record_none_if_earliest(self):
600+
self.poll.question = "ask questions?"
601+
self.poll.save()
602+
first_record = self.poll.history.filter(question="what's up?").get()
603+
self.assertIsNone(first_record.prev_record)
604+
605+
def get_prev_record_with_custom_manager_name(self):
606+
instance = CustomManagerNameModel(name="Test name 1")
607+
instance.save()
608+
instance.name = "Test name 2"
609+
first_record = instance.log.filter(name="Test name").get()
610+
second_record = instance.log.filter(name="Test name 2").get()
611+
612+
self.assertRecordsMatch(second_record.prev_record, first_record)
613+
614+
def test_get_next_record(self):
615+
self.poll.question = "ask questions?"
616+
self.poll.save()
617+
self.poll.question = "eh?"
618+
self.poll.save()
619+
self.poll.question = "one more?"
620+
self.poll.save()
621+
first_record = self.poll.history.filter(question="what's up?").get()
622+
second_record = self.poll.history.filter(question="ask questions?").get()
623+
third_record = self.poll.history.filter(question="eh?").get()
624+
fourth_record = self.poll.history.filter(question="one more?").get()
625+
self.assertIsNone(fourth_record.next_record)
626+
627+
self.assertRecordsMatch(first_record.next_record, second_record)
628+
self.assertRecordsMatch(second_record.next_record, third_record)
629+
self.assertRecordsMatch(third_record.next_record, fourth_record)
630+
631+
def test_get_next_record_none_if_only(self):
632+
self.assertEqual(self.poll.history.count(), 1)
633+
record = self.poll.history.get()
634+
self.assertIsNone(record.next_record)
635+
636+
def test_get_next_record_none_if_most_recent(self):
637+
self.poll.question = "ask questions?"
638+
self.poll.save()
639+
recent_record = self.poll.history.filter(question="ask questions?").get()
640+
self.assertIsNone(recent_record.next_record)
641+
642+
def get_next_record_with_custom_manager_name(self):
643+
instance = CustomManagerNameModel(name="Test name 1")
644+
instance.save()
645+
instance.name = "Test name 2"
646+
first_record = instance.log.filter(name="Test name").get()
647+
second_record = instance.log.filter(name="Test name 2").get()
648+
649+
self.assertRecordsMatch(first_record.next_record, second_record)
650+
651+
645652
class CreateHistoryModelTests(unittest.TestCase):
646653
def test_create_history_model_with_one_to_one_field_to_integer_field(self):
647654
records = HistoricalRecords()

0 commit comments

Comments
 (0)