-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_open311.py
295 lines (251 loc) · 11.7 KB
/
test_open311.py
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
#!/usr/bin/env python
"""Unit tests for the Python Open311 API wrapper."""
import unittest
from mock import Mock
from open311.api import api
from open311 import open311
from open311 import Open311
class TestOpen311Init(unittest.TestCase):
def test_Open311_empty_init(self):
open_311 = Open311()
self.assertEquals(open_311.api_key, '')
self.assertEquals(open_311.endpoint, '')
self.assertEquals(open_311.format, 'xml')
self.assertEquals(open_311.jurisdiction, '')
self.assertEquals(open_311.proxy, '')
def test_Open311_init_with_kwargs(self):
open_311 = Open311(api_key='my_api_key', endpoint='http://test.com')
self.assertEquals(open_311.api_key, 'my_api_key')
self.assertEquals(open_311.endpoint, 'http://test.com')
class TestConfigureMethod(unittest.TestCase):
def test_configure_with_api_key_kwarg(self):
open_311 = Open311()
self.assertEquals(open_311.api_key, '')
open_311.configure(api_key='my_api_key')
self.assertEquals(open_311.api_key, 'my_api_key')
def test_configure_with_multiple_kwargs(self):
open_311 = Open311()
self.assertEquals(open_311.endpoint, '')
self.assertEquals(open_311.jurisdiction, '')
endpoint = 'http://api.dc.org/open311/v2_dev'
open_311.configure(endpoint=endpoint, jurisdiction='dc.gov')
self.assertEquals(open_311.endpoint, endpoint)
self.assertEquals(open_311.jurisdiction, 'dc.gov')
class TestResetMethod(unittest.TestCase):
def test_reset_method_restores_initial_properties(self):
open_311 = Open311()
open_311.configure(api_key='my_api_key')
self.assertEquals(open_311.api_key, 'my_api_key')
open_311.reset()
self.assertEquals(open_311.api_key, '')
class TestServiceListMethod(unittest.TestCase):
def setUp(self):
xml_services = """<?xml version="1.0" encoding="utf-8"?>
<services>
<service>
<service_code>001</service_code>
<service_name>Cans left out 24x7</service_name>
<description>
Garbage or recycling cans that have been left out for more
than 24 hours after collection. Violators will be cited.
</description>
<metadata>true</metadata>
<type>realtime</type>
<keywords>lorem, ipsum, dolor</keywords>
<group>sanitation</group>
</service>
<service>
<service_code>002</service_code>
<metadata>true</metadata>
<type>realtime</type>
<keywords>lorem, ipsum, dolor</keywords>
<group>street</group>
<service_name>Construction plate shifted</service_name>
<description>
Metal construction plate covering the street or sidewalk
has been moved.
</description>
</service>
<service>
<service_code>003</service_code>
<metadata>true</metadata>
<type>realtime</type>
<keywords>lorem, ipsum, dolor</keywords>
<group>street</group>
<service_name>Curb or curb ramp defect</service_name>
<description>
Sidewalk curb or ramp has problems such as cracking,
missing pieces, holes, and/or chipped curb.
</description>
</service>
</services>"""
api.urlopen = Mock()
api.urlopen().read.return_value = xml_services
def test_service_list_method(self):
endpoint = 'http://api.dc.org/open311/v2_dev'
data = Open311(endpoint=endpoint).service_list()
self.assertTrue(isinstance(data, list))
self.assertTrue(isinstance(data[0], dict))
self.assertEquals(len(data), 3)
class TestServiceDefinitionMethod(unittest.TestCase):
def setUp(self):
xml_service_definition = """<?xml version="1.0" encoding="utf-8"?>
<service_definition>
<service_code>033</service_code>
<attributes>
<attribute>
<variable>true</variable>
<code>WHISHETN</code>
<datatype>singlevaluelist</datatype>
<required>true</required>
<datatype_description></datatype_description>
<order>1</order>
<description>What is the ticket/tag/DL number?</description>
<values>
<value>
<key>123</key>
<name>Ford</name>
</value>
<value>
<key>124</key>
<name>Chrysler</name>
</value>
</values>
</attribute>
</attributes>
</service_definition>"""
api.urlopen = Mock()
api.urlopen().read.return_value = xml_service_definition
def test_default_service_definition_method(self):
endpoint = 'http://api.dc.org/open311/v2_dev'
open_311 = Open311(endpoint=endpoint, jurisdiction='dc.org')
data = open_311.service_definition('033')
expected_url = ('http://api.dc.org/open311/v2_dev/service/'
'033.xml?jurisdiction_id=dc.org')
api.urlopen.assert_called_with(expected_url)
self.assertTrue(isinstance(data, dict))
def test_service_definition_method_with_int(self):
endpoint = 'http://api.dc.org/open311/v2_dev'
open_311 = Open311(endpoint=endpoint, jurisdiction='dc.org')
open_311.service_definition(33)
expected_url = ('http://api.dc.org/open311/v2_dev/service/'
'33.xml?jurisdiction_id=dc.org')
api.urlopen.assert_called_with(expected_url)
class TestServiceRequestsMethod(unittest.TestCase):
def setUp(self):
xml_service_requests = """<?xml version="1.0" encoding="utf-8"?>
<service_requests>
<request>
<service_request_id>293944</service_request_id>
<service_notice>
The City will inspect and require the responsible party to
correct within 24 hours and/or issue a Correction Notice
or Notice of Violation of the Public Works Code
</service_notice>
<account_id/>
</request>
</service_requests>"""
api.urlopen = Mock()
api.urlopen().read.return_value = xml_service_requests
def test_default_service_requests_method(self):
endpoint = 'http://api.dc.org/open311/v2_dev'
open_311 = Open311(endpoint=endpoint, jurisdiction='dc.org')
data = open_311.service_requests()
api.urlopen.assert_called_with('http://api.dc.org/open311/v2_dev/'
'requests.xml?jurisdiction_id=dc.org')
self.assertTrue(isinstance(data, dict))
class TestGetServiceRequestMethod(unittest.TestCase):
def setUp(self):
xml_service_request = """<?xml version="1.0" encoding="utf-8"?>
<service_requests>
<request>
<service_request_id>638344</service_request_id>
<status>closed</status>
<status_notes>Duplicate request.</status_notes>
<service_name>Sidewalk and Curb Issues</service_name>
<service_code>006</service_code>
<description></description>
<agency_responsible></agency_responsible>
<service_notice></service_notice>
<requested_datetime>2010-04-14T06:37:38-08:00</requested_datetime>
<updated_datetime>2010-04-14T06:37:38-08:00</updated_datetime>
<expected_datetime>2010-04-15T06:37:38-08:00</expected_datetime>
<address>8TH AVE and JUDAH ST</address>
<address_id>545483</address_id>
<zipcode>94122</zipcode>
<lat>37.762221815</lat>
<long>-122.4651145</long>
<media_url>http://open311.city.gov.s3.amazonaws.com/requests/media/638344.rss </media_url>
</request>
</service_requests>"""
api.urlopen = Mock()
api.urlopen().read.return_value = xml_service_request
def test_default_get_service_request_method(self):
open_311 = Open311(endpoint='https://open311.sfgov.org/dev/v2',
jurisdiction='sfgov.org')
open_311.get_service_request('123456')
expected_url = ('https://open311.sfgov.org/dev/v2/requests/123456.xml?'
'jurisdiction_id=sfgov.org')
api.urlopen.assert_called_with(expected_url)
def test_get_service_request_method_with_int(self):
open_311 = Open311(endpoint='https://open311.sfgov.org/dev/v2',
jurisdiction='sfgov.org')
open_311.get_service_request(123456)
expected_url = ('https://open311.sfgov.org/dev/v2/requests/123456.xml?'
'jurisdiction_id=sfgov.org')
api.urlopen.assert_called_with(expected_url)
class TestRequestIdFromTokenMethod(unittest.TestCase):
def setUp(self):
xml_service_request = """<?xml version="1.0" encoding="utf-8"?>
<service_requests>
<request>
<service_request_id>638344</service_request_id>
<token>123456</token>
</request>
</service_requests>"""
api.urlopen = Mock()
api.urlopen().read.return_value = xml_service_request
def test_default_request_id_from_token_method(self):
open_311 = Open311(endpoint='https://open311.sfgov.org/dev/v2',
jurisdiction='sfgov.org')
open_311.request_id_from_token('123456')
expected_url = ('https://open311.sfgov.org/dev/v2/tokens/123456.xml?'
'jurisdiction_id=sfgov.org')
api.urlopen.assert_called_with(expected_url)
def test_request_id_from_token_method_with_int(self):
open_311 = Open311(endpoint='https://open311.sfgov.org/dev/v2',
jurisdiction='sfgov.org')
open_311.request_id_from_token(123456)
expected_url = ('https://open311.sfgov.org/dev/v2/tokens/123456.xml?'
'jurisdiction_id=sfgov.org')
api.urlopen.assert_called_with(expected_url)
class TestPostServiceRequestMethod(unittest.TestCase):
def setUp(self):
xml_service_request = """<?xml version="1.0" encoding="utf-8"?>
<service_requests>
<request>
<service_request_id>293944</service_request_id>
<service_notice>
The City will inspect and require the responsible party to
correct within 24 hours and/or issue a Correction Notice
or Notice of Violation of the Public Works Code
</service_notice>
<account_id/>
</request>
</service_requests>"""
open311.urlopen = Mock()
open311.urlopen().read.return_value = xml_service_request
def test_post_service_request_method(self):
open_311 = Open311(endpoint='https://open311.sfgov.org/dev/v2',
jurisdiction='sfgov.org')
open_311.post_service_request(
description='Large pothole at this location',
address_string='123 Any Street',
email='my@email.com',
first_name='Zach',
last_name='Williams',
phone='111-111-1111',
media_url='http://imgur.com/123_street_pothole.png')
assert open311.urlopen.called
if __name__ == '__main__':
unittest.main()