/
test_server.py
488 lines (380 loc) · 13.5 KB
/
test_server.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
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
from __future__ import absolute_import, division, print_function
import pytest
pytest.importorskip('flask')
from base64 import b64encode
import datashape
import numpy as np
from datetime import datetime
from pandas import DataFrame
from toolz import pipe
from odo import odo
from blaze.utils import example
from blaze import discover, symbol, by, CSV, compute, join, into, resource
from blaze.server.client import mimetype
from blaze.server.server import Server, to_tree, from_tree
from blaze.server.serialization import all_formats
accounts = DataFrame([['Alice', 100], ['Bob', 200]],
columns=['name', 'amount'])
cities = DataFrame([['Alice', 'NYC'], ['Bob', 'LA']],
columns=['name', 'city'])
events = DataFrame([[1, datetime(2000, 1, 1, 12, 0, 0)],
[2, datetime(2000, 1, 2, 12, 0, 0)]],
columns=['value', 'when'])
db = resource('sqlite:///' + example('iris.db'))
data = {'accounts': accounts,
'cities': cities,
'events': events,
'db': db}
@pytest.fixture(scope='module')
def server():
s = Server(data, all_formats)
s.app.testing = True
return s
@pytest.yield_fixture
def test(server):
with server.app.test_client() as c:
yield c
def test_datasets(test):
response = test.get('/datashape')
assert response.data.decode('utf-8') == str(discover(data))
@pytest.mark.parametrize('serial', all_formats)
def test_bad_responses(test, serial):
assert 'OK' not in test.post(
'/compute/accounts.{name}'.format(name=serial.name),
data=serial.dumps(500),
).status
assert 'OK' not in test.post(
'/compute/non-existent-table.{name}'.format(name=serial.name),
data=serial.dumps(0),
).status
assert 'OK' not in test.post(
'/compute/accounts.{name}'.format(name=serial.name),
).status
def test_to_from_json():
t = symbol('t', 'var * {name: string, amount: int}')
assert from_tree(to_tree(t)).isidentical(t)
assert from_tree(to_tree(t.amount + 1)).isidentical(t.amount + 1)
def test_to_tree():
t = symbol('t', 'var * {name: string, amount: int32}')
expr = t.amount.sum()
expected = {'op': 'sum',
'args': [{'op': 'Field',
'args':
[
{'op': 'Symbol',
'args': [
't',
'var * {name: string, amount: int32}',
None
]
},
'amount'
]
}, [0], False]
}
assert to_tree(expr) == expected
@pytest.mark.parametrize('serial', all_formats)
def test_to_tree_slice(serial):
t = symbol('t', 'var * {name: string, amount: int32}')
expr = t[:5]
expr2 = pipe(expr, to_tree, serial.dumps, serial.loads, from_tree)
assert expr.isidentical(expr2)
def test_to_from_tree_namespace():
t = symbol('t', 'var * {name: string, amount: int32}')
expr = t.name
tree = to_tree(expr, names={t: 't'})
assert tree == {'op': 'Field', 'args': ['t', 'name']}
new = from_tree(tree, namespace={'t': t})
assert new.isidentical(expr)
def test_from_tree_is_robust_to_unnecessary_namespace():
t = symbol('t', 'var * {name: string, amount: int32}')
expr = t.amount + 1
tree = to_tree(expr) # don't use namespace
assert from_tree(tree, {'t': t}).isidentical(expr)
t = symbol('t', discover(data))
@pytest.mark.parametrize('serial', all_formats)
def test_compute(test, serial):
expr = t.accounts.amount.sum()
query = {'expr': to_tree(expr)}
expected = 300
response = test.post(
'/compute',
data=serial.dumps(query),
headers=mimetype(serial)
)
assert 'OK' in response.status
data = serial.loads(response.data)
assert data['data'] == expected
assert data['names'] == ['amount_sum']
@pytest.mark.parametrize('serial', all_formats)
def test_get_datetimes(test, serial):
expr = t.events
query = {'expr': to_tree(expr)}
response = test.post(
'/compute',
data=serial.dumps(query),
headers=mimetype(serial)
)
assert 'OK' in response.status
data = serial.loads(response.data)
ds = datashape.dshape(data['datashape'])
result = into(np.ndarray, data['data'], dshape=ds)
assert into(list, result) == into(list, events)
assert data['names'] == events.columns.tolist()
@pytest.mark.parametrize('serial', all_formats)
def dont_test_compute_with_namespace(test, serial):
query = {'expr': {'op': 'Field',
'args': ['accounts', 'name']}}
expected = ['Alice', 'Bob']
response = test.post(
'/compute',
data=serial.dumps(query),
headers=mimetype(serial)
)
assert 'OK' in response.status
data = serial.loads(response.data)
assert data['data'] == expected
assert data['names'] == ['name']
@pytest.yield_fixture
def iris_server():
iris = CSV(example('iris.csv'))
s = Server(iris, all_formats)
s.app.testing = True
with s.app.test_client() as c:
yield c
iris = CSV(example('iris.csv'))
@pytest.mark.parametrize('serial', all_formats)
def test_compute_with_variable_in_namespace(iris_server, serial):
test = iris_server
t = symbol('t', discover(iris))
pl = symbol('pl', 'float32')
expr = t[t.petal_length > pl].species
tree = to_tree(expr, {pl: 'pl'})
blob = serial.dumps({'expr': tree, 'namespace': {'pl': 5}})
resp = test.post(
'/compute',
data=blob,
headers=mimetype(serial)
)
assert 'OK' in resp.status
data = serial.loads(resp.data)
result = data['data']
expected = list(compute(expr._subs({pl: 5}), {t: iris}))
assert result == expected
assert data['names'] == ['species']
@pytest.mark.parametrize('serial', all_formats)
def test_compute_by_with_summary(iris_server, serial):
test = iris_server
t = symbol('t', discover(iris))
expr = by(
t.species,
max=t.petal_length.max(),
sum=t.petal_width.sum(),
)
tree = to_tree(expr)
blob = serial.dumps({'expr': tree})
resp = test.post(
'/compute',
data=blob,
headers=mimetype(serial)
)
assert 'OK' in resp.status
data = serial.loads(resp.data)
result = DataFrame(data['data']).values
expected = compute(expr, iris).values
np.testing.assert_array_equal(result[:, 0], expected[:, 0])
np.testing.assert_array_almost_equal(result[:, 1:], expected[:, 1:])
assert data['names'] == ['species', 'max', 'sum']
@pytest.mark.parametrize('serial', all_formats)
def test_compute_column_wise(iris_server, serial):
test = iris_server
t = symbol('t', discover(iris))
subexpr = ((t.petal_width / 2 > 0.5) &
(t.petal_length / 2 > 0.5))
expr = t[subexpr]
tree = to_tree(expr)
blob = serial.dumps({'expr': tree})
resp = test.post(
'/compute',
data=blob,
headers=mimetype(serial)
)
assert 'OK' in resp.status
data = serial.loads(resp.data)
result = data['data']
expected = compute(expr, iris)
assert list(map(tuple, result)) == into(list, expected)
assert data['names'] == t.fields
@pytest.mark.parametrize('serial', all_formats)
def test_multi_expression_compute(test, serial):
s = symbol('s', discover(data))
expr = join(s.accounts, s.cities)
resp = test.post(
'/compute',
data=serial.dumps(dict(expr=to_tree(expr))),
headers=mimetype(serial)
)
assert 'OK' in resp.status
respdata = serial.loads(resp.data)
result = respdata['data']
expected = compute(expr, {s: data})
assert list(map(tuple, result)) == into(list, expected)
assert respdata['names'] == expr.fields
@pytest.mark.parametrize('serial', all_formats)
def test_leaf_symbol(test, serial):
query = {'expr': {'op': 'Field', 'args': [':leaf', 'cities']}}
resp = test.post(
'/compute',
data=serial.dumps(query),
headers=mimetype(serial)
)
data = serial.loads(resp.data)
a = data['data']
b = into(list, cities)
assert list(map(tuple, a)) == b
assert data['names'] == cities.columns.tolist()
@pytest.mark.parametrize('serial', all_formats)
def test_sqlalchemy_result(test, serial):
expr = t.db.iris.head(5)
query = {'expr': to_tree(expr)}
response = test.post(
'/compute',
data=serial.dumps(query),
headers=mimetype(serial)
)
assert 'OK' in response.status
data = serial.loads(response.data)
result = data['data']
assert all(isinstance(item, (tuple, list)) for item in result)
assert data['names'] == t.db.iris.fields
def test_server_accepts_non_nonzero_ables():
Server(DataFrame())
@pytest.mark.parametrize('serial', all_formats)
def test_server_can_compute_sqlalchemy_reductions(test, serial):
expr = t.db.iris.petal_length.sum()
query = {'expr': to_tree(expr)}
response = test.post(
'/compute',
data=serial.dumps(query),
headers=mimetype(serial)
)
assert 'OK' in response.status
respdata = serial.loads(response.data)
result = respdata['data']
assert result == odo(compute(expr, {t: data}), int)
assert respdata['names'] == ['petal_length_sum']
@pytest.mark.parametrize('serial', all_formats)
def test_serialization_endpoints(test, serial):
expr = t.db.iris.petal_length.sum()
query = {'expr': to_tree(expr)}
response = test.post(
'/compute',
data=serial.dumps(query),
headers=mimetype(serial)
)
assert 'OK' in response.status
respdata = serial.loads(response.data)
result = respdata['data']
assert result == odo(compute(expr, {t: data}), int)
assert respdata['names'] == ['petal_length_sum']
@pytest.fixture
def has_bokeh():
try:
from bokeh.server.crossdomain import crossdomain
except ImportError as e:
pytest.skip(str(e))
@pytest.mark.parametrize('serial', all_formats)
def test_cors_compute(test, serial, has_bokeh):
expr = t.db.iris.petal_length.sum()
res = test.post(
'/compute',
data=serial.dumps(dict(expr=to_tree(expr))),
headers=mimetype(serial)
)
assert res.status_code == 200
assert res.headers['Access-Control-Allow-Origin'] == '*'
assert 'HEAD' in res.headers['Access-Control-Allow-Methods']
assert 'OPTIONS' in res.headers['Access-Control-Allow-Methods']
assert 'POST' in res.headers['Access-Control-Allow-Methods']
# we don't allow gets because we're always sending data
assert 'GET' not in res.headers['Access-Control-Allow-Methods']
@pytest.mark.parametrize('method',
['get',
pytest.mark.xfail('head', raises=AssertionError),
pytest.mark.xfail('options', raises=AssertionError),
pytest.mark.xfail('post', raises=AssertionError)])
def test_cors_datashape(test, method, has_bokeh):
res = getattr(test, method)('/datashape')
assert res.status_code == 200
assert res.headers['Access-Control-Allow-Origin'] == '*'
assert 'HEAD' not in res.headers['Access-Control-Allow-Methods']
assert 'OPTIONS' not in res.headers['Access-Control-Allow-Methods']
assert 'POST' not in res.headers['Access-Control-Allow-Methods']
# we only allow GET requests
assert 'GET' in res.headers['Access-Control-Allow-Methods']
@pytest.fixture(scope='module')
def username():
return 'blaze-dev'
@pytest.fixture(scope='module')
def password():
return 'SecretPassword123'
@pytest.fixture(scope='module')
def server_with_auth(username, password):
def auth(a):
return a and a.username == username and a.password == password
s = Server(data, all_formats, authorization=auth)
s.app.testing = True
return s
@pytest.yield_fixture
def test_with_auth(server_with_auth):
with server_with_auth.app.test_client() as c:
yield c
def basic_auth(username, password):
return (
b'Basic ' + b64encode(':'.join((username, password)).encode('utf-8'))
)
@pytest.mark.parametrize('serial', all_formats)
def test_auth(test_with_auth, username, password, serial):
expr = t.accounts.amount.sum()
query = {'expr': to_tree(expr)}
r = test_with_auth.get(
'/datashape',
headers={'authorization': basic_auth(username, password)},
)
assert r.status_code == 200
headers = mimetype(serial)
headers['authorization'] = basic_auth(username, password)
s = test_with_auth.post(
'/compute',
data=serial.dumps(query),
headers=headers,
)
assert s.status_code == 200
u = test_with_auth.get(
'/datashape',
headers={'authorization': basic_auth(username + 'a', password + 'a')},
)
assert u.status_code == 401
headers['authorization'] = basic_auth(username + 'a', password + 'a')
v = test_with_auth.post(
'/compute',
data=serial.dumps(query),
headers=headers,
)
assert v.status_code == 401
@pytest.mark.parametrize('serial', all_formats)
def test_minute_query(test, serial):
expr = t.events.when.minute
query = {'expr': to_tree(expr)}
result = test.post(
'/compute',
headers=mimetype(serial),
data=serial.dumps(query)
)
expected = {
'data': [0, 0],
'names': ['when_minute'],
'datashape': '2 * int64'
}
assert result.status_code == 200
assert expected == serial.loads(result.data)