Coverage for apis_core/history/tests/test_simple_history.py: 100%
89 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-22 07:51 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-22 07:51 +0000
1from datetime import datetime, timedelta
3from django.contrib.contenttypes.models import ContentType
4from django.test import TestCase
6from apis_core.apis_relations.models import Property, TempTriple
7from sample_project.models import Person, Place, Profession
10class SimpleHistoryTestCase(TestCase):
11 """Test of the simple_history package using the demo project"""
13 def setUp(self):
14 self.Person = Person
15 self.Place = Place
16 self.Profession = Profession
18 prop = Property.objects.create(
19 name_forward="geboren in", name_reverse="Geburtsort von"
20 )
21 prop.subj_class.add(ContentType.objects.get(model="person"))
22 prop.obj_class.add(ContentType.objects.get(model="place"))
23 Person.objects.create(forename="John", surname="Doe")
24 Place.objects.create(
25 label="Steyr", _history_date=datetime.now() - timedelta(hours=0, minutes=50)
26 )
28 def test_history(self):
29 """Tests the simple version of attributes changed on a models instance."""
30 pers = self.Person.objects.get(forename="John")
31 pers.forename = "Jane"
32 pers.save()
33 self.assertEqual(pers.forename, "Jane")
34 pers_history = pers.history.all()
35 assert len(pers_history) == 2
36 self.assertEqual("Jane", pers.history.most_recent().forename)
37 self.assertEqual("John", pers.history.earliest().forename)
39 def test_history_through_triples(self):
40 """Tests the newly introduced function for retrieving triples for a specific version of a model instance."""
41 # first entity
42 pers = self.Person.objects.get(forename="John")
43 # second entity
44 place = self.Place.objects.first()
45 # create a triple
46 tt1 = TempTriple.objects.create(
47 subj=pers, prop=Property.objects.first(), obj=place
48 )
49 # test that we get one triple version for both entities
50 self.assertEqual(len(pers.history.earliest().get_triples_for_version()), 1)
51 self.assertEqual(len(place.history.earliest().get_triples_for_version()), 1)
52 # change the triple and test again
53 tt1.start_date_written = "2020-01-01"
54 tt1.save()
55 triples = pers.history.earliest().get_triples_for_version()
56 self.assertEqual(len(triples), 1)
57 self.assertEqual(triples[0].start_date_written, "2020-01-01")
58 # change the place and test again
59 place2 = self.Place.objects.create(label="testplace")
60 tt1.obj = place2
61 tt1.save()
62 triples = place.history.earliest().get_triples_for_version()
63 self.assertEqual(len(triples), 1)
64 self.assertEqual(triples[0].obj, place)
65 triples = place2.history.earliest().get_triples_for_version()
66 self.assertEqual(len(triples), 1)
67 self.assertEqual(triples[0].obj, place2)
68 # given that the function should return the latest version of a triple, we should get the new place
69 triples = pers.history.earliest().get_triples_for_version()
70 self.assertEqual(len(triples), 1)
71 self.assertEqual(triples[0].obj, place2)
72 # test retrieving triples for a specific datetime
73 triples = pers.history.earliest().get_triples_for_version(
74 history_date=datetime.now() - timedelta(hours=0, minutes=40)
75 )
76 self.assertEqual(len(triples), 0)
78 def test_history_tag(self):
79 """Tests the version tag function."""
80 pers = self.Person.objects.get(forename="John")
81 pers.history.latest().set_version_tag("test_tag")
82 self.assertEqual(pers.history.latest().version_tag, "test_tag")
83 pers_history = pers.history.all()
84 self.assertEqual(pers_history.count(), 1)
85 self.assertEqual(pers_history[0].version_tag, "test_tag")
86 # test with TemTriple
87 pers2 = self.Person.objects.create(forename="Jane", surname="Doe")
88 place = self.Place.objects.first()
89 TempTriple.objects.create(subj=pers2, prop=Property.objects.first(), obj=place)
90 pers2.history.latest().set_version_tag("test_tag")
91 self.assertEqual(pers2.history.latest().version_tag, "test_tag")
92 triples = pers2.history.earliest().get_triples_for_version()
93 self.assertEqual(triples[0].version_tag, "test_tag")
95 def test_history_delete_entry(self):
96 """Tests the deletion of an entry."""
97 pers = self.Person.objects.get(forename="John")
98 pers.delete()
99 assert len(self.Person.history.all()) == 2
101 def test_history_merge(self):
102 """Tests the merge function of the Place model. This is still expected to fail."""
103 pl1 = self.Place.objects.first()
104 pl2 = self.Place.objects.create(
105 label="Test", _history_date=datetime.now() - timedelta(hours=0, minutes=10)
106 )
107 pl1.merge_with([pl2])
108 print("save()")
110 def test_m2m_save(self):
111 """Test if m2m profession is saved correctly."""
112 pers = self.Person.objects.create(
113 forename="John",
114 _history_date=datetime.now() - timedelta(hours=0, minutes=10),
115 )
116 self.assertEqual(pers.forename, "John")
117 pers_history = pers.history.all()
118 assert len(pers_history) == 1
119 prof = self.Profession.objects.create(name="Test")
120 pers.profession.add(prof)
121 pers.forename = "Jane"
122 pers.save()
123 assert len(pers.profession.all()) == 1
124 assert len(pers.history.latest().profession.all()) == 1
125 assert len(pers.history.earliest().profession.all()) == 0
127 def test_history_date(self):
128 """Test that history is set correctly when not manually set."""
129 pers = self.Person.objects.all().first()
130 pers.forename = "Jane"
131 pers.save()
132 assert pers.history.earliest().history_date < pers.history.latest().history_date