From 204263441e8f1dc11dff1e407233c674b4e48184 Mon Sep 17 00:00:00 2001 From: Antoine Auger Date: Mon, 27 Mar 2023 12:09:03 +0200 Subject: [PATCH] test(m2m-fields): add tests that should pass --- simple_history/tests/admin.py | 2 ++ simple_history/tests/models.py | 22 ++++++++++++++++++++++ simple_history/tests/tests/test_models.py | 7 +++++++ 3 files changed, 31 insertions(+) diff --git a/simple_history/tests/admin.py b/simple_history/tests/admin.py index 24cf5f7ac..a625edcde 100644 --- a/simple_history/tests/admin.py +++ b/simple_history/tests/admin.py @@ -10,6 +10,7 @@ Document, Employee, FileModel, + Group, Paper, Person, Planet, @@ -43,6 +44,7 @@ def test_method(self, obj): history_list_display = ["title", "test_method"] +admin.site.register(Group, SimpleHistoryAdmin) admin.site.register(Poll, SimpleHistoryAdmin) admin.site.register(Choice, ChoiceAdmin) admin.site.register(Person, PersonAdmin) diff --git a/simple_history/tests/models.py b/simple_history/tests/models.py index 99c6a2f87..6f1375e12 100644 --- a/simple_history/tests/models.py +++ b/simple_history/tests/models.py @@ -145,6 +145,28 @@ class PollWithManyToMany(models.Model): history = HistoricalRecords(m2m_fields=[places]) +class Membership(models.Model): + group = models.ForeignKey("Group", on_delete=models.CASCADE) + person = models.ForeignKey("Person", on_delete=models.CASCADE) + inviter = models.ForeignKey( + "Person", + on_delete=models.CASCADE, + related_name="membership_invites", + ) + invite_reason = models.CharField(max_length=64) + + +class Group(models.Model): + name = models.CharField(max_length=128) + members = models.ManyToManyField( + "Person", + through="tests.Membership", + through_fields=("group", "person"), + ) + + history = HistoricalRecords(m2m_fields=[members]) + + class PollWithManyToManyCustomHistoryID(models.Model): question = models.CharField(max_length=200) pub_date = models.DateTimeField("date published") diff --git a/simple_history/tests/tests/test_models.py b/simple_history/tests/tests/test_models.py index 484df73f9..d5228100f 100644 --- a/simple_history/tests/tests/test_models.py +++ b/simple_history/tests/tests/test_models.py @@ -101,6 +101,7 @@ PollWithHistoricalIPAddress, PollWithManyToMany, PollWithManyToManyCustomHistoryID, + Group, PollWithManyToManyWithIPAddress, PollWithNonEditableField, PollWithSelfManyToMany, @@ -1939,6 +1940,12 @@ def setUp(self): self.poll = self.model.objects.create(question="what's up?", pub_date=today) +class GroupWithManyToManyThroughFieldsTest(TestCase): + def setUp(self): + self.model = Group + self.history_model = self.model.history.model + + class ManyToManyTest(TestCase): def setUp(self): self.model = PollWithManyToMany