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'),
            '-i', 'ID_PG',
            verbosity=0)

        # Ensure old settings
        layer = Layer.objects.all()[0]
        self.assertNotEqual('new_name', layer.name)
        self.assertNotEqual('new_group', layer.group)
        self.assertNotEqual({'foo': 'bar'}, layer.schema)
        self.assertNotEqual({'foo': 'bar'}, layer.settings)

        # 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('new_group', layer.group)
        self.assertEqual({'foo': 'bar'}, layer.settings)
예제 #2
0
 def test_import_geojson_layer_with_bad_settings(self):
     empty_geojson = get_files_tests('empty.json')
     bad_json = get_files_tests('bad.json')
     with self.assertRaises(CommandError) as error:
         call_command('import_geojson',
                      empty_geojson,
                      '-ls',
                      bad_json,
                      verbosity=0)
     self.assertEqual("Please provide a valid layer settings file",
                      str(error.exception))
 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',
             '-iID_PG',
             '-gs', sample_shapefile.name,
             '-ls', bad_settings_json,
             verbosity=0
         )
     self.assertEqual("Please provide a valid layer settings file", str(error.exception))
예제 #4
0
 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)
예제 #5
0
    def test_schema_generated(self):
        call_command('import_geojson',
                     get_files_tests('bati.geojson'),
                     '-gs',
                     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)
예제 #6
0
 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)
예제 #7
0
 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)
예제 #8
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())
예제 #9
0
    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)
예제 #10
0
 def test_import_geojson_layer_with_pk_layer(self):
     layer = LayerFactory()
     self.assertEqual(len(layer.features.all()), 0)
     geojson_sample = get_files_tests('toulouse.geojson')
     call_command('import_geojson',
                  f'--layer-pk={layer.pk}',
                  geojson_sample,
                  verbosity=0)
     self.assertEqual(len(layer.features.all()), 838)
예제 #11
0
 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',
                      f'--layer-pk=999',
                      '-gs',
                      geojson_sample,
                      verbosity=0)
     self.assertIn("Layer with pk 999 doesn't exist", str(error.exception))
예제 #12
0
 def test_default_group_nogroup_rollback(self):
     output = StringIO()
     call_command('import_geojson',
                  f'--dry-run',
                  get_files_tests('empty.json'),
                  verbosity=1,
                  stdout=output)
     self.assertIn("The created layer pk is", output.getvalue())
     # Retrieve the layer
     self.assertEqual(Layer.objects.count(), 0)
예제 #13
0
    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('__nogroup__', layer.group)
예제 #14
0
 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',
             f'--layer-pk=999',
             '-iID_PG',
             '-gs', sample_shapefile.name,
             verbosity=0
         )
     self.assertIn("Layer with pk 999 doesn't exist", str(error.exception))
예제 #15
0
    def test_default_group_nogroup(self):
        call_command(
            'import_shapefile',
            get_files_tests('shapefile-WGS84.zip'),
            '-i', 'ID_PG',
            verbosity=0)

        # Retrieve the layer
        layer = Layer.objects.all()[0]
        self.assertEqual('__nogroup__', layer.group)

        # Assert the identifier is not an UUID4
        self.assertTrue(len(str(layer.features.first().identifier)) < 32)
예제 #16
0
    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)
예제 #17
0
 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',
         f'--layer-pk={layer.pk}',
         '-iID_PG',
         '-gs', sample_shapefile.name,
         verbosity=0
     )
     self.assertEqual(len(layer.features.all()), 8)
예제 #18
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())
예제 #19
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)
예제 #20
0
    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',
            f'-iID_PG',
            f'--dry-run',
            f'{sample_shapefile.name}',
            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)
예제 #21
0
 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=terracommon.data_importers.tests'
                       '.test_dataimporter_functions.empty_operation'),
                      ('--operation=terracommon.terra.transformations'
                       '.set_geometry_from_options'), '--layer=companies',
                      '--key=SIREN', '--key=NIC', 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"))
예제 #22
0
    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('__nogroup__', layer.group)

        # 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)
예제 #23
0
    def test_layer_edit(self):
        # Ensure old settings
        layer = Layer.objects.all()[0]
        self.assertNotEqual('new_name', layer.name)
        self.assertNotEqual('new_group', layer.group)
        self.assertNotEqual({'foo': 'bar'}, layer.settings)

        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('new_group', layer.group)
        self.assertEqual({'foo': 'bar'}, layer.settings)
예제 #24
0
    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('terra:layer-shapefile', args=[
                    layer.pk,
                ]), {
                    'shapefile': shapefile,
                })

        self.assertEqual(HTTP_200_OK, response.status_code)
        self.assertEqual(8, layer.features.all().count())
예제 #25
0
    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)
예제 #26
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)
예제 #27
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()])
예제 #28
0
 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