diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-01-07 12:06:07 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-01-11 17:30:46 +0100 |
commit | e9f6af279c98260e2bd81d1a4b34f000e9469636 (patch) | |
tree | c2d3b328e45b85a4440c7ad4cc8cd6e781c18a2f | |
parent | a87eb643c09fb8af86c0b7198cccd097eef1aac6 (diff) | |
download | Ishtar-e9f6af279c98260e2bd81d1a4b34f000e9469636.tar.bz2 Ishtar-e9f6af279c98260e2bd81d1a4b34f000e9469636.zip |
Tests: M2M history on finds - save and display
-rw-r--r-- | archaeological_finds/tests.py | 85 | ||||
-rw-r--r-- | ishtar_common/models.py | 6 | ||||
-rw-r--r-- | ishtar_common/templatetags/link_to_window.py | 1 |
3 files changed, 87 insertions, 5 deletions
diff --git a/archaeological_finds/tests.py b/archaeological_finds/tests.py index 876bbecb2..107240389 100644 --- a/archaeological_finds/tests.py +++ b/archaeological_finds/tests.py @@ -35,6 +35,7 @@ from archaeological_finds import models, views from archaeological_warehouse.models import Warehouse, WarehouseType from ishtar_common import forms_common +from ishtar_common.utils import HISTORY_M2M_SPLIT from ishtar_common.tests import WizardTest, WizardTestFormData as FormData, \ TestCase, create_user, create_superuser @@ -821,16 +822,94 @@ class FindHistoryTest(FindInit, TestCase): def setUp(self): self.create_finds(data_base={"label": u"Find 1"}, force=True) - self.create_finds(data_base={"label": u"Find 2"}, force=True) self.username, self.password, self.user = create_superuser() self.client = Client() self.client.login(username=self.username, password=self.password) def test_m2m_history_save(self): - pass + find = self.finds[0] + user = self.get_default_user() + nb_hist = find.history.count() + + ceram = models.MaterialType.objects.get(txt_idx='ceramic').pk + glass = models.MaterialType.objects.get(txt_idx='glass').pk + find.material_types.add(ceram) + find.material_types.add(glass) + find = models.Find.objects.get(pk=find.pk) + self.assertIn( + find.historical_material_types, + [HISTORY_M2M_SPLIT.join(['ceramic', 'glass']), # order do not + HISTORY_M2M_SPLIT.join(['glass', 'ceramic'])]) # matter + historical_material_types = find.historical_material_types + find.label = "hop hop hop1" + find.history_modifier = user + find._force_history = True + find.save() + + find = models.Find.objects.get(pk=find.pk) + find.material_types.remove(ceram) + find.label = "hop hop hop2" + find.history_modifier = user + if hasattr(find, 'skip_history_when_saving'): + delattr(find, 'skip_history_when_saving') + find._force_history = True + find.save() + + find = models.Find.objects.get(pk=find.pk) + self.assertEqual(find.historical_material_types, 'glass') + self.assertEqual(find.history.count(), nb_hist + 2) + self.assertEqual(find.history.all()[1].historical_material_types, + historical_material_types) + self.assertEqual(find.history.all()[0].historical_material_types, + "glass") def test_m2m_history_display(self): - pass + c = Client() + user = self.get_default_user() + find = self.finds[0] + + find = models.Find.objects.get(pk=find.pk) + ceram = models.MaterialType.objects.get(txt_idx='ceramic').pk + glass = models.MaterialType.objects.get(txt_idx='glass').pk + find.material_types.add(ceram) + find.material_types.add(glass) + find.history_modifier = user + find.label = "hop hop hop1" + find._force_history = True + if hasattr(find, 'skip_history_when_saving'): + delattr(find, 'skip_history_when_saving') + find.save() + + find = models.Find.objects.get(pk=find.pk) + find.material_types.remove(ceram) + find.history_modifier = user + find.label = "hop hop hop2" + find._force_history = True + if hasattr(find, 'skip_history_when_saving'): + delattr(find, 'skip_history_when_saving') + find.save() + + find = models.Find.objects.get(pk=find.pk) + history_date = find.history.all()[1].history_date.strftime( + '%Y-%m-%dT%H:%M:%S.%f') + + c.login(username=self.username, password=self.password) + response = c.get(reverse('show-historized-find', + kwargs={'pk': find.pk})) + self.assertEqual(response.status_code, 200) + self.assertIn('class="card sheet"', response.content) + self.assertNotIn( + models.MaterialType.objects.get(txt_idx='ceramic').label, + response.content.decode('utf-8')) + + response = c.get(reverse('show-historized-find', + kwargs={'pk': find.pk, + 'date': history_date})) + self.assertEqual(response.status_code, 200) + self.assertIn('class="card sheet"', response.content) + self.assertIn( + models.MaterialType.objects.get(txt_idx='ceramic').label, + response.content.decode('utf-8')) def test_m2m_history_restore(self): pass diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 319f19539..c7223c898 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -175,9 +175,13 @@ class HistoricalRecords(BaseHistoricalRecords): q = q_history.filter(history_date__isnull=False, history_date__gt=min_history_date) \ .order_by('-history_date', '-history_id') - if q.count(): + force = getattr(instance, "_force_history", False) + if not force and q.count(): return + if force: + delattr(instance, '_force_history') + if 'history_date' not in attrs or not attrs['history_date']: attrs['history_date'] = datetime.datetime.now() # record a new version only if data have been changed diff --git a/ishtar_common/templatetags/link_to_window.py b/ishtar_common/templatetags/link_to_window.py index 892492895..439602665 100644 --- a/ishtar_common/templatetags/link_to_window.py +++ b/ishtar_common/templatetags/link_to_window.py @@ -30,7 +30,6 @@ def link_to_window(item, context): else: return u"" if not item.can_view(request): - print(item, "NOK3") return u"" return simple_link_to_window(item) |