From cc4b9d92baf57f29ff57fec6f9fdda3d6d4602e0 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Mon, 19 Feb 2018 17:41:32 +0100 Subject: Creation of virtual operation for site (refs #3935) --- archaeological_operations/tests.py | 89 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) (limited to 'archaeological_operations/tests.py') diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index 947be204e..8fe1d8850 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -1926,3 +1926,92 @@ class OperationSourceWizardModificationTest(WizardTest, OperationInitTest, def post_wizard(self): source = models.OperationSource.objects.get(pk=self.source.pk) self.assertEqual(source.authors.count(), 0) + + +class SiteTest(TestCase, OperationInitTest): + fixtures = FILE_FIXTURES + + def setUp(self): + IshtarSiteProfile.objects.get_or_create( + slug='default', active=True) + self.username, self.password, self.user = create_superuser() + self.alt_username, self.alt_password, self.alt_user = create_user() + self.alt_user.user_permissions.add(Permission.objects.get( + codename='view_own_operation')) + self.orgas = self.create_orgas(self.user) + + def test_create_or_update_top_operation(self): + operation_0 = self.create_operation(self.user, self.orgas[0])[0] + operation_1 = self.create_operation(self.alt_user, self.orgas[0])[1] + site = models.ArchaeologicalSite.objects.create( + reference="ref-site" + ) + site.create_or_update_top_operation() + q = models.ArchaeologicalSite.objects.filter(reference='ref-site') + site = q.all()[0] + # creation not forced - no creation + self.assertIsNone(site.top_operation) + + site.create_or_update_top_operation(create=True) + site = q.all()[0] + # a default operation has been created + self.assertIsNotNone(site.top_operation) + self.assertTrue(site.top_operation.virtual_operation) + self.assertEqual(site.top_operation.right_relations.count(), 0) + + # create with one operation attached + site.top_operation.delete() + site = q.all()[0] + site.operations.add(operation_0) + site.create_or_update_top_operation(create=True) + site = q.all()[0] + self.assertIsNotNone(site.top_operation) + self.assertTrue(site.top_operation.virtual_operation) + self.assertEqual(site.top_operation.right_relations.count(), 1) + self.assertEqual( + site.top_operation.right_relations.all()[0].right_record, + operation_0 + ) + + # create with two operations attached + site.top_operation.delete() + site = q.all()[0] + site.operations.add(operation_0) + site.operations.add(operation_1) + site.create_or_update_top_operation(create=True) + site = q.all()[0] + self.assertIsNotNone(site.top_operation) + self.assertTrue(site.top_operation.virtual_operation) + self.assertEqual(site.top_operation.right_relations.count(), 2) + attached = [ + rel.right_record + for rel in site.top_operation.right_relations.all() + ] + self.assertIn(operation_0, attached) + self.assertIn(operation_1, attached) + + # detach one operation + site.operations.remove(operation_1) + site.create_or_update_top_operation() + site = q.all()[0] + self.assertIsNotNone(site.top_operation) + self.assertTrue(site.top_operation.virtual_operation) + self.assertEqual(site.top_operation.right_relations.count(), 1) + self.assertEqual( + site.top_operation.right_relations.all()[0].right_record, + operation_0 + ) + + # reattach it + site.operations.add(operation_1) + site.create_or_update_top_operation() + site = q.all()[0] + self.assertIsNotNone(site.top_operation) + self.assertTrue(site.top_operation.virtual_operation) + self.assertEqual(site.top_operation.right_relations.count(), 2) + attached = [ + rel.right_record + for rel in site.top_operation.right_relations.all() + ] + self.assertIn(operation_0, attached) + self.assertIn(operation_1, attached) -- cgit v1.2.3