|
| 1 | +"""This module provides unit tests for the time_to_answer module.""" |
| 2 | + |
| 3 | +import unittest |
| 4 | +from datetime import timedelta |
| 5 | +from typing import List |
| 6 | + |
| 7 | +from classes import IssueWithMetrics |
| 8 | +from time_to_answer import get_average_time_to_answer, measure_time_to_answer |
| 9 | + |
| 10 | + |
| 11 | +class TestGetAverageTimeToAnswer(unittest.TestCase): |
| 12 | + """A test case for the get_average_time_to_answer function. |
| 13 | +
|
| 14 | + This test case includes three test methods: |
| 15 | + - test_returns_none_for_empty_list |
| 16 | + - test_returns_none_for_list_with_no_time_to_answer |
| 17 | + - test_returns_average_time_to_answer |
| 18 | + """ |
| 19 | + |
| 20 | + def test_returns_none_for_empty_list(self): |
| 21 | + """Tests that the function returns None when given an empty list of issues.""" |
| 22 | + # Arrange |
| 23 | + issues_with_metrics: List[IssueWithMetrics] = [] |
| 24 | + |
| 25 | + # Act |
| 26 | + result = get_average_time_to_answer(issues_with_metrics) |
| 27 | + |
| 28 | + # Assert |
| 29 | + self.assertIsNone(result) |
| 30 | + |
| 31 | + def test_returns_none_for_list_with_no_time_to_answer(self): |
| 32 | + """ |
| 33 | + Tests that the function returns None when given a list of |
| 34 | + issues with no time to answer. |
| 35 | + """ |
| 36 | + # Arrange |
| 37 | + issues_with_metrics = [ |
| 38 | + IssueWithMetrics("issue1", None), |
| 39 | + IssueWithMetrics("issue2", None), |
| 40 | + ] |
| 41 | + |
| 42 | + # Act |
| 43 | + result = get_average_time_to_answer(issues_with_metrics) |
| 44 | + |
| 45 | + # Assert |
| 46 | + self.assertIsNone(result) |
| 47 | + |
| 48 | + def test_returns_average_time_to_answer(self): |
| 49 | + """ |
| 50 | + Tests that the function correctly calculates the average |
| 51 | + time to answer for a list of issues with time to answer. |
| 52 | + """ |
| 53 | + |
| 54 | + # Arrange |
| 55 | + issues_with_metrics = [ |
| 56 | + IssueWithMetrics("issue1", "url1", None, None, timedelta(seconds=10)), |
| 57 | + IssueWithMetrics("issue2", "url2", None, None, timedelta(seconds=20)), |
| 58 | + IssueWithMetrics("issue3", "url3", None, None, timedelta(seconds=30)), |
| 59 | + ] |
| 60 | + |
| 61 | + # Act |
| 62 | + result = get_average_time_to_answer(issues_with_metrics) |
| 63 | + |
| 64 | + # Assert |
| 65 | + self.assertEqual(result, timedelta(seconds=20)) |
| 66 | + |
| 67 | + |
| 68 | +class TestMeasureTimeToAnswer(unittest.TestCase): |
| 69 | + """A test case for the measure_time_to_answer function. |
| 70 | +
|
| 71 | + This test case includes three test methods: |
| 72 | + - test_returns_none_if_answer_chosen_at_is_missing |
| 73 | + - test_returns_none_if_created_at_is_missing |
| 74 | + - test_returns_time_to_answer |
| 75 | + """ |
| 76 | + |
| 77 | + def test_returns_none_if_answer_chosen_at_is_missing(self): |
| 78 | + """ |
| 79 | + Tests that the function returns None when the answerChosenAt |
| 80 | + field is missing from the discussion object. |
| 81 | + """ |
| 82 | + # Arrange |
| 83 | + discussion = {"createdAt": "2022-01-01T00:00:00Z", "answerChosenAt": None} |
| 84 | + |
| 85 | + # Act |
| 86 | + result = measure_time_to_answer(discussion) |
| 87 | + |
| 88 | + # Assert |
| 89 | + self.assertIsNone(result) |
| 90 | + |
| 91 | + def test_returns_none_if_created_at_is_missing(self): |
| 92 | + """ |
| 93 | + Tests that the function returns None when the createdAt |
| 94 | + field is missing from the discussion object. |
| 95 | + """ |
| 96 | + # Arrange |
| 97 | + discussion = {"createdAt": None, "answerChosenAt": "2022-01-01T00:00:00Z"} |
| 98 | + |
| 99 | + # Act |
| 100 | + result = measure_time_to_answer(discussion) |
| 101 | + |
| 102 | + # Assert |
| 103 | + self.assertIsNone(result) |
| 104 | + |
| 105 | + def test_returns_time_to_answer(self): |
| 106 | + """ |
| 107 | + Tests that the function correctly calculates the time to answer for |
| 108 | + a discussion object with both createdAt and answerChosenAt fields. |
| 109 | + """ |
| 110 | + # Arrange |
| 111 | + discussion = { |
| 112 | + "createdAt": "2022-01-01T00:00:00Z", |
| 113 | + "answerChosenAt": "2022-01-01T00:01:00Z", |
| 114 | + } |
| 115 | + |
| 116 | + # Act |
| 117 | + result = measure_time_to_answer(discussion) |
| 118 | + |
| 119 | + # Assert |
| 120 | + self.assertEqual(result, timedelta(minutes=1)) |
0 commit comments