Skip to content

Commit d78f1b3

Browse files
Merge pull request #883 from aanastasiou/rc/5.5.1_fix#882
Fixes #882
2 parents cf61fc7 + 58bb276 commit d78f1b3

File tree

2 files changed

+129
-7
lines changed

2 files changed

+129
-7
lines changed

neomodel/contrib/spatial_properties.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,47 +220,47 @@ def crs(self):
220220
@property
221221
def x(self):
222222
if not self._crs.startswith("cartesian"):
223-
raise AttributeError(
223+
raise TypeError(
224224
f'Invalid coordinate ("x") for points defined over {self.crs}'
225225
)
226226
return super().x
227227

228228
@property
229229
def y(self):
230230
if not self._crs.startswith("cartesian"):
231-
raise AttributeError(
231+
raise TypeError(
232232
f'Invalid coordinate ("y") for points defined over {self.crs}'
233233
)
234234
return super().y
235235

236236
@property
237237
def z(self):
238238
if self._crs != "cartesian-3d":
239-
raise AttributeError(
239+
raise TypeError(
240240
f'Invalid coordinate ("z") for points defined over {self.crs}'
241241
)
242242
return super().z
243243

244244
@property
245245
def latitude(self):
246246
if not self._crs.startswith("wgs-84"):
247-
raise AttributeError(
247+
raise TypeError(
248248
f'Invalid coordinate ("latitude") for points defined over {self.crs}'
249249
)
250250
return super().y
251251

252252
@property
253253
def longitude(self):
254254
if not self._crs.startswith("wgs-84"):
255-
raise AttributeError(
255+
raise TypeError(
256256
f'Invalid coordinate ("longitude") for points defined over {self.crs}'
257257
)
258258
return super().x
259259

260260
@property
261261
def height(self):
262262
if self._crs != "wgs-84-3d":
263-
raise AttributeError(
263+
raise TypeError(
264264
f'Invalid coordinate ("height") for points defined over {self.crs}'
265265
)
266266
return super().z
@@ -511,7 +511,7 @@ def __eq__(self, other):
511511
Compare objects by value
512512
"""
513513
if not isinstance(other, (ShapelyPoint, NeomodelPoint)):
514-
raise ValueException(
514+
raise ValueError(
515515
f"NeomodelPoint equality comparison expected NeomodelPoint or Shapely Point, received {type(other)}"
516516
)
517517
else:
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
from neomodel.contrib.spatial_properties import NeomodelPoint
2+
import pytest
3+
4+
def test_failed_initialisation():
5+
"""
6+
Tests that a NeomodelPoint cannot be instantiated in an erroneous state
7+
"""
8+
9+
# Must be instantiated with a meaningful CRS
10+
with pytest.raises(ValueError):
11+
p1 = NeomodelPoint(crs="blah")
12+
13+
# Must be instantiated with the right dimensionality of coordinates (either 2 or 3)
14+
with pytest.raises(ValueError):
15+
p1 = NeomodelPoint([0,0,0,0])
16+
17+
with pytest.raises(ValueError):
18+
p1 = NeomodelPoint([0,])
19+
20+
# Copy constructor only on the right type of object
21+
with pytest.raises(TypeError):
22+
p1 = NeomodelPoint("Definitely not a Neomodel point")
23+
24+
# Cannot be instantiated in a cartesian or geographical CRS at the same time
25+
with pytest.raises(ValueError):
26+
p1 = NeomodelPoint(x=0, y=0, z=0, longitude=0, latitude=0, height=0)
27+
28+
# Cannot be instantiated without actually pointing somehwere in space
29+
with pytest.raises(ValueError):
30+
p1 = NeomodelPoint()
31+
32+
# CRS and supplied arguments should match (3 for a 3d crs, 2 for a 2d crs)
33+
with pytest.raises(ValueError):
34+
p1 = NeomodelPoint(x=0, y=0, z=0, crs="cartesian")
35+
36+
with pytest.raises(ValueError):
37+
p1 = NeomodelPoint(x=0, y=0, crs="cartesian-3d")
38+
39+
def test_succesful_initialisation():
40+
"""
41+
Expected initialisation and copy constructor
42+
"""
43+
44+
p1 = NeomodelPoint(x=0, y=0)
45+
p2 = NeomodelPoint(x=0, y=0, z=0)
46+
p3 = NeomodelPoint(longitude=0, latitude=0)
47+
p4 = NeomodelPoint(longitude=0, latitude=0, height=0)
48+
p5 = NeomodelPoint([0,0])
49+
p6 = NeomodelPoint([0,0,0])
50+
p7 = NeomodelPoint(p6)
51+
52+
assert p1.crs=="cartesian" and p1.x==0 and p1.y==0
53+
assert p2.crs=="cartesian-3d" and p2.x==0 and p2.y==0 and p2.z==0
54+
55+
assert p5.crs=="cartesian" and p5.x==0 and p5.y==0
56+
assert p6.crs=="cartesian-3d" and p6.x==0 and p6.y==0 and p6.z==0
57+
58+
assert p3.crs=="wgs-84" and p3.longitude==0 and p3.latitude==0
59+
assert p4.crs=="wgs-84-3d" and p4.longitude==0 and p4.latitude==0 and p4.height==0
60+
61+
assert p7.crs=="cartesian-3d" and p7.x==0 and p7.y==0 and p7.z==0
62+
63+
def test_property_access():
64+
"""
65+
Points initialised as 2d cannot offer access to 3d coordinates
66+
"""
67+
68+
p1 = NeomodelPoint(x=0,y=0)
69+
p2 = NeomodelPoint(longitude=0, latitude=0, height=0)
70+
71+
with pytest.raises(TypeError):
72+
assert p1.longitude == 0
73+
74+
with pytest.raises(TypeError):
75+
assert p1.latitude == 0
76+
77+
with pytest.raises(TypeError):
78+
assert p1.height == 0
79+
80+
with pytest.raises(TypeError):
81+
assert p2.x == 0
82+
83+
with pytest.raises(TypeError):
84+
assert p2.y == 0
85+
86+
with pytest.raises(TypeError):
87+
assert p2.z == 0
88+
89+
def test_equality_success():
90+
"""
91+
Points with identical coordinates and CRS are equal in value
92+
"""
93+
p1 = NeomodelPoint(x=0, y=0, z=0)
94+
p2 = NeomodelPoint(x=0, y=0, z=0)
95+
96+
assert p1 == p2
97+
98+
def test_equality_fails():
99+
"""
100+
Points are comparable only with points
101+
"""
102+
p1 = NeomodelPoint(x=0, y=0, z=0)
103+
104+
with pytest.raises(ValueError):
105+
assert p1 == 4
106+
107+
108+
def test_equality_successful():
109+
"""
110+
Points with identical coordinates and CRS are equal in value
111+
"""
112+
p1 = NeomodelPoint(x=0, y=0, z=0)
113+
p2 = NeomodelPoint(x=0, y=0, z=0)
114+
p3 = NeomodelPoint(longitude=0, latitude=0, height=0)
115+
p4 = NeomodelPoint(longitude=0, latitude=0, height=0)
116+
117+
assert p1 == p2
118+
assert p3 == p4
119+
assert p1!=p4
120+
assert p3!=p2
121+
122+

0 commit comments

Comments
 (0)