forked from github/issue-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_time_to_close.py
109 lines (91 loc) · 3.96 KB
/
test_time_to_close.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
109
"""A module containing unit tests for the time_to_close module.
This module contains unit tests for the measure_time_to_close and
get_stats_time_to_close functions in the time_to_close module.
The tests use mock GitHub issues to test the functions' behavior.
Classes:
TestMeasureTimeToClose: A class to test the measure_time_to_close function.
TestGetStatsTimeToClose: A class to test the get_stats_time_to_close function.
"""
from datetime import timedelta
import unittest
from unittest.mock import MagicMock
from classes import IssueWithMetrics
from time_to_close import get_stats_time_to_close, measure_time_to_close
class TestGetAverageTimeToClose(unittest.TestCase):
"""Test suite for the get_stats_time_to_close function."""
def test_get_stats_time_to_close(self):
"""Test that the function correctly calculates the average time to close."""
# Create mock data
issues_with_metrics = [
IssueWithMetrics(
"Issue 1",
"https://github.com/user/repo/issues/1",
"alice",
None,
timedelta(days=2),
),
IssueWithMetrics(
"Issue 2",
"https://github.com/user/repo/issues/2",
"bob",
None,
timedelta(days=4),
),
IssueWithMetrics(
"Issue 3", "https://github.com/user/repo/issues/3", "carol", None, None
),
]
# Call the function and check the result
result = get_stats_time_to_close(issues_with_metrics)['avg']
expected_result = timedelta(days=3)
self.assertEqual(result, expected_result)
def test_get_stats_time_to_close_no_issues(self):
"""Test that the function returns None if there are no issues with time to close."""
# Create mock data
issues_with_metrics = [
IssueWithMetrics(
"Issue 1", "https://github.com/user/repo/issues/1", "alice", None, None
),
IssueWithMetrics(
"Issue 2", "https://github.com/user/repo/issues/2", "bob", None, None
),
IssueWithMetrics(
"Issue 3", "https://github.com/user/repo/issues/3", "carol", None, None
),
]
# Call the function and check the result
result = get_stats_time_to_close(issues_with_metrics)
expected_result = None
self.assertEqual(result, expected_result)
class TestMeasureTimeToClose(unittest.TestCase):
"""Test suite for the measure_time_to_close function."""
def test_measure_time_to_close(self):
"""Test that the function correctly measures the time to close an issue."""
# Create a mock issue object
issue = MagicMock()
issue.state = "closed"
issue.created_at = "2021-01-01T00:00:00Z"
issue.closed_at = "2021-01-03T00:00:00Z"
# Call the function and check the result
result = measure_time_to_close(issue, None)
expected_result = timedelta(days=2)
self.assertEqual(result, expected_result)
def test_measure_time_to_close_returns_none(self):
"""Test that the function returns None if the issue is not closed."""
# Create a mock issue object
issue = MagicMock()
issue.state = "open"
# Call the function and check that it returns None
self.assertEqual(None, measure_time_to_close(issue, None))
def test_measure_time_to_close_discussion(self):
"""
Test that the function correctly measures the time to close for a discussion.
"""
# Create an issue dictionary with createdAt and closedAt fields
issue = {}
issue["createdAt"] = "2021-01-01T00:00:00Z"
issue["closedAt"] = "2021-01-03T00:00:00Z"
# Call the function and check the result
result = measure_time_to_close(None, issue)
expected_result = timedelta(days=2)
self.assertEqual(result, expected_result)