summaryrefslogtreecommitdiff
path: root/archaeological_operations/tests.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2024-10-16 11:20:06 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2024-10-16 11:20:06 +0200
commit6221b6260a6b97a03557acbdfcbed2c26304ed3f (patch)
treee5ba182a96b6f27a436c31c5198bf91d9ce39a0d /archaeological_operations/tests.py
parentf4c049914fbe5d9cbf88fc265ff6e9dd4876fe8b (diff)
downloadIshtar-6221b6260a6b97a03557acbdfcbed2c26304ed3f.tar.bz2
Ishtar-6221b6260a6b97a03557acbdfcbed2c26304ed3f.zip
✅ test site-operation relations formset
Diffstat (limited to 'archaeological_operations/tests.py')
-rw-r--r--archaeological_operations/tests.py95
1 files changed, 94 insertions, 1 deletions
diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py
index b26ad4bcb..b0b9c9dae 100644
--- a/archaeological_operations/tests.py
+++ b/archaeological_operations/tests.py
@@ -1760,7 +1760,7 @@ class ParcelTest(ImportTest, TestCase):
nb_parcels = models.Parcel.objects.all().count()
town_pk = models.Town.objects.all().first().pk
data = {}
- for idx in range(nb_parcels + views.PARCEL_FORMSET_EXTRA_FORM):
+ for idx in range(nb_parcels + views.RELATION_FORMSET_EXTRA_FORM):
data.update({
f"form-{idx}-pk": "",
f"form-{idx}-year": "",
@@ -2580,6 +2580,99 @@ class OperationTest(TestCase, OperationInitTest):
values = operation.get_containers_values([], [])
self.assertEqual(len(values), 1)
+ def formset_init_data(self, model):
+ nb_items = model.objects.all().count()
+ length = nb_items + views.RELATION_FORMSET_EXTRA_FORM
+ data = {
+ 'form-TOTAL_FORMS': length,
+ 'form-INITIAL_FORMS': 0,
+ 'form-MIN_NUM_FORMS': 0,
+ 'form-MAX_NUM_FORMS': 100,
+ }
+ for idx in range(length):
+ data.update({
+ f"form-{idx}-right_record": "",
+ f"form-{idx}-DELETE": '',
+ })
+
+ return data
+
+ def test_operation_site_formset(self):
+ ope1, ope2 = self.create_operation()[:2]
+ site1 = models.ArchaeologicalSite.objects.create(reference="ref-site")
+ site2 = models.ArchaeologicalSite.objects.create(reference="ref-site2")
+ ope1.archaeological_sites.add(site1)
+ c = Client()
+
+ c.login(username=self.alt_username, password=self.alt_password)
+ url = reverse("operation-site-relations-modify", kwargs={"pk": ope1.pk})
+ response = c.get(url)
+ self.assertRedirects(response, "/") # permission failed, redirect to index
+
+ c.login(username=self.username, password=self.password)
+ response = c.get(url)
+ self.assertEqual(response.status_code, 200)
+ self.assertContains(response, site1.reference)
+
+ # add a site
+ data = self.formset_init_data(models.ArchaeologicalSite)
+ nb_relation_initial = ope1.archaeological_sites.count()
+ data["form-1-right_record"] = site2.pk
+ # form-0 not initialized: has DELETE is not checked, this should not
+ # delete the relation
+ post_response = c.post(url, data)
+ self.assertEqual(post_response.status_code, 200)
+ self.assertEqual(ope1.archaeological_sites.count(), nb_relation_initial + 1)
+ lst = [s.pk for s in ope1.archaeological_sites.all()]
+ self.assertIn(site1.pk, lst)
+ self.assertIn(site2.pk, lst)
+
+ # delete all
+ data["form-0-right_record"] = site1.pk
+ data["form-0-DELETE"] = 1
+ data["form-1-DELETE"] = 1
+ post_response = c.post(url, data)
+ self.assertEqual(post_response.status_code, 200)
+ self.assertEqual(ope1.archaeological_sites.count(), 0)
+
+ def test_site_operation_formset(self):
+ ope1, ope2 = self.create_operation()[:2]
+ site1 = models.ArchaeologicalSite.objects.create(reference="ref-site")
+ site2 = models.ArchaeologicalSite.objects.create(reference="ref-site2")
+ ope1.archaeological_sites.add(site1)
+ c = Client()
+
+ c.login(username=self.alt_username, password=self.alt_password)
+ url = reverse("site-operation-relations-modify", kwargs={"pk": site1.pk})
+ response = c.get(url)
+ self.assertRedirects(response, "/") # permission failed, redirect to index
+
+ c.login(username=self.username, password=self.password)
+ response = c.get(url)
+ self.assertEqual(response.status_code, 200)
+ self.assertContains(response, ope1.code_patriarche)
+
+ # add a site
+ data = self.formset_init_data(models.Operation)
+ nb_relation_initial = site1.operations.count()
+ data["form-1-right_record"] = ope2.pk
+ # form-0 not initialized: has DELETE is not checked, this should not
+ # delete the relation
+ post_response = c.post(url, data)
+ self.assertEqual(post_response.status_code, 200)
+ self.assertEqual(site1.operations.count(), nb_relation_initial + 1)
+ lst = [o.pk for o in site1.operations.all()]
+ self.assertIn(ope1.pk, lst)
+ self.assertIn(ope2.pk, lst)
+
+ # delete all
+ data["form-0-right_record"] = ope1.pk
+ data["form-0-DELETE"] = 1
+ data["form-1-DELETE"] = 1
+ post_response = c.post(url, data)
+ self.assertEqual(post_response.status_code, 200)
+ self.assertEqual(site1.operations.count(), 0)
+
class LockTest(TestCase, OperationInitTest):
fixtures = FILE_FIXTURES