|
1 | 1 | from unittest import TestCase, main |
| 2 | +import logging |
2 | 3 | import os |
3 | 4 | import sys |
4 | 5 |
|
5 | 6 | from tempoapiclient.client_v4 import Tempo |
6 | 7 |
|
7 | 8 | # please set TEMPO_AUTH_TOKEN to environment before running this test |
8 | 9 |
|
| 10 | +log = logging.getLogger() |
| 11 | + |
9 | 12 |
|
10 | 13 | class TestClient(TestCase): |
11 | 14 |
|
12 | 15 | def setUp(self): |
13 | 16 | self.tempo = Tempo(auth_token=os.environ.get('TEMPO_AUTH_TOKEN')) |
14 | 17 | self.dateFrom = "2020-09-01" |
15 | 18 | self.dateTo = "2020-10-01" |
| 19 | + |
| 20 | + self.accountId = os.environ.get('TEMPO_ACCOUNT_ID') |
| 21 | + self.issueId = os.environ.get('TEMPO_ISSUE_ID') |
| 22 | + |
16 | 23 |
|
17 | 24 | def test_client_creation(self): |
18 | 25 | self.assertTrue(isinstance(self.tempo, Tempo)) |
@@ -59,6 +66,135 @@ def test_get_team_members(self): |
59 | 66 | self.assertIsInstance(team_members[0], dict) |
60 | 67 | self.assertIsInstance(team_members[0]['member'], dict) |
61 | 68 |
|
| 69 | + @staticmethod |
| 70 | + def _compare_worklog_to_parameters(worklog, parameters): |
| 71 | + """ |
| 72 | + Compares worklog values to parameters of create_worklog and update_worklog methods |
| 73 | +
|
| 74 | + :param worklog: Worklog returned from Tempo |
| 75 | + :param parameters: parameters for create_worklog and update_worklog methods |
| 76 | + """ |
| 77 | + return ( |
| 78 | + all(( |
| 79 | + worklog[keys[0]] == parameters[keys[1]] for keys in ( |
| 80 | + ("startDate", "dateFrom", ), |
| 81 | + ("timeSpentSeconds", "timeSpentSeconds", ), |
| 82 | + ("description", "description", ), |
| 83 | + ) |
| 84 | + )) |
| 85 | + and |
| 86 | + worklog["author"]["accountId"] == parameters["accountId"] |
| 87 | + ) |
| 88 | + |
| 89 | + @staticmethod |
| 90 | + def _compare_worklogs(worklog_a, worklog_b): |
| 91 | + """ |
| 92 | + Compares values of two worklogs together |
| 93 | + """ |
| 94 | + return all(( |
| 95 | + worklog_a[key] == worklog_b[key] for key in ( |
| 96 | + "tempoWorklogId", "author", "issue", "startDate", |
| 97 | + "timeSpentSeconds", "description", ) |
| 98 | + )) |
| 99 | + |
| 100 | + def test_worklog_cycle(self): |
| 101 | + """ |
| 102 | + Tests worklog creation, read, update and deletion |
| 103 | +
|
| 104 | + This test creates a new worklog, reads it, updated it and finally deletes it. |
| 105 | + Thus, you may not want to run this test in a production environment. |
| 106 | +
|
| 107 | + The following environments are required in order to run this test: |
| 108 | + * TEMPO_ACCOUNT_ID: the Jira account id to be set as the author of the worklog |
| 109 | + * TEMPO_ISSUE_ID: the Jira issue id the Worklog is created |
| 110 | + """ |
| 111 | + |
| 112 | + # Test worklog creation (and read) |
| 113 | + create_parameters = { |
| 114 | + "accountId": self.accountId, |
| 115 | + "issueId": self.issueId, |
| 116 | + "dateFrom": self.dateFrom, |
| 117 | + "timeSpentSeconds": 7200, |
| 118 | + "description": "TEST worklog", |
| 119 | + } |
| 120 | + |
| 121 | + created_worklog = self.tempo.create_worklog(**create_parameters) |
| 122 | + |
| 123 | + log.debug("created_worklog: %r", created_worklog) |
| 124 | + print(f"test_worklog_cycle: created worklog with ID: " |
| 125 | + f"{created_worklog.get('tempoWorklogId')}") |
| 126 | + |
| 127 | + self.assertIsInstance(created_worklog, dict) |
| 128 | + self.assertEqual(created_worklog["issue"]["id"], int(create_parameters["issueId"])) |
| 129 | + self.assertTrue(self._compare_worklog_to_parameters(created_worklog, |
| 130 | + create_parameters)) |
| 131 | + |
| 132 | + read_created_worklog = self.tempo.get_worklogs( |
| 133 | + self.dateFrom, self.dateFrom, |
| 134 | + worklogId=created_worklog.get('tempoWorklogId') |
| 135 | + ) |
| 136 | + log.debug("read_created_worklog: %r", read_created_worklog) |
| 137 | + |
| 138 | + self.assertIsInstance(read_created_worklog, dict) |
| 139 | + self.assertTrue(self._compare_worklogs(created_worklog, read_created_worklog)) |
| 140 | + |
| 141 | + # Test worklog update (and read) |
| 142 | + update_parameters = { |
| 143 | + "id": created_worklog["tempoWorklogId"], |
| 144 | + "accountId": self.accountId, |
| 145 | + "dateFrom": self.dateFrom, |
| 146 | + "timeSpentSeconds": 10800, |
| 147 | + "description": "Updated TEST worklog", |
| 148 | + } |
| 149 | + |
| 150 | + updated_worklog = self.tempo.update_worklog(**update_parameters) |
| 151 | + |
| 152 | + log.debug("updated_worklog: %r", updated_worklog) |
| 153 | + print(f"test_worklog_cycle: updated worklog with ID: " |
| 154 | + f"{updated_worklog.get('tempoWorklogId')}") |
| 155 | + |
| 156 | + self.assertIsInstance(updated_worklog, dict) |
| 157 | + self.assertEqual(created_worklog["tempoWorklogId"], updated_worklog["tempoWorklogId"]) |
| 158 | + self.assertTrue(self._compare_worklog_to_parameters(updated_worklog, |
| 159 | + update_parameters)) |
| 160 | + |
| 161 | + read_updated_worklog = self.tempo.get_worklogs( |
| 162 | + self.dateFrom, self.dateFrom, |
| 163 | + worklogId=created_worklog.get('tempoWorklogId') |
| 164 | + ) |
| 165 | + log.debug("read_updated_worklog: %r", read_updated_worklog) |
| 166 | + |
| 167 | + self.assertIsInstance(read_updated_worklog, dict) |
| 168 | + self.assertTrue(self._compare_worklogs(updated_worklog, read_updated_worklog)) |
| 169 | + |
| 170 | + # Test worklog deletion |
| 171 | + deleted_worklog = self.tempo.delete_worklog(created_worklog['tempoWorklogId']) |
| 172 | + |
| 173 | + log.debug("deleted_worklog: %r", deleted_worklog) |
| 174 | + |
| 175 | + self.assertIsInstance(deleted_worklog, dict) |
| 176 | + self.assertFalse(deleted_worklog) |
| 177 | + |
| 178 | + # The worklog should not exist anymore |
| 179 | + with self.assertRaises(SystemExit) as exc: |
| 180 | + read_deleted_worklog = self.tempo.get_worklogs( |
| 181 | + self.dateFrom, self.dateFrom, |
| 182 | + worklogId=created_worklog.get('tempoWorklogId') |
| 183 | + ) |
| 184 | + |
| 185 | + print(f"test_worklog_cycle: deleted worklog with ID: {created_worklog.get('tempoWorklogId')}") |
| 186 | + |
| 187 | + def test_get_worklogs(self): |
| 188 | + """ |
| 189 | + Tests reading worklogs |
| 190 | + """ |
| 191 | + worklogs = self.tempo.get_worklogs(self.dateFrom, self.dateTo) |
| 192 | + |
| 193 | + log.debug("worklogs: %r", worklogs) |
| 194 | + print(f"get_worklogs: {len(worklogs)}") |
| 195 | + |
| 196 | + self.assertIsInstance(worklogs, list) |
| 197 | + |
62 | 198 | #def test_get_team_memberships(self): |
63 | 199 | # l = self.tempo.get_team_memberships(membershipId=) |
64 | 200 | # display(l) |
|
0 commit comments