1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
"""Test of deprecations following RFC 1"""
import pytest
from fiona.errors import FionaDeprecationWarning
from fiona.model import (
_Geometry,
Feature,
Geometry,
Object,
ObjectEncoder,
Properties,
decode_object,
)
def test_object_len():
"""object len is correct"""
obj = Object(g=1)
assert len(obj) == 1
def test_object_iter():
"""object iter is correct"""
obj = Object(g=1)
assert [obj[k] for k in obj] == [1]
def test_object_setitem_warning():
"""Warn about __setitem__"""
obj = Object()
with pytest.warns(FionaDeprecationWarning, match="immutable"):
obj["g"] = 1
assert "g" in obj
assert obj["g"] == 1
def test_object_update_warning():
"""Warn about update"""
obj = Object()
with pytest.warns(FionaDeprecationWarning, match="immutable"):
obj.update(g=1)
assert "g" in obj
assert obj["g"] == 1
def test_object_popitem_warning():
"""Warn about pop"""
obj = Object(g=1)
with pytest.warns(FionaDeprecationWarning, match="immutable"):
assert obj.pop("g") == 1
assert "g" not in obj
def test_object_delitem_warning():
"""Warn about __delitem__"""
obj = Object(g=1)
with pytest.warns(FionaDeprecationWarning, match="immutable"):
del obj["g"]
assert "g" not in obj
def test_object_setitem_delegated():
"""Delegation in __setitem__ works"""
class ThingDelegate:
def __init__(self, value):
self.value = value
class Thing(Object):
_delegated_properties = ["value"]
def __init__(self, value=None, **data):
self._delegate = ThingDelegate(value)
super().__init__(**data)
thing = Thing()
assert thing["value"] is None
with pytest.warns(FionaDeprecationWarning, match="immutable"):
thing["value"] = 1
assert thing["value"] == 1
def test_object_delitem_delegated():
"""Delegation in __delitem__ works"""
class ThingDelegate:
def __init__(self, value):
self.value = value
class Thing(Object):
_delegated_properties = ["value"]
def __init__(self, value=None, **data):
self._delegate = ThingDelegate(value)
super().__init__(**data)
thing = Thing(1)
assert thing["value"] == 1
with pytest.warns(FionaDeprecationWarning, match="immutable"):
del thing["value"]
assert thing["value"] is None
def test__geometry_ctor():
"""Construction of a _Geometry works"""
geom = _Geometry(type="Point", coordinates=(0, 0))
assert geom.type == "Point"
assert geom.coordinates == (0, 0)
def test_geometry_type():
"""Geometry has a type"""
geom = Geometry(type="Point")
assert geom.type == "Point"
def test_geometry_coordinates():
"""Geometry has coordinates"""
geom = Geometry(coordinates=[(0, 0), (1, 1)])
assert geom.coordinates == [(0, 0), (1, 1)]
def test_geometry__props():
"""Geometry properties as a dict"""
assert Geometry(coordinates=(0, 0), type="Point")._props() == {
"coordinates": (0, 0),
"type": "Point",
"geometries": None,
}
def test_geometry_gi():
"""Geometry __geo_interface__"""
gi = Geometry(coordinates=(0, 0), type="Point", geometries=[]).__geo_interface__
assert gi["type"] == "Point"
assert gi["coordinates"] == (0, 0)
def test_feature_no_geometry():
"""Feature has no attribute"""
feat = Feature()
assert feat.geometry is None
def test_feature_geometry():
"""Feature has a geometry attribute"""
geom = Geometry(type="Point")
feat = Feature(geometry=geom)
assert feat.geometry is geom
def test_feature_no_id():
"""Feature has no id"""
feat = Feature()
assert feat.id is None
def test_feature_id():
"""Feature has an id"""
feat = Feature(id="123")
assert feat.id == "123"
def test_feature_no_properties():
"""Feature has no properties"""
feat = Feature()
assert len(feat.properties) == 0
def test_feature_properties():
"""Feature has properties"""
feat = Feature(properties=Properties(foo=1))
assert len(feat.properties) == 1
assert feat.properties["foo"] == 1
def test_feature_from_dict_kwargs():
"""Feature can be created from GeoJSON kwargs"""
data = {
"id": "foo",
"type": "Feature",
"geometry": {"type": "Point", "coordinates": (0, 0)},
"properties": {"a": 0, "b": "bar"},
"extras": {"this": 1},
}
feat = Feature.from_dict(**data)
assert feat.id == "foo"
assert feat.type == "Feature"
assert feat.geometry.type == "Point"
assert feat.geometry.coordinates == (0, 0)
assert len(feat.properties) == 2
assert feat.properties["a"] == 0
assert feat.properties["b"] == "bar"
assert feat["extras"]["this"] == 1
def test_feature_from_dict_obj():
"""Feature can be created from GeoJSON obj"""
data = {
"id": "foo",
"type": "Feature",
"geometry": {"type": "Point", "coordinates": (0, 0)},
"properties": {"a": 0, "b": "bar"},
"extras": {"this": 1},
}
feat = Feature.from_dict(data)
assert feat.id == "foo"
assert feat.type == "Feature"
assert feat.geometry.type == "Point"
assert feat.geometry.coordinates == (0, 0)
assert len(feat.properties) == 2
assert feat.properties["a"] == 0
assert feat.properties["b"] == "bar"
assert feat["extras"]["this"] == 1
def test_feature_from_dict_kwargs_2():
"""From GeoJSON kwargs using Geometry and Properties"""
data = {
"id": "foo",
"type": "Feature",
"geometry": Geometry(type="Point", coordinates=(0, 0)),
"properties": Properties(a=0, b="bar"),
"extras": {"this": 1},
}
feat = Feature.from_dict(**data)
assert feat.id == "foo"
assert feat.type == "Feature"
assert feat.geometry.type == "Point"
assert feat.geometry.coordinates == (0, 0)
assert len(feat.properties) == 2
assert feat.properties["a"] == 0
assert feat.properties["b"] == "bar"
assert feat["extras"]["this"] == 1
def test_geometry_encode():
"""Can encode a geometry"""
assert ObjectEncoder().default(Geometry(type="Point", coordinates=(0, 0))) == {
"type": "Point",
"coordinates": (0, 0),
}
def test_feature_encode():
"""Can encode a feature"""
o_dict = ObjectEncoder().default(
Feature(
id="foo",
geometry=Geometry(type="Point", coordinates=(0, 0)),
properties=Properties(a=1, foo="bar", bytes=b"01234"),
)
)
assert o_dict["id"] == "foo"
assert o_dict["geometry"]["type"] == "Point"
assert o_dict["geometry"]["coordinates"] == (0, 0)
assert o_dict["properties"]["bytes"] == b'3031323334'
def test_decode_object_hook():
"""Can decode a feature"""
data = {
"id": "foo",
"type": "Feature",
"geometry": {"type": "Point", "coordinates": (0, 0)},
"properties": {"a": 0, "b": "bar"},
"extras": {"this": 1},
}
feat = decode_object(data)
assert feat.id == "foo"
assert feat.type == "Feature"
assert feat.geometry.type == "Point"
assert feat.geometry.coordinates == (0, 0)
assert len(feat.properties) == 2
assert feat.properties["a"] == 0
assert feat.properties["b"] == "bar"
assert feat["extras"]["this"] == 1
def test_decode_object_hook_geometry():
"""Can decode a geometry"""
data = {"type": "Point", "coordinates": (0, 0)}
geometry = decode_object(data)
assert geometry.type == "Point"
assert geometry.coordinates == (0, 0)
@pytest.mark.parametrize("o", [{}, {"a": 1}, {"type": "FeatureCollection"}])
def test_decode_object_hook_fallback(o):
"""Pass through an ordinary dict"""
assert decode_object(o) == o
def test_properties():
"""Property factory works"""
assert Properties.from_dict(a=1, foo="bar")["a"] == 1
def test_feature_gi():
"""Feature __geo_interface__."""
gi = Feature(
id="foo",
geometry=Geometry(type="Point", coordinates=(0, 0)),
properties=Properties(a=1, foo="bar"),
)
assert gi["id"] == "foo"
assert gi["geometry"]["type"] == "Point"
assert gi["geometry"]["coordinates"] == (0, 0)
def test_encode_bytes():
"""Bytes are encoded using base64."""
assert ObjectEncoder().default(b"01234") == b'3031323334'
def test_null_property_encoding():
"""A null feature property is retained."""
# Verifies fix for gh-1270.
assert ObjectEncoder().default(Properties(a=1, b=None)) == {"a": 1, "b": None}
def test_null_geometry_encoding():
"""A null feature geometry is retained."""
# Verifies fix for gh-1270.
o_dict = ObjectEncoder().default(Feature())
assert o_dict["geometry"] is None
def test_geometry_collection_encoding():
"""No coordinates in a GeometryCollection."""
assert "coordinates" not in ObjectEncoder().default(
Geometry(type="GeometryCollection", geometries=[])
)
def test_feature_repr():
feat = Feature(
id="1",
geometry=Geometry(type="LineString", coordinates=[(0, 0)] * 100),
properties=Properties(a=1, foo="bar"),
)
assert repr(feat) == "fiona.Feature(geometry=fiona.Geometry(coordinates=[(0, 0), ...], type='LineString'), id='1', properties=fiona.Properties(a=1, foo='bar'))"
def test_issue1430():
"""__getitem__() returns property, not disconnected dict."""
feat = Feature(properties=Properties())
with pytest.warns(FionaDeprecationWarning, match="immutable"):
feat["properties"]["foo"] = "bar"
assert feat["properties"]["foo"] == "bar"
assert feat.properties["foo"] == "bar"
|