test_stats: try more realistic fake time.time()

Maybe windows+py36 was failing because "456" is too small of a value, and the
OS time libraries can't figure out UTC for something back in 1970. Switch to
a fixed timestamp from just a few minutes ago, to see if it likes 2018
better.
This commit is contained in:
Brian Warner 2018-02-19 13:23:40 -08:00
parent 071471a353
commit a9680918b9

View File

@ -12,14 +12,15 @@ class DB(unittest.TestCase):
return db return db
def test_db(self): def test_db(self):
T = 1519075308.0
d = self.mktemp() d = self.mktemp()
os.mkdir(d) os.mkdir(d)
usage_db = os.path.join(d, "usage.sqlite") usage_db = os.path.join(d, "usage.sqlite")
with mock.patch("time.time", return_value=456.0): with mock.patch("time.time", return_value=T+0):
t = Transit(blur_usage=None, log_file=None, usage_db=usage_db) t = Transit(blur_usage=None, log_file=None, usage_db=usage_db)
db = self.open_db(usage_db) db = self.open_db(usage_db)
with mock.patch("time.time", return_value=457.0): with mock.patch("time.time", return_value=T+1):
t.recordUsage(started=123, result="happy", total_bytes=100, t.recordUsage(started=123, result="happy", total_bytes=100,
total_time=10, waiting_time=2) total_time=10, waiting_time=2)
self.assertEqual(db.execute("SELECT * FROM `usage`").fetchall(), self.assertEqual(db.execute("SELECT * FROM `usage`").fetchall(),
@ -27,11 +28,11 @@ class DB(unittest.TestCase):
total_bytes=100, total_time=10, waiting_time=2), total_bytes=100, total_time=10, waiting_time=2),
]) ])
self.assertEqual(db.execute("SELECT * FROM `current`").fetchone(), self.assertEqual(db.execute("SELECT * FROM `current`").fetchone(),
dict(rebooted=456, updated=457, dict(rebooted=T+0, updated=T+1,
incomplete_bytes=0, incomplete_bytes=0,
waiting=0, connected=0)) waiting=0, connected=0))
with mock.patch("time.time", return_value=458.0): with mock.patch("time.time", return_value=T+2):
t.recordUsage(started=150, result="errory", total_bytes=200, t.recordUsage(started=150, result="errory", total_bytes=200,
total_time=11, waiting_time=3) total_time=11, waiting_time=3)
self.assertEqual(db.execute("SELECT * FROM `usage`").fetchall(), self.assertEqual(db.execute("SELECT * FROM `usage`").fetchall(),
@ -41,14 +42,14 @@ class DB(unittest.TestCase):
total_bytes=200, total_time=11, waiting_time=3), total_bytes=200, total_time=11, waiting_time=3),
]) ])
self.assertEqual(db.execute("SELECT * FROM `current`").fetchone(), self.assertEqual(db.execute("SELECT * FROM `current`").fetchone(),
dict(rebooted=456, updated=458, dict(rebooted=T+0, updated=T+2,
incomplete_bytes=0, incomplete_bytes=0,
waiting=0, connected=0)) waiting=0, connected=0))
with mock.patch("time.time", return_value=459.0): with mock.patch("time.time", return_value=T+3):
t.timerUpdateStats() t.timerUpdateStats()
self.assertEqual(db.execute("SELECT * FROM `current`").fetchone(), self.assertEqual(db.execute("SELECT * FROM `current`").fetchone(),
dict(rebooted=456, updated=459, dict(rebooted=T+0, updated=T+3,
incomplete_bytes=0, incomplete_bytes=0,
waiting=0, connected=0)) waiting=0, connected=0))