Coverage for CIResults/tests/test_metrics.py: 100%

93 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-19 09:20 +0000

1from django.test import TestCase 

2 

3from CIResults.models import BugTracker 

4from CIResults.metrics import Periodizer, Period, bugs_followed_since, Rate, Statistics 

5 

6import datetime 

7import pytz 

8 

9 

10class PeriodizerTests(TestCase): 

11 def __common_checks(self, periods): 

12 self.assertEqual(periods[0].start, datetime.datetime(2018, 3, 19, 0, 0)) 

13 self.assertEqual(periods[0].end, datetime.datetime(2018, 3, 26, 0, 0)) 

14 self.assertEqual(periods[-1].start, datetime.datetime(2018, 10, 8, 0, 0)) 

15 self.assertEqual(periods[-1].end, datetime.datetime(2018, 10, 15, 0, 0)) 

16 

17 self.assertEqual(len(periods), 30) 

18 

19 # Check that the time between each day is the expected one 

20 for p, period in enumerate(periods): 

21 self.assertEqual(period.end - period.start, datetime.timedelta(days=7)) 

22 if p > 1: 

23 self.assertEqual(period.start, periods[p-1].end) 

24 

25 def test_monday_midnight(self): 

26 periods = list(Periodizer(end_date=datetime.datetime(year=2018, month=10, day=8, 

27 hour=0, minute=0, second=0))) 

28 self.__common_checks(periods) 

29 

30 def test_wednesday(self): 

31 periods = list(Periodizer(end_date=datetime.datetime(year=2018, month=10, day=10, 

32 hour=14, minute=23, second=10))) 

33 self.__common_checks(periods) 

34 

35 def test_sunday_1s_to_midnight(self): 

36 periods = list(Periodizer(end_date=datetime.datetime(year=2018, month=10, day=14, 

37 hour=23, minute=59, second=59))) 

38 self.__common_checks(periods) 

39 

40 def __from_json(self, json_str, expected, description=None): 

41 period = Periodizer.from_json(json_str) 

42 

43 self.assertEqual(list(period), expected) 

44 if description is not None: 

45 self.assertEqual(period.description, description) 

46 

47 return period 

48 

49 def test_from_json__days(self): 

50 expected = [ 

51 Period(datetime.datetime(2019, 7, 8, 0, 0, tzinfo=pytz.utc), 

52 datetime.datetime(2019, 7, 9, 0, 0, tzinfo=pytz.utc)), 

53 Period(datetime.datetime(2019, 7, 9, 0, 0, tzinfo=pytz.utc), 

54 datetime.datetime(2019, 7, 10, 0, 0, tzinfo=pytz.utc)), 

55 Period(datetime.datetime(2019, 7, 10, 0, 0, tzinfo=pytz.utc), 

56 datetime.datetime(2019, 7, 11, 0, 0, tzinfo=pytz.utc)) 

57 ] 

58 self.__from_json('{"period": "day", "count": 3, "end": "2019-07-10 06:06:06.123456"}', expected, 

59 'last 3 days before 2019-07-11') 

60 

61 def test_from_json__days_from_now(self): 

62 period = Periodizer.from_json('{"period": "day", "count": 3}') 

63 self.assertEqual(len(list(period)), 3) 

64 self.assertNotIn('before', period.description) 

65 

66 def test_from_json__weeks(self): 

67 expected = [ 

68 Period(datetime.datetime(2019, 6, 24, 0, 0, tzinfo=pytz.utc), 

69 datetime.datetime(2019, 7, 1, 0, 0, tzinfo=pytz.utc)), 

70 Period(datetime.datetime(2019, 7, 1, 0, 0, tzinfo=pytz.utc), 

71 datetime.datetime(2019, 7, 8, 0, 0, tzinfo=pytz.utc)), 

72 Period(datetime.datetime(2019, 7, 8, 0, 0, tzinfo=pytz.utc), 

73 datetime.datetime(2019, 7, 15, 0, 0, tzinfo=pytz.utc)), 

74 ] 

75 description = 'last 3 weeks before 2019-07-15 / WW-2019.28' 

76 self.__from_json('{"period": "week", "count": 3, "end": "2019-07-10 06:06:06.123456"}', expected, 

77 description) 

78 self.__from_json('{"period": "invalid", "count": 3, "end": "2019-07-10 06:06:06.123456"}', expected, 

79 description) 

80 self.__from_json('{"count": 3, "end": "2019-07-10 06:06:06.123456"}', expected, description) 

81 

82 def test_from_json__months(self): 

83 expected = [ 

84 Period(datetime.datetime(2019, 5, 1, 0, 0, tzinfo=pytz.utc), 

85 datetime.datetime(2019, 6, 1, 0, 0, tzinfo=pytz.utc)), 

86 Period(datetime.datetime(2019, 6, 1, 0, 0, tzinfo=pytz.utc), 

87 datetime.datetime(2019, 7, 1, 0, 0, tzinfo=pytz.utc)), 

88 Period(datetime.datetime(2019, 7, 1, 0, 0, tzinfo=pytz.utc), 

89 datetime.datetime(2019, 8, 1, 0, 0, tzinfo=pytz.utc)), 

90 ] 

91 self.__from_json('{"period": "month", "count": 3, "end": "2019-07-10 06:06:06.123456"}', expected, 

92 'last 3 months before August 2019') 

93 

94 

95class bugs_followed_sinceTests(TestCase): 

96 def test_no_bugtrackers(self): 

97 self.assertEqual(bugs_followed_since(), None) 

98 

99 def test_one_bugtrackers_without_components_followed_since(self): 

100 BugTracker.objects.create(public=True) 

101 self.assertEqual(bugs_followed_since(), None) 

102 

103 def test_one_bugtrackers_with_components_followed_since(self): 

104 followed_since = datetime.datetime(2019, 5, 1, 0, 0, tzinfo=pytz.utc) 

105 BugTracker.objects.create(public=True, components_followed_since=followed_since) 

106 self.assertEqual(bugs_followed_since(), followed_since) 

107 

108 def test_two_bugtrackers_with_components_followed_since(self): 

109 followed_since1 = datetime.datetime(2019, 5, 1, 0, 0, tzinfo=pytz.utc) 

110 followed_since2 = datetime.datetime(2019, 5, 2, 0, 0, tzinfo=pytz.utc) 

111 

112 BugTracker.objects.create(public=True, name='bt1', components_followed_since=followed_since2) 

113 BugTracker.objects.create(public=True, name='bt2', components_followed_since=followed_since1) 

114 

115 self.assertEqual(bugs_followed_since(), followed_since2) 

116 

117 

118class RateTests(TestCase): 

119 def test_basic(self): 

120 rate = Rate(count=5, total=10) 

121 self.assertEqual(rate.count, 5) 

122 self.assertEqual(rate.total, 10) 

123 self.assertEqual(rate.percent, 50.0) 

124 

125 def test_empty(self): 

126 rate = Rate(count=0, total=0) 

127 self.assertEqual(rate.count, 0) 

128 self.assertEqual(rate.total, 0) 

129 self.assertEqual(rate.percent, 0.0) 

130 

131 

132class StatisticsTests(TestCase): 

133 def test_basic(self): 

134 stats = Statistics('s', [10, 1, 4, -5, 5, 2, 6]) 

135 self.assertEqual(stats.min, -5) 

136 self.assertAlmostEqual(stats.mean, 3.2857, places=3) 

137 self.assertEqual(stats.median, 4) 

138 self.assertEqual(stats.max, 10) 

139 self.assertAlmostEqual(stats.stdev, 4.6802, places=3) 

140 self.assertEqual(str(stats), "3.286 s ±142.4%") 

141 

142 def test_add(self): 

143 stats = Statistics('s', [-1]) 

144 stats.add(0) 

145 stats += 1 

146 

147 self.assertEqual(stats.min, -1) 

148 self.assertEqual(stats.mean, 0) 

149 self.assertEqual(stats.median, 0) 

150 self.assertEqual(stats.max, 1) 

151 self.assertEqual(stats.stdev, 1) 

152 self.assertEqual(str(stats), "0 s")