Пример #1
0
def test_datetime_field_type_marked_not_supported_is_not_supported(
        tmpdir, driver, field_type, monkeypatch):
    """ Test if a date/datetime/time field type marked as not not supported is really not supported

    Warning: Success of this test does not necessary mean that a field is not supported. E.g. errors can occour due to
    special schema requirements of drivers. This test only covers the standard case.

    """

    if driver == "BNA" and GDALVersion.runtime() < GDALVersion(2, 0):
        pytest.skip("BNA driver segfaults with gdal 1.11")

    monkeypatch.delitem(
        fiona.drvsupport._driver_field_type_unsupported[field_type], driver)

    schema = get_schema(driver, field_type)
    path = str(tmpdir.join(get_temp_filename(driver)))
    values_in, values_out = zip(*generate_testdata(field_type, driver))
    records = get_records(driver, values_in)

    is_good = True
    try:
        with fiona.open(path, 'w', driver=driver, schema=schema) as c:
            c.writerecords(records)

        with fiona.open(path, 'r') as c:
            if not get_schema_field(driver, c.schema) == field_type:
                is_good = False
            items = [get_field(driver, f) for f in c]
            for val_in, val_out in zip(items, values_out):
                if not val_in == val_out:
                    is_good = False
    except:
        is_good = False
    assert not is_good
Пример #2
0
def test_write_does_not_work_when_gdal_smaller_mingdal(tmpdir, driver,
                                                       testdata_generator,
                                                       monkeypatch):
    """
        Test if driver really can't write for gdal < driver_mode_mingdal

        If this test fails, it should be considered to update driver_mode_mingdal in drvsupport.py.

    """

    if driver == "BNA" and GDALVersion.runtime() < GDALVersion(2, 0):
        pytest.skip("BNA driver segfaults with gdal 1.11")
    if (driver == 'FlatGeobuf' and calc_gdal_version_num(3, 1, 0) <=
            get_gdal_version_num() < calc_gdal_version_num(3, 1, 3)):
        pytest.skip("See https://github.com/Toblerity/Fiona/pull/924")

    schema, crs, records1, _, test_equal, create_kwargs = testdata_generator(
        driver, range(0, 10), [])
    path = str(tmpdir.join(get_temp_filename(driver)))

    if (driver in driver_mode_mingdal['w'] and get_gdal_version_num() <
            calc_gdal_version_num(*driver_mode_mingdal['w'][driver])):
        monkeypatch.delitem(fiona.drvsupport.driver_mode_mingdal['w'], driver)

        with pytest.raises(Exception):
            with fiona.open(path,
                            'w',
                            driver=driver,
                            crs=crs,
                            schema=schema,
                            **create_kwargs) as c:
                c.writerecords(records1)
Пример #3
0
def test_no_write_driver_cannot_write(tmpdir, driver, testdata_generator,
                                      monkeypatch):
    """Test if read only driver cannot write

    If this test fails, it should be considered to enable write support for the respective driver in drvsupport.py.

    """

    monkeypatch.setitem(fiona.drvsupport.supported_drivers, driver, 'rw')
    schema, crs, records1, _, test_equal, create_kwargs = testdata_generator(
        driver, range(0, 5), [])

    if driver == "BNA" and GDALVersion.runtime() < GDALVersion(2, 0):
        pytest.skip("BNA driver segfaults with gdal 1.11")

    if driver == "FlatGeobuf":
        pytest.xfail(
            "FlatGeobuf doesn't raise an error but doesn't have write support")

    path = str(tmpdir.join(get_temp_filename(driver)))

    with pytest.raises(Exception):
        with fiona.open(path,
                        'w',
                        driver=driver,
                        crs=crs,
                        schema=schema,
                        **create_kwargs) as c:
            c.writerecords(records1)
Пример #4
0
def test_append_or_driver_error(tmpdir, testdata_generator, driver):
    """ Test if driver supports append mode.

    Some driver only allow a specific schema. These drivers can be excluded by adding them to blacklist_append_drivers.

    """
    if driver == "DGN":
        pytest.xfail("DGN schema has changed")

    if driver == "BNA" and GDALVersion.runtime() < GDALVersion(2, 0):
        pytest.skip("BNA driver segfaults with gdal 1.11")

    path = str(tmpdir.join(get_temp_filename(driver)))
    schema, crs, records1, records2, test_equal, create_kwargs = testdata_generator(
        driver, range(0, 5), range(5, 10))

    # If driver is not able to write, we cannot test append
    if (driver in driver_mode_mingdal['w'] and get_gdal_version_num() <
            calc_gdal_version_num(*driver_mode_mingdal['w'][driver])):
        return

    # Create test file to append to
    with fiona.open(path,
                    'w',
                    driver=driver,
                    crs=crs,
                    schema=schema,
                    **create_kwargs) as c:

        c.writerecords(records1)

    if (driver in driver_mode_mingdal['a'] and get_gdal_version_num() <
            calc_gdal_version_num(*driver_mode_mingdal['a'][driver])):

        # Test if DriverError is raised for gdal < driver_mode_mingdal
        with pytest.raises(DriverError):
            with fiona.open(path, 'a', driver=driver) as c:
                c.writerecords(records2)

    else:
        # Test if we can append
        with fiona.open(path, 'a', driver=driver) as c:
            c.writerecords(records2)

        if driver in {'FileGDB', 'OpenFileGDB'}:
            open_driver = driver
        else:
            open_driver = None
        with fiona.open(path, driver=open_driver) as c:
            assert c.driver == driver
            items = list(c)
            assert len(items) == len(records1) + len(records2)
            for val_in, val_out in zip(records1 + records2, items):
                assert test_equal(
                    driver, val_in,
                    val_out), "in: {val_in}, out: {val_out}".format(
                        val_in=val_in, val_out=val_out)
Пример #5
0
def test_no_append_driver_cannot_append(tmpdir, driver, testdata_generator,
                                        monkeypatch):
    """
    Test if a driver that supports write and not append cannot also append

    If this test fails, it should be considered to enable append support for the respective driver in drvsupport.py.

    """

    monkeypatch.setitem(fiona.drvsupport.supported_drivers, driver, 'raw')

    if driver == "BNA" and GDALVersion.runtime() < GDALVersion(2, 0):
        pytest.skip("BNA driver segfaults with gdal 1.11")

    path = str(tmpdir.join(get_temp_filename(driver)))
    schema, crs, records1, records2, test_equal, create_kwargs = testdata_generator(
        driver, range(0, 5), range(5, 10))

    # If driver is not able to write, we cannot test append
    if (driver in driver_mode_mingdal['w'] and get_gdal_version_num() <
            calc_gdal_version_num(*driver_mode_mingdal['w'][driver])):
        return

    # Create test file to append to
    with fiona.open(path,
                    'w',
                    driver=driver,
                    crs=crs,
                    schema=schema,
                    **create_kwargs) as c:

        c.writerecords(records1)

    is_good = True
    try:
        with fiona.open(path, 'a', driver=driver) as c:
            c.writerecords(records2)

        if driver in {'FileGDB', 'OpenFileGDB'}:
            open_driver = driver
        else:
            open_driver = None
        with fiona.open(path, driver=open_driver) as c:
            assert c.driver == driver
            items = list(c)
            is_good = is_good and len(items) == len(records1) + len(records2)
            for val_in, val_out in zip(records1 + records2, items):
                is_good = is_good and test_equal(driver, val_in, val_out)
    except:
        is_good = False

    assert not is_good
Пример #6
0
def test_bounds(tmpdir, driver):
    """Test if bounds are correctly calculated after writing

    """
    if driver == 'BNA' and GDALVersion.runtime() < GDALVersion(2, 0):
        # BNA driver segfaults with gdal 1.11
        return

    extension = driver_extensions.get(driver, "bar")
    path = str(tmpdir.join('foo.{}'.format(extension)))

    with fiona.open(path,
                    'w',
                    driver=driver,
                    schema={
                        'geometry': 'Point',
                        'properties': [('title', 'str')]
                    },
                    fiona_force_driver=True) as c:

        c.writerecords([{
            'geometry': {
                'type': 'Point',
                'coordinates': (1.0, 10.0)
            },
            'properties': {
                'title': 'One'
            }
        }])

        try:
            bounds = c.bounds
            assert bounds == (1.0, 10.0, 1.0, 10.0)
        except Exception as e:
            assert isinstance(e, DriverError)

        c.writerecords([{
            'geometry': {
                'type': 'Point',
                'coordinates': (2.0, 20.0)
            },
            'properties': {
                'title': 'Two'
            }
        }])

        try:
            bounds = c.bounds
            assert bounds == (1.0, 10.0, 2.0, 20.0)
        except Exception as e:
            assert isinstance(e, DriverError)
Пример #7
0
def test_write_or_driver_error(tmpdir, driver, testdata_generator):
    """
        Test if write mode works.

    """

    if driver == "BNA" and GDALVersion.runtime() < GDALVersion(2, 0):
        pytest.skip("BNA driver segfaults with gdal 1.11")

    schema, crs, records1, _, test_equal, create_kwargs = testdata_generator(
        driver, range(0, 10), [])
    path = str(tmpdir.join(get_temp_filename(driver)))

    if (driver in driver_mode_mingdal['w'] and get_gdal_version_num() <
            calc_gdal_version_num(*driver_mode_mingdal['w'][driver])):

        # Test if DriverError is raised for gdal < driver_mode_mingdal
        with pytest.raises(DriverError):
            with fiona.open(path,
                            'w',
                            driver=driver,
                            crs=crs,
                            schema=schema,
                            **create_kwargs) as c:
                c.writerecords(records1)

    else:
        # Test if we can write
        with fiona.open(path,
                        'w',
                        driver=driver,
                        crs=crs,
                        schema=schema,
                        **create_kwargs) as c:

            c.writerecords(records1)

        if driver in {'FileGDB', 'OpenFileGDB'}:
            open_driver = driver
        else:
            open_driver = None
        with fiona.open(path, driver=open_driver) as c:
            assert c.driver == driver
            items = list(c)
            assert len(items) == len(records1)
            for val_in, val_out in zip(records1, items):
                assert test_equal(
                    driver, val_in,
                    val_out), "in: {val_in}, out: {val_out}".format(
                        val_in=val_in, val_out=val_out)
Пример #8
0
def test_append_does_not_work_when_gdal_smaller_mingdal(
        tmpdir, driver, testdata_generator, monkeypatch):
    """ Test if driver supports append mode.

    If this test fails, it should be considered to update driver_mode_mingdal in drvsupport.py.

    """

    if driver == "BNA" and GDALVersion.runtime() < GDALVersion(2, 0):
        pytest.skip("BNA driver segfaults with gdal 1.11")

    path = str(tmpdir.join(get_temp_filename(driver)))
    schema, crs, records1, records2, test_equal, create_kwargs = testdata_generator(
        driver, range(0, 5), range(5, 10))

    # If driver is not able to write, we cannot test append
    if (driver in driver_mode_mingdal['w'] and get_gdal_version_num() <
            calc_gdal_version_num(*driver_mode_mingdal['w'][driver])):
        return

    # Create test file to append to
    with fiona.open(path,
                    'w',
                    driver=driver,
                    crs=crs,
                    schema=schema,
                    **create_kwargs) as c:

        c.writerecords(records1)

    if (driver in driver_mode_mingdal['a'] and get_gdal_version_num() <
            calc_gdal_version_num(*driver_mode_mingdal['a'][driver])):
        # Test if driver really can't append for gdal < driver_mode_mingdal

        monkeypatch.delitem(fiona.drvsupport.driver_mode_mingdal['a'], driver)

        with pytest.raises(Exception):
            with fiona.open(path, 'a', driver=driver) as c:
                c.writerecords(records2)

            if driver in {'FileGDB', 'OpenFileGDB'}:
                open_driver = driver
            else:
                open_driver = None
            with fiona.open(path, driver=open_driver) as c:
                assert c.driver == driver
                items = list(c)
                assert len(items) == len(records1) + len(records2)
                for val_in, val_out in zip(records1 + records2, items):
                    assert test_equal(driver, val_in, val_out)
Пример #9
0
def test_geometry_only_schema_update(tmpdir, driver):

    # Guard unsupported drivers
    if driver in driver_mode_mingdal['a'] and GDALVersion.runtime(
    ) < GDALVersion(*driver_mode_mingdal['a'][driver][:2]):
        return

    schema = {
        "geometry": "Polygon",
        # No properties defined here.
    }

    record1 = {
        'geometry': {
            'type':
            'Polygon',
            'coordinates': [[(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (1.0, 0.0),
                             (0.0, 0.0)]]
        }
    }
    record2 = {
        'geometry': {
            'type':
            'Polygon',
            'coordinates': [[(0.0, 0.0), (2.0, 0.0), (2.0, 2.0), (2.0, 0.0),
                             (0.0, 0.0)]]
        }
    }

    path = str(tmpdir.join(get_temp_filename(driver)))

    # Create file
    with fiona.open(path, mode='w', driver=driver, schema=schema) as c:
        c.write(record1)

    # Append record
    with fiona.open(path, mode='a', driver=driver) as c:
        c.write(record2)

    with fiona.open(path, mode='r', driver=driver) as c:
        data = [f for f in c]
        assert len(data) == 2

        for f in data:
            assert len(f.get('properties', {})) == 0
        assert data[0]['geometry'] == record1['geometry']
        assert data[1]['geometry'] == record2['geometry']
Пример #10
0
def test_property_only_schema_update(tmpdir, driver):

    # Guard unsupported drivers
    if driver in driver_mode_mingdal['a'] and GDALVersion.runtime(
    ) < GDALVersion(*driver_mode_mingdal['a'][driver][:2]):
        return

    schema = {
        # No geometry defined here.
        "properties": {
            'prop1': 'str'
        }
    }

    record1 = {'properties': {'prop1': 'one'}}
    record2 = {'properties': {'prop1': 'two'}}

    path = str(tmpdir.join(get_temp_filename(driver)))

    # Create file
    with fiona.open(path, mode='w', driver=driver, schema=schema) as c:
        c.write(record1)

    # Append record
    with fiona.open(path, mode='a', driver=driver) as c:
        c.write(record2)

    with fiona.open(path, mode='r', driver=driver) as c:
        data = [f for f in c]
        assert len(data) == 2
        for f in data:
            assert len(f.get('properties', {})) == 1
            assert 'geometry' not in f or f['geometry'] is None
        assert 'prop1' in data[0]['properties'] and data[0]['properties'][
            'prop1'] == 'one'
        assert 'prop1' in data[1]['properties'] and data[1]['properties'][
            'prop1'] == 'two'
Пример #11
0
    request.cls.data_dir = data_dir


@pytest.fixture(scope='class')
def uttc_path_gpx(path_gpx, request):
    """Make the ``path_gpx`` fixture work with a ``unittest.TestCase()``.
    ``uttc`` stands for unittest test case."""
    request.cls.path_gpx = path_gpx


# GDAL 2.3.x silently converts ESRI WKT to OGC WKT
# The regular expression below will match against either
WGS84PATTERN = 'GEOGCS\["(?:GCS_WGS_1984|WGS 84)",DATUM\["WGS_1984",SPHEROID\["WGS[_ ]84"'

# Define helpers to skip tests based on GDAL version
gdal_version = GDALVersion.runtime()

requires_only_gdal1 = pytest.mark.skipif(gdal_version.major != 1,
                                         reason="Only relevant for GDAL 1.x")

requires_gdal2 = pytest.mark.skipif(not gdal_version.major >= 2,
                                    reason="Requires GDAL 2.x")

requires_gdal21 = pytest.mark.skipif(not gdal_version.at_least('2.1'),
                                     reason="Requires GDAL 2.1.x")

requires_gdal22 = pytest.mark.skipif(not gdal_version.at_least('2.2'),
                                     reason="Requires GDAL 2.2.x")

requires_gdal24 = pytest.mark.skipif(not gdal_version.at_least('2.4'),
                                     reason="Requires GDAL 2.4.x")
Пример #12
0
    request.cls.data_dir = data_dir


@pytest.fixture(scope='class')
def uttc_path_gpx(path_gpx, request):
    """Make the ``path_gpx`` fixture work with a ``unittest.TestCase()``.
    ``uttc`` stands for unittest test case."""
    request.cls.path_gpx = path_gpx


# GDAL 2.3.x silently converts ESRI WKT to OGC WKT
# The regular expression below will match against either
WGS84PATTERN = 'GEOGCS\["(?:GCS_WGS_1984|WGS 84)",DATUM\["WGS_1984",SPHEROID\["WGS[_ ]84"'

# Define helpers to skip tests based on GDAL version
gdal_version = GDALVersion.runtime()

requires_only_gdal1 = pytest.mark.skipif(
    gdal_version.major != 1,
    reason="Only relevant for GDAL 1.x")

requires_gdal2 = pytest.mark.skipif(
    not gdal_version.major >= 2,
    reason="Requires GDAL 2.x")

requires_gdal21 = pytest.mark.skipif(
    not gdal_version.at_least('2.1'),
    reason="Requires GDAL 2.1.x")

requires_gdal22 = pytest.mark.skipif(
    not gdal_version.at_least('2.2'),