1 | from datetime import datetime
|
---|
2 | import json
|
---|
3 | from typing import List
|
---|
4 | import unittest
|
---|
5 | from unittest.mock import Mock
|
---|
6 |
|
---|
7 | from pyson.ObjectMapper import ObjectMapper
|
---|
8 | from unitpy.GeneralTests import GeneralTests
|
---|
9 |
|
---|
10 | from geniusweb import progress
|
---|
11 | from geniusweb.deadline.DeadlineRounds import DeadlineRounds
|
---|
12 | from geniusweb.progress.Progress import Progress
|
---|
13 | from geniusweb.progress.ProgressRounds import ProgressRounds
|
---|
14 | from geniusweb.progress.ProgressTime import ProgressTime
|
---|
15 |
|
---|
16 |
|
---|
17 | class ProgressRoundsTest(unittest.TestCase, GeneralTests[Progress]):
|
---|
18 | deadline = Mock(DeadlineRounds)
|
---|
19 | TESTROUNDS = 10
|
---|
20 | TESTTIME = 10 # milliseconds!
|
---|
21 |
|
---|
22 | date = datetime.fromtimestamp(98765 / 1000)
|
---|
23 | date0 = datetime.fromtimestamp(100000 / 1000)
|
---|
24 |
|
---|
25 | deadline1 = ProgressRounds(20, 0, date)
|
---|
26 | deadline2 = ProgressRounds(20, 0, date)
|
---|
27 | deadline3 = ProgressRounds(30, 0, date)
|
---|
28 | deadline4 = ProgressTime(20, date0)
|
---|
29 | deadline5 = ProgressTime(20, date)
|
---|
30 | jackson = ObjectMapper()
|
---|
31 | progressstring = "{\"ProgressRounds\":{\"duration\":10,\"currentRound\":0,\"endtime\":98765}}"
|
---|
32 |
|
---|
33 | start = 1234
|
---|
34 | end = datetime.fromtimestamp((start + TESTTIME) / 1000)
|
---|
35 | pr = ProgressRounds(TESTROUNDS, 4, end)
|
---|
36 |
|
---|
37 | def setUp(self):
|
---|
38 | self.deadline.getRounds = Mock(return_value=self.TESTROUNDS)
|
---|
39 | self.progress = ProgressRounds(self.deadline.getRounds(), 0, self.date)
|
---|
40 |
|
---|
41 | def getGeneralTestData(self) -> List[List[Progress]]:
|
---|
42 | return [[self.deadline1, self.deadline2], [self.deadline3], [self.deadline4], [self.deadline5]]
|
---|
43 |
|
---|
44 | def getGeneralTestStrings(self) -> List[str]:
|
---|
45 | return ["ProgressRounds\\[0.*20\\]",
|
---|
46 | "ProgressRounds\\[0.*30\\]", "ProgressTime\\[100000.*20ms\\]",
|
---|
47 | "ProgressTime\\[98765.*20ms\\]"]
|
---|
48 |
|
---|
49 | def testInit(self):
|
---|
50 | self.assertEqual(0, self.progress.get(1))
|
---|
51 |
|
---|
52 | def testCurrentRound(self):
|
---|
53 | self.assertEqual(0, self.progress.getCurrentRound())
|
---|
54 |
|
---|
55 | def testAdvance(self):
|
---|
56 | p = self.progress
|
---|
57 | n = 0;
|
---|
58 | while n <= self.TESTROUNDS:
|
---|
59 | self.assertEqual((float(n) / self.TESTROUNDS), p.get(1))
|
---|
60 | n = n + 1
|
---|
61 | p = p.advance()
|
---|
62 |
|
---|
63 | def testAdvanceWhenDeadlineReached(self):
|
---|
64 | p = ProgressRounds(self.TESTROUNDS, 9, self.date)
|
---|
65 | p = p.advance()
|
---|
66 | self.assertEqual(1, p.get(1))
|
---|
67 |
|
---|
68 | def testSerialize(self):
|
---|
69 | print(json.dumps(self.jackson.toJson(self.progress)))
|
---|
70 | self.assertEqual(json.loads(self.progressstring), self.jackson.toJson(self.progress))
|
---|
71 |
|
---|
72 | def testDeserialize(self):
|
---|
73 | self.assertEqual(self.progress,
|
---|
74 | self.jackson.parse(json.loads(self.progressstring), Progress))
|
---|
75 |
|
---|
76 | def testIllegalArg1(self):
|
---|
77 | self.assertRaises(ValueError, lambda:ProgressRounds(10, 11, self.date))
|
---|
78 |
|
---|
79 | def testIllegalArg2(self):
|
---|
80 | self.assertRaises(ValueError, lambda:ProgressRounds(10, -1, self.date))
|
---|
81 |
|
---|
82 | def pastDeadlinetest(self):
|
---|
83 | self.assertFalse(self.pr.isPastDeadline(self.start));
|
---|
84 | self.assertTrue(self.pr.isPastDeadline(self.start + self.TESTTIME + 1))
|
---|
85 | self.assertFalse(self.pr.isPastDeadline(self.start + self.TESTTIME - 1))
|
---|
86 | self.assertTrue(ProgressRounds(self.TESTROUNDS, self.TESTROUNDS, self.end)
|
---|
87 | .isPastDeadline(self.start + self.TESTTIME + 1))
|
---|
88 |
|
---|
89 | def terminationTimetest(self):
|
---|
90 | self.assertEquals(self.start + self.TESTTIME, self.pr.getTerminationTime().getTime())
|
---|
91 |
|
---|
92 | def gettest(self):
|
---|
93 | # time does not matter for rounds-based deadline
|
---|
94 | self.assertEqual(0.4, self.pr.get(self.start));
|
---|
95 | self.assertEqual(0.4, self.pr.get(self.start + self.TESTTIME + 1))
|
---|
96 | self.assertEqual(0.4, self.pr.get(self.start + self.TESTTIME))
|
---|
97 | self.assertEqual(0.4, self.pr.get(self.start + self.TESTTIME / 2))
|
---|
98 | self.assertEqual(0.4, self.pr.get(self.start - 10))
|
---|
99 | self.assertEqual(0.4, self.pr.get(self.start + self.TESTTIME + 10))
|
---|
100 |
|
---|
101 | self.assertEqual(0.1,
|
---|
102 | ProgressRounds(self.TESTROUNDS, 1, self.end).get(self.start))
|
---|
103 | self.assertEqual(1,
|
---|
104 | ProgressRounds(self.TESTROUNDS, self.TESTROUNDS, self.end).get(self.start));
|
---|
105 |
|
---|
106 | def totalRoundstest(self):
|
---|
107 | self.assertEqual(10, self.pr.getTotalRounds())
|
---|