-
Notifications
You must be signed in to change notification settings - Fork 55
/
test_time_in_draft.py
108 lines (89 loc) · 3.47 KB
/
test_time_in_draft.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""A test suite for the measure_time_in_draft function."""
import unittest
from datetime import datetime, timedelta
from unittest.mock import MagicMock
import pytz
from time_in_draft import get_stats_time_in_draft, measure_time_in_draft
class TestMeasureTimeInDraft(unittest.TestCase):
"""
Unit tests for the measure_time_in_draft function.
"""
def setUp(self):
"""
Setup common test data and mocks.
"""
self.issue = MagicMock()
self.issue.issue.created_at = datetime(2021, 1, 1, tzinfo=pytz.utc)
self.issue.issue.state = "open"
def test_time_in_draft_with_ready_for_review(self):
"""
Test measure_time_in_draft when ready_for_review_at is provided.
"""
ready_for_review_at = datetime(2021, 1, 3, tzinfo=pytz.utc)
result = measure_time_in_draft(self.issue, ready_for_review_at)
expected = timedelta(days=2)
self.assertEqual(result, expected, "The time in draft should be 2 days.")
def test_time_in_draft_without_ready_for_review(self):
"""
Test measure_time_in_draft when ready_for_review_at is not provided and issue is still open.
"""
now = datetime(2021, 1, 4, tzinfo=pytz.utc)
with unittest.mock.patch("time_in_draft.datetime") as mock_datetime:
mock_datetime.now.return_value = now
result = measure_time_in_draft(self.issue, None)
expected = timedelta(days=3)
self.assertEqual(result, expected, "The time in draft should be 3 days.")
def test_time_in_draft_without_ready_for_review_and_closed(self):
"""
Test measure_time_in_draft when ready_for_review_at is not provided and issue is closed.
"""
self.issue.issue.state = "closed"
result = measure_time_in_draft(self.issue, None)
self.assertIsNone(
result, "The result should be None when draft was never used."
)
class TestGetStatsTimeInDraft(unittest.TestCase):
"""
Unit tests for the get_stats_time_in_draft function.
"""
def test_get_stats_time_in_draft_with_data(self):
"""
Test get_stats_time_in_draft with valid draft times.
"""
issues = [
MagicMock(time_in_draft=timedelta(days=1)),
MagicMock(time_in_draft=timedelta(days=2)),
MagicMock(time_in_draft=timedelta(days=3)),
]
result = get_stats_time_in_draft(issues)
expected = {
"avg": timedelta(days=2),
"med": timedelta(days=2),
"90p": timedelta(days=2, seconds=69120),
}
self.assertEqual(
result, expected, "The statistics for time in draft are incorrect."
)
def test_get_stats_time_in_draft_no_data(self):
"""
Test get_stats_time_in_draft with no draft times.
"""
issues = [
MagicMock(time_in_draft=None),
MagicMock(time_in_draft=None),
]
result = get_stats_time_in_draft(issues)
self.assertIsNone(
result, "The result should be None when there are no draft times."
)
def test_get_stats_time_in_draft_empty_list(self):
"""
Test get_stats_time_in_draft with an empty list of issues.
"""
issues = []
result = get_stats_time_in_draft(issues)
self.assertIsNone(
result, "The result should be None when the list of issues is empty."
)
if __name__ == "__main__":
unittest.main()