-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic311.py
326 lines (297 loc) · 13.2 KB
/
basic311.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
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
"""
Usage: basic311.py <host> <apikey>
Arguments:
host open311 API endpoint
apikey access key for API
"""
'''
Open311 tests that cover all feature, but are tollerant for tiny variations
This covers core usecases and should represent default open311 usage.
If you pass, most applications should work with your implementation
'''
import json
from dateutil import parser
import datetime
import valideer
from valideer import ValidationError
import xmltodict
from georeportv2 import georeporter
from lxml import etree
from docopt import docopt
class TestSuite(object):
'''Open311 basic test. Inherited classes just run it's own tests'''
SCHEMA_SERVICE = {
"+services":
{
"+service": [{
"+service_code": "string",
"+service_name": "string",
"description": "string",
"+metadata": valideer.Enum(["true", "false"]),
"+type": valideer.Enum(["realtime", "batch", "blackbox"]),
"+keywords": "string",
"group": "string",
}]
}
}
SCHEMA_DEF = {
"+service_code": "string",
"+attributes":
[{"+variable": valideer.Enum(["true", "false"]),
"+code": "string",
"+datatype": valideer.Enum(["string", "number", "datetime", "text", "singlevaluelist", "multivaluelist"]),
"+required": valideer.Enum(["true", "false"]),
"+datatype_description": "string",
"+order": valideer.Range(valideer.Number, 0),
"description": "string"}]
}
SCHEMA_REQUESTS = {
"+service_requests":
{
"+request":
[{"+service_request_id": "string",
"+status": valideer.Enum(["open", "closed"]),
"status_notes": "string",
"+service_name": "string",
"+service_code": "string",
"description": "string",
"agency_responsible": "string",
"service_notice": "string",
"requested_datetime": valideer.AdaptBy(parser.parse),
"updated_datetime": valideer.AdaptBy(parser.parse),
"expected_datetime": valideer.AdaptBy(parser.parse),
"address": "string", #TODO: Make Position XOR address
"address_id": "string",
"zipcode": "string",
"+lat": valideer.AdaptTo(float, "number"),
"+long": valideer.AdaptTo(float, "number"),
"media_url": "string", }] #TODO: make URL regex
}
}
SCHEMA_REQUEST = {"+service_requests":
{ #TODO: Try to remove slightly redundant schema
"+request":
{"+service_request_id": "string",
"+status": valideer.Enum(["open", "closed"]),
"status_notes": "string",
"+service_name": "string",
"+service_code": "string",
"description": "string",
"agency_responsible": "string",
"service_notice": "string",
"requested_datetime": valideer.AdaptBy(parser.parse),
"updated_datetime": valideer.AdaptBy(parser.parse),
"expected_datetime": valideer.AdaptBy(parser.parse),
"address": "string", #TODO: Make Position XOR address
"address_id": "string",
"+zipcode": "string",
"+lat": valideer.AdaptTo(float, "number"),
"+long": valideer.AdaptTo(float, "number"),
"media_url": "string", } #TODO: make URL regex
}
}
@staticmethod
def __noNone(path, key, value):
'''we skip dict none values for like <key/> elements '''
if value is None:
return key, ""
'''we skip additional typisation fields'''
if (isinstance(value, dict)):
if "@nil" in value:
return key, ""
if "@type" in value:
if "#text" in value:
return key, value["#text"]
#if value.hasKey("@type"): oder alle mit @
return key, value
def __init__(self, host, api=None, apikey=None):
self.host = host
if api is None:
api = georeporter(host, apikey)
self.api = api
self.__cacheTransfers()
def showDifferences(self, expect, result, msgTest):
'''helper for visual compare of two lists'''
#TODO: Add normalizing option - lowercase, trim, ...
s = set(expect)
diff = [x for x in result if x not in s]
print("- "+msgTest),
if len(diff) == 0:
print("OK")
else:
print("differs at: %s (expected: %s)" % (str(diff), str(expect)))
def __cacheTransfers(self):
'''call api just one time for defaults'''
print "caching..."
print "-",
self.cache_services = self.api.getServices()
print "-",
self.cache_requests = self.api.getRequests()
#TODO: Write to disk?
def __getFirstServiceCode(self):
serviceCode = None
root = etree.fromstring(self.cache_services.content)
for service in root.getchildren():
fields = self.xmlToDict(etree.tostring(service))
try:
if fields["metadata"] == "true":
serviceCode = fields["service_code"]
return serviceCode
except:
pass #TODO: Log that metadata is mandatory?
return serviceCode
def __getFirstRequestId(self):
srid = None
root = etree.fromstring(self.cache_requests.content)
for servicereq in root.getchildren():
fields = self.xmlToDict(etree.tostring(servicereq))["request"]
if "service_request_id" in fields:
srid = fields["service_request_id"]
return srid
return srid
def xmlToDict(self, repl):
'''Transform XML tree to dict tree'''
tmp = xmltodict.parse(repl, postprocessor=self.__noNone)
replFields = json.loads(json.dumps(tmp))
return replFields
#TODO: Test discovery
def testGetServices(self):
print("Testing GET services")
repl = self.cache_services
expect = [200, "UTF-8", "text/xml; charset=utf-8"]
resp = [repl.status_code, repl.encoding, repl.headers["content-type"]]
self.showDifferences(expect, resp, "http skelleton")
print("- XML structure"),
with valideer.parsing(additional_properties=True):
validator = valideer.parse(self.SCHEMA_SERVICE)
replFields = self.xmlToDict(repl.content)
try:
validator.validate(replFields)
print("OK")
except ValidationError as e:
print("differs %s " % (str(e)))
def testGetServicsDef(self):
#TODO: walk trough all definitions
print("Testing GET service definition"),
firstCode = self.__getFirstServiceCode()
if firstCode is not None:
repl = self.api.getServiceDef(firstCode)
expect = [200, "UTF-8", "text/xml; charset=utf-8"]
resp = [repl.status_code, repl.encoding, repl.headers["content-type"]]
self.showDifferences(expect, resp, "http skelleton")
print("- XML structure"),
with valideer.parsing(additional_properties=True):
validator = valideer.parse(self.SCHEMA_DEF)
replFields = self.xmlToDict(repl)
try:
validator.validate(replFields)
print("OK")
except ValidationError as e:
print("differs at %s " % (str(e)))
else:
print "(No service definitions available for testing)"
def testCreateRequest(self, email="[email protected]", lat=54.0867, lon=12.1359, descr="test", title="test", code="18"):
#TODO: Extract attributes
print("Testing POST request"),
repl = self.api.createRequest(lat, lon, code, email, descr, attr={"title": title}, jurisdinction="rostock.de", tolerantSSL=True)
expect = [200, "UTF-8", "text/xml; charset=utf-8"]
resp = [repl.status_code, repl.encoding, repl.headers["content-type"]]
self.showDifferences(expect, resp, "http skelleton")
#TODO: Attributes of metadata / service definitions
#TODO: what more can we check here?
#TODO: Some might return token instead
def testGetRequests(self):
print("Testing GET requests"),
repl = self.cache_requests
expect = [200, "UTF-8", "text/xml; charset=utf-8"]
resp = [repl.status_code, repl.encoding, repl.headers["content-type"]]
self.showDifferences(expect, resp, "http skelleton")
print("- XML structure"),
with valideer.parsing(additional_properties=True):
validator = valideer.parse(self.SCHEMA_REQUESTS)
replFields = self.xmlToDict(repl.content)
try:
validator.validate(replFields)
print("OK")
except ValidationError as e:
print("differs at %s " % (str(e)))
def testGetRequestsFiltered(self):
print("Testing GET requests with filters")
#TODO: How to generate indipendend queries?
#TODO: How to check logic on expected results?
print("-service_request_id"),
repl = self.api.getRequests(service_request_id="3,5")
self.testEmptyRequestSet(repl)
print ("-service_code")
repl = self.api.getRequests(service_code=18)
self.testEmptyRequestSet(repl)
print ("-start_date")
past = datetime.datetime.utcnow() - datetime.timedelta(2)
repl = self.api.getRequests(start_date=past.isoformat())
self.testEmptyRequestSet(repl)
print ("-end_date")
repl = self.api.getRequests(end_date=past.isoformat())
self.testEmptyRequestSet(repl)
print ("-status")
repl = self.api.getRequests(status="closed")
self.testEmptyRequestSet(repl)
#TODO: None older than 90days?
def testGetRequest(self):
print("Testing GET request"),
srid = self.__getFirstRequestId()
repl = self.api.getRequest(srid)
expect = [200, "UTF-8", "text/xml; charset=utf-8"]
resp = [repl.status_code, repl.encoding, repl.headers["content-type"]]
self.showDifferences(expect, resp, "http skelleton")
print("- XML structure")
with valideer.parsing(additional_properties=True):
validator = valideer.parse(self.SCHEMA_REQUEST)
replFields = self.xmlToDict(repl.content)
try:
validator.validate(replFields)
print("OK")
except ValidationError as e:
print("differs at %s " % (str(e)))
def testGetRequestFromToken(self):
print("Testing GET ID by token"),
mytoken = "123" #TODO: Implement lookup for an valid token?
repl = self.api.getIdByToken(mytoken)
expect = [200, "UTF-8", "text/xml; charset=utf-8"]
resp = [repl.status_code, repl.encoding, repl.headers["content-type"]]
self.showDifferences(expect, resp, "http skelleton")
def testEmptyRequestSet(self, repl):
expect = [200, "UTF-8", "text/xml; charset=utf-8"]
resp = [repl.status_code, repl.encoding, repl.headers["content-type"]]
self.showDifferences(expect, resp, "http skelleton")
print "- queryset",
replFields = self.xmlToDict(repl.content)
amount = 1 #if no subdicts, just a single request
try:
for item in replFields["service_requests"]["request"]:
if isinstance(item, dict):
amount += 1
if (amount > 0):
print "ok (%d)" % amount
else:
print ("failed (empty)")
except KeyError:
print ("failed (empty)")
def cutArgs(args):
'''return only true values'''
temp = {}
for arg in args:
temp[arg] = args[arg].split("=")[1]
return temp
if __name__ == '__main__':
arguments = docopt(__doc__)
arguments = cutArgs(arguments)
tests = TestSuite(arguments["<host>"], None, arguments["<apikey>"])
tests.testGetServices()
tests.testGetServicsDef()
tests.testCreateRequest()
tests.testGetRequests()
tests.testGetRequest()
tests.testGetRequestFromToken()
tests.testGetRequestsFiltered()
#TODO: encoding
#TODO: test Errorcodes