File: changeset_test.py

package info (click to toggle)
python-osmapi 3.1.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 760 kB
  • sloc: python: 3,340; xml: 1,591; makefile: 41; sh: 16
file content (731 lines) | stat: -rw-r--r-- 22,601 bytes parent folder | download
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
import osmapi
import xmltodict
import datetime
import pytest
from responses import GET, PUT, POST


def xmltosorteddict(xml):
    xml_dict = xmltodict.parse(xml, dict_constructor=dict)
    return xml_dict


def test_Changeset_contextmanager(auth_api, add_response):
    # Setup mock
    resp = add_response(PUT, '/changeset/create', filename='test_Changeset_create.xml')
    resp = add_response(PUT, '/node/create', filename='test_Changeset_create_node.xml')
    resp = add_response(PUT, '/changeset/1414/close', filename='test_Changeset_close.xml')

    test_node = {
        'lat': 47.123,
        'lon': 8.555,
        'tag': {
            'amenity': 'place_of_worship',
            'religion': 'pastafarian'
        }
    }

    # use context manager
    with auth_api.Changeset() as changeset_id:
        assert changeset_id == 1414

        # add test node
        node = auth_api.NodeCreate(test_node)
        assert node['id'] == 7272

    # check requests
    assert len(resp.calls) == 3


def test_ChangesetGet(api, add_response):
    # Setup mock
    add_response(GET, '/changeset/123')

    # Call
    result = api.ChangesetGet(123)

    test_changeset = {
        'id': 123,
        'closed_at': datetime.datetime(2009, 9, 7, 22, 57, 37),
        'created_at': datetime.datetime(2009, 9, 7, 21, 57, 36),
        'discussion': [],
        'max_lat': '52.4710193',
        'max_lon': '-1.4831815',
        'min_lat': '45.9667901',
        'min_lon': '-1.4998534',
        'open': False,
        'user': 'randomjunk',
        'uid': 3,
        'tag': {
            'comment': 'correct node bug',
            'created_by': 'Potlatch 1.2a',
        },
    }
    assert result == test_changeset


def test_ChangesetUpdate(auth_api, add_response):
    # Setup mock
    resp = add_response(PUT, '/changeset/create', filename='test_ChangesetCreate.xml')
    resp = add_response(PUT, '/changeset/4321', filename='test_ChangesetUpdate.xml')

    # Call
    result = auth_api.ChangesetCreate()
    assert result == 4321

    result = auth_api.ChangesetUpdate({'test': 'foobar'})
    changeset_xml = xmltosorteddict(
        b'<?xml version="1.0" encoding="UTF-8"?>\n'
        b'<osm version="0.6" generator="osmapi/3.1.0">\n'
        b'  <changeset visible="true">\n'
        b'    <tag k="test" v="foobar"/>\n'
        b'    <tag k="created_by" v="osmapi/3.1.0"/>\n'
        b'  </changeset>\n'
        b'</osm>\n'
    )
    assert xmltosorteddict(resp.calls[1].request.body) == changeset_xml
    assert result == 4321


def test_ChangesetUpdate_with_created_by(auth_api, add_response):
    # Setup mock
    resp = add_response(PUT, '/changeset/create', filename='test_ChangesetCreate.xml')
    resp = add_response(PUT, '/changeset/4321', filename='test_ChangesetUpdate.xml')

    # Call
    result = auth_api.ChangesetCreate()
    assert result == 4321

    result = auth_api.ChangesetUpdate(
        {
            'test': 'foobar',
            'created_by': 'MyTestOSMApp'
        }
    )
    changeset_xml = xmltosorteddict(
        b'<?xml version="1.0" encoding="UTF-8"?>\n'
        b'<osm version="0.6" generator="osmapi/3.1.0">\n'
        b'  <changeset visible="true">\n'
        b'    <tag k="test" v="foobar"/>\n'
        b'    <tag k="created_by" v="MyTestOSMApp"/>\n'
        b'  </changeset>\n'
        b'</osm>\n'
    )
    assert xmltosorteddict(resp.calls[1].request.body) == changeset_xml
    assert result == 4321


def test_ChangesetUpdate_wo_changeset(auth_api):
    with pytest.raises(osmapi.NoChangesetOpenError) as execinfo:
        auth_api.ChangesetUpdate({'test': 'foobar'})
    assert str(execinfo.value) == 'No changeset currently opened'


def test_ChangesetCreate(auth_api, add_response):
    resp = add_response(PUT, '/changeset/create')
    result = auth_api.ChangesetCreate(
        {
            'foobar': 'A new test changeset'
        }
    )
    assert result == 4321

    changeset_xml = xmltosorteddict(
        b'<?xml version="1.0" encoding="UTF-8"?>\n'
        b'<osm version="0.6" generator="osmapi/3.1.0">\n'
        b'  <changeset visible="true">\n'
        b'    <tag k="foobar" v="A new test changeset"/>\n'
        b'    <tag k="created_by" v="osmapi/3.1.0"/>\n'
        b'  </changeset>\n'
        b'</osm>\n'
    )
    assert xmltosorteddict(resp.calls[0].request.body) == changeset_xml


def test_ChangesetCreate_with_created_by(auth_api, add_response):
    resp = add_response(PUT, '/changeset/create')

    result = auth_api.ChangesetCreate(
        {
            'foobar': 'A new test changeset',
            'created_by': 'CoolTestApp',
        }
    )
    assert result == 1234

    changeset_xml = xmltosorteddict(
        b'<?xml version="1.0" encoding="UTF-8"?>\n'
        b'<osm version="0.6" generator="osmapi/3.1.0">\n'
        b'  <changeset visible="true">\n'
        b'    <tag k="foobar" v="A new test changeset"/>\n'
        b'    <tag k="created_by" v="CoolTestApp"/>\n'
        b'  </changeset>\n'
        b'</osm>\n'
    )
    assert xmltosorteddict(resp.calls[0].request.body) == changeset_xml


def test_ChangesetCreate_with_open_changeset(auth_api, add_response):
    add_response(PUT, '/changeset/create')

    auth_api.ChangesetCreate(
        {
            'test': 'an already open changeset',
        }
    )

    with pytest.raises(osmapi.ChangesetAlreadyOpenError) as execinfo:
        auth_api.ChangesetCreate({'test': 'foobar'})
    assert str(execinfo.value) == 'Changeset already opened'


def test_ChangesetCreate_with_prod_api_and_test_comment(prod_api):
    with pytest.raises(osmapi.OsmApiError) as execinfo:
        prod_api.ChangesetCreate(
            {
                'comment': 'My first test',
            }
        )
    assert str(execinfo.value) == 'DO NOT CREATE test changesets on the production server'


def test_ChangesetClose(auth_api, add_response):
    # setup mock
    resp = add_response(PUT, '/changeset/create', filename='test_Changeset_create.xml')
    resp = add_response(PUT, '/changeset/1414/close')

    # Call
    auth_api.ChangesetCreate()
    auth_api.ChangesetClose()

    assert '/api/0.6/changeset/1414/close' in resp.calls[1].request.url


def test_ChangesetClose_with_no_changeset(auth_api):
    with pytest.raises(osmapi.NoChangesetOpenError) as execinfo:
        auth_api.ChangesetClose()
    assert str(execinfo.value) == 'No changeset currently opened'


def test_ChangesetUpload_create_node(auth_api, add_response):
    # Setup
    resp = add_response(PUT, '/changeset/create', body='4444')
    resp = add_response(POST, '/changeset/4444/upload')

    changesdata = [
        {
            'type': 'node',
            'action': 'create',
            'data': {
                'lat': 47.123,
                'lon': 8.555,
                'tag': {
                    'amenity': 'place_of_worship',
                    'religion': 'pastafarian'
                }
            }
        }
    ]

    upload_xml = xmltosorteddict(
        b'<?xml version="1.0" encoding="UTF-8"?>\n'
        b'<osmChange version="0.6" generator="osmapi/3.1.0">\n'
        b'<create>\n'
        b'  <node lat="47.123" lon="8.555" visible="true" '
        b'changeset="4444">\n'
        b'    <tag k="amenity" v="place_of_worship"/>\n'
        b'    <tag k="religion" v="pastafarian"/>\n'
        b'  </node>\n'
        b'</create>\n'
        b'</osmChange>'
    )

    # Call
    auth_api.ChangesetCreate()
    result = auth_api.ChangesetUpload(changesdata)

    # Assert
    assert xmltosorteddict(resp.calls[1].request.body) == upload_xml
    assert result[0]['type'] == changesdata[0]['type']
    assert result[0]['action'] == changesdata[0]['action']

    data = result[0]['data']
    assert data['lat'] == changesdata[0]['data']['lat']
    assert data['lon'] == changesdata[0]['data']['lon']
    assert data['tag'] == changesdata[0]['data']['tag']
    assert data['id'] == 4295832900
    assert result[0]['data']['version'] == 1


def test_ChangesetUpload_modify_way(auth_api, add_response):
    # setup mock
    resp = add_response(PUT, '/changeset/create', body='4444')
    resp = add_response(POST, '/changeset/4444/upload')

    changesdata = [
        {
            'type': 'way',
            'action': 'modify',
            'data': {
                'id': 4294967296,
                'version': 2,
                'nd': [
                    4295832773,
                    4295832773,
                    4294967304,
                    4294967303,
                    4294967300,
                    4608751,
                    4294967305,
                    4294967302,
                    8548430,
                    4294967296,
                    4294967301,
                    4294967298,
                    4294967306,
                    7855737,
                    4294967297,
                    4294967299
                ],
                'tag': {
                    'highway': 'secondary',
                    'name': 'Stansted Road'
                }
            }
        }
    ]

    upload_xml = xmltosorteddict(
        b'<?xml version="1.0" encoding="UTF-8"?>\n'
        b'<osmChange version="0.6" generator="osmapi/3.1.0">\n'
        b'<modify>\n'
        b'  <way id="4294967296" version="2" visible="true" '
        b'changeset="4444">\n'
        b'    <tag k="highway" v="secondary"/>\n'
        b'    <tag k="name" v="Stansted Road"/>\n'
        b'    <nd ref="4295832773"/>\n'
        b'    <nd ref="4295832773"/>\n'
        b'    <nd ref="4294967304"/>\n'
        b'    <nd ref="4294967303"/>\n'
        b'    <nd ref="4294967300"/>\n'
        b'    <nd ref="4608751"/>\n'
        b'    <nd ref="4294967305"/>\n'
        b'    <nd ref="4294967302"/>\n'
        b'    <nd ref="8548430"/>\n'
        b'    <nd ref="4294967296"/>\n'
        b'    <nd ref="4294967301"/>\n'
        b'    <nd ref="4294967298"/>\n'
        b'    <nd ref="4294967306"/>\n'
        b'    <nd ref="7855737"/>\n'
        b'    <nd ref="4294967297"/>\n'
        b'    <nd ref="4294967299"/>\n'
        b'  </way>\n'
        b'</modify>\n'
        b'</osmChange>'
    )

    # Call
    auth_api.ChangesetCreate()
    result = auth_api.ChangesetUpload(changesdata)

    # Assert
    assert xmltosorteddict(resp.calls[1].request.body) == upload_xml

    assert result[0]['type'] == changesdata[0]['type']
    assert result[0]['action'] == changesdata[0]['action']

    data = result[0]['data']
    assert data['nd'] == changesdata[0]['data']['nd']
    assert data['tag'] == changesdata[0]['data']['tag']
    assert data['id'] == 4294967296
    assert data['version'] == 3


def test_ChangesetUpload_delete_relation(auth_api, add_response):
    # setup mock
    resp = add_response(PUT, '/changeset/create', body='4444')
    resp = add_response(POST, '/changeset/4444/upload')

    changesdata = [
        {
            'type': 'relation',
            'action': 'delete',
            'data': {
                'id': 676,
                'version': 2,
                'member': [
                    {
                        'ref': 4799,
                        'role': 'outer',
                        'type': 'way'
                    },
                    {
                        'ref': 9391,
                        'role': 'outer',
                        'type': 'way'
                    },
                ],
                'tag': {
                    'admin_level': '9',
                    'boundary': 'administrative',
                    'type': 'multipolygon'
                }
            }
        }
    ]

    upload_xml = xmltosorteddict(
        b'<?xml version="1.0" encoding="UTF-8"?>\n'
        b'<osmChange version="0.6" generator="osmapi/3.1.0">\n'
        b'<delete>\n'
        b'  <relation id="676" version="2" visible="true" '
        b'changeset="4444">\n'
        b'    <tag k="admin_level" v="9"/>\n'
        b'    <tag k="boundary" v="administrative"/>\n'
        b'    <tag k="type" v="multipolygon"/>\n'
        b'    <member type="way" ref="4799" role="outer"/>\n'
        b'    <member type="way" ref="9391" role="outer"/>\n'
        b'  </relation>\n'
        b'</delete>\n'
        b'</osmChange>'
    )

    # Call
    auth_api.ChangesetCreate()
    result = auth_api.ChangesetUpload(changesdata)

    # Assert
    assert xmltosorteddict(resp.calls[1].request.body) == upload_xml
    assert result[0]['type'] == changesdata[0]['type']
    assert result[0]['action'] == changesdata[0]['action']

    data = result[0]['data']
    assert data['member'], changesdata[0]['data']['member']
    assert data['tag'] == changesdata[0]['data']['tag']
    assert data['id'] == 676
    assert 'version' not in data


def test_ChangesetUpload_invalid_response(auth_api, add_response):
    # setup mock
    add_response(PUT, '/changeset/create', body='4444')
    add_response(POST, '/changeset/4444/upload', body='4444')

    changesdata = [
        {
            'type': 'relation',
            'action': 'delete',
            'data': {
                'id': 676,
                'version': 2,
                'member': [
                    {
                        'ref': 4799,
                        'role': 'outer',
                        'type': 'way'
                    },
                    {
                        'ref': 9391,
                        'role': 'outer',
                        'type': 'way'
                    },
                ],
                'tag': {
                    'admin_level': '9',
                    'boundary': 'administrative',
                    'type': 'multipolygon'
                }
            }
        }
    ]

    # Call + assert
    auth_api.ChangesetCreate()
    with pytest.raises(osmapi.XmlResponseInvalidError) as execinfo:
        auth_api.ChangesetUpload(changesdata)
    assert 'The XML response from the OSM API is invalid' in str(execinfo.value)


def test_ChangesetUpload_no_auth(api):
    changesdata = [
        {
            'type': 'node',
            'action': 'create',
            'data': {
                'lat': 47.123,
                'lon': 8.555,
                'tag': {
                    'amenity': 'place_of_worship',
                    'religion': 'pastafarian'
                }
            }
        }
    ]

    with pytest.raises(osmapi.UsernamePasswordMissingError) as execinfo:
        api.ChangesetUpload(changesdata)
    assert str(execinfo.value) == "Username/Password missing"


def test_ChangesetDownload(api, add_response):
    # Setup mock
    add_response(GET, '/changeset/23123/download')

    # Call
    result = api.ChangesetDownload(23123)

    # Assertion
    assert len(result) == 16
    assert result[1] == (
        {
            'action': 'create',
            'type': 'node',
            'data': {
                'changeset': 23123,
                'id': 4295668171,
                'lat': 46.4909781,
                'lon': 11.2743295,
                'tag': {
                    'highway': 'traffic_signals'
                },
                'timestamp': datetime.datetime(2013, 5, 14, 10, 33, 4),
                'uid': 1178,
                'user': 'tyrTester06',
                'version': 1,
                'visible': True
            }
        }
    )


def test_ChangesetDownload_invalid_response(api, add_response):
    add_response(GET, '/changeset/23123/download')
    with pytest.raises(osmapi.XmlResponseInvalidError) as execinfo:
        api.ChangesetDownload(23123)
    assert 'The XML response from the OSM API is invalid' in str(execinfo.value)


def test_ChangesetDownloadContainingUnicode(api, add_response):
    add_response(GET, '/changeset/37393499/download')

    # This changeset contains unicode tag values
    # Note that the fixture data has been reduced from the
    # original from openstreetmap.org
    result = api.ChangesetDownload(37393499)

    assert len(result) == 2
    assert result[1] == (
        {
            'action': 'create',
            'type': 'way',
            'data': {
                'changeset': 37393499,
                'id': 399491497,
                'nd': [4022271571, 4022271567, 4022271565],
                'tag': {'highway': 'service',
                        # UTF-8 encoded 'LATIN SMALL LETTER O WITH STROKE'
                        # Aka. 0xf8 in latin-1/ISO 8859-1
                        'name': b'S\xc3\xb8nderskovvej'.decode('utf-8'),
                        'service': 'driveway'},
                'timestamp': datetime.datetime(2016, 2, 23, 16, 55, 35),
                'uid': 328556,
                'user': 'InternationalUser',
                'version': 1,
                'visible': True
            }
        }
    )


def test_ChangesetsGet(api, add_response):
    resp = add_response(GET, '/changesets')

    result = api.ChangesetsGet(
        only_closed=True,
        username='metaodi'
    )

    assert resp.calls[0].request.params == {'display_name': 'metaodi', 'closed': '1'}
    assert len(result) == 10
    assert result[41417] == ({
        'closed_at': datetime.datetime(2014, 4, 29, 20, 25, 1),
        'created_at': datetime.datetime(2014, 4, 29, 20, 25, 1),
        'id': 41417,
        'discussion': [],
        'max_lat': '58.8997467',
        'max_lon': '22.7364427',
        'min_lat': '58.8501594',
        'min_lon': '22.6984333',
        'open': False,
        'tag': {
            'comment': 'Test delete of relation',
            'created_by': 'iD 1.3.9',
            'imagery_used': 'Bing'
        },
        'uid': 1841,
        'user': 'metaodi'
    })


def test_ChangesetGetWithComment(api, add_response):
    resp = add_response(GET, '/changeset/52924')

    result = api.ChangesetGet(52924, include_discussion=True)

    assert resp.calls[0].request.params == {'include_discussion': 'true'}
    assert result == {
        'id': 52924,
        'closed_at': datetime.datetime(2015, 1, 1, 14, 54, 2),
        'created_at': datetime.datetime(2015, 1, 1, 14, 54, 1),
        'comments_count': 3,
        'max_lat': '58.3369242',
        'max_lon': '25.8829107',
        'min_lat': '58.336813',
        'min_lon': '25.8823273',
        'discussion': [
            {
                'date':  datetime.datetime(2015, 1, 1, 18, 56, 48),
                'text': 'test',
                'uid': 1841,
                'user': 'metaodi',
            },
            {
                'date':  datetime.datetime(2015, 1, 1, 18, 58, 3),
                'text': 'another comment',
                'uid': 1841,
                'user': 'metaodi',
            },
            {
                'date':  datetime.datetime(2015, 1, 1, 19, 16, 5),
                'text': 'hello',
                'uid': 1841,
                'user': 'metaodi',
            },
        ],
        'open': False,
        'user': 'metaodi',
        'uid': 1841,
        'tag': {
            'comment': 'My test',
            'created_by': 'osmapi/0.4.1',
        },
    }


def test_ChangesetComment(auth_api, add_response):
    resp = add_response(POST, '/changeset/123/comment')

    result = auth_api.ChangesetComment(
        123,
        comment="test comment"
    )

    assert resp.calls[0].request.body == "text=test+comment"
    assert result == {
        'id': 123,
        'closed_at': datetime.datetime(2009, 9, 7, 22, 57, 37),
        'created_at': datetime.datetime(2009, 9, 7, 21, 57, 36),
        'discussion': [],
        'max_lat': '52.4710193',
        'max_lon': '-1.4831815',
        'min_lat': '45.9667901',
        'min_lon': '-1.4998534',
        'open': False,
        'user': 'randomjunk',
        'uid': 3,
        'tag': {
            'comment': 'correct node bug',
            'created_by': 'Potlatch 1.2a',
        },
    }


def test_ChangesetComment_no_auth(api):
    with pytest.raises(osmapi.UsernamePasswordMissingError) as execinfo:
        api.ChangesetComment(
            123,
            comment="test comment"
        )
    assert str(execinfo.value) == "Username/Password missing"


def test_ChangesetSubscribe(auth_api, add_response):
    add_response(POST, '/changeset/123/subscribe')

    result = auth_api.ChangesetSubscribe(123)

    assert result == {
        'id': 123,
        'closed_at': datetime.datetime(2009, 9, 7, 22, 57, 37),
        'created_at': datetime.datetime(2009, 9, 7, 21, 57, 36),
        'discussion': [],
        'max_lat': '52.4710193',
        'max_lon': '-1.4831815',
        'min_lat': '45.9667901',
        'min_lon': '-1.4998534',
        'open': False,
        'user': 'randomjunk',
        'uid': 3,
        'tag': {
            'comment': 'correct node bug',
            'created_by': 'Potlatch 1.2a',
        },
    }


def test_ChangesetSubscribeWhenAlreadySubscribed(auth_api, add_response):
    add_response(POST, '/changeset/52924/subscribe', status=409)

    with pytest.raises(osmapi.AlreadySubscribedApiError) as execinfo:
        auth_api.ChangesetSubscribe(52924)

    assert execinfo.value.payload == b"You are already subscribed to changeset 52924."
    assert execinfo.value.reason == 'Conflict'
    assert execinfo.value.status == 409


def test_ChangesetSubscribe_no_auth(api):
    with pytest.raises(osmapi.UsernamePasswordMissingError) as execinfo:
        api.ChangesetSubscribe(45627)
    assert str(execinfo.value) == "Username/Password missing"


def test_ChangesetUnsubscribe(auth_api, add_response):
    add_response(POST, '/changeset/123/unsubscribe')

    result = auth_api.ChangesetUnsubscribe(123)

    assert result == {
        'id': 123,
        'closed_at': datetime.datetime(2009, 9, 7, 22, 57, 37),
        'created_at': datetime.datetime(2009, 9, 7, 21, 57, 36),
        'discussion': [],
        'max_lat': '52.4710193',
        'max_lon': '-1.4831815',
        'min_lat': '45.9667901',
        'min_lon': '-1.4998534',
        'open': False,
        'user': 'randomjunk',
        'uid': 3,
        'tag': {
            'comment': 'correct node bug',
            'created_by': 'Potlatch 1.2a',
        },
    }


def test_ChangesetUnsubscribeWhenNotSubscribed(auth_api, add_response):
    add_response(POST, '/changeset/52924/unsubscribe', status=404)

    with pytest.raises(osmapi.NotSubscribedApiError) as execinfo:
        auth_api.ChangesetUnsubscribe(52924)

    assert execinfo.value.payload == b"You are not subscribed to changeset 52924."
    assert execinfo.value.reason == 'Not Found'
    assert execinfo.value.status == 404


def test_ChangesetUnsubscribe_no_auth(api):
    with pytest.raises(osmapi.UsernamePasswordMissingError) as execinfo:
        api.ChangesetUnsubscribe(45627)
    assert str(execinfo.value) == "Username/Password missing"