diff --git a/labels.py b/labels.py index 2bfcab3..9b4e4df 100644 --- a/labels.py +++ b/labels.py @@ -74,7 +74,7 @@ def get_label_metrics(issue: github3.issues.Issue, labels: List[str]) -> dict: for label in labels: # if the label is still on there, add the time from the last event to now - if label in labeled and label not in unlabeled: + if label in labeled: # if the issue is closed, add the time from the issue creation to the closed_at time if issue.state == "closed": label_metrics[label] += datetime.fromisoformat( diff --git a/test_labels.py b/test_labels.py index 26f696f..d5b6d43 100644 --- a/test_labels.py +++ b/test_labels.py @@ -16,7 +16,7 @@ class TestLabels(unittest.TestCase): def setUp(self): self.issue = MagicMock() # type: ignore self.issue.issue = MagicMock(spec=github3.issues.Issue) # type: ignore - self.issue.created_at = "2020-01-01T00:00:00Z" + self.issue.created_at = "2021-01-01T00:00:00Z" self.issue.closed_at = "2021-01-05T00:00:00Z" self.issue.state = "closed" self.issue.issue.events.return_value = [ @@ -35,21 +35,27 @@ def setUp(self): label={"name": "bug"}, created_at=datetime(2021, 1, 3, tzinfo=pytz.UTC), ), + MagicMock( + event="labeled", + label={"name": "bug"}, + created_at=datetime(2021, 1, 4, tzinfo=pytz.UTC), + ), ] def test_get_label_events(self): """Test get_label_events""" labels = ["bug"] events = get_label_events(self.issue, labels) - self.assertEqual(len(events), 2) + self.assertEqual(len(events), 3) self.assertEqual(events[0].label["name"], "bug") self.assertEqual(events[1].label["name"], "bug") + self.assertEqual(events[2].label["name"], "bug") def test_get_label_metrics_closed_issue(self): """Test get_label_metrics using a closed issue""" labels = ["bug", "feature"] metrics = get_label_metrics(self.issue, labels) - self.assertEqual(metrics["bug"], timedelta(days=2)) + self.assertEqual(metrics["bug"], timedelta(days=3)) self.assertEqual(metrics["feature"], timedelta(days=3)) def test_get_label_metrics_open_issue(self): @@ -57,7 +63,14 @@ def test_get_label_metrics_open_issue(self): self.issue.state = "open" labels = ["bug", "feature"] metrics = get_label_metrics(self.issue, labels) - self.assertEqual(metrics["bug"], timedelta(days=2)) + self.assertLessEqual( + metrics["bug"], + datetime.now(pytz.utc) - datetime(2021, 1, 2, tzinfo=pytz.UTC), + ) + self.assertGreater( + metrics["bug"], + datetime.now(pytz.utc) - datetime(2021, 1, 3, tzinfo=pytz.UTC), + ) self.assertLessEqual( metrics["feature"], datetime.now(pytz.utc) - datetime(2021, 1, 2, tzinfo=pytz.UTC),