1 | from datetime import datetime
|
---|
2 | import json
|
---|
3 | from typing import List
|
---|
4 | import unittest
|
---|
5 |
|
---|
6 | from pyson.ObjectMapper import ObjectMapper
|
---|
7 | from unitpy.GeneralTests import GeneralTests
|
---|
8 |
|
---|
9 | from geniusweb.progress.Progress import Progress
|
---|
10 | from geniusweb.progress.ProgressTime import ProgressTime
|
---|
11 |
|
---|
12 |
|
---|
13 | class ProgressTimeTest (unittest.TestCase, GeneralTests[Progress]):
|
---|
14 | # we set T0 quite high because Python has trouble handling small datetime
|
---|
15 | T0 = 100000
|
---|
16 | TIMEZERO = datetime.fromtimestamp(T0 / 1000)
|
---|
17 | TESTTIME = 10 # milliseconds!
|
---|
18 | progress1 = ProgressTime(TESTTIME, TIMEZERO)
|
---|
19 | progress1a = ProgressTime(TESTTIME, TIMEZERO)
|
---|
20 | progress2 = ProgressTime(200, TIMEZERO)
|
---|
21 |
|
---|
22 | jackson = ObjectMapper()
|
---|
23 | progressstring = "{\"ProgressTime\":{\"duration\":10,\"start\":100000}}"
|
---|
24 |
|
---|
25 | start = 1234
|
---|
26 | pr = ProgressTime(TESTTIME, datetime.fromtimestamp(start / 1000))
|
---|
27 |
|
---|
28 | def getGeneralTestData(self) -> List[List[Progress]]:
|
---|
29 | return [[self.progress1, self.progress1a], [self.progress2]]
|
---|
30 |
|
---|
31 | def getGeneralTestStrings(self) -> List[str]:
|
---|
32 | return ["ProgressTime\\[100000.*10ms\\]",
|
---|
33 | "ProgressTime\\[100000.*200ms\\]"]
|
---|
34 |
|
---|
35 | def testInit(self):
|
---|
36 | self.assertEqual(0, self.progress1.get(0))
|
---|
37 |
|
---|
38 | def testAdvancingTime(self):
|
---|
39 | for n in range(0, self.TESTTIME):
|
---|
40 | self.assertEqual(float(n) / self.TESTTIME,
|
---|
41 | self.progress1.get(self.T0 + n))
|
---|
42 |
|
---|
43 | def testAdvanceWhenDeadlineReached(self):
|
---|
44 | progress = ProgressTime(self.TESTTIME, self.TIMEZERO)
|
---|
45 | self.assertEqual(1, self.progress1.get(self.T0 + self.TESTTIME));
|
---|
46 |
|
---|
47 | def testSerialize(self):
|
---|
48 | actual = self.jackson.toJson(self.progress1)
|
---|
49 | print(actual)
|
---|
50 | self.assertEqual(json.loads(self.progressstring), actual)
|
---|
51 |
|
---|
52 | def testDeserialize(self):
|
---|
53 | newprog = self.jackson.parse(json.loads(self.progressstring), ProgressTime)
|
---|
54 | # we can't directly compare with progress since that's a hacked
|
---|
55 | # object...
|
---|
56 | self.assertEqual(self.TESTTIME, newprog.getDuration())
|
---|
57 | self.assertEqual(self.T0, 1000 * newprog.getStart().timestamp())
|
---|
58 |
|
---|
59 | def pastDeadlinetest(self):
|
---|
60 | self.assertTrue(self.pr.isPastDeadline(self.start + self.TESTTIME + 1))
|
---|
61 | self.assertFalse(self.pr.isPastDeadline(self.start + self.TESTTIME - 1))
|
---|
62 |
|
---|
63 | def terminationTimetest(self):
|
---|
64 | self.assertEqual(self.start + self.TESTTIME, self.pr.getTerminationTime().getTime())
|
---|
65 |
|
---|
66 | def gettest(self):
|
---|
67 | self.assertEqual(0, self.pr.get(self.start))
|
---|
68 | self.assertEqual(1, self.pr.get(self.start + self.TESTTIME));
|
---|
69 | self.assertEqual(0.5, self.pr.get(self.start + self.TESTTIME / 2));
|
---|
70 | self.assertEqual(0, self.pr.get(self.start - 10))
|
---|
71 | self.assertEqual(1, self.pr.get(self.start + self.TESTTIME + 10))
|
---|