|
| 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