Skip to content

Commit

Permalink
Compares timestamps with assertAlmostEqual (#1262)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
colinschoen authored May 5, 2018
1 parent 90c5a99 commit 44aadee
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import datetime as dt
import dateutil.parser
import json
import random

from server import constants

from server.models import (db, Assignment, Backup, Course, User,
Version, Group)
from server.utils import encode_id
Expand Down Expand Up @@ -128,11 +131,24 @@ def test_get_backup(self):
"email": backup.submitter.email,
"id": encode_id(backup.submitter_id),
}
assert response.json['data'] == {
response_json = response.json['data']
time_threshold = dt.timedelta(seconds=5)
self.assertAlmostEqual(dateutil.parser.parse(response_json['created']),
backup.created,
delta=time_threshold)
self.assertAlmostEqual(dateutil.parser.parse(response_json['submission_time']),
submission_time,
delta=time_threshold)
self.assertAlmostEqual(dateutil.parser.parse(response_json['messages'][0]['created']),
backup.created,
delta=time_threshold)
# Unset timestamps already tested.
del response_json['created']
del response_json['submission_time']
del response_json['messages'][0]['created']
assert response_json == {
"submitter": user_json,
"submit": backup.submit,
"created": backup.created.isoformat(),
"submission_time": submission_time.isoformat(),
"group": [user_json],
"is_late": backup.is_late,
"external_files": [],
Expand All @@ -151,7 +167,6 @@ def test_get_backup(self):
{
"kind": "file_contents",
"contents": backup.files(),
"created": backup.created.isoformat(),
},
],
}
Expand Down

0 comments on commit 44aadee

Please sign in to comment.