This repository has been archived by the owner on Apr 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_app.py
62 lines (50 loc) · 1.97 KB
/
test_app.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
import json
import time
import uuid
from app import app
NEW_EXPERIMENT = {
'name': 'Test experiment',
'policy': 'epsilon_greedy',
'choices': ['A', 'B', 'C'],
'parameters': {'epsilon': 0.1},
}
def test_index():
request, response = app.test_client.get('/')
assert response.status == 200
assert 'session' in request
def test_reset_session():
request, response = app.test_client.get('/')
initial_session_id = request['session'].sid
request, response = app.test_client.get('/reset-session')
assert list(request['session'].keys()) == []
assert initial_session_id != request['session'].sid
def test_register_experiment():
request, response = app.test_client.post('/', data=json.dumps(NEW_EXPERIMENT))
assert response.status == 200
# TODO Test that we cant create multiple by same id
# TODO test expr with invalid data
def test_get_experiment():
request, response = app.test_client.post('/', data=json.dumps(NEW_EXPERIMENT))
new_experiment_id = response.json['experiment_id']
request, response = app.test_client.get('/' + new_experiment_id)
assert response.status == 200
assert 'message' in response.json
assert 'choice' in response.json
# TODO test fetching invalid experiment
# TODO test that choice stays the same per session
pass
def test_success():
request, response = app.test_client.post('/', data=json.dumps(NEW_EXPERIMENT))
new_experiment_id = response.json['experiment_id']
# Make a bunch of 0 successes
choice = '0'
for i in range(10):
app.test_client.get('/' + new_experiment_id)
app.test_client.post('/' + new_experiment_id + '/' + choice)
# 90% of these should exploit the fact that 0 is popular
# it's very unlikely 0 isn't the most common
choices = []
for i in range(10):
request, response = app.test_client.get('/' + new_experiment_id)
choices.append(response.json.get('choice'))
assert max(set(choices), key=choices.count) == 0