Пример #1
0
 def test_to_geojson(self, gdf):
     vc = DriverVectorCube(gdf)
     assert vc.to_geojson() == DictSubSet({
         "type":
         "FeatureCollection",
         "features": [
             DictSubSet({
                 "type": "Feature",
                 "geometry": {
                     "type": "Polygon",
                     "coordinates": (((1, 1), (3, 1), (2, 3), (1, 1)), )
                 },
                 "properties": {
                     "id": "first",
                     "pop": 1234
                 },
             }),
             DictSubSet({
                 "type": "Feature",
                 "geometry": {
                     "type": "Polygon",
                     "coordinates": (((4, 2), (5, 4), (3, 4), (4, 2)), )
                 },
                 "properties": {
                     "id": "second",
                     "pop": 5678
                 },
             }),
         ]
     })
Пример #2
0
def test_dict_subset_nesting():
    assert {1: 2, 3: 4, 5: {6: 7, 8: 9}} == DictSubSet({})
    assert {1: 2, 3: 4, 5: {6: 7, 8: 9}} == DictSubSet({5: DictSubSet({})})
    assert {
        1: 2,
        3: 4,
        5: {
            6: 7,
            8: 9
        }
    } == DictSubSet({
        3: 4,
        5: DictSubSet({8: 9})
    })
    assert {1: {2: 3, 4: 5}} == {1: DictSubSet({4: 5})}
    assert {
        1: {
            2: {
                3: {
                    4: 5,
                    6: 7
                },
                8: 9
            }
        }
    } == {
        1: {
            2: DictSubSet({3: DictSubSet({})})
        }
    }
Пример #3
0
def test_dict_subset():
    expected = DictSubSet({"foo": "bar"})
    assert {"foo": "bar"} == expected
    assert {"foo": "nope"} != expected
    assert {"foo": "bar", "meh": 4} == expected
    assert {"foo": "nope", "meh": 4} != expected
    assert {"meh": 4} != expected
    assert "foo" != expected
    assert 3 != expected
Пример #4
0
def test_oidc_provider_down(app, requests_mock, oidc_provider, fail_kwargs,
                            fail_url):
    # Setup connection failure in OIDC provider
    requests_mock.get(oidc_provider.get_issuer() + fail_url, **fail_kwargs)

    with app.test_client() as client:
        headers = {"Authorization": f"Bearer oidc/{oidc_provider.id}/f00b6r"}
        resp = client.get("/personal/hello", headers=headers)
        assert (resp.status_code,
                resp.json) == (503,
                               DictSubSet(
                                   code="OidcProviderUnavailable",
                                   message="OIDC Provider is unavailable"))
Пример #5
0
def test_list_and_dict_subset_nesting():
    assert [1, {2: 3}, {5: 6, 7: [8, 9]}] == ListSubSet([])
    assert [1, {2: 3}, {5: 6, 7: [8, 9]}] == ListSubSet([1])
    assert [1, {2: 3}, {5: 6, 7: [8, 9]}] == ListSubSet([{2: 3}])
    assert [1, {2: 3}, {5: 6, 7: [8, 9]}] == ListSubSet([1, {2: 3}])
    assert [1, {2: 3}, {5: 6, 7: [8, 9]}] == ListSubSet([1, DictSubSet({})])
    assert [1, {
        2: 3
    }, {
        5: 6,
        7: [8, 9]
    }] == ListSubSet([1, DictSubSet({2: 3})])
    assert [1, {
        2: 3
    }, {
        5: 6,
        7: [8, 9]
    }] == ListSubSet([1, DictSubSet({7: [8, 9]})])
    assert [1, {
        2: 3
    }, {
        5: 6,
        7: [8, 9]
    }] == ListSubSet([1, DictSubSet({7: ListSubSet([8])})])
Пример #6
0
 def test_with_cube_to_geojson(self, gdf):
     vc1 = DriverVectorCube(gdf)
     dims, coords = vc1.get_xarray_cube_basics()
     dims += ("bands", )
     coords["bands"] = ["red", "green"]
     cube = xarray.DataArray(data=[[1, 2], [3, 4]],
                             dims=dims,
                             coords=coords)
     vc2 = vc1.with_cube(cube, flatten_prefix="bandz")
     assert vc1.to_geojson() == DictSubSet({
         "type":
         "FeatureCollection",
         "features": [
             DictSubSet({
                 "type": "Feature",
                 "geometry": {
                     "type": "Polygon",
                     "coordinates": (((1, 1), (3, 1), (2, 3), (1, 1)), )
                 },
                 "properties": {
                     "id": "first",
                     "pop": 1234
                 },
             }),
             DictSubSet({
                 "type": "Feature",
                 "geometry": {
                     "type": "Polygon",
                     "coordinates": (((4, 2), (5, 4), (3, 4), (4, 2)), )
                 },
                 "properties": {
                     "id": "second",
                     "pop": 5678
                 },
             }),
         ]
     })
     assert vc2.to_geojson() == DictSubSet({
         "type":
         "FeatureCollection",
         "features": [
             DictSubSet({
                 "type": "Feature",
                 "geometry": {
                     "type": "Polygon",
                     "coordinates": (((1, 1), (3, 1), (2, 3), (1, 1)), )
                 },
                 "properties": {
                     "id": "first",
                     "pop": 1234,
                     "bandz~red": 1,
                     "bandz~green": 2
                 },
             }),
             DictSubSet({
                 "type": "Feature",
                 "geometry": {
                     "type": "Polygon",
                     "coordinates": (((4, 2), (5, 4), (3, 4), (4, 2)), )
                 },
                 "properties": {
                     "id": "second",
                     "pop": 5678,
                     "bandz~red": 3,
                     "bandz~green": 4
                 },
             }),
         ]
     })
Пример #7
0
 def test_from_geojson(self, geojson, expected):
     vc = DriverVectorCube.from_geojson(geojson)
     assert vc.to_geojson() == DictSubSet({
         "type": "FeatureCollection",
         "features": expected,
     })
Пример #8
0
class TestDriverVectorCube:
    @pytest.fixture
    def gdf(self) -> gpd.GeoDataFrame:
        """Fixture for a simple GeoPandas DataFrame from file"""
        path = str(get_path("geojson/FeatureCollection02.json"))
        df = gpd.read_file(path)
        return df

    def test_basic(self, gdf):
        vc = DriverVectorCube(gdf)
        assert vc.get_bounding_box() == (1, 1, 5, 4)

    def test_to_multipolygon(self, gdf):
        vc = DriverVectorCube(gdf)
        mp = vc.to_multipolygon()
        assert isinstance(mp, MultiPolygon)
        assert len(mp) == 2
        assert mp.equals(
            MultiPolygon([
                Polygon([(1, 1), (2, 3), (3, 1), (1, 1)]),
                Polygon([(4, 2), (5, 4), (3, 4), (4, 2)]),
            ]))

    def test_get_geometries(self, gdf):
        vc = DriverVectorCube(gdf)
        geometries = vc.get_geometries()
        assert len(geometries) == 2
        expected_geometries = [
            Polygon([(1, 1), (2, 3), (3, 1), (1, 1)]),
            Polygon([(4, 2), (5, 4), (3, 4), (4, 2)]),
        ]
        for geometry, expected in zip(geometries, expected_geometries):
            assert geometry.equals(expected)

    def test_to_geojson(self, gdf):
        vc = DriverVectorCube(gdf)
        assert vc.to_geojson() == DictSubSet({
            "type":
            "FeatureCollection",
            "features": [
                DictSubSet({
                    "type": "Feature",
                    "geometry": {
                        "type": "Polygon",
                        "coordinates": (((1, 1), (3, 1), (2, 3), (1, 1)), )
                    },
                    "properties": {
                        "id": "first",
                        "pop": 1234
                    },
                }),
                DictSubSet({
                    "type": "Feature",
                    "geometry": {
                        "type": "Polygon",
                        "coordinates": (((4, 2), (5, 4), (3, 4), (4, 2)), )
                    },
                    "properties": {
                        "id": "second",
                        "pop": 5678
                    },
                }),
            ]
        })

    def test_to_wkt(self, gdf):
        vc = DriverVectorCube(gdf)
        assert vc.to_wkt() == (
            [
                'POLYGON ((1 1, 3 1, 2 3, 1 1))',
                'POLYGON ((4 2, 5 4, 3 4, 4 2))'
            ],
            'EPSG:4326',
        )

    def test_with_cube_to_geojson(self, gdf):
        vc1 = DriverVectorCube(gdf)
        dims, coords = vc1.get_xarray_cube_basics()
        dims += ("bands", )
        coords["bands"] = ["red", "green"]
        cube = xarray.DataArray(data=[[1, 2], [3, 4]],
                                dims=dims,
                                coords=coords)
        vc2 = vc1.with_cube(cube, flatten_prefix="bandz")
        assert vc1.to_geojson() == DictSubSet({
            "type":
            "FeatureCollection",
            "features": [
                DictSubSet({
                    "type": "Feature",
                    "geometry": {
                        "type": "Polygon",
                        "coordinates": (((1, 1), (3, 1), (2, 3), (1, 1)), )
                    },
                    "properties": {
                        "id": "first",
                        "pop": 1234
                    },
                }),
                DictSubSet({
                    "type": "Feature",
                    "geometry": {
                        "type": "Polygon",
                        "coordinates": (((4, 2), (5, 4), (3, 4), (4, 2)), )
                    },
                    "properties": {
                        "id": "second",
                        "pop": 5678
                    },
                }),
            ]
        })
        assert vc2.to_geojson() == DictSubSet({
            "type":
            "FeatureCollection",
            "features": [
                DictSubSet({
                    "type": "Feature",
                    "geometry": {
                        "type": "Polygon",
                        "coordinates": (((1, 1), (3, 1), (2, 3), (1, 1)), )
                    },
                    "properties": {
                        "id": "first",
                        "pop": 1234,
                        "bandz~red": 1,
                        "bandz~green": 2
                    },
                }),
                DictSubSet({
                    "type": "Feature",
                    "geometry": {
                        "type": "Polygon",
                        "coordinates": (((4, 2), (5, 4), (3, 4), (4, 2)), )
                    },
                    "properties": {
                        "id": "second",
                        "pop": 5678,
                        "bandz~red": 3,
                        "bandz~green": 4
                    },
                }),
            ]
        })

    @pytest.mark.parametrize(["geojson", "expected"], [
        (
            {
                "type": "Polygon",
                "coordinates": [[(1, 1), (3, 1), (2, 3), (1, 1)]]
            },
            [
                DictSubSet({
                    "type": "Feature",
                    "geometry": {
                        "type": "Polygon",
                        "coordinates": ((
                            (1, 1),
                            (3, 1),
                            (2, 3),
                            (1, 1),
                        ), )
                    },
                    "properties": {},
                }),
            ],
        ),
        (
            {
                "type": "MultiPolygon",
                "coordinates": [[[(1, 1), (3, 1), (2, 3), (1, 1)]]]
            },
            [
                DictSubSet({
                    "type": "Feature",
                    "geometry": {
                        "type": "MultiPolygon",
                        "coordinates": [((
                            (1, 1),
                            (3, 1),
                            (2, 3),
                            (1, 1),
                        ), )]
                    },
                    "properties": {},
                }),
            ],
        ),
        (
            {
                "type": "Feature",
                "geometry": {
                    "type": "MultiPolygon",
                    "coordinates": [[[(1, 1), (3, 1), (2, 3), (1, 1)]]]
                },
                "properties": {
                    "id": "12_3"
                },
            },
            [
                DictSubSet({
                    "type": "Feature",
                    "geometry": {
                        "type": "MultiPolygon",
                        "coordinates": [((
                            (1, 1),
                            (3, 1),
                            (2, 3),
                            (1, 1),
                        ), )]
                    },
                    "properties": {
                        "id": "12_3"
                    },
                }),
            ],
        ),
        (
            {
                "type":
                "FeatureCollection",
                "features": [
                    {
                        "type": "Feature",
                        "geometry": {
                            "type": "Polygon",
                            "coordinates": [[(1, 1), (3, 1), (2, 3), (1, 1)]]
                        },
                        "properties": {
                            "id": 1
                        },
                    },
                    {
                        "type": "Feature",
                        "geometry": {
                            "type": "MultiPolygon",
                            "coordinates": [[[(1, 1), (3, 1), (2, 3), (1, 1)]]]
                        },
                        "properties": {
                            "id": 2
                        },
                    },
                ],
            },
            [
                DictSubSet({
                    "type": "Feature",
                    "geometry": {
                        "type": "Polygon",
                        "coordinates": ((
                            (1, 1),
                            (3, 1),
                            (2, 3),
                            (1, 1),
                        ), )
                    },
                    "properties": {
                        "id": 1
                    },
                }),
                DictSubSet({
                    "type": "Feature",
                    "geometry": {
                        "type": "MultiPolygon",
                        "coordinates": [((
                            (1, 1),
                            (3, 1),
                            (2, 3),
                            (1, 1),
                        ), )]
                    },
                    "properties": {
                        "id": 2
                    },
                }),
            ],
        ),
    ])
    def test_from_geojson(self, geojson, expected):
        vc = DriverVectorCube.from_geojson(geojson)
        assert vc.to_geojson() == DictSubSet({
            "type": "FeatureCollection",
            "features": expected,
        })
Пример #9
0
def test_dict_subset_init():
    assert {"foo": "bar"} == DictSubSet({"foo": "bar"})
    assert {"foo": "bar"} == DictSubSet(foo="bar")
    assert {"foo": "bar"} == DictSubSet({"foo": "meh"}, foo="bar")