diff options
Diffstat (limited to 'archaeological_operations/tests.py')
| -rw-r--r-- | archaeological_operations/tests.py | 131 | 
1 files changed, 100 insertions, 31 deletions
| diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index 9a61d8bbb..ba7722d84 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -566,7 +566,73 @@ class OperationTest(TestCase, OperationInitTest):          parcel.save()          self.assertEqual(parcel.external_id, 'blabla') -    def testSearch(self): +    def create_relations(self): +        rel1 = models.RelationType.objects.create( +            symmetrical=True, label='Include', txt_idx='include') +        rel2 = models.RelationType.objects.create( +            symmetrical=False, label='Included', txt_idx='included', +            inverse_relation=rel1) +        models.RecordRelations.objects.create( +            left_record=self.operations[0], +            right_record=self.operations[1], +            relation_type=rel1) +        return rel1, rel2 + +    def testPostDeleteRelations(self): +        self.create_relations() +        self.operations[0].delete() + +    def testPostDeleteParcels(self): +        ope = self.operations[0] +        town = Town.objects.create(name='plouf', numero_insee='20000') +        parcel = models.Parcel.objects.create(town=town) +        parcel_nb = models.Parcel.objects.count() +        ope.parcels.add(parcel) +        ope.delete() +        # our parcel has no operation attached and should be deleted +        self.assertEqual(parcel_nb - 1, models.Parcel.objects.count()) +        ope = self.operations[1] +        parcel = models.Parcel.objects.create(town=town) +        parcel_nb = models.Parcel.objects.count() +        ope.parcels.add(parcel) +        ope.parcels.clear()  # no signal raised... should resave +        models.Parcel.objects.filter(pk=parcel.pk).all()[0].save() +        # our parcel has no operation attached and should be deleted +        self.assertEqual(parcel_nb - 1, models.Parcel.objects.count()) + +    def testIndex(self): +        ope = create_operation(self.user, values={'year': 2042}) +        self.assertEqual(ope.operation_code, 1) +        ope2 = create_operation(self.user, values={'year': 2042}) +        self.assertEqual(ope2.operation_code, 2) +        ope = create_operation(self.user, values={'year': 0}) +        self.assertEqual(ope.operation_code, 1) +        ope2 = create_operation(self.user, values={'year': 0}) +        self.assertEqual(ope2.operation_code, 2) + + +class OperationSearchTest(TestCase, OperationInitTest): +    fixtures = [settings.ROOT_PATH + +                '../fixtures/initial_data-auth-fr.json', +                settings.ROOT_PATH + +                '../ishtar_common/fixtures/initial_data-fr.json', +                settings.ROOT_PATH + +                '../archaeological_files/fixtures/initial_data.json', +                settings.ROOT_PATH + +                '../archaeological_operations/fixtures/initial_data-fr.json'] + +    def setUp(self): +        IshtarSiteProfile.objects.create() +        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) +        self.operations = self.create_operation(self.user, self.orgas[0]) +        self.operations += self.create_operation(self.alt_user, self.orgas[0]) +        self.item = self.operations[0] + +    def testBaseSearch(self):          c = Client()          response = c.get(reverse('get-operation'), {'year': '2010'})          # no result when no authentification @@ -603,27 +669,40 @@ class OperationTest(TestCase, OperationInitTest):          response = c.get(reverse('get-operation'), search)          self.assertTrue(json.loads(response.content)['total'] == 2) -    def testPostDeleteRelations(self): -        self.create_relations() -        self.operations[0].delete() - -    def testPostDeleteParcels(self): -        ope = self.operations[0] -        town = Town.objects.create(name='plouf', numero_insee='20000') -        parcel = models.Parcel.objects.create(town=town) -        parcel_nb = models.Parcel.objects.count() -        ope.parcels.add(parcel) -        ope.delete() -        # our parcel has no operation attached and should be deleted -        self.assertEqual(parcel_nb - 1, models.Parcel.objects.count()) +    def testHierarchicSearch(self):          ope = self.operations[1] -        parcel = models.Parcel.objects.create(town=town) -        parcel_nb = models.Parcel.objects.count() -        ope.parcels.add(parcel) -        ope.parcels.clear()  # no signal raised... should resave -        models.Parcel.objects.filter(pk=parcel.pk).all()[0].save() -        # our parcel has no operation attached and should be deleted -        self.assertEqual(parcel_nb - 1, models.Parcel.objects.count()) +        c = Client() + +        neo = models.Period.objects.get(txt_idx='neolithic') +        final_neo = models.Period.objects.get(txt_idx='final_neolithic') +        recent_neo = models.Period.objects.get(txt_idx='recent_neolithic') +        ope.periods.add(final_neo) + +        search = {'periods': final_neo.pk} + +        # no result when no authentication +        response = c.get(reverse('get-operation'), search) +        self.assertEqual(response.status_code, 200) +        self.assertTrue(not json.loads(response.content)) +        c.login(username=self.username, password=self.password) + +        # one result for exact search +        response = c.get(reverse('get-operation'), search) +        self.assertEqual(response.status_code, 200) +        self.assertEqual(json.loads(response.content)['total'], 1) + +        # no result for the brother +        search = {'periods': recent_neo.pk} +        response = c.get(reverse('get-operation'), search) +        self.assertEqual(response.status_code, 200) +        self.assertEqual(json.loads(response.content)['total'], 0) + +        # one result for the father +        search = {'periods': neo.pk} +        response = c.get(reverse('get-operation'), search) +        self.assertEqual(response.status_code, 200) +        self.assertEqual(json.loads(response.content)['total'], 1) +      def testOwnSearch(self):          c = Client() @@ -638,16 +717,6 @@ class OperationTest(TestCase, OperationInitTest):                           {'operator': self.orgas[0].pk})          self.assertTrue(json.loads(response.content)['total'] == 1) -    def testIndex(self): -        ope = create_operation(self.user, values={'year': 2042}) -        self.assertEqual(ope.operation_code, 1) -        ope2 = create_operation(self.user, values={'year': 2042}) -        self.assertEqual(ope2.operation_code, 2) -        ope = create_operation(self.user, values={'year': 0}) -        self.assertEqual(ope.operation_code, 1) -        ope2 = create_operation(self.user, values={'year': 0}) -        self.assertEqual(ope2.operation_code, 2) -  def create_administrativact(user, operation):      act_type, created = models.ActType.objects.get_or_create( | 
