| 1 |
from datetime import datetime |
| 2 |
|
| 3 |
import osmapi |
| 4 |
import pytest |
| 5 |
from responses import GET, POST |
| 6 |
|
| 7 |
from .conftest import API_BASE |
| 8 |
|
| 9 |
|
| 10 |
def test_notes_get(api, add_response): |
| 11 |
resp = add_response(GET, "/notes") |
| 12 |
|
| 13 |
result = api.notes_get(-1.4998534, 45.9667901, -1.4831815, 52.4710193) |
| 14 |
|
| 15 |
assert resp.calls[0].request.method == "GET" |
| 16 |
assert resp.calls[0].request.params == { |
| 17 |
"bbox": "-1.499853,45.966790,-1.483181,52.471019", |
| 18 |
"limit": "100", |
| 19 |
"closed": "7", |
| 20 |
} |
| 21 |
assert len(result) == 14 |
| 22 |
assert result[2] == { |
| 23 |
"id": "231775", |
| 24 |
"lon": -1.4929605, |
| 25 |
"lat": 52.4107312, |
| 26 |
"date_created": datetime(2014, 8, 28, 19, 25, 37), |
| 27 |
"date_closed": datetime(2014, 9, 27, 9, 21, 41), |
| 28 |
"status": "closed", |
| 29 |
"comments": [ |
| 30 |
{ |
| 31 |
"date": datetime(2014, 8, 28, 19, 25, 37), |
| 32 |
"action": "opened", |
| 33 |
"text": "Is it Paynes or Payne's", |
| 34 |
"html": "<p>Is it Paynes or Payne's</p>", |
| 35 |
"uid": "1486336", |
| 36 |
"user": "Wyken Seagrave", |
| 37 |
}, |
| 38 |
{ |
| 39 |
"date": datetime(2014, 9, 26, 13, 5, 33), |
| 40 |
"action": "commented", |
| 41 |
"text": "Royal Mail's postcode finder has PAYNES LANE", |
| 42 |
"html": "<p>Royal Mail's postcode finder has PAYNES LANE</p>", |
| 43 |
"uid": None, |
| 44 |
"user": None, |
| 45 |
}, |
| 46 |
], |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
def test_notes_get_empty(api, add_response): |
| 51 |
resp = add_response(GET, "/notes") |
| 52 |
|
| 53 |
result = api.notes_get( |
| 54 |
-93.8472901, 35.9763601, -80, 36.176360100000004, limit=1, closed=0 |
| 55 |
) |
| 56 |
|
| 57 |
assert resp.calls[0].request.params == { |
| 58 |
"bbox": "-93.847290,35.976360,-80.000000,36.176360", |
| 59 |
"limit": "1", |
| 60 |
"closed": "0", |
| 61 |
} |
| 62 |
assert result == [] |
| 63 |
|
| 64 |
|
| 65 |
def test_note_get(api, add_response): |
| 66 |
resp = add_response(GET, "/notes/1111") |
| 67 |
|
| 68 |
result = api.note_get(1111) |
| 69 |
|
| 70 |
assert resp.calls[0].request.url == f"{API_BASE}/api/0.6/notes/1111" |
| 71 |
assert result == { |
| 72 |
"id": "1111", |
| 73 |
"lon": 12.3133135, |
| 74 |
"lat": 37.9305489, |
| 75 |
"date_created": datetime(2013, 5, 1, 20, 58, 21), |
| 76 |
"date_closed": datetime(2013, 8, 21, 16, 43, 26), |
| 77 |
"status": "closed", |
| 78 |
"comments": [ |
| 79 |
{ |
| 80 |
"date": datetime(2013, 5, 1, 20, 58, 21), |
| 81 |
"action": "opened", |
| 82 |
"text": "It does not exist this path", |
| 83 |
"html": "<p>It does not exist this path</p>", |
| 84 |
"uid": "1363438", |
| 85 |
"user": "giuseppemari", |
| 86 |
}, |
| 87 |
{ |
| 88 |
"date": datetime(2013, 8, 21, 16, 43, 26), |
| 89 |
"action": "closed", |
| 90 |
"text": "there is no path signed", |
| 91 |
"html": "<p>there is no path signed</p>", |
| 92 |
"uid": "1714220", |
| 93 |
"user": "luschi", |
| 94 |
}, |
| 95 |
], |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
def test_note_get_invalid_xml(api, add_response): |
| 100 |
add_response(GET, "/notes/1111") |
| 101 |
|
| 102 |
with pytest.raises(osmapi.XmlResponseInvalidError): |
| 103 |
api.note_get(1111) |
| 104 |
|
| 105 |
|
| 106 |
def test_note_create(auth_api, add_response): |
| 107 |
resp = add_response(POST, "/notes") |
| 108 |
note = {"lat": 47.123, "lon": 8.432, "text": "This is a test"} |
| 109 |
|
| 110 |
result = auth_api.note_create(note) |
| 111 |
|
| 112 |
assert resp.calls[0].request.method == "POST" |
| 113 |
assert resp.calls[0].request.params == { |
| 114 |
"lat": "47.123", |
| 115 |
"lon": "8.432", |
| 116 |
"text": "This is a test", |
| 117 |
} |
| 118 |
assert result == { |
| 119 |
"id": "816", |
| 120 |
"lat": 47.123, |
| 121 |
"lon": 8.432, |
| 122 |
"date_created": datetime(2014, 10, 3, 15, 21, 21), |
| 123 |
"date_closed": None, |
| 124 |
"status": "open", |
| 125 |
"comments": [ |
| 126 |
{ |
| 127 |
"date": datetime(2014, 10, 3, 15, 21, 22), |
| 128 |
"action": "opened", |
| 129 |
"text": "This is a test", |
| 130 |
"html": "<p>This is a test</p>", |
| 131 |
"uid": "1841", |
| 132 |
"user": "metaodi", |
| 133 |
} |
| 134 |
], |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
def test_note_create_anonymous(api, add_response): |
| 139 |
resp = add_response(POST, "/notes") |
| 140 |
note = {"lat": 47.123, "lon": 8.432, "text": "test 123"} |
| 141 |
|
| 142 |
result = api.note_create(note) |
| 143 |
|
| 144 |
assert resp.calls[0].request.params == { |
| 145 |
"lat": "47.123", |
| 146 |
"lon": "8.432", |
| 147 |
"text": "test 123", |
| 148 |
} |
| 149 |
assert result == { |
| 150 |
"id": "842", |
| 151 |
"lat": 58.3368222, |
| 152 |
"lon": 25.8826183, |
| 153 |
"date_created": datetime(2015, 1, 3, 10, 49, 39), |
| 154 |
"date_closed": None, |
| 155 |
"status": "open", |
| 156 |
"comments": [ |
| 157 |
{ |
| 158 |
"date": datetime(2015, 1, 3, 10, 49, 39), |
| 159 |
"action": "opened", |
| 160 |
"text": "test 123", |
| 161 |
"html": "<p>test 123</p>", |
| 162 |
"uid": None, |
| 163 |
"user": None, |
| 164 |
} |
| 165 |
], |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
def test_note_comment(auth_api, add_response): |
| 170 |
resp = add_response(POST, "/notes/812/comment") |
| 171 |
|
| 172 |
result = auth_api.note_comment(812, "This is a comment") |
| 173 |
|
| 174 |
assert resp.calls[0].request.method == "POST" |
| 175 |
assert resp.calls[0].request.params == {"text": "This is a comment"} |
| 176 |
assert result == { |
| 177 |
"id": "812", |
| 178 |
"lat": 47.123, |
| 179 |
"lon": 8.432, |
| 180 |
"date_created": datetime(2014, 10, 3, 15, 11, 5), |
| 181 |
"date_closed": None, |
| 182 |
"status": "open", |
| 183 |
"comments": [ |
| 184 |
{ |
| 185 |
"date": datetime(2014, 10, 3, 15, 11, 5), |
| 186 |
"action": "opened", |
| 187 |
"text": "This is a test", |
| 188 |
"html": "<p>This is a test</p>", |
| 189 |
"uid": "1841", |
| 190 |
"user": "metaodi", |
| 191 |
}, |
| 192 |
{ |
| 193 |
"date": datetime(2014, 10, 4, 22, 36, 35), |
| 194 |
"action": "commented", |
| 195 |
"text": "This is a comment", |
| 196 |
"html": "<p>This is a comment</p>", |
| 197 |
"uid": "1841", |
| 198 |
"user": "metaodi", |
| 199 |
}, |
| 200 |
], |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
def test_note_comment_anonymous(api, add_response): |
| 205 |
resp = add_response(POST, "/notes/842/comment") |
| 206 |
|
| 207 |
result = api.note_comment(842, "blubb") |
| 208 |
|
| 209 |
assert resp.calls[0].request.params == {"text": "blubb"} |
| 210 |
assert result["id"] == "842" |
| 211 |
assert result["comments"][1] == { |
| 212 |
"date": datetime(2015, 1, 3, 11, 6, 0), |
| 213 |
"action": "commented", |
| 214 |
"text": "blubb", |
| 215 |
"html": "<p>blubb</p>", |
| 216 |
"uid": None, |
| 217 |
"user": None, |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
def test_note_comment_on_closed_note(api, add_response): |
| 222 |
add_response(POST, "/notes/817/comment", status=409) |
| 223 |
|
| 224 |
with pytest.raises(osmapi.NoteAlreadyClosedApiError) as execinfo: |
| 225 |
api.note_comment(817, "Comment on closed note") |
| 226 |
|
| 227 |
assert execinfo.value.status == 409 |
| 228 |
assert ( |
| 229 |
execinfo.value.payload_str |
| 230 |
== "The note 817 was closed at 2022-04-29 20:57:20 UTC" |
| 231 |
) |
| 232 |
|
| 233 |
|
| 234 |
def test_note_comment_non_existing_note(api, add_response): |
| 235 |
add_response(POST, "/notes/817/comment", body="", status=404) |
| 236 |
|
| 237 |
with pytest.raises(osmapi.ElementNotFoundApiError) as execinfo: |
| 238 |
api.note_comment(817, "Comment on closed note") |
| 239 |
|
| 240 |
assert execinfo.value.status == 404 |
| 241 |
|
| 242 |
|
| 243 |
def test_note_close(auth_api, add_response): |
| 244 |
resp = add_response(POST, "/notes/819/close") |
| 245 |
|
| 246 |
result = auth_api.note_close(819, "Close this note!") |
| 247 |
|
| 248 |
assert resp.calls[0].request.method == "POST" |
| 249 |
assert resp.calls[0].request.params == {"text": "Close this note!"} |
| 250 |
assert result == { |
| 251 |
"id": "815", |
| 252 |
"lat": 47.123, |
| 253 |
"lon": 8.432, |
| 254 |
"date_created": datetime(2014, 10, 3, 15, 20, 57), |
| 255 |
"date_closed": datetime(2014, 10, 5, 16, 35, 13), |
| 256 |
"status": "closed", |
| 257 |
"comments": [ |
| 258 |
{ |
| 259 |
"date": datetime(2014, 10, 3, 15, 20, 57), |
| 260 |
"action": "opened", |
| 261 |
"text": "This is a test", |
| 262 |
"html": "<p>This is a test</p>", |
| 263 |
"uid": "1841", |
| 264 |
"user": "metaodi", |
| 265 |
}, |
| 266 |
{ |
| 267 |
"date": datetime(2014, 10, 5, 16, 35, 13), |
| 268 |
"action": "closed", |
| 269 |
"text": "Close this note!", |
| 270 |
"html": "<p>Close this note!</p>", |
| 271 |
"uid": "1841", |
| 272 |
"user": "metaodi", |
| 273 |
}, |
| 274 |
], |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
def test_note_already_closed(auth_api, add_response): |
| 279 |
add_response(POST, "/notes/819/close", status=409) |
| 280 |
|
| 281 |
with pytest.raises(osmapi.NoteAlreadyClosedApiError) as execinfo: |
| 282 |
auth_api.note_close(819, "Close this note!") |
| 283 |
|
| 284 |
assert execinfo.value.status == 409 |
| 285 |
assert ( |
| 286 |
execinfo.value.payload_str |
| 287 |
== "The note 819 was closed at 2022-04-29 20:57:20 UTC" |
| 288 |
) |
| 289 |
|
| 290 |
|
| 291 |
def test_note_reopen(auth_api, add_response): |
| 292 |
resp = add_response(POST, "/notes/815/reopen") |
| 293 |
|
| 294 |
result = auth_api.note_reopen(815, "Reopen this note!") |
| 295 |
|
| 296 |
assert resp.calls[0].request.method == "POST" |
| 297 |
assert resp.calls[0].request.params == {"text": "Reopen this note!"} |
| 298 |
assert result["id"] == "815" |
| 299 |
assert result["status"] == "open" |
| 300 |
assert result["date_closed"] is None |
| 301 |
assert result["comments"][2] == { |
| 302 |
"date": datetime(2014, 10, 5, 16, 44, 56), |
| 303 |
"action": "reopened", |
| 304 |
"text": "Reopen this note!", |
| 305 |
"html": "<p>Reopen this note!</p>", |
| 306 |
"uid": "1841", |
| 307 |
"user": "metaodi", |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
def test_notes_search(api, add_response): |
| 312 |
resp = add_response(GET, "/notes/search") |
| 313 |
|
| 314 |
result = api.notes_search("street") |
| 315 |
|
| 316 |
assert resp.calls[0].request.params == { |
| 317 |
"q": "street", |
| 318 |
"limit": "100", |
| 319 |
"closed": "7", |
| 320 |
} |
| 321 |
assert len(result) == 3 |
| 322 |
assert result[1] == { |
| 323 |
"id": "788", |
| 324 |
"lon": 11.96395, |
| 325 |
"lat": 57.70301, |
| 326 |
"date_created": datetime(2014, 7, 16, 16, 12, 41), |
| 327 |
"date_closed": None, |
| 328 |
"status": "open", |
| 329 |
"comments": [ |
| 330 |
{ |
| 331 |
"date": datetime(2014, 7, 16, 16, 12, 41), |
| 332 |
"action": "opened", |
| 333 |
"text": "One way street:\ncomment", |
| 334 |
"html": "<p>One way street:\n<br />comment</p>", |
| 335 |
"uid": None, |
| 336 |
"user": None, |
| 337 |
} |
| 338 |
], |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
def test_notes_search_with_limit_and_closed(api, add_response): |
| 343 |
resp = add_response(GET, "/notes/search", filename="test_notes_search.xml") |
| 344 |
|
| 345 |
api.notes_search("street", limit=10, closed=-1) |
| 346 |
|
| 347 |
assert resp.calls[0].request.params == { |
| 348 |
"q": "street", |
| 349 |
"limit": "10", |
| 350 |
"closed": "-1", |
| 351 |
} |