def test_LocationSiteFormUpdateView_allow_edit(self):
        user = UserF.create(id=1)
        self.client.login(username=user.username, password='******')
        location_site = LocationSiteF.create(creator=user, )
        post_data = self.post_data
        post_data['id'] = location_site.id
        post_request = self.client.post(
            '/location-site-form/update/?id={}'.format(location_site.id),
            post_data,
            follow=True)
        # Test if user is not the creator
        location_site_2 = LocationSiteF.create()
        post_data['id'] = location_site_2.id
        post_request_2 = self.client.post(
            '/location-site-form/update/?id={}'.format(location_site_2.id),
            self.post_data,
            follow=True)
        # Test if user is the owner but not the creator
        location_site_3 = LocationSiteF.create(owner=user, creator=None)
        post_data['id'] = location_site_3.id
        post_request_3 = self.client.post(
            '/location-site-form/update/?id={}'.format(location_site_3.id),
            self.post_data,
            follow=True)

        self.assertEqual(post_request.status_code, 200)
        self.assertEqual(post_request_2.status_code, 404)
        self.assertEqual(post_request_3.status_code, 200)
    def test_LocationSiteFormUpdateView_post_data(self):
        location_context_path = staticfiles_storage.path(
            'data/location_context_document.json')
        location_context_file = open(location_context_path)
        location_context_document = json.load(location_context_file)
        post_data = self.post_data
        post_data['refined_geomorphological_zone'] = ''
        loc_type = LocationTypeF(name='PointObservation',
                                 allowed_geometry='POINT')

        user = UserF.create(id=1)
        self.client.login(
            username=user.username,
            password='******',
        )
        location_site = LocationSiteF.create(
            location_type=loc_type,
            creator=user,
            location_context_document=location_context_document)
        post_data['id'] = location_site.id

        self.client.post('/location-site-form/update/?id={}'.format(
            location_site.id),
                         post_data,
                         follow=True)
        updated_location_site = LocationSite.objects.get(id=location_site.id)
        updated_location_context = LocationContext.objects.filter(
            site=location_site)
        self.assertEqual(updated_location_site.river.name, 'NXAMAGELE')
        self.assertTrue(
            updated_location_context.filter(
                group__key__icontains='catchment_area').exists())
        self.assertTrue(
            updated_location_context.filter(
                group__key='geo_class_recoded').exists())
        self.assertTrue(
            updated_location_context.value_from_key('geo_class_recoded') ==
            'Mountain headwater stream')

        # Test if there are no location context data
        location_site_2 = LocationSiteF.create(
            location_type=loc_type,
            creator=user,
        )
        post_data['id'] = location_site_2.id
        post_request_2 = self.client.post(
            '/location-site-form/update/?id={}'.format(location_site_2.id),
            post_data,
            follow=True)
        updated_location_context_2 = LocationContext.objects.filter(
            site=location_site_2)
        self.assertTrue(
            updated_location_context_2.filter(
                group__key__icontains='catchment_area').exists())
        self.assertTrue(
            updated_location_context_2.value_from_key('geo_class_recoded') ==
            'Mountain headwater stream')
        self.assertTrue(
            post_request_2.redirect_chain[0][0],
            '/location-site-form/update/?id={}'.format(location_site_2.id))
Example #3
0
 def setUp(self):
     """
     Sets up before each test
     """
     self.location_site_1 = LocationSiteF.create(pk=1)
     self.location_site_2 = LocationSiteF.create(pk=2)
     pass
 def test_LocationSiteFormView_delete(self):
     loc_type = LocationTypeF(name='PointObservation',
                              allowed_geometry='POINT')
     user = UserF.create(id=1)
     location_site_2 = LocationSiteF.create(
         location_type=loc_type,
         creator=user,
     )
     self.client.login(
         username=user.username,
         password='******',
     )
     post_data = {}
     post_request_2 = self.client.post(
         '/location-site-form/delete/{}/'.format(location_site_2.id),
         post_data,
         follow=True)
     self.assertFalse(
         LocationSite.objects.filter(id=location_site_2.id).exists())
     self.assertEqual(post_request_2.redirect_chain[0][0],
                      reverse('location-site-form'))
     location_site_3 = LocationSiteF.create(location_type=loc_type, )
     self.client.post('/location-site-form/delete/{}/'.format(
         location_site_3.id),
                      post_data,
                      follow=True)
     self.assertTrue(
         LocationSite.objects.filter(id=location_site_3.id).exists())
Example #5
0
 def test_LocationSite_update_location_context_document(self, mock_get):
     """Test updating location context document"""
     location_site = LocationSiteF.create(geometry_point=Point(0, 0))
     self.assertIsNone(location_site.location_context_document)
     old_point = {
         'geometry_point': Point(27, -31),
     }
     location_site.__dict__.update(old_point)
     # update_location_context_document is called here
     location_site.save()
     old_context = location_site.location_context_document
     self.assertIsNotNone(old_context)
     self.assertEqual(old_context, json.dumps(first_json_data))
     self.assertEqual(json.loads(old_context), first_json_data)
     new_point = {
         'geometry_point': Point(26, -30),
     }
     location_site.__dict__.update(new_point)
     # update_location_context_document is called here
     location_site.save()
     self.assertIsNotNone(location_site.location_context_document)
     self.assertNotEqual(
         location_site.location_context_document, old_context)
     self.assertEqual(
         location_site.location_context_document, json.dumps(
             second_json_data))
Example #6
0
    def test_LocationSite_read(self):
        """
        Tests location site model read
        """
        location_type = LocationTypeF.create(name=u'custom type', )
        model = LocationSiteF.create(location_type=location_type)

        self.assertTrue(model.location_type.name == 'custom type')
Example #7
0
    def test_LocationSite_delete(self):
        """
        Tests location site model delete
        """
        model = LocationSiteF.create()
        model.delete()

        # check if deleted
        self.assertTrue(model.pk is None)
Example #8
0
    def test_LocationSite_update_lat_lon_manually(self):
        location_site = LocationSiteF.create()
        new_data = {
            'latitude': -33.3,
            'longitude': 25,
        }
        location_site.__dict__.update(new_data)

        # check if updated
        for key, val in new_data.items():
            self.assertEqual(location_site.__dict__.get(key), val)
Example #9
0
 def setUp(self):
     self.factory = APIRequestFactory()
     self.location_site = LocationSiteF.create(
         pk=1, location_context_document='""')
     self.fish_collection_1 = BiologicalCollectionRecordF.create(
         pk=1,
         original_species_name=u'Test fish species name 1',
         site=self.location_site)
     self.fish_collection_2 = BiologicalCollectionRecordF.create(
         pk=2,
         original_species_name=u'Test fish species name 2',
         site=self.location_site)
Example #10
0
    def test_LocationSite_update_not_in_allowed_geometry(self):
        """
        Tests location site model update if geometry is not in allowed
        geometry
        """
        location_site = LocationSiteF.create()
        new_data = {
            'geometry_point': None,
            'geometry_line': LineString((1, 1), (2, 2)),
        }
        location_site.__dict__.update(new_data)

        # check if validation error raised
        self.assertRaises(ValidationError, location_site.save)
Example #11
0
    def test_LocationSite_create(self):
        """
        Tests location site creation
        """
        model = LocationSiteF.create()

        # check if pk exists
        self.assertTrue(model.pk is not None)

        # check if location_type exists
        self.assertTrue(model.location_type is not None)

        # check if geometry exists
        self.assertTrue(model.geometry_point is not None)
Example #12
0
    def test_LocationSite_update(self):
        """
        Tests location site model update
        """
        location_type = LocationTypeF.create(name=u'custom type', )
        model = LocationSiteF.create()
        new_data = {
            'location_type': location_type,
        }
        model.__dict__.update(new_data)
        model.save()

        # check if updated
        for key, val in new_data.items():
            self.assertEqual(model.__dict__.get(key), val)
Example #13
0
    def setUp(self):
        self.factory = APIRequestFactory()
        self.location_site = LocationSiteF.create(
            pk=1, location_context_document='""')

        self.taxonomy_class_1 = TaxonomyF.create(scientific_name='Aves',
                                                 rank=TaxonomicRank.CLASS.name)
        self.taxonomy_1 = TaxonomyF.create(scientific_name='Some aves name 1',
                                           canonical_name='aves name 1',
                                           rank=TaxonomicRank.SPECIES.name,
                                           parent=self.taxonomy_class_1)
        self.taxonomy_2 = TaxonomyF.create(scientific_name='Some aves name 2',
                                           canonical_name='aves name 2',
                                           rank=TaxonomicRank.SPECIES.name,
                                           parent=self.taxonomy_class_1)
        self.aves_collection_1 = BiologicalCollectionRecordF.create(
            original_species_name=u'Aves collection 1',
            site=self.location_site,
            validated=True,
            ready_for_validation=True,
            taxonomy=self.taxonomy_1)
        self.aves_collection_2 = BiologicalCollectionRecordF.create(
            original_species_name=u'Aves collection 2',
            site=self.location_site,
            validated=True,
            ready_for_validation=True,
            taxonomy=self.taxonomy_2)

        self.fish_collection_1 = BiologicalCollectionRecordF.create(
            original_species_name=u'Test fish species name 1',
            site=self.location_site,
            validated=True)
        self.fish_collection_2 = BiologicalCollectionRecordF.create(
            original_species_name=u'Test fish species name 2',
            site=self.location_site,
            validated=True)
        self.admin_user = UserF.create(is_superuser=True, is_staff=True)
        self.rebuild_index()
Example #14
0
 def setUp(self):
     """
     Sets up before each test
     """
     self.client = Client()
     self.location_site = LocationSiteF.create()
Example #15
0
 def test_site_visit_form_view(self):
     site = LocationSiteF.create()
     site_visit = SiteVisitF.create(location_site=site)
     response = self.client.get('/sass/view/{}/'.format(site_visit.id))
     self.assertEqual(response.context['sass_version'], 5)