コード例 #1
0
 def test_init(self):
     ensure_dirs()
     user = UserFactory.build()
     project = ProjectFactory.build()
     form = forms.DownloadForm(project, user)
     assert form.project == project
     assert form.user == user
コード例 #2
0
 def test_init(self):
     ensure_dirs()
     user = UserFactory.build()
     project = ProjectFactory.build()
     form = forms.DownloadForm(project, user)
     assert form.project == project
     assert form.user == user
コード例 #3
0
    def test_get_shape_download(self):
        ensure_dirs()
        data = {'type': 'shp'}
        user = UserFactory.create()
        project = ProjectFactory.create()
        form = forms.DownloadForm(project, user, data=data)
        assert form.is_valid() is True
        path, mime = form.get_file()
        assert '{}-{}'.format(project.id, user.id) in path
        assert (mime == 'application/zip')

        with ZipFile(path, 'r') as testzip:
            assert len(testzip.namelist()) == 16
            assert project.slug + '-point.dbf' in testzip.namelist()
            assert project.slug + '-point.prj' in testzip.namelist()
            assert project.slug + '-point.shp' in testzip.namelist()
            assert project.slug + '-point.shx' in testzip.namelist()
            assert project.slug + '-line.dbf' in testzip.namelist()
            assert project.slug + '-line.prj' in testzip.namelist()
            assert project.slug + '-line.shp' in testzip.namelist()
            assert project.slug + '-line.shx' in testzip.namelist()
            assert project.slug + '-polygon.dbf' in testzip.namelist()
            assert project.slug + '-polygon.prj' in testzip.namelist()
            assert project.slug + '-polygon.shp' in testzip.namelist()
            assert project.slug + '-polygon.shx' in testzip.namelist()
            assert 'relationships.csv' in testzip.namelist()
            assert 'parties.csv' in testzip.namelist()
            assert 'locations.csv' in testzip.namelist()
            assert 'README.txt' in testzip.namelist()
コード例 #4
0
    def test_make_download(self):
        ensure_dirs()
        exporter = XLSExporter(self.project)
        path, mime = exporter.make_download("file")

        assert path == os.path.join(settings.MEDIA_ROOT, "temp/file.xlsx")
        assert mime == "application/vnd.openxmlformats-officedocument." "spreadsheetml.sheet"
コード例 #5
0
    def test_make_download_with_null_geometry(self):
        ensure_dirs()
        original_es_dump_path = os.path.join(
            os.path.dirname(settings.BASE_DIR),
            'search/tests/files/test_es_dump_null_geometry.esjson')
        es_dump_path = os.path.join(test_dir, 'test-shp5.esjson')
        shutil.copy(original_es_dump_path, es_dump_path)

        exporter = ShapeExporter(self.project, True)
        zip_path, mime_type = exporter.make_download(es_dump_path)

        assert zip_path == os.path.join(test_dir, 'test-shp5-shp.zip')
        assert mime_type == ('application/zip')

        with ZipFile(zip_path) as myzip:
            files = myzip.namelist()
            assert len(files) == 2
            assert 'README.txt' in files
            assert 'locations.csv' in files

            with myzip.open('locations.csv') as csv_file:
                rows = list(csv.reader(io.TextIOWrapper(csv_file)))
                assert rows[0][0] == 'id'
                assert rows[0][1] == 'type'
                assert rows[0][2] == 'quality'
                assert rows[0][3] == 'infrastructure'
                assert rows[1][0] == 'ID0'
                assert rows[1][1] == 'PA'
                assert rows[1][2] == 'point'
                assert rows[1][3] == 'food, electricity'
コード例 #6
0
    def test_make_download(self):
        ensure_dirs()
        project = ProjectFactory.create()
        exporter = ResourceExporter(project)

        ResourceFactory.create(project=project, original_file='res.png')
        ResourceFactory.create(project=project, original_file='res.png')
        ResourceFactory.create(project=project, original_file='resources.xlsx')
        deleted = ResourceFactory.create(project=project,
                                         original_file='image1.jpg',
                                         archived=True)

        t = round(time.time() * 1000)
        path, mime = exporter.make_download('res-test-' + str(t))
        assert path == os.path.join(settings.MEDIA_ROOT,
                                    'temp/res-test-{}.zip'.format(t))
        assert mime == 'application/zip'

        with ZipFile(path, 'r') as testzip:
            assert len(testzip.namelist()) == 4
            assert 'res.png' in testzip.namelist()
            assert 'res_1.png' in testzip.namelist()
            assert 'resources_1.xlsx' in testzip.namelist()
            assert 'resources.xlsx' in testzip.namelist()
            assert deleted.original_file not in testzip.namelist()
コード例 #7
0
    def test_get_shape_download(self):
        ensure_dirs()
        data = {"type": "shp"}
        user = UserFactory.create()
        project = ProjectFactory.create()
        form = forms.DownloadForm(project, user, data=data)
        assert form.is_valid() is True
        path, mime = form.get_file()
        assert "{}-{}".format(project.id, user.id) in path
        assert mime == "application/zip"

        with ZipFile(path, "r") as testzip:
            assert len(testzip.namelist()) == 16
            assert project.slug + "-point.dbf" in testzip.namelist()
            assert project.slug + "-point.prj" in testzip.namelist()
            assert project.slug + "-point.shp" in testzip.namelist()
            assert project.slug + "-point.shx" in testzip.namelist()
            assert project.slug + "-line.dbf" in testzip.namelist()
            assert project.slug + "-line.prj" in testzip.namelist()
            assert project.slug + "-line.shp" in testzip.namelist()
            assert project.slug + "-line.shx" in testzip.namelist()
            assert project.slug + "-polygon.dbf" in testzip.namelist()
            assert project.slug + "-polygon.prj" in testzip.namelist()
            assert project.slug + "-polygon.shp" in testzip.namelist()
            assert project.slug + "-polygon.shx" in testzip.namelist()
            assert "relationships.csv" in testzip.namelist()
            assert "parties.csv" in testzip.namelist()
            assert "locations.csv" in testzip.namelist()
            assert "README.txt" in testzip.namelist()
コード例 #8
0
    def test_make_download(self):
        ensure_dirs()
        exporter = XLSExporter(self.project)
        path, mime = exporter.make_download('file')

        assert path == os.path.join(settings.MEDIA_ROOT, 'temp/file.xlsx')
        assert (mime == 'application/vnd.openxmlformats-officedocument.'
                'spreadsheetml.sheet')
コード例 #9
0
def mock_subprocess_run_curl(*args):
    assert args[0][0] == 'curl'
    ensure_dirs()
    original_es_dump_path = os.path.join(
        os.path.dirname(settings.BASE_DIR),
        'search/tests/files/test_es_dump_basic.esjson')
    shutil.copy(original_es_dump_path, args[0][2])
    return MyCompletedProcess(0)
コード例 #10
0
    def test_create_datasource(self):
        ensure_dirs()
        project = ProjectFactory.create()
        exporter = ShapeExporter(project)

        dst_dir = os.path.join(settings.MEDIA_ROOT, "temp/file")
        ds = exporter.create_datasource(dst_dir, "file0")
        assert ds.GetName() == os.path.join(settings.MEDIA_ROOT, "temp/file/file0-point.shp")
        ds.Destroy()
コード例 #11
0
    def test_create_datasource(self):
        ensure_dirs()
        project = ProjectFactory.create()
        exporter = ShapeExporter(project)

        dst_dir = os.path.join(settings.MEDIA_ROOT, 'temp/file')
        ds = exporter.create_datasource(dst_dir)
        assert (ds.GetName() == os.path.join(settings.MEDIA_ROOT, 'temp/file'))
        ds.Destroy()
コード例 #12
0
    def test_write_features(self):
        ensure_dirs()
        project = ProjectFactory.create(current_questionnaire='123abc')
        exporter = ShapeExporter(project)

        content_type = ContentType.objects.get(app_label='spatial',
                                               model='spatialunit')
        schema = Schema.objects.create(content_type=content_type,
                                       selectors=(
                                           project.organization.id,
                                           project.id,
                                           '123abc',
                                       ))
        attr_type = AttributeType.objects.get(name='text')
        Attribute.objects.create(schema=schema,
                                 name='key',
                                 long_name='Test field',
                                 attr_type=attr_type,
                                 index=0,
                                 required=False,
                                 omit=False)

        su1 = SpatialUnitFactory.create(project=project,
                                        geometry='POINT (1 1)',
                                        attributes={'key': 'value 1'})
        su2 = SpatialUnitFactory.create(project=project,
                                        geometry='POINT (2 2)',
                                        attributes={'key': 'value 2'})

        dst_dir = os.path.join(settings.MEDIA_ROOT, 'temp/file4')
        ds = exporter.create_datasource(dst_dir, 'file4')
        layers = exporter.create_shp_layers(ds, 'file4')

        csvfile = os.path.join(dst_dir, 'locations.csv')
        exporter.write_features(layers, csvfile)

        assert len(layers[0]) == 2
        f = layers[0].GetNextFeature()
        while f:
            geom = f.geometry()
            assert geom.ExportToWkt() in ['POINT (1 1)', 'POINT (2 2)']
            assert f.GetFieldAsString('id') in [su1.id, su2.id]

            f = layers[0].GetNextFeature()

        ds.Destroy()

        with open(csvfile) as csvfile:
            csvreader = csv.reader(csvfile)
            for i, row in enumerate(csvreader):
                if i == 0:
                    assert row == ['id', 'type', 'key']
                elif row[0] == su1.id:
                    assert row == [su1.id, su1.type, 'value 1']
                elif row[1] == su2.id:
                    assert row == [su2.id, su2.type, 'value 2']
コード例 #13
0
    def test_create_datasource(self):
        ensure_dirs()
        project = ProjectFactory.create()
        exporter = ShapeExporter(project)

        dst_dir = os.path.join(settings.MEDIA_ROOT, 'temp/file')
        ds = exporter.create_datasource(dst_dir)
        assert (ds.GetName() ==
                os.path.join(settings.MEDIA_ROOT, 'temp/file'))
        ds.Destroy()
コード例 #14
0
 def test_get_resources_download(self):
     ensure_dirs()
     data = {"type": "res"}
     user = UserFactory.create()
     project = ProjectFactory.create()
     form = forms.DownloadForm(project, user, data=data)
     assert form.is_valid() is True
     path, mime = form.get_file()
     assert "{}-{}".format(project.id, user.id) in path
     assert mime == "application/zip"
コード例 #15
0
 def test_get_resources_download(self):
     ensure_dirs()
     data = {'type': 'res'}
     user = UserFactory.create()
     project = ProjectFactory.create()
     form = forms.DownloadForm(project, user, data=data)
     assert form.is_valid() is True
     path, mime = form.get_file()
     assert '{}-{}'.format(project.id, user.id) in path
     assert mime == 'application/zip'
コード例 #16
0
 def test_get_xls_download(self):
     ensure_dirs()
     data = {'type': 'xls'}
     user = UserFactory.create()
     project = ProjectFactory.create()
     form = forms.DownloadForm(project, user, data=data)
     assert form.is_valid() is True
     path, mime = form.get_file()
     assert '{}-{}'.format(project.id, user.id) in path
     assert (mime == 'application/vnd.openxmlformats-officedocument.'
             'spreadsheetml.sheet')
コード例 #17
0
 def test_get_xls_download(self):
     ensure_dirs()
     data = {"type": "xls"}
     user = UserFactory.create()
     project = ProjectFactory.create()
     geometry = "SRID=4326;POINT (30 10)"
     SpatialUnitFactory.create(project=project, geometry=geometry)
     form = forms.DownloadForm(project, user, data=data)
     assert form.is_valid() is True
     path, mime = form.get_file()
     assert "{}-{}".format(project.id, user.id) in path
     assert mime == "application/vnd.openxmlformats-officedocument." "spreadsheetml.sheet"
コード例 #18
0
    def test_get_shape_download(self):
        ensure_dirs()
        data = {'type': 'shp'}
        user = UserFactory.create()
        project = ProjectFactory.create()
        content_type = ContentType.objects.get(app_label='spatial',
                                               model='spatialunit')
        schema = Schema.objects.create(
            content_type=content_type,
            selectors=(project.organization.id, project.id))
        attr_type = AttributeType.objects.get(name='text')
        Attribute.objects.create(
            schema=schema,
            name='key', long_name='Test field',
            attr_type=attr_type, index=0,
            required=False, omit=False
        )

        su1 = SpatialUnitFactory.create(
            project=project,
            geometry='POINT (1 1)',
            attributes={'key': 'value 1'})
        SpatialUnitFactory.create(
            project=project,
            geometry='SRID=4326;'
                     'MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),'
                     '((15 5, 40 10, 10 20, 5 10, 15 5)))',
            attributes={'key': 'value 2'})
        party = PartyFactory.create(project=project)
        TenureRelationshipFactory.create(
            spatial_unit=su1, party=party, project=project)

        form = forms.DownloadForm(project, user, data=data)
        assert form.is_valid() is True
        path, mime = form.get_file()
        assert '{}-{}'.format(project.id, user.id) in path
        assert (mime == 'application/zip')

        with ZipFile(path, 'r') as testzip:
            assert len(testzip.namelist()) == 12
            assert 'point.dbf' in testzip.namelist()
            assert 'point.prj' in testzip.namelist()
            assert 'point.shp' in testzip.namelist()
            assert 'point.shx' in testzip.namelist()
            assert 'multipolygon.dbf' in testzip.namelist()
            assert 'multipolygon.prj' in testzip.namelist()
            assert 'multipolygon.shp' in testzip.namelist()
            assert 'multipolygon.shx' in testzip.namelist()
            assert 'relationships.csv' in testzip.namelist()
            assert 'parties.csv' in testzip.namelist()
            assert 'locations.csv' in testzip.namelist()
            assert 'README.txt' in testzip.namelist()
コード例 #19
0
    def test_create_shp_layers(self):
        ensure_dirs()
        project = ProjectFactory.create()
        exporter = ShapeExporter(project)

        dst_dir = os.path.join(settings.MEDIA_ROOT, "temp/file6")
        ds = exporter.create_datasource(dst_dir, "file6")
        layers = exporter.create_shp_layers(ds, "file6")
        assert len(layers) == 3
        assert layers[0].GetName() == "file6-point"
        assert layers[1].GetName() == "file6-line"
        assert layers[2].GetName() == "file6-polygon"
        ds.Destroy()
コード例 #20
0
    def test_make_download_empty_not_standalone(self):
        ensure_dirs()
        original_es_dump_path = os.path.join(
            os.path.dirname(settings.BASE_DIR),
            'search/tests/files/test_es_dump_empty.esjson')
        es_dump_path = os.path.join(test_dir, 'test-shp4.esjson')
        shutil.copy(original_es_dump_path, es_dump_path)

        exporter = ShapeExporter(self.project, is_standalone=False)
        dir_path = exporter.make_download(es_dump_path)

        assert dir_path == os.path.join(test_dir, 'test-shp4-shp-dir')
        assert os.listdir(dir_path) == []
コード例 #21
0
    def test_create_shp_layers(self):
        ensure_dirs()
        project = ProjectFactory.create()
        exporter = ShapeExporter(project)

        dst_dir = os.path.join(settings.MEDIA_ROOT, 'temp/file6')
        ds = exporter.create_datasource(dst_dir, 'file6')
        layers = exporter.create_shp_layers(ds, 'file6')
        assert len(layers) == 3
        assert layers[0].GetName() == 'file6-point'
        assert layers[1].GetName() == 'file6-line'
        assert layers[2].GetName() == 'file6-polygon'
        ds.Destroy()
コード例 #22
0
    def test_make_download_empty(self):
        ensure_dirs()
        original_es_dump_path = os.path.join(
            os.path.dirname(settings.BASE_DIR),
            'search/tests/files/test_es_dump_empty.esjson')
        es_dump_path = os.path.join(test_dir, 'test-res3.esjson')
        shutil.copy(original_es_dump_path, es_dump_path)

        exporter = ResourceExporter(self.project)
        zip_path, mime_type = exporter.make_download(es_dump_path)

        assert zip_path == os.path.join(test_dir, 'test-res3-res.zip')
        assert mime_type == ('application/zip')
        assert ZipFile(zip_path).namelist() == []
コード例 #23
0
    def test_make_download(self):
        ensure_dirs()
        project = ProjectFactory.create(current_questionnaire='123abc')
        exporter = ShapeExporter(project)

        content_type = ContentType.objects.get(app_label='spatial',
                                               model='spatialunit')
        schema = Schema.objects.create(content_type=content_type,
                                       selectors=(
                                           project.organization.id,
                                           project.id,
                                           '123abc',
                                       ))
        attr_type = AttributeType.objects.get(name='text')
        Attribute.objects.create(schema=schema,
                                 name='key',
                                 long_name='Test field',
                                 attr_type=attr_type,
                                 index=0,
                                 required=False,
                                 omit=False)

        SpatialUnitFactory.create(project=project,
                                  geometry='POINT (1 1)',
                                  attributes={'key': 'value 1'})
        SpatialUnitFactory.create(
            project=project,
            geometry='SRID=4326;'
            'MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),'
            '((15 5, 40 10, 10 20, 5 10, 15 5)))',
            attributes={'key': 'value 2'})

        path, mime = exporter.make_download('file5')

        assert path == os.path.join(settings.MEDIA_ROOT, 'temp/file5.zip')
        assert mime == 'application/zip'

        with ZipFile(path, 'r') as testzip:
            assert len(testzip.namelist()) == 10
            assert 'point.dbf' in testzip.namelist()
            assert 'point.prj' in testzip.namelist()
            assert 'point.shp' in testzip.namelist()
            assert 'point.shx' in testzip.namelist()
            assert 'multipolygon.dbf' in testzip.namelist()
            assert 'multipolygon.prj' in testzip.namelist()
            assert 'multipolygon.shp' in testzip.namelist()
            assert 'multipolygon.shx' in testzip.namelist()
            assert 'locations.csv' in testzip.namelist()
            assert 'README.txt' in testzip.namelist()
コード例 #24
0
    def test_make_download(self):
        ensure_dirs()
        project = ProjectFactory.create()
        exporter = ResourceExporter(project)
        res = ResourceFactory.create(project=project)

        t = round(time.time() * 1000)
        path, mime = exporter.make_download("res-test-" + str(t))
        assert path == os.path.join(settings.MEDIA_ROOT, "temp/res-test-{}.zip".format(t))
        assert mime == "application/zip"

        with ZipFile(path, "r") as testzip:
            assert len(testzip.namelist()) == 2
            assert res.original_file in testzip.namelist()
            assert "resources.xlsx" in testzip.namelist()
コード例 #25
0
    def test_create_layer(self):
        ensure_dirs()
        project = ProjectFactory.create()
        exporter = ShapeExporter(project)

        dst_dir = os.path.join(settings.MEDIA_ROOT, 'temp/file6')
        ds = exporter.create_datasource(dst_dir)
        for layer_type in [
                'point', 'linestring', 'polygon',
                'multilinestring', 'multipoint', 'multipolygon']:
            layer = exporter.create_layer(ds, layer_type)
            assert layer is not None
            assert layer.GetName() == layer_type
            assert layer.GetGeomType() in [1, 2, 3, 4]
            assert layer.GetLayerDefn().GetFieldCount() == 1
        ds.Destroy()
コード例 #26
0
    def test_make_download(self):
        ensure_dirs()
        project = ProjectFactory.create()
        exporter = ResourceExporter(project)
        res = ResourceFactory.create(project=project)

        t = round(time.time() * 1000)
        path, mime = exporter.make_download('res-test-' + str(t))
        assert path == os.path.join(settings.MEDIA_ROOT,
                                    'temp/res-test-{}.zip'.format(t))
        assert mime == 'application/zip'

        with ZipFile(path, 'r') as testzip:
            assert len(testzip.namelist()) == 2
            assert res.original_file in testzip.namelist()
            assert 'resources.xlsx' in testzip.namelist()
コード例 #27
0
    def test_create_layer(self):
        ensure_dirs()
        project = ProjectFactory.create()
        exporter = ShapeExporter(project)

        dst_dir = os.path.join(settings.MEDIA_ROOT, 'temp/file6')
        ds = exporter.create_datasource(dst_dir)
        for layer_type in [
                'point', 'linestring', 'polygon',
                'multilinestring', 'multipoint', 'multipolygon']:
            layer = exporter.create_layer(ds, layer_type)
            assert layer is not None
            assert layer.GetName() == layer_type
            assert layer.GetGeomType() in [1, 2, 3, 4]
            assert layer.GetLayerDefn().GetFieldCount() == 1
        ds.Destroy()
コード例 #28
0
    def test_make_download_with_dupe_filenames(self):
        ensure_dirs()
        original_es_dump_path = os.path.join(
            os.path.dirname(settings.BASE_DIR),
            'search/tests/files/test_es_dump_dupe_resources.esjson')
        es_dump_path = os.path.join(test_dir, 'test-res2.esjson')
        shutil.copy(original_es_dump_path, es_dump_path)

        exporter = ResourceExporter(self.project)
        zip_path, mime_type = exporter.make_download(es_dump_path)

        assert zip_path == os.path.join(test_dir, 'test-res2-res.zip')
        assert mime_type == ('application/zip')

        with ZipFile(zip_path) as myzip:
            files = myzip.namelist()
            assert len(files) == 3
            assert 'resources.xlsx' in files
            assert 'resources/text.csv' in files
            assert 'resources/text (2).csv' in files

            myzip.extract('resources.xlsx', test_dir)
            wb = load_workbook(os.path.join(test_dir, 'resources.xlsx'))
            assert wb.get_sheet_names() == ['resources']

            ws = wb['resources']
            assert ws['A1'].value == 'id'
            assert ws['B1'].value == 'name'
            assert ws['C1'].value == 'description'
            assert ws['D1'].value == 'filename'
            assert ws['E1'].value == 'locations'
            assert ws['F1'].value == 'parties'
            assert ws['G1'].value == 'relationships'
            assert ws['A2'].value == 'ID3'
            assert ws['B2'].value == "File 1"
            assert ws['C2'].value == "Description 1"
            assert ws['D2'].value == "text.csv"
            assert ws['E2'].value is None
            assert ws['F2'].value is None
            assert ws['G2'].value is None
            assert ws['A3'].value == 'ID4'
            assert ws['B3'].value == "File 2"
            assert ws['C3'].value == "Description 2"
            assert ws['D3'].value == "text (2).csv"
            assert ws['E3'].value is None
            assert ws['F3'].value is None
            assert ws['G3'].value is None
コード例 #29
0
    def test_make_download(self):
        ensure_dirs()
        project = ProjectFactory.create(current_questionnaire='123abc')
        exporter = ShapeExporter(project)

        content_type = ContentType.objects.get(app_label='spatial',
                                               model='spatialunit')
        schema = Schema.objects.create(
            content_type=content_type,
            selectors=(project.organization.id, project.id, '123abc', ))
        attr_type = AttributeType.objects.get(name='text')
        Attribute.objects.create(
            schema=schema,
            name='key', long_name='Test field',
            attr_type=attr_type, index=0,
            required=False, omit=False
        )

        SpatialUnitFactory.create(
            project=project,
            geometry='POINT (1 1)',
            attributes={'key': 'value 1'})
        SpatialUnitFactory.create(
            project=project,
            geometry='SRID=4326;'
                     'MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),'
                     '((15 5, 40 10, 10 20, 5 10, 15 5)))',
            attributes={'key': 'value 2'})

        path, mime = exporter.make_download('file5')

        assert path == os.path.join(settings.MEDIA_ROOT, 'temp/file5.zip')
        assert mime == 'application/zip'

        with ZipFile(path, 'r') as testzip:
            assert len(testzip.namelist()) == 10
            assert 'point.dbf' in testzip.namelist()
            assert 'point.prj' in testzip.namelist()
            assert 'point.shp' in testzip.namelist()
            assert 'point.shx' in testzip.namelist()
            assert 'multipolygon.dbf' in testzip.namelist()
            assert 'multipolygon.prj' in testzip.namelist()
            assert 'multipolygon.shp' in testzip.namelist()
            assert 'multipolygon.shx' in testzip.namelist()
            assert 'locations.csv' in testzip.namelist()
            assert 'README.txt' in testzip.namelist()
コード例 #30
0
    def test_make_download_empty(self):
        ensure_dirs()
        original_es_dump_path = os.path.join(
            os.path.dirname(settings.BASE_DIR),
            'search/tests/files/test_es_dump_empty.esjson')
        es_dump_path = os.path.join(test_dir, 'test-xls2.esjson')
        shutil.copy(original_es_dump_path, es_dump_path)

        exporter = XLSExporter(self.project)
        xls_path, mime_type = exporter.make_download(es_dump_path)

        assert xls_path == os.path.join(test_dir, 'test-xls2.xlsx')
        assert mime_type == ('application/vnd.openxmlformats-officedocument.'
                             'spreadsheetml.sheet')

        wb = load_workbook(xls_path)
        assert wb.get_sheet_names() == ['Sheet']
        assert wb['Sheet']['A1'].value is None
コード例 #31
0
    def test_make_resource_worksheet(self):
        ensure_dirs()
        project = ProjectFactory.create()
        exporter = ResourceExporter(project)

        data = [["1", "n1", "d1", "f1", "l1", "p1", "r1"], ["2", "n2", "d2", "f2", "l2", "p2", "r2"]]

        res_path = exporter.make_resource_worksheet("test-res", data)
        wb = load_workbook(filename=res_path, read_only=True)
        sheet = wb[wb.get_sheet_names()[0]]

        expected_headers = ["id", "name", "description", "filename", "locations", "parties", "relationships"]
        for i, h in enumerate(expected_headers):
            assert sheet[chr(i + 97) + "1"].value == h

        for i in range(0, len(data[0])):
            assert sheet[chr(i + 97) + "2"].value == data[0][i]
            assert sheet[chr(i + 97) + "3"].value == data[1][i]
コード例 #32
0
    def test_write_features(self):
        ensure_dirs()
        project = ProjectFactory.create(current_questionnaire="123abc")
        exporter = ShapeExporter(project)

        content_type = ContentType.objects.get(app_label="spatial", model="spatialunit")
        schema = Schema.objects.create(
            content_type=content_type, selectors=(project.organization.id, project.id, "123abc")
        )
        attr_type = AttributeType.objects.get(name="text")
        Attribute.objects.create(
            schema=schema, name="key", long_name="Test field", attr_type=attr_type, index=0, required=False, omit=False
        )

        su1 = SpatialUnitFactory.create(project=project, geometry="POINT (1 1)", attributes={"key": "value 1"})
        su2 = SpatialUnitFactory.create(project=project, geometry="POINT (2 2)", attributes={"key": "value 2"})

        dst_dir = os.path.join(settings.MEDIA_ROOT, "temp/file4")
        ds = exporter.create_datasource(dst_dir, "file4")
        layers = exporter.create_shp_layers(ds, "file4")

        csvfile = os.path.join(dst_dir, "locations.csv")
        exporter.write_features(layers, csvfile)

        assert len(layers[0]) == 2
        f = layers[0].GetNextFeature()
        while f:
            geom = f.geometry()
            assert geom.ExportToWkt() in ["POINT (1 1)", "POINT (2 2)"]
            assert f.GetFieldAsString("id") in [su1.id, su2.id]

            f = layers[0].GetNextFeature()

        ds.Destroy()

        with open(csvfile) as csvfile:
            csvreader = csv.reader(csvfile)
            for i, row in enumerate(csvreader):
                if i == 0:
                    assert row == ["id", "type", "key"]
                elif row[0] == su1.id:
                    assert row == [su1.id, su1.type, "value 1"]
                elif row[1] == su2.id:
                    assert row == [su2.id, su2.type, "value 2"]
コード例 #33
0
    def test_get_all_download(self):
        ensure_dirs()
        data = {'type': 'all'}
        user = UserFactory.create()
        project = ProjectFactory.create()
        res = ResourceFactory.create(project=project)

        form = forms.DownloadForm(project, user, data=data)
        assert form.is_valid() is True
        path, mime = form.get_file()
        assert '{}-{}'.format(project.id, user.id) in path
        assert mime == 'application/zip'

        with ZipFile(path, 'r') as testzip:
            assert len(testzip.namelist()) == 4
            assert res.original_file in testzip.namelist()
            assert 'resources.xlsx' in testzip.namelist()
            assert 'data.xlsx' in testzip.namelist()
            assert 'data-shp.zip' in testzip.namelist()
コード例 #34
0
    def test_make_download_not_standalone(self):
        ensure_dirs()
        original_es_dump_path = os.path.join(
            os.path.dirname(settings.BASE_DIR),
            'search/tests/files/test_es_dump_basic.esjson')
        es_dump_path = os.path.join(test_dir, 'test-shp2.esjson')
        shutil.copy(original_es_dump_path, es_dump_path)

        exporter = ShapeExporter(self.project, is_standalone=False)
        dir_path = exporter.make_download(es_dump_path)

        assert dir_path == os.path.join(test_dir, 'test-shp2-shp-dir')

        files = os.listdir(dir_path)
        assert len(files) == 5
        assert 'README.txt' in files
        assert 'point.dbf' in files
        assert 'point.prj' in files
        assert 'point.shp' in files
        assert 'point.shx' in files
コード例 #35
0
    def test_get_all_download(self):
        ensure_dirs()
        data = {"type": "all"}
        user = UserFactory.create()
        project = ProjectFactory.create()
        geometry = "SRID=4326;POINT (30 10)"
        SpatialUnitFactory.create(project=project, geometry=geometry)
        res = ResourceFactory.create(project=project)

        form = forms.DownloadForm(project, user, data=data)
        assert form.is_valid() is True
        path, mime = form.get_file()
        assert "{}-{}".format(project.id, user.id) in path
        assert mime == "application/zip"

        with ZipFile(path, "r") as testzip:
            assert len(testzip.namelist()) == 4
            assert res.original_file in testzip.namelist()
            assert "resources.xlsx" in testzip.namelist()
            assert "data.xlsx" in testzip.namelist()
            assert "data-shp.zip" in testzip.namelist()
コード例 #36
0
    def test_make_download_empty(self):
        ensure_dirs()
        original_es_dump_path = os.path.join(
            os.path.dirname(settings.BASE_DIR),
            'search/tests/files/test_es_dump_empty.esjson')
        es_dump_path = os.path.join(test_dir, 'test-all2.esjson')
        shutil.copy(original_es_dump_path, es_dump_path)

        exporter = AllExporter(self.project)
        zip_path, mime_type = exporter.make_download(es_dump_path)

        assert zip_path == os.path.join(test_dir, 'test-all2-res.zip')
        assert mime_type == ('application/zip')

        with ZipFile(zip_path) as myzip:
            assert myzip.namelist() == ['data.xlsx']
            myzip.extract('data.xlsx', test_dir)
            wb = load_workbook(os.path.join(test_dir, 'data.xlsx'))
            sheetnames = wb.get_sheet_names()
            assert sheetnames == ['Sheet']
            assert wb['Sheet']['A1'].value is None
コード例 #37
0
    def test_get_all_download(self):
        ensure_dirs()
        data = {'type': 'all'}
        user = UserFactory.create()
        project = ProjectFactory.create()
        geometry = 'SRID=4326;POINT (30 10)'
        SpatialUnitFactory.create(project=project, geometry=geometry)
        res = ResourceFactory.create(project=project)

        form = forms.DownloadForm(project, user, data=data)
        assert form.is_valid() is True
        path, mime = form.get_file()
        assert '{}-{}'.format(project.id, user.id) in path
        assert mime == 'application/zip'

        with ZipFile(path, 'r') as testzip:
            assert len(testzip.namelist()) == 4
            assert res.original_file in testzip.namelist()
            assert 'resources.xlsx' in testzip.namelist()
            assert 'data.xlsx' in testzip.namelist()
            assert 'data-shp.zip' in testzip.namelist()
コード例 #38
0
    def test_make_resource_worksheet(self):
        ensure_dirs()
        project = ProjectFactory.create()
        exporter = ResourceExporter(project)

        data = [['1', 'n1', 'd1', 'f1', 'l1', 'p1', 'r1'],
                ['2', 'n2', 'd2', 'f2', 'l2', 'p2', 'r2']]

        res_path = exporter.make_resource_worksheet('test-res', data)
        wb = load_workbook(filename=res_path, read_only=True)
        sheet = wb[wb.get_sheet_names()[0]]

        expected_headers = [
            'id', 'name', 'description', 'filename', 'locations', 'parties',
            'relationships'
        ]
        for i, h in enumerate(expected_headers):
            assert sheet[chr(i + 97) + '1'].value == h

        for i in range(0, len(data[0])):
            assert sheet[chr(i + 97) + '2'].value == data[0][i]
            assert sheet[chr(i + 97) + '3'].value == data[1][i]
コード例 #39
0
    def test_make_resource_worksheet(self):
        ensure_dirs()
        project = ProjectFactory.create()
        exporter = ResourceExporter(project)

        data = [
            ['1', 'n1', 'd1', 'f1', 'l1', 'p1', 'r1'],
            ['2', 'n2', 'd2', 'f2', 'l2', 'p2', 'r2']
        ]

        res_path = exporter.make_resource_worksheet('test-res', data)
        wb = load_workbook(filename=res_path, read_only=True)
        sheet = wb[wb.get_sheet_names()[0]]

        expected_headers = ['id', 'name', 'description', 'filename',
                            'locations', 'parties', 'relationships']
        for i, h in enumerate(expected_headers):
            assert sheet[chr(i + 97) + '1'].value == h

        for i in range(0, len(data[0])):
            assert sheet[chr(i + 97) + '2'].value == data[0][i]
            assert sheet[chr(i + 97) + '3'].value == data[1][i]
コード例 #40
0
    def test_make_download(self):
        ensure_dirs()
        project = ProjectFactory.create(current_questionnaire="123abc")
        exporter = ShapeExporter(project)

        content_type = ContentType.objects.get(app_label="spatial", model="spatialunit")
        schema = Schema.objects.create(
            content_type=content_type, selectors=(project.organization.id, project.id, "123abc")
        )
        attr_type = AttributeType.objects.get(name="text")
        Attribute.objects.create(
            schema=schema, name="key", long_name="Test field", attr_type=attr_type, index=0, required=False, omit=False
        )

        SpatialUnitFactory.create(project=project, geometry="POINT (1 1)", attributes={"key": "value 1"})

        path, mime = exporter.make_download("file5")

        assert path == os.path.join(settings.MEDIA_ROOT, "temp/file5.zip")
        assert mime == "application/zip"

        with ZipFile(path, "r") as testzip:
            assert len(testzip.namelist()) == 16
            assert project.slug + "-point.dbf" in testzip.namelist()
            assert project.slug + "-point.prj" in testzip.namelist()
            assert project.slug + "-point.shp" in testzip.namelist()
            assert project.slug + "-point.shx" in testzip.namelist()
            assert project.slug + "-line.dbf" in testzip.namelist()
            assert project.slug + "-line.prj" in testzip.namelist()
            assert project.slug + "-line.shp" in testzip.namelist()
            assert project.slug + "-line.shx" in testzip.namelist()
            assert project.slug + "-polygon.dbf" in testzip.namelist()
            assert project.slug + "-polygon.prj" in testzip.namelist()
            assert project.slug + "-polygon.shp" in testzip.namelist()
            assert project.slug + "-polygon.shx" in testzip.namelist()
            assert "relationships.csv" in testzip.namelist()
            assert "parties.csv" in testzip.namelist()
            assert "locations.csv" in testzip.namelist()
            assert "README.txt" in testzip.namelist()
コード例 #41
0
 def setup_models(self):
     ensure_dirs()
     self.project = ProjectFactory.create()
     self.user = UserFactory.create()
コード例 #42
0
    def test_write_features(self):
        ensure_dirs()
        project = ProjectFactory.create(current_questionnaire='123abc')
        exporter = ShapeExporter(project)

        content_type = ContentType.objects.get(app_label='spatial',
                                               model='spatialunit')
        schema = Schema.objects.create(
            content_type=content_type,
            selectors=(project.organization.id, project.id, '123abc', ))
        attr_type = AttributeType.objects.get(name='text')
        Attribute.objects.create(
            schema=schema,
            name='geom_type', long_name='Test field',
            attr_type=attr_type, index=0,
            required=False, omit=False
        )

        su1 = SpatialUnitFactory.create(
            project=project,
            geometry='SRID=4326;POINT (30 10)',
            attributes={'geom_type': 'point'})
        su2 = SpatialUnitFactory.create(
            project=project,
            geometry='SRID=4326;LINESTRING (30 10, 10 30, 40 40)',
            attributes={'geom_type': 'linestring'})
        su3 = SpatialUnitFactory.create(
            project=project,
            geometry='SRID=4326;POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))',
            attributes={'geom_type': 'polygon'})
        su4 = SpatialUnitFactory.create(
            project=project,
            geometry='SRID=4326;'
                     'MULTIPOINT ((10 40), (40 30), (20 20), (30 10))',
            attributes={'geom_type': 'multipoint'})
        su5 = SpatialUnitFactory.create(
            project=project,
            geometry='SRID=4326;'
                     'MULTILINESTRING ((10 10, 20 20, 10 40),'
                     '(40 40, 30 30, 40 20, 30 10))',
            attributes={'geom_type': 'multilinestring'})
        su6 = SpatialUnitFactory.create(
            project=project,
            geometry='SRID=4326;'
                     'MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),'
                     '((15 5, 40 10, 10 20, 5 10, 15 5)))',
            attributes={'geom_type': 'multipolygon'})

        dst_dir = os.path.join(settings.MEDIA_ROOT, 'temp/file4')
        ds = exporter.create_datasource(dst_dir)
        filename = os.path.join(dst_dir, 'locations.csv')

        layers = exporter.write_features(ds, filename)
        assert len(layers.keys()) == 6
        for layer_name, layer in layers.items():
            su = SpatialUnit.objects.get(attributes={'geom_type': layer_name})
            geom = su.geometry
            feature = layer.GetNextFeature()
            assert geom.equals(GEOSGeometry(feature.geometry().ExportToWkt()))
            assert feature.GetFieldAsString('id') == su.id
        ds.Destroy()

        with open(filename) as csvfile:
            csvreader = csv.reader(csvfile)
            for i, row in enumerate(csvreader):
                if i == 0:
                    assert row == ['id', 'type', 'geom_type']
                if i == 1:
                    assert row == [su1.id, su1.type, 'point']
                if i == 2:
                    assert row == [su2.id, su2.type, 'linestring']
                if i == 3:
                    assert row == [su3.id, su3.type, 'polygon']
                if i == 4:
                    assert row == [su4.id, su4.type, 'multipoint']
                if i == 5:
                    assert row == [su5.id, su5.type, 'multilinestring']
                if i == 6:
                    assert row == [su6.id, su6.type, 'multipolygon']

        # remove this so other tests pass
        os.remove(filename)
コード例 #43
0
    def test_make_download(self):
        res = ResourceFactory.create(project=self.project)
        loc1 = SpatialUnitFactory.create(project=self.project)
        loc2 = SpatialUnitFactory.create(project=self.project)
        par = PartyFactory.create(project=self.project)
        rel = TenureRelationshipFactory.create(project=self.project)

        ContentObject.objects.create(resource=res, content_object=loc1)
        ContentObject.objects.create(resource=res, content_object=loc2)
        ContentObject.objects.create(resource=res, content_object=par)
        ContentObject.objects.create(resource=res, content_object=rel)

        ensure_dirs()
        original_es_dump_path = os.path.join(
            os.path.dirname(settings.BASE_DIR),
            'search/tests/files/test_es_dump_basic.esjson')
        es_dump_path = os.path.join(test_dir, 'test-res1-orig.esjson')
        shutil.copy(original_es_dump_path, es_dump_path)

        # Rewrite the ES dump file to inject the resource ID
        with open(es_dump_path, 'r') as infile:
            es_dump_path = os.path.join(test_dir, 'test-res1.esjson')
            fwrite = open(es_dump_path, 'w')
            for line in infile:
                fwrite.write(line.replace('ID3', res.id))
            fwrite.close()

        exporter = ResourceExporter(self.project)
        zip_path, mime_type = exporter.make_download(es_dump_path)

        assert zip_path == os.path.join(test_dir, 'test-res1-res.zip')
        assert mime_type == ('application/zip')

        with ZipFile(zip_path) as myzip:
            files = myzip.namelist()
            assert len(files) == 2
            assert 'resources.xlsx' in files
            assert 'resources/baby_goat.jpeg' in files

            myzip.extract('resources.xlsx', test_dir)
            wb = load_workbook(os.path.join(test_dir, 'resources.xlsx'))
            assert wb.get_sheet_names() == ['resources']

            ws = wb['resources']
            assert ws['A1'].value == 'id'
            assert ws['B1'].value == 'name'
            assert ws['C1'].value == 'description'
            assert ws['D1'].value == 'filename'
            assert ws['E1'].value == 'locations'
            assert ws['F1'].value == 'parties'
            assert ws['G1'].value == 'relationships'
            assert ws['A2'].value == res.id
            assert ws['B2'].value == "Goat"
            assert ws['C2'].value == "Let's pretend there's a description."
            assert ws['D2'].value == "baby_goat.jpeg"
            ids = ws['E2'].value.split(',')
            assert len(ids) == 2
            assert loc1.id in ids
            assert loc2.id in ids
            assert ws['F2'].value == par.id
            assert ws['G2'].value == rel.id
コード例 #44
0
    def test_write_features(self):
        ensure_dirs()
        project = ProjectFactory.create(current_questionnaire='123abc')
        exporter = ShapeExporter(project)

        content_type = ContentType.objects.get(app_label='spatial',
                                               model='spatialunit')
        schema = Schema.objects.create(content_type=content_type,
                                       selectors=(
                                           project.organization.id,
                                           project.id,
                                           '123abc',
                                       ))
        attr_type = AttributeType.objects.get(name='text')
        Attribute.objects.create(schema=schema,
                                 name='geom_type',
                                 long_name='Test field',
                                 attr_type=attr_type,
                                 index=0,
                                 required=False,
                                 omit=False)

        su1 = SpatialUnitFactory.create(project=project,
                                        geometry='SRID=4326;POINT (30 10)',
                                        attributes={'geom_type': 'point'})
        su2 = SpatialUnitFactory.create(
            project=project,
            geometry='SRID=4326;LINESTRING (30 10, 10 30, 40 40)',
            attributes={'geom_type': 'linestring'})
        su3 = SpatialUnitFactory.create(
            project=project,
            geometry='SRID=4326;POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))',
            attributes={'geom_type': 'polygon'})
        su4 = SpatialUnitFactory.create(
            project=project,
            geometry='SRID=4326;'
            'MULTIPOINT ((10 40), (40 30), (20 20), (30 10))',
            attributes={'geom_type': 'multipoint'})
        su5 = SpatialUnitFactory.create(
            project=project,
            geometry='SRID=4326;'
            'MULTILINESTRING ((10 10, 20 20, 10 40),'
            '(40 40, 30 30, 40 20, 30 10))',
            attributes={'geom_type': 'multilinestring'})
        su6 = SpatialUnitFactory.create(
            project=project,
            geometry='SRID=4326;'
            'MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),'
            '((15 5, 40 10, 10 20, 5 10, 15 5)))',
            attributes={'geom_type': 'multipolygon'})

        dst_dir = os.path.join(settings.MEDIA_ROOT, 'temp/file4')
        ds = exporter.create_datasource(dst_dir)
        filename = os.path.join(dst_dir, 'locations.csv')

        layers = exporter.write_features(ds, filename)
        assert len(layers.keys()) == 6
        for layer_name, layer in layers.items():
            su = SpatialUnit.objects.get(attributes={'geom_type': layer_name})
            geom = su.geometry
            feature = layer.GetNextFeature()
            assert geom.equals(GEOSGeometry(feature.geometry().ExportToWkt()))
            assert feature.GetFieldAsString('id') == su.id
        ds.Destroy()

        with open(filename) as csvfile:
            csvreader = csv.reader(csvfile)
            for i, row in enumerate(csvreader):
                if i == 0:
                    assert row == ['id', 'type', 'geom_type']
                if i == 1:
                    assert row == [su1.id, su1.type, 'point']
                if i == 2:
                    assert row == [su2.id, su2.type, 'linestring']
                if i == 3:
                    assert row == [su3.id, su3.type, 'polygon']
                if i == 4:
                    assert row == [su4.id, su4.type, 'multipoint']
                if i == 5:
                    assert row == [su5.id, su5.type, 'multilinestring']
                if i == 6:
                    assert row == [su6.id, su6.type, 'multipolygon']

        # remove this so other tests pass
        os.remove(filename)
コード例 #45
0
    def test_make_download(self):
        ensure_dirs()
        original_es_dump_path = os.path.join(
            os.path.dirname(settings.BASE_DIR),
            'search/tests/files/test_es_dump_basic.esjson')
        es_dump_path = os.path.join(test_dir, 'test-xls1.esjson')
        shutil.copy(original_es_dump_path, es_dump_path)

        exporter = XLSExporter(self.project)
        xls_path, mime_type = exporter.make_download(es_dump_path)

        assert xls_path == os.path.join(test_dir, 'test-xls1.xlsx')
        assert mime_type == ('application/vnd.openxmlformats-officedocument.'
                             'spreadsheetml.sheet')

        wb = load_workbook(xls_path)
        assert wb.get_sheet_names() == [
            'locations', 'parties', 'relationships'
        ]

        ws = wb['locations']
        assert ws['A1'].value == 'id'
        assert ws['B1'].value == 'geometry.ewkt'
        assert ws['C1'].value == 'type'
        assert ws['D1'].value == 'quality'
        assert ws['E1'].value == 'infrastructure'
        assert ws['A2'].value == 'ID0'
        assert ws['B2'].value == 'SRID=4326;POINT (1 1)'
        assert ws['C2'].value == 'PA'
        assert ws['D2'].value == 'point'
        assert ws['E2'].value == 'food, electricity'

        ws = wb['parties']
        assert ws['A1'].value == 'id'
        assert ws['B1'].value == 'name'
        assert ws['C1'].value == 'type'
        assert ws['D1'].value == 'notes'
        assert ws['E1'].value == 'gender'
        assert ws['F1'].value == 'homeowner'
        assert ws['G1'].value == 'dob'
        assert ws['H1'].value == 'number_of_members'
        assert ws['I1'].value == 'date_formed'
        assert ws['A2'].value == 'ID1'
        assert ws['B2'].value == "Cadastanaut"
        assert ws['C2'].value == 'IN'
        assert ws['D2'].value is None
        assert ws['E2'].value == 'm'
        assert ws['F2'].value == 'yes'
        assert ws['G2'].value == '1951-05-05'
        assert ws['H2'].value is None
        assert ws['I2'].value is None

        ws = wb['relationships']
        assert ws['A1'].value == 'id'
        assert ws['B1'].value == 'party_id'
        assert ws['C1'].value == 'spatial_unit_id'
        assert ws['D1'].value == 'tenure_type_id'
        assert ws['E1'].value == 'notes'
        assert ws['A2'].value == 'ID2'
        assert ws['B2'].value == 'ID1'
        assert ws['C2'].value == 'ID0'
        assert ws['D2'].value == 'CU'
        assert ws['E2'].value == "The best relationship!"
コード例 #46
0
    def test_make_download_standalone(self):
        ensure_dirs()
        original_es_dump_path = os.path.join(
            os.path.dirname(settings.BASE_DIR),
            'search/tests/files/test_es_dump_basic.esjson')
        es_dump_path = os.path.join(test_dir, 'test-shp1.esjson')
        shutil.copy(original_es_dump_path, es_dump_path)

        exporter = ShapeExporter(self.project)
        zip_path, mime_type = exporter.make_download(es_dump_path)

        assert zip_path == os.path.join(test_dir, 'test-shp1-shp.zip')
        assert mime_type == ('application/zip')

        with ZipFile(zip_path) as myzip:
            files = myzip.namelist()
            assert len(files) == 8
            assert 'README.txt' in files
            assert 'locations.csv' in files
            assert 'parties.csv' in files
            assert 'relationships.csv' in files
            assert 'point.dbf' in files
            assert 'point.prj' in files
            assert 'point.shp' in files
            assert 'point.shx' in files

            with myzip.open('locations.csv') as csv_file:
                rows = list(csv.reader(io.TextIOWrapper(csv_file)))
                assert rows[0][0] == 'id'
                assert rows[0][1] == 'type'
                assert rows[0][2] == 'quality'
                assert rows[0][3] == 'infrastructure'
                assert rows[1][0] == 'ID0'
                assert rows[1][1] == 'PA'
                assert rows[1][2] == 'point'
                assert rows[1][3] == 'food, electricity'

            with myzip.open('parties.csv') as csv_file:
                rows = list(csv.reader(io.TextIOWrapper(csv_file)))
                assert rows[0][0] == 'id'
                assert rows[0][1] == 'name'
                assert rows[0][2] == 'type'
                assert rows[0][3] == 'notes'
                assert rows[0][4] == 'gender'
                assert rows[0][5] == 'homeowner'
                assert rows[0][6] == 'dob'
                assert rows[0][7] == 'number_of_members'
                assert rows[0][8] == 'date_formed'
                assert rows[1][0] == 'ID1'
                assert rows[1][1] == "Cadastanaut"
                assert rows[1][2] == 'IN'
                assert rows[1][3] is ''
                assert rows[1][4] == 'm'
                assert rows[1][5] == 'yes'
                assert rows[1][6] == '1951-05-05'
                assert rows[1][7] is ''
                assert rows[1][8] is ''

            with myzip.open('relationships.csv') as csv_file:
                rows = list(csv.reader(io.TextIOWrapper(csv_file)))
                assert rows[0][0] == 'id'
                assert rows[0][1] == 'party_id'
                assert rows[0][2] == 'spatial_unit_id'
                assert rows[0][3] == 'tenure_type_id'
                assert rows[0][4] == 'notes'
                assert rows[1][0] == 'ID2'
                assert rows[1][1] == 'ID1'
                assert rows[1][2] == 'ID0'
                assert rows[1][3] == 'CU'
                assert rows[1][4] == "The best relationship!"
コード例 #47
0
    def test_make_download(self):
        res = ResourceFactory.create(project=self.project)
        loc1 = SpatialUnitFactory.create(project=self.project)
        loc2 = SpatialUnitFactory.create(project=self.project)
        par = PartyFactory.create(project=self.project)
        rel = TenureRelationshipFactory.create(project=self.project)

        ContentObject.objects.create(resource=res, content_object=loc1)
        ContentObject.objects.create(resource=res, content_object=loc2)
        ContentObject.objects.create(resource=res, content_object=par)
        ContentObject.objects.create(resource=res, content_object=rel)

        ensure_dirs()
        original_es_dump_path = os.path.join(
            os.path.dirname(settings.BASE_DIR),
            'search/tests/files/test_es_dump_basic.esjson')
        es_dump_path = os.path.join(test_dir, 'test-all1-orig.esjson')
        shutil.copy(original_es_dump_path, es_dump_path)

        # Rewrite the ES dump file to inject the resource ID
        with open(es_dump_path, 'r') as infile:
            es_dump_path = os.path.join(test_dir, 'test-all1.esjson')
            fwrite = open(es_dump_path, 'w')
            for line in infile:
                fwrite.write(line.replace('ID3', res.id))
            fwrite.close()

        exporter = AllExporter(self.project)
        zip_path, mime_type = exporter.make_download(es_dump_path)

        assert zip_path == os.path.join(test_dir, 'test-all1-res.zip')
        assert mime_type == ('application/zip')

        with ZipFile(zip_path) as myzip:
            files = myzip.namelist()
            assert len(files) == 8
            assert 'data.xlsx' in files
            assert 'resources.xlsx' in files
            assert 'resources/baby_goat.jpeg' in files
            assert 'shape_files/README.txt' in files
            assert 'shape_files/point.dbf' in files
            assert 'shape_files/point.prj' in files
            assert 'shape_files/point.shp' in files
            assert 'shape_files/point.shx' in files

            myzip.extract('resources.xlsx', test_dir)
            wb = load_workbook(os.path.join(test_dir, 'resources.xlsx'))
            assert wb.get_sheet_names() == ['resources']

            ws = wb['resources']
            assert ws['A1'].value == 'id'
            assert ws['B1'].value == 'name'
            assert ws['C1'].value == 'description'
            assert ws['D1'].value == 'filename'
            assert ws['E1'].value == 'locations'
            assert ws['F1'].value == 'parties'
            assert ws['G1'].value == 'relationships'
            assert ws['A2'].value == res.id
            assert ws['B2'].value == "Goat"
            assert ws['C2'].value == "Let's pretend there's a description."
            assert ws['D2'].value == "baby_goat.jpeg"
            ids = ws['E2'].value.split(',')
            assert len(ids) == 2
            assert loc1.id in ids
            assert loc2.id in ids
            assert ws['F2'].value == par.id
            assert ws['G2'].value == rel.id

            myzip.extract('data.xlsx', test_dir)
            wb = load_workbook(os.path.join(test_dir, 'data.xlsx'))
            assert wb.get_sheet_names() == [
                'locations', 'parties', 'relationships'
            ]

            ws = wb['locations']
            assert ws['A1'].value == 'id'
            assert ws['B1'].value == 'geometry.ewkt'
            assert ws['C1'].value == 'type'
            assert ws['D1'].value == 'quality'
            assert ws['E1'].value == 'infrastructure'
            assert ws['A2'].value == 'ID0'
            assert ws['B2'].value == 'SRID=4326;POINT (1 1)'
            assert ws['C2'].value == 'PA'
            assert ws['D2'].value == 'point'
            assert ws['E2'].value == 'food, electricity'

            ws = wb['parties']
            assert ws['A1'].value == 'id'
            assert ws['B1'].value == 'name'
            assert ws['C1'].value == 'type'
            assert ws['D1'].value == 'notes'
            assert ws['E1'].value == 'gender'
            assert ws['F1'].value == 'homeowner'
            assert ws['G1'].value == 'dob'
            assert ws['H1'].value == 'number_of_members'
            assert ws['I1'].value == 'date_formed'
            assert ws['A2'].value == 'ID1'
            assert ws['B2'].value == "Cadastanaut"
            assert ws['C2'].value == 'IN'
            assert ws['D2'].value is None
            assert ws['E2'].value == 'm'
            assert ws['F2'].value == 'yes'
            assert ws['G2'].value == '1951-05-05'
            assert ws['H2'].value is None
            assert ws['I2'].value is None

            ws = wb['relationships']
            assert ws['A1'].value == 'id'
            assert ws['B1'].value == 'party_id'
            assert ws['C1'].value == 'spatial_unit_id'
            assert ws['D1'].value == 'tenure_type_id'
            assert ws['E1'].value == 'notes'
            assert ws['A2'].value == 'ID2'
            assert ws['B2'].value == 'ID1'
            assert ws['C2'].value == 'ID0'
            assert ws['D2'].value == 'CU'
            assert ws['E2'].value == "The best relationship!"