forked from github/issue-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_time_to_answer.py
125 lines (98 loc) · 3.72 KB
/
test_time_to_answer.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""This module provides unit tests for the time_to_answer module."""
import unittest
from datetime import timedelta
from classes import IssueWithMetrics
from time_to_answer import get_stats_time_to_answer, measure_time_to_answer
class TestGetAverageTimeToAnswer(unittest.TestCase):
"""A test case for the get_stats_time_to_answer function.
This test case includes three test methods:
- test_returns_none_for_empty_list
- test_returns_none_for_list_with_no_time_to_answer
- test_returns_stats_time_to_answer
"""
def test_returns_none_for_empty_list(self):
"""Tests that the function returns None when given an empty list of issues."""
# Arrange
issues_with_metrics = []
# Act
result = get_stats_time_to_answer(issues_with_metrics)
# Assert
self.assertIsNone(result)
def test_returns_none_for_list_with_no_time_to_answer(self):
"""
Tests that the function returns None when given a list of
issues with no time to answer.
"""
# Arrange
issues_with_metrics = [
IssueWithMetrics("issue1", None, None),
IssueWithMetrics("issue2", None, None),
]
# Act
result = get_stats_time_to_answer(issues_with_metrics)
# Assert
self.assertIsNone(result)
def test_returns_stats_time_to_answer(self):
"""
Tests that the function correctly calculates the average
time to answer for a list of issues with time to answer.
"""
# Arrange
issues_with_metrics = [
IssueWithMetrics(
"issue1", "url1", "alice", None, None, timedelta(seconds=10)
),
IssueWithMetrics(
"issue2", "url2", "bob", None, None, timedelta(seconds=20)
),
IssueWithMetrics(
"issue3", "url3", "carol", None, None, timedelta(seconds=30)
),
]
# Act
result = get_stats_time_to_answer(issues_with_metrics)["avg"]
# Assert
self.assertEqual(result, timedelta(seconds=20))
class TestMeasureTimeToAnswer(unittest.TestCase):
"""A test case for the measure_time_to_answer function.
This test case includes three test methods:
- test_returns_none_if_answer_chosen_at_is_missing
- test_returns_none_if_created_at_is_missing
- test_returns_time_to_answer
"""
def test_returns_none_if_answer_chosen_at_is_missing(self):
"""
Tests that the function returns None when the answerChosenAt
field is missing from the discussion object.
"""
# Arrange
discussion = {"createdAt": "2022-01-01T00:00:00Z", "answerChosenAt": None}
# Act
result = measure_time_to_answer(discussion)
# Assert
self.assertIsNone(result)
def test_returns_none_if_created_at_is_missing(self):
"""
Tests that the function returns None when the createdAt
field is missing from the discussion object.
"""
# Arrange
discussion = {"createdAt": None, "answerChosenAt": "2022-01-01T00:00:00Z"}
# Act
result = measure_time_to_answer(discussion)
# Assert
self.assertIsNone(result)
def test_returns_time_to_answer(self):
"""
Tests that the function correctly calculates the time to answer for
a discussion object with both createdAt and answerChosenAt fields.
"""
# Arrange
discussion = {
"createdAt": "2022-01-01T00:00:00Z",
"answerChosenAt": "2022-01-01T00:01:00Z",
}
# Act
result = measure_time_to_answer(discussion)
# Assert
self.assertEqual(result, timedelta(minutes=1))