File indexing completed on 2024-04-28 13:37:13

0001 import sys
0002 import os
0003 import unittest
0004 from io import StringIO
0005 from unittest.mock import MagicMock, patch
0006 
0007 sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../")
0008 
0009 from lab.issue import IssueConnection
0010 
0011 
0012 class MockIssueConnection(IssueConnection):
0013     """
0014     Subclass the original class to be able to override __init__.
0015     Otherwise IssueConnection would try to talk to the API.
0016     """
0017 
0018     def __init__(self, issue):
0019         self.issue = issue
0020 
0021 
0022 class IssueTestCase(unittest.TestCase):
0023 
0024     def test_print_estimate(self):
0025         """
0026         Tests that the output for some sample data matches the expectation.
0027         """
0028         with patch("sys.stdout", new=StringIO()) as mock_stdout:
0029             mock_issue = MagicMock()
0030             mock_issue.title = "Fancy Title"
0031             mock_issue.attributes = {'time_stats': {
0032                 'time_estimate': 520,
0033                 'total_time_spent': 600,
0034                 'human_time_estimate': '9m',
0035                 'human_total_time_spent': '10m',
0036             }}
0037             issue = MockIssueConnection(mock_issue)
0038             issue.print_spent()
0039 
0040             mock_stdout.seek(0)
0041             self.assertEqual(
0042                 mock_stdout.read(),
0043                 '\x1b[1mFancy Title\x1b[0m has 10m tracked (estimated: \x1b[0;31m9m\x1b[0m)\n'
0044             )
0045 
0046     def test_print_spent(self):
0047         """
0048         Tests that the output for some sample data matches the expectation.
0049         """
0050         with patch("sys.stdout", new=StringIO()) as mock_stdout:
0051             mock_issue = MagicMock()
0052             mock_issue.title = "Fancy Title"
0053             mock_issue.attributes = {'time_stats': {
0054                 'time_estimate': 28800,
0055                 'total_time_spent': 25200,
0056                 'human_time_estimate': '8h',
0057                 'human_total_time_spent': '7h',
0058             }}
0059             issue = MockIssueConnection(mock_issue)
0060             issue.print_spent()
0061 
0062             mock_stdout.seek(0)
0063             self.assertEqual(
0064                 mock_stdout.read(),
0065                 '\x1b[1mFancy Title\x1b[0m has 7h tracked (estimated: \x1b[0;32m8h\x1b[0m)\n'
0066             )