-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver_test.py
146 lines (128 loc) · 4.3 KB
/
server_test.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
import json
import pprint
import unittest
from decimal import Decimal
from server import app, api, db
from database import Run, RunPermission, Protocol, ProtocolPermission
from api.protocol import api as protocols
from api.run import api as runs
class ServerTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
app.config["TESTING"] = True
app.config["AUTH_PROVIDER"] = "none"
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:////tmp/datalayer-test.db"
# import pdb; pdb.set_trace()
api.add_namespace(protocols)
api.add_namespace(runs)
def setUp(self):
with app.test_client() as client:
with app.app_context():
db.create_all()
self.client = client
self.maxDiff = None
def tearDown(self):
db.session.remove()
db.drop_all()
def test_read_runs(self):
# Load example runs.
example_run_1 = Run()
example_run_1.id = 6
example_run_1.data = {
"name": "Example Run 9",
"notes": "Example notes!",
"data_link": "https://s3.amazon.com/..."
}
db.session.add(example_run_1)
example_run_1_permission = RunPermission()
example_run_1_permission.run = example_run_1
example_run_1_permission.user_id = 42
db.session.add(example_run_1_permission)
response = self.client.get("/run")
if response.json is None:
pprint.pprint(response)
self.assertEqual(response.json, [{
"id": 6,
"name": "Example Run 9",
"notes": "Example notes!",
"data_link": "https://s3.amazon.com/..."
}])
def test_create_and_get_run(self):
# Load example protocols.
example_protocol_1 = Protocol()
example_protocol_1.id = 6
example_protocol_1.data = {
"name": "Example Protocol 9",
"notes": "Example notes!"
}
db.session.add(example_protocol_1)
data = {
"protocol_id": 6,
"name": "Test Run",
"notes": "Test notes!",
"data_link": "https://google.com/"
}
response = self.client.post(
"/run",
data=json.dumps(data),
headers={"Content-Type": "application/json"}
)
if response.json is None:
pprint.pprint(response)
self.assertEqual(response.json, {
"id": response.json["id"],
**data
})
response = self.client.get(f"/run/{response.json['id']}")
if response.json is None:
pprint.pprint(response)
self.assertEqual(response.json, {
"id": response.json["id"],
**data
})
def test_read_protocols(self):
# Load example protocols.
example_protocol_1 = Protocol()
example_protocol_1.id = 6
example_protocol_1.data = {
"name": "Example Protocol 9",
"notes": "Example notes!"
}
db.session.add(example_protocol_1)
example_protocol_1_permission = ProtocolPermission()
example_protocol_1_permission.protocol = example_protocol_1
example_protocol_1_permission.user_id = 42
db.session.add(example_protocol_1_permission)
response = self.client.get("/protocol")
if response.json is None:
pprint.pprint(response)
self.assertEqual(response.json, [{
"id": 6,
"name": "Example Protocol 9",
"notes": "Example notes!"
}])
def test_create_and_get_protocol(self):
data = {
"name": "Test Protocol",
"notes": "Test notes!"
}
response = self.client.post(
"/protocol",
data=json.dumps(data),
headers={"Content-Type": "application/json"}
)
if response.json is None:
pprint.pprint(response)
self.assertEqual(response.json, {
"id": response.json["id"],
**data
})
response = self.client.get(f"/protocol/{response.json['id']}")
if response.json is None:
pprint.pprint(response)
self.assertEqual(response.json, {
"id": response.json["id"],
**data
})
if __name__ == "__main__":
unittest.main()