| 1 |
import datetime |
| 2 |
|
| 3 |
import osmapi |
| 4 |
import pytest |
| 5 |
from responses import DELETE, GET, PUT |
| 6 |
|
| 7 |
from .conftest import API_BASE, OPEN_CHANGESET_ID |
| 8 |
|
| 9 |
|
| 10 |
def test_way_get(api, add_response): |
| 11 |
resp = add_response(GET, "/way/321") |
| 12 |
|
| 13 |
result = api.way_get(321) |
| 14 |
|
| 15 |
assert resp.calls[0].request.method == "GET" |
| 16 |
assert resp.calls[0].request.url == f"{API_BASE}/api/0.6/way/321" |
| 17 |
assert result == { |
| 18 |
"id": 321, |
| 19 |
"changeset": 298, |
| 20 |
"uid": 12, |
| 21 |
"timestamp": datetime.datetime(2009, 9, 14, 23, 23, 18), |
| 22 |
"visible": True, |
| 23 |
"version": 1, |
| 24 |
"user": "green525", |
| 25 |
"tag": {"admin_level": "9", "boundary": "administrative"}, |
| 26 |
"nd": [ |
| 27 |
11949, |
| 28 |
11950, |
| 29 |
11951, |
| 30 |
11952, |
| 31 |
11953, |
| 32 |
11954, |
| 33 |
11955, |
| 34 |
11956, |
| 35 |
11957, |
| 36 |
11958, |
| 37 |
11959, |
| 38 |
11960, |
| 39 |
11961, |
| 40 |
11962, |
| 41 |
11963, |
| 42 |
11964, |
| 43 |
11949, |
| 44 |
], |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
def test_way_get_with_version(api, add_response): |
| 49 |
resp = add_response(GET, "/way/4294967296/2") |
| 50 |
|
| 51 |
result = api.way_get(4294967296, 2) |
| 52 |
|
| 53 |
assert resp.calls[0].request.url == f"{API_BASE}/api/0.6/way/4294967296/2" |
| 54 |
assert result["id"] == 4294967296 |
| 55 |
assert result["changeset"] == 41303 |
| 56 |
assert result["user"] == "metaodi" |
| 57 |
|
| 58 |
|
| 59 |
def test_way_get_nodata(api, add_response): |
| 60 |
add_response(GET, "/way/321") |
| 61 |
|
| 62 |
with pytest.raises(osmapi.ResponseEmptyApiError): |
| 63 |
api.way_get(321) |
| 64 |
|
| 65 |
|
| 66 |
def test_way_create(changeset_api, add_response, assert_request_xml): |
| 67 |
resp = add_response(PUT, "/way/create") |
| 68 |
test_way = { |
| 69 |
"nd": [11949, 11950], |
| 70 |
"tag": {"highway": "unclassified", "name": "Osmapi Street"}, |
| 71 |
} |
| 72 |
|
| 73 |
result = changeset_api.way_create(test_way) |
| 74 |
|
| 75 |
assert resp.calls[0].request.method == "PUT" |
| 76 |
assert resp.calls[0].request.url == f"{API_BASE}/api/0.6/way/create" |
| 77 |
assert_request_xml( |
| 78 |
resp.calls[0].request, |
| 79 |
f'<way visible="true" changeset="{OPEN_CHANGESET_ID}">' |
| 80 |
'<tag k="highway" v="unclassified"/>' |
| 81 |
'<tag k="name" v="Osmapi Street"/>' |
| 82 |
'<nd ref="11949"/>' |
| 83 |
'<nd ref="11950"/>' |
| 84 |
"</way>", |
| 85 |
) |
| 86 |
|
| 87 |
assert result["id"] == 5454 |
| 88 |
assert result["version"] == 1 |
| 89 |
assert result["nd"] == test_way["nd"] |
| 90 |
assert result["tag"] == test_way["tag"] |
| 91 |
|
| 92 |
|
| 93 |
def test_way_create_existing_node(changeset_api): |
| 94 |
test_way = { |
| 95 |
"id": 456, |
| 96 |
"nd": [11949, 11950], |
| 97 |
"tag": {"highway": "unclassified", "name": "Osmapi Street"}, |
| 98 |
} |
| 99 |
|
| 100 |
with pytest.raises( |
| 101 |
osmapi.OsmTypeAlreadyExistsError, match="This way already exists" |
| 102 |
): |
| 103 |
changeset_api.way_create(test_way) |
| 104 |
|
| 105 |
|
| 106 |
def test_way_update(changeset_api, add_response, assert_request_xml): |
| 107 |
resp = add_response(PUT, "/way/876") |
| 108 |
test_way = { |
| 109 |
"id": 876, |
| 110 |
"nd": [11949, 11950], |
| 111 |
"tag": {"highway": "unclassified", "name": "Osmapi Street Update"}, |
| 112 |
} |
| 113 |
|
| 114 |
result = changeset_api.way_update(test_way) |
| 115 |
|
| 116 |
assert resp.calls[0].request.url == f"{API_BASE}/api/0.6/way/876" |
| 117 |
assert_request_xml( |
| 118 |
resp.calls[0].request, |
| 119 |
f'<way id="876" visible="true" changeset="{OPEN_CHANGESET_ID}">' |
| 120 |
'<tag k="highway" v="unclassified"/>' |
| 121 |
'<tag k="name" v="Osmapi Street Update"/>' |
| 122 |
'<nd ref="11949"/>' |
| 123 |
'<nd ref="11950"/>' |
| 124 |
"</way>", |
| 125 |
) |
| 126 |
|
| 127 |
assert result["id"] == 876 |
| 128 |
assert result["version"] == 7 |
| 129 |
assert result["nd"] == test_way["nd"] |
| 130 |
assert result["tag"] == test_way["tag"] |
| 131 |
|
| 132 |
|
| 133 |
def test_way_update_precondition_failed(changeset_api, add_response): |
| 134 |
add_response(PUT, "/way/876", status=412) |
| 135 |
test_way = { |
| 136 |
"id": 876, |
| 137 |
"nd": [11949, 11950], |
| 138 |
"tag": {"highway": "unclassified", "name": "Osmapi Street Update"}, |
| 139 |
} |
| 140 |
|
| 141 |
with pytest.raises(osmapi.PreconditionFailedApiError) as execinfo: |
| 142 |
changeset_api.way_update(test_way) |
| 143 |
|
| 144 |
assert execinfo.value.status == 412 |
| 145 |
assert execinfo.value.payload_str == ( |
| 146 |
"Way 876 requires the nodes with id in (11950), " |
| 147 |
"which either do not exist, or are not visible." |
| 148 |
) |
| 149 |
|
| 150 |
|
| 151 |
def test_way_delete(changeset_api, add_response, assert_request_xml): |
| 152 |
resp = add_response(DELETE, "/way/876") |
| 153 |
|
| 154 |
result = changeset_api.way_delete({"id": 876}) |
| 155 |
|
| 156 |
assert resp.calls[0].request.method == "DELETE" |
| 157 |
assert resp.calls[0].request.url == f"{API_BASE}/api/0.6/way/876" |
| 158 |
assert_request_xml( |
| 159 |
resp.calls[0].request, |
| 160 |
f'<way id="876" visible="true" changeset="{OPEN_CHANGESET_ID}"/>', |
| 161 |
) |
| 162 |
|
| 163 |
assert result["id"] == 876 |
| 164 |
assert result["version"] == 8 |
| 165 |
assert result["visible"] is False |
| 166 |
|
| 167 |
|
| 168 |
def test_way_history(api, add_response): |
| 169 |
resp = add_response(GET, "/way/4294967296/history") |
| 170 |
|
| 171 |
result = api.way_history(4294967296) |
| 172 |
|
| 173 |
assert resp.calls[0].request.url == f"{API_BASE}/api/0.6/way/4294967296/history" |
| 174 |
assert len(result) == 2 |
| 175 |
assert result[1]["id"] == 4294967296 |
| 176 |
assert result[1]["version"] == 1 |
| 177 |
assert result[1]["tag"] == { |
| 178 |
"highway": "unclassified", |
| 179 |
"name": "Stansted Road", |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
def test_way_relations(api, add_response): |
| 184 |
resp = add_response(GET, "/way/4295032193/relations") |
| 185 |
|
| 186 |
result = api.way_relations(4295032193) |
| 187 |
|
| 188 |
assert resp.calls[0].request.url == f"{API_BASE}/api/0.6/way/4295032193/relations" |
| 189 |
assert len(result) == 1 |
| 190 |
assert result[0]["id"] == 4294968148 |
| 191 |
assert result[0]["changeset"] == 23123 |
| 192 |
assert result[0]["member"][4] == {"role": "", "ref": 4295032193, "type": "way"} |
| 193 |
assert result[0]["tag"] == {"type": "fancy"} |
| 194 |
|
| 195 |
|
| 196 |
def test_way_relations_unused_element(api, add_response): |
| 197 |
resp = add_response(GET, "/way/4295032193/relations") |
| 198 |
|
| 199 |
result = api.way_relations(4295032193) |
| 200 |
|
| 201 |
assert resp.calls[0].request.url == f"{API_BASE}/api/0.6/way/4295032193/relations" |
| 202 |
assert result == [] |
| 203 |
|
| 204 |
|
| 205 |
def test_way_full(api, add_response): |
| 206 |
resp = add_response(GET, "/way/321/full") |
| 207 |
|
| 208 |
result = api.way_full(321) |
| 209 |
|
| 210 |
assert resp.calls[0].request.url == f"{API_BASE}/api/0.6/way/321/full" |
| 211 |
assert len(result) == 17 |
| 212 |
assert result[0]["data"]["id"] == 11949 |
| 213 |
assert result[0]["data"]["changeset"] == 298 |
| 214 |
assert result[0]["type"] == "node" |
| 215 |
assert result[16]["data"]["id"] == 321 |
| 216 |
assert result[16]["data"]["changeset"] == 298 |
| 217 |
assert result[16]["type"] == "way" |
| 218 |
|
| 219 |
|
| 220 |
def test_way_full_invalid_response(api, add_response): |
| 221 |
add_response(GET, "/way/321/full") |
| 222 |
|
| 223 |
with pytest.raises(osmapi.XmlResponseInvalidError): |
| 224 |
api.way_full(321) |
| 225 |
|
| 226 |
|
| 227 |
def test_ways_get(api, add_response): |
| 228 |
resp = add_response(GET, "/ways") |
| 229 |
|
| 230 |
result = api.ways_get([456, 678]) |
| 231 |
|
| 232 |
assert resp.calls[0].request.url == f"{API_BASE}/api/0.6/ways?ways=456,678" |
| 233 |
assert len(result) == 2 |
| 234 |
assert isinstance(result[456], dict) |
| 235 |
assert isinstance(result[678], dict) |
| 236 |
with pytest.raises(KeyError): |
| 237 |
result[123] |