def test_default_group(self): # Fake json foo_bar_json = get_files_tests('foo_bar.json') # Import a shapefile call_command( 'import_shapefile', get_files_tests('shapefile-WGS84.zip'), identifier='ID_PG', verbosity=0) # Ensure old settings layer = Layer.objects.all()[0] self.assertNotEqual('new_name', layer.name) self.assertEqual(layer.layer_groups.count(), 1) self.assertEqual(layer.layer_groups.first().name, 'default') self.assertNotEqual({'foo': 'bar'}, layer.schema) self.assertNotEqual({'foo': 'bar'}, layer.settings) # Change settings call_command( 'layer_edit', layer='new_name', layer_pk=layer.pk, groups=['new_group', ], layer_settings=open(foo_bar_json) ) # Ensure new settings layer = Layer.objects.all()[0] self.assertEqual('new_name', layer.name) self.assertEqual(layer.layer_groups.count(), 1) self.assertEqual(layer.layer_groups.first().name, 'new_group') self.assertEqual({'foo': 'bar'}, layer.settings)
def test_import_geojson_layer_with_bad_settings(self): bad_json = get_files_tests('bad.json') with self.assertRaises(CommandError) as error: call_command('import_geojson', get_files_tests('empty.json'), f'--layer-settings={bad_json}', verbosity=0) self.assertEqual("Please provide a valid layer settings file", str(error.exception))
def test_layer_edit_bad_settings(self): layer = Layer.objects.first() bad_json = get_files_tests('bad.json') # Change settings with self.assertRaises(CommandError): call_command('layer_edit', '-pk', layer.pk, '-l', 'new_name', '-gr', 'new_group', '-ls', bad_json)
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()])
def test_import_shapefile_layer_with_bad_settings(self): # Sample ShapeFile shapefile_path = get_files_tests('shapefile-WGS84.zip') sample_shapefile = open(shapefile_path, 'rb') bad_settings_json = get_files_tests('bad.json') # Change settings with self.assertRaises(CommandError) as error: call_command( 'import_shapefile', sample_shapefile.name, identifier='ID_PG', generate_schema=True, layer_settings=open(bad_settings_json), verbosity=0 ) self.assertEqual("Please provide a valid layer settings file", str(error.exception))
def test_projection_does_not_exist(self): output = StringIO() with self.assertRaises(GEOSException): call_command( 'import_shapefile', get_files_tests('shapefile-WGS84-no-proj.zip'), verbosity=1, stdout=output)
def test_schema_generated(self): call_command('import_geojson', get_files_tests('bati.geojson'), '--generate-schema', verbosity=0) # Retrieve the layer layer = Layer.objects.get() # Assert schema properties are presents self.assertNotEqual( layer.schema.get('properties').keys() - [ 'ALTITUDE', 'ETIQUETTE', 'HAUTEUR', 'ID', 'ID_PG', 'NATURE', 'NOM', 'ORIGIN_BAT', 'PUB_XDECAL', 'PUB_YDECAL', 'ROTATION', 'ROTATION_S', 'XDECAL', 'XDECAL_SYM', 'YDECAL', 'YDECAL_SYM', 'Z_MAX', 'Z_MIN', ], True)
def test_layer_edit_schema_bad_file(self): layer = Layer.objects.first() bad_file = get_files_tests('shapefile-RFG93.zip') # Change settings with self.assertRaises(CommandError): call_command('layer_edit', '-pk', layer.pk, '-l', 'new_name', '-gr', 'new_group', '-s', bad_file, '-ls', bad_file)
def setUp(self): shapefile_path = get_files_tests('shapefile-WGS84.zip') sample_shapefile = open(shapefile_path, 'rb') call_command('import_shapefile', sample_shapefile.name, '-i', 'ID_PG', verbosity=0)
def test_shapefile_import(self): layer = LayerFactory() shapefile_path = get_files_tests('shapefile-WGS84.zip') with open(shapefile_path, 'rb') as shapefile: layer.from_shapefile(shapefile) self.assertEqual(8, layer.features.all().count())
def test_import_geojson_layer_with_pk_layer(self): layer = LayerFactory() self.assertEqual(len(layer.features.all()), 0) call_command('import_geojson', get_files_tests('toulouse.geojson'), layer_pk=layer.pk, verbosity=0) self.assertEqual(len(layer.features.all()), 838)
def test_layer_edit_fail_wrong_pk(self): # Ensure old settings foo_bar_json = get_files_tests('foo_bar.json') # Change settings with self.assertRaises(CommandError): call_command('layer_edit', '-pk', 999, '-l', 'new_name', '-gr', 'new_group', '-s', foo_bar_json, '-ls', foo_bar_json)
def test_default_group_nogroup_rollback(self): output = StringIO() call_command('import_geojson', get_files_tests('empty.json'), '--dry-run', '--verbosity=1', stdout=output) self.assertIn("The created layer pk is", output.getvalue()) # Retrieve the layer self.assertEqual(Layer.objects.count(), 0)
def test_projection_custom(self): output = StringIO() call_command( 'import_shapefile', get_files_tests('shapefile-no-proj.zip'), verbosity=1, stdout=output) # Retrieve the layer layer = Layer.objects.all()[0] self.assertEqual(layer.layer_groups.count(), 1) self.assertEqual(layer.layer_groups.first().name, 'default')
def test_default_group(self): output = StringIO() call_command('import_geojson', get_files_tests('empty.json'), '--verbosity=1', stdout=output) # Retrieve the layer layer = Layer.objects.first() self.assertIn(f'The created layer pk is {layer.pk}', output.getvalue()) self.assertEqual(layer.layer_groups.count(), 1) self.assertEqual(layer.layer_groups.first().name, 'default')
def test_command_launch(self): test_file = get_files_tests('test.csv') with self.assertLogs(level=logging.WARNING) as cm: call_command('import_csv', ('--operation=geostore.transformations' '.set_geometry_from_options'), '--layer=companies', '--key=SIREN', '--key=NIC', '--bulk', f'--source={test_file}') self.assertEqual(len(cm.records), 1) log_record = cm.records[0] self.assertEqual(set_geometry_from_options.__name__, log_record.funcName) self.assertIn('019778745', log_record.msg) # SIREN key self.assertIn('00018', log_record.msg) # NIC key self.assertIsNotNone(Layer.objects.filter(name="companies"))
def test_import_shapefile_layer_with_wrong_pk_layer(self): # Sample ShapeFile shapefile_path = get_files_tests('shapefile-WGS84.zip') sample_shapefile = open(shapefile_path, 'rb') with self.assertRaises(CommandError) as error: call_command( 'import_shapefile', sample_shapefile.name, layer_pk=999, identifier='ID_PG', generate_schema=True, verbosity=0 ) self.assertIn("Layer with pk 999 doesn't exist", str(error.exception))
def test_default_group_nogroup(self): call_command( 'import_shapefile', get_files_tests('shapefile-WGS84.zip'), identifier='ID_PG', verbosity=0) # Retrieve the layer layer = Layer.objects.all()[0] self.assertEqual(layer.layer_groups.count(), 1) self.assertEqual(layer.layer_groups.first().name, 'default') # Assert the identifier is not an UUID4 self.assertTrue(len(str(layer.features.first().identifier)) < 32)
def test_import_shapefile_layer_with_pk_layer(self): # Sample ShapeFile layer = LayerFactory() self.assertEqual(len(layer.features.all()), 0) shapefile_path = get_files_tests('shapefile-WGS84.zip') sample_shapefile = open(shapefile_path, 'rb') call_command( 'import_shapefile', sample_shapefile.name, layer_pk=layer.pk, identifier='ID_PG', generate_schema=True, verbosity=0 ) self.assertEqual(len(layer.features.all()), 8)
def test_layer_processing_by_pk(self): empty_json = get_files_tests('empty.json') geojson = get_files_tests('toulouse.geojson') call_command( 'import_geojson', f'{geojson}', f'-ls={empty_json}', verbosity=0) # Retrieve the layer in_layer = Layer.objects.first() out_layer = LayerFactory(name='out') call_command( 'layer_processing', f'--layer-name-ins={in_layer.name}', f'--layer-pk-out={out_layer.pk}', f'--sql-centroid', verbosity=0) out_layer = Layer.objects.get(name='out') self.assertTrue(out_layer.features.count() > 0)
def test_update_topology_rollback(self): geojson = get_files_tests('toulouse.geojson') call_command( 'import_geojson', f'{geojson}', verbosity=0) # Retrieve the layer in_layer = Layer.objects.first() output = StringIO() call_command( 'update_topology', '--dry-run', f'--layer-pk={in_layer.pk}', verbosity=1, stdout=output)
def test_shapefile_import_view(self): self.user.user_permissions.add(Permission.objects.get(codename='can_import_layers')) layer = LayerFactory() shapefile_path = get_files_tests('shapefile-WGS84.zip') with open(shapefile_path, 'rb') as fd: shapefile = SimpleUploadedFile('shapefile-WGS84.zip', fd.read()) response = self.client.post( reverse('layer-shapefile', args=[layer.pk, ]), {'shapefile': shapefile, } ) self.assertEqual(HTTP_200_OK, response.status_code) self.assertEqual(8, layer.features.all().count())
def test_default_group_nogroup_rollback(self): # Sample ShapeFile shapefile_path = get_files_tests('shapefile-WGS84.zip') sample_shapefile = open(shapefile_path, 'rb') output = StringIO() call_command( 'import_shapefile', sample_shapefile.name, identifier='ID_PG', dry_run=True, verbosity=1, stdout=output) self.assertIn("The created layer pk is", output.getvalue()) # Retrieve the layer layer = Layer.objects.all() self.assertEqual(len(layer), 0)
def test_update_topology(self): geojson = get_files_tests('toulouse.geojson') call_command( 'import_geojson', f'{geojson}', verbosity=0) # Retrieve the layer in_layer = Layer.objects.first() output = StringIO() call_command( 'update_topology', f'--layer-pk={in_layer.pk}', verbosity=1, stdout=output) self.assertIn('Topology successfully updated', output.getvalue())
def test_reprojection(self): output = StringIO() call_command( 'import_shapefile', get_files_tests('shapefile-RFG93.zip'), verbosity=1, stdout=output) # Retrieve the layer layer = Layer.objects.all()[0] self.assertEqual(layer.layer_groups.count(), 1) self.assertEqual(layer.layer_groups.first().name, 'default') # assert data was reprojected bbox = layer.features.first().get_bounding_box() self.assertTrue(-180 <= bbox[0]) self.assertTrue(-90 <= bbox[1]) self.assertTrue(bbox[2] <= 180) self.assertTrue(bbox[3] <= 90)
def test_layer_edit(self): # Ensure old settings layer = Layer.objects.all()[0] self.assertNotEqual('new_name', layer.name) self.assertNotEqual({'foo': 'bar'}, layer.settings) self.assertEqual(layer.layer_groups.count(), 1) self.assertEqual(layer.layer_groups.first().name, 'default') foo_bar_json = get_files_tests('foo_bar.json') # Change settings call_command('layer_edit', '-pk', layer.pk, '-l', 'new_name', '-gr', 'new_group', '-ls', foo_bar_json) # Ensure new settings layer = Layer.objects.all()[0] self.assertEqual('new_name', layer.name) self.assertEqual({'foo': 'bar'}, layer.settings) self.assertEqual(layer.layer_groups.count(), 1) self.assertEqual(layer.layer_groups.first().name, 'new_group')
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_guess_maxzoom(self): # guess_maxzoom returning -1 when TypeError is raised14) self.assertEqual(guess_maxzoom(self.layerPoint), 14) self.assertEqual(guess_maxzoom(self.layer) is not None, True) # test guess_maxzoom returns sensible value from OSM Fontainebleau paths&tracks chunk_fontainebleau_geojson = get_files_tests( 'chunk_fontainebleau.geojson') call_command('import_geojson', chunk_fontainebleau_geojson, '-gr', 'maxzoom_test', '-ln', 'chunk_fontainebleau', verbosity=0) layer_chunk_fontainebleau = Layer.objects.get( name='chunk_fontainebleau') self.assertEqual(guess_maxzoom(layer_chunk_fontainebleau), 13)
def get_good_data(self): overpass_path = get_files_tests('overpass.osm') with open(overpass_path, 'rb') as overpass_file: overpass_data = overpass_file.read() return overpass_data
def test_import_geojson_layer_with_wrong_pk_layer(self): geojson_sample = get_files_tests('toulouse.geojson') with self.assertRaises(CommandError) as error: call_command('import_geojson', geojson_sample, '--layer-pk=999', '--generate-schema', '--verbosity=0') self.assertIn("Layer with pk 999 doesn't exist", str(error.exception))