Skip to content

Commit 44aadee

Browse files
authored
Compares timestamps with assertAlmostEqual (#1262)
* Compares timestamps with assertAlmostEqual The test_get_backup test has been flaking due to doing equality comparisons on timestamps before and after database transactions. These comparisons have been changed to ensuring the timestamps are within a certain threshold (currently 5 seconds). Fixes #1255 Signed-off-by: Colin Schoen <[email protected]> * Add additional datetime string formatting constants Signed-off-by: Colin Schoen <[email protected]> * Use new response_json local variable. Signed-off-by: Colin Schoen <[email protected]> * Fix typo in spelling datetime incorrectly Signed-off-by: Colin Schoen <[email protected]> * Use dateutil parser instead of datetime library Signed-off-by: Colin Schoen <[email protected]>
1 parent 90c5a99 commit 44aadee

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

tests/test_api.py

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import datetime as dt
2+
import dateutil.parser
23
import json
34
import random
45

6+
from server import constants
7+
58
from server.models import (db, Assignment, Backup, Course, User,
69
Version, Group)
710
from server.utils import encode_id
@@ -128,11 +131,24 @@ def test_get_backup(self):
128131
"email": backup.submitter.email,
129132
"id": encode_id(backup.submitter_id),
130133
}
131-
assert response.json['data'] == {
134+
response_json = response.json['data']
135+
time_threshold = dt.timedelta(seconds=5)
136+
self.assertAlmostEqual(dateutil.parser.parse(response_json['created']),
137+
backup.created,
138+
delta=time_threshold)
139+
self.assertAlmostEqual(dateutil.parser.parse(response_json['submission_time']),
140+
submission_time,
141+
delta=time_threshold)
142+
self.assertAlmostEqual(dateutil.parser.parse(response_json['messages'][0]['created']),
143+
backup.created,
144+
delta=time_threshold)
145+
# Unset timestamps already tested.
146+
del response_json['created']
147+
del response_json['submission_time']
148+
del response_json['messages'][0]['created']
149+
assert response_json == {
132150
"submitter": user_json,
133151
"submit": backup.submit,
134-
"created": backup.created.isoformat(),
135-
"submission_time": submission_time.isoformat(),
136152
"group": [user_json],
137153
"is_late": backup.is_late,
138154
"external_files": [],
@@ -151,7 +167,6 @@ def test_get_backup(self):
151167
{
152168
"kind": "file_contents",
153169
"contents": backup.files(),
154-
"created": backup.created.isoformat(),
155170
},
156171
],
157172
}

0 commit comments

Comments
 (0)