def test_features_filter_by_properties_with_several_string_field(self): layer = LayerFactory() FeatureFactory( layer=layer, geom=GEOSGeometry(json.dumps(self.fake_geometry)), properties={ 'text': 'foobar', 'sentence': 'foobar is here' }, ) FeatureFactory( layer=layer, geom=GEOSGeometry(json.dumps(self.fake_geometry)), properties={ 'text': 'foo', 'sentence': 'foobar is missing' }, ) FeatureFactory( layer=layer, geom=GEOSGeometry(json.dumps(self.fake_geometry)), properties={ 'text': 'foobar', 'sentence': 'foobar is here' }, ) response = self.client.get( reverse('terra:feature-list', kwargs={'layer': layer.pk}), { 'properties__text': 'foobar', 'properties__sentence': 'foobar is here' }) self.assertEqual(response.status_code, HTTP_200_OK) json_response = response.json() self.assertEqual(json_response['count'], 2) self.assertEqual(len(json_response['results']), 2)
def test_shapefile_same_import_export(self): self.user.user_permissions.add( Permission.objects.get(codename='can_import_layers')) self.user.user_permissions.add( Permission.objects.get(codename='can_export_layers')) FeatureFactory(layer=self.layer, properties={'key1': [{ 'key3': 'hello world', }]}) shape_url = reverse('terra:layer-shapefile', args=[ self.layer.pk, ]) response = self.client.get(shape_url) self.assertEqual(HTTP_200_OK, response.status_code) shapefile = SimpleUploadedFile('shapefile-WGS84.zip', response.content) new_layer = LayerFactory() response = self.client.post( reverse('terra:layer-shapefile', args=[ new_layer.pk, ]), { 'shapefile': shapefile, }) self.assertEqual(HTTP_200_OK, response.status_code) self.assertEqual(self.layer.features.first().properties, new_layer.features.first().properties)
def test_features_intersections(self): layer = LayerFactory(group=self.group_name) FeatureFactory(layer=layer, geom=GEOSGeometry( json.dumps(self.intersect_ref_geometry))) """The layer below must intersect""" response = self.client.post( reverse('terra:layer-intersects', args=[ layer.pk, ]), {'geom': json.dumps(self.intersect_geometry)}) self.assertEqual(HTTP_200_OK, response.status_code) response = response.json().get('results', {}) self.assertEqual(1, len(response.get('features'))) self.assertDictEqual(self.intersect_ref_geometry, response.get('features')[0].get('geometry')) """The layer below must NOT intersect""" response = self.client.post( reverse('terra:layer-intersects', args=[ layer.name, ]), {'geom': json.dumps(self.fake_geometry)}) self.assertEqual(HTTP_200_OK, response.status_code) response = response.json().get('results', {}) self.assertEqual(0, len(response.get('features'))) """Tests that the intersects view throw an error if geometry is invalid """ response = self.client.post( reverse('terra:layer-intersects', args=[ layer.pk, ]), {'geom': '''Invalid geometry'''}) self.assertEqual(HTTP_400_BAD_REQUEST, response.status_code)
def test_no_permission(self): FeatureFactory(layer=self.layer, properties={'a': 'b'}) response = self.client.patch( reverse('terra:layer-detail', args=[ self.layer.name, ]), {}) self.assertEqual(HTTP_403_FORBIDDEN, response.status_code)
def test_to_geojson_no_permission(self): # Create at least one feature in the layer, so it's not empty FeatureFactory(layer=self.layer) geojson_url = reverse('terra:layer-geojson', args=[ self.layer.pk, ]) response = self.client.get(geojson_url) self.assertEqual(HTTP_403_FORBIDDEN, response.status_code)
def test_to_geojson(self): # Create at least one feature in the layer, so it's not empty FeatureFactory(layer=self.layer) FeatureFactory( layer=self.layer, geom=GEOSGeometry(json.dumps(self.fake_geometry)), properties={ 'number': 1, 'digit': 34 }, ) self.assertEqual( str(self.layer.to_geojson()['features'][0]['geometry']), "{'type': 'Point', 'coordinates': [2.4609375, 45.583289756006316]}" ) self.assertEqual( str(self.layer.to_geojson()['features'][1]['geometry']), "{'type': 'Point', 'coordinates': [2.0, 45.0]}") self.assertEqual( str(self.layer.to_geojson()['features'][1]['properties']), "{'digit': 34, 'number': 1}")
def test_features_filter_by_properties_with_wrong_field(self): layer = LayerFactory(group=self.group_name) FeatureFactory( layer=layer, geom=GEOSGeometry(json.dumps(self.fake_geometry)), properties={'number': 1}, ) FeatureFactory( layer=layer, geom=GEOSGeometry(json.dumps(self.fake_geometry)), properties={'number': 2}, ) response = self.client.get( reverse('terra:feature-list', kwargs={'layer': layer.pk}), {'properties__wrongfield': 'wrong value'}, ) self.assertEqual(response.status_code, HTTP_200_OK) json_response = response.json() self.assertEqual(json_response['count'], 0) self.assertEqual(len(json_response['results']), 0)
def test_features_filter_by_properties_with_several_int_field(self): layer = LayerFactory() FeatureFactory( layer=layer, geom=GEOSGeometry(json.dumps(self.fake_geometry)), properties={ 'number': 2, 'digit': 42 }, ) FeatureFactory( layer=layer, geom=GEOSGeometry(json.dumps(self.fake_geometry)), properties={ 'number': 1, 'digit': 42 }, ) FeatureFactory( layer=layer, geom=GEOSGeometry(json.dumps(self.fake_geometry)), properties={ 'number': 1, 'digit': 34 }, ) response = self.client.get( reverse('terra:feature-list', kwargs={'layer': layer.pk}), { 'properties__number': 1, 'properties__digit': 42 }, ) self.assertEqual(response.status_code, HTTP_200_OK) json_response = response.json() self.assertEqual(json_response['count'], 1) self.assertEqual(len(json_response['results']), 1)
def test_update(self): self.user.user_permissions.add( Permission.objects.get(codename='can_update_features_properties')) geom = GEOSGeometry(json.dumps(self.geometry)) feature = FeatureFactory(layer=self.layer, geom=geom, properties={'a': 'b'}) updated_properties = { 'c': 'd', 'a': 'd', } response = self.client.patch(reverse( 'terra:layer-detail', args=[ self.layer.name, ]), { "type": "FeatureCollection", "features": [ { "type": "Feature", 'geometry': self.geometry, 'properties': updated_properties, }, ] }, format='json') self.assertEqual(HTTP_200_OK, response.status_code) response = response.json() self.assertEqual(response['features'][0]['properties'], updated_properties) feature.refresh_from_db() self.assertDictEqual(feature.properties, updated_properties)
def test_shapefile_export(self): # Create at least one feature in the layer, so it's not empty self.user.user_permissions.add( Permission.objects.get(codename='can_export_layers')) FeatureFactory(layer=self.layer) shape_url = reverse('terra:layer-shapefile', args=[ self.layer.pk, ]) response = self.client.get(shape_url) self.assertEqual(HTTP_200_OK, response.status_code) zip = ZipFile(BytesIO(response.content), 'r') self.assertListEqual(sorted(['prj', 'cpg', 'shx', 'shp', 'dbf']), sorted([f.split('.')[1] for f in zip.namelist()]))
def setUpTestData(cls): cls.feature = FeatureFactory() cls.user = TerraUserFactory() # Create viewpoint with draft picture attached to it cls.viewpoint = ViewpointFactory(label="Basic viewpoint") # Create viewpoint with accepted picture attached to it cls.viewpoint_with_accepted_picture = ViewpointFactory( label="Viewpoint with accepted picture", pictures__state=STATES.ACCEPTED, properties={'test_update': 'ko'}, ) # Create viewpoints with no picture attached to it cls.viewpoint_without_picture = ViewpointFactory( label="Viewpoint without picture", pictures=None, properties={'test_update': 'ko'}, )
def test_to_geojson(self): # Create at least one feature in the layer, so it's not empty self.user.user_permissions.add( Permission.objects.get(codename='can_export_layers')) FeatureFactory(layer=self.layer) geojson_url = reverse('terra:layer-geojson', args=[ self.layer.pk, ]) response = self.client.get(geojson_url) self.assertEqual(HTTP_200_OK, response.status_code) response = response.json() self.assertEqual('FeatureCollection', response.get('type')) self.assertEqual(self.layer.features.all().count(), len(response.get('features')))
def test_feature_from_layer_name(self): layer = LayerFactory() feature = FeatureFactory( layer=layer, geom=GEOSGeometry(json.dumps(self.fake_geometry)), properties={ 'text': 'foobar', 'sentence': 'foobar is here' }, ) response = self.client.get( reverse('terra:feature-detail', kwargs={ 'layer': str(layer.name), 'identifier': str(feature.identifier) }), ) self.assertEqual(response.status_code, HTTP_200_OK)
def test_layer_processing_by_name(self): geojson = get_files_tests('toulouse.geojson') call_command('import_geojson', geojson, verbosity=0) # Retrieve the layer in_layer = Layer.objects.first() out_layer = Layer.objects.create(name='out') FeatureFactory(layer=out_layer, properties="Test") call_command('layer_processing', f'--layer-name-ins={in_layer.name}', f'--layer-name-out={out_layer.name}', f'--sql-centroid', verbosity=0) out_layer = Layer.objects.get(name='out') self.assertIn( 'Test', [feature.properties for feature in out_layer.features.all()]) self.assertTrue(len(out_layer.features.all()) > 0)
def test_layer_processing_clear_output(self): geojson = get_files_tests('toulouse.geojson') call_command('import_geojson', f'{geojson}', verbosity=0) # Retrieve the layer in_layer = Layer.objects.first() out_layer = LayerFactory(name='out') FeatureFactory(layer=out_layer, properties="Test") self.assertEqual(out_layer.features.count(), 1) call_command('layer_processing', f'--layer-name-ins={in_layer.name}', f'--layer-pk-out={out_layer.pk}', f'--sql-centroid', f'-co', verbosity=0) out_layer = Layer.objects.get(name='out') self.assertTrue(out_layer.features.count() > 1) self.assertNotIn( 'Test', [feature.properties for feature in out_layer.features.all()])