forked from jcgharvey/vsm-svr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·124 lines (82 loc) · 3.47 KB
/
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
import unittest
import json
import os
from datetime import datetime
# set before importing from models to use in memory db
os.environ['DATABASE_URL'] = 'sqlite://'
from models import db, Base, engine, Patient, CheckIn, Department
from server import app
class VsmTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.app = app.test_client()
def setUp(self):
self.now = datetime.now().isoformat()
Base.metadata.bind = engine
Base.metadata.drop_all()
Base.metadata.create_all()
class TestPatient(VsmTest):
def test_new_patient(self):
self.app.post('/patients', data=json.dumps({"nhi": "123ABC"}))
assert Patient.query.get('123ABC') is not None
def test_new_patient_idempotent(self):
self.app.post('/patients', data=json.dumps({"nhi": "123ABC"}))
self.app.post('/patients', data=json.dumps({"nhi": "123ABC"}))
assert Patient.query.get('123ABC') is not None
def test_new_patient_with_checkin(self):
self.app.post('/patients',
data=json.dumps({"nhi": "111AAA", "checkin": {"checkin_time": self.now}}))
assert Patient.query.get('111AAA').checkins
def test_delete_patient(self):
self.app.delete('/patients/123ABC')
assert Patient.query.get('123ABC') is None
class TestCheckIn(VsmTest):
def setUp(self):
super(TestCheckIn, self).setUp()
db.add(Patient(nhi="123ABC"))
db.add(CheckIn(patient_nhi="123ABC", checkin_time=self.now))
db.commit()
def test_new_checkin(self):
self.app.post('/patients/123ABC/checkins',
data=json.dumps({"checkin_time": self.now}))
assert len(Patient.query.get('123ABC').checkins) == 2
def test_update_checkin(self):
self.app.post('/patients/123ABC/checkins/1',
data=json.dumps({"location": "Been updated"}))
assert CheckIn.query.get(1).location == "Been updated"
def test_delete_checkin(self):
self.app.delete('/patients/123ABC/checkins/1')
assert CheckIn.query.get(1) is None
def test_latest_checkin(self):
self.app.post('/patients/123ABC/checkins',
data=json.dumps({"checkin_time": self.now}))
assert Patient.query.get(
'123ABC').latest_checkin_time.isoformat() == self.now
class TestDepartment(VsmTest):
def setUp(self):
super(TestDepartment, self).setUp()
cardiology = Department(department_name="Cardiology")
oncology = Department(department_name="Oncology")
p1 = Patient(nhi="A")
p2 = Patient(nhi="B")
p3 = Patient(nhi="C")
p1.departments.append(cardiology)
p2.departments.append(oncology)
p3.departments.append(cardiology)
p3.departments.append(oncology)
map(db.add, (p1, p2, p3))
db.commit()
self.app.post('/login', data={"department": "Cardiology"})
def test_departments_get(self):
rv = self.app.get('/departments')
departments = json.loads(rv.data)
assert len(departments) == 2
def test_department_patients_get(self):
rv = self.app.get('/patients')
patients = json.loads(rv.data)
assert all(any(d['department_name'] == 'Cardiology' for d in p['departments']) for p in patients)
def test_department_patient_get(self):
rv = self.app.get('/patients/B')
assert rv.data == "No patient with NHI B"
if __name__ == '__main__':
unittest.main()