|
| 1 | +"""Test assignee functionality added to issue metrics.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import unittest |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +from classes import IssueWithMetrics |
| 8 | +from markdown_writer import get_non_hidden_columns |
| 9 | + |
| 10 | + |
| 11 | +class TestAssigneeFunctionality(unittest.TestCase): |
| 12 | + """Test suite for the assignee functionality.""" |
| 13 | + |
| 14 | + @patch.dict( |
| 15 | + os.environ, |
| 16 | + { |
| 17 | + "GH_TOKEN": "test_token", |
| 18 | + "SEARCH_QUERY": "is:issue is:open repo:user/repo", |
| 19 | + "HIDE_ASSIGNEE": "false", |
| 20 | + "HIDE_AUTHOR": "false", |
| 21 | + }, |
| 22 | + clear=True, |
| 23 | + ) |
| 24 | + def test_get_non_hidden_columns_includes_assignee_by_default(self): |
| 25 | + """Test that assignee column is included by default.""" |
| 26 | + columns = get_non_hidden_columns(labels=None) |
| 27 | + self.assertIn("Assignee", columns) |
| 28 | + self.assertIn("Author", columns) |
| 29 | + |
| 30 | + @patch.dict( |
| 31 | + os.environ, |
| 32 | + { |
| 33 | + "GH_TOKEN": "test_token", |
| 34 | + "SEARCH_QUERY": "is:issue is:open repo:user/repo", |
| 35 | + "HIDE_ASSIGNEE": "true", |
| 36 | + "HIDE_AUTHOR": "false", |
| 37 | + }, |
| 38 | + clear=True, |
| 39 | + ) |
| 40 | + def test_get_non_hidden_columns_hides_assignee_when_env_set(self): |
| 41 | + """Test that assignee column is hidden when HIDE_ASSIGNEE is true.""" |
| 42 | + columns = get_non_hidden_columns(labels=None) |
| 43 | + self.assertNotIn("Assignee", columns) |
| 44 | + self.assertIn("Author", columns) |
| 45 | + |
| 46 | + @patch.dict( |
| 47 | + os.environ, |
| 48 | + { |
| 49 | + "GH_TOKEN": "test_token", |
| 50 | + "SEARCH_QUERY": "is:issue is:open repo:user/repo", |
| 51 | + "HIDE_ASSIGNEE": "false", |
| 52 | + "HIDE_AUTHOR": "true", |
| 53 | + }, |
| 54 | + clear=True, |
| 55 | + ) |
| 56 | + def test_get_non_hidden_columns_shows_assignee_but_hides_author(self): |
| 57 | + """Test that assignee can be shown while author is hidden.""" |
| 58 | + columns = get_non_hidden_columns(labels=None) |
| 59 | + self.assertIn("Assignee", columns) |
| 60 | + self.assertNotIn("Author", columns) |
| 61 | + |
| 62 | + @patch.dict( |
| 63 | + os.environ, |
| 64 | + { |
| 65 | + "GH_TOKEN": "test_token", |
| 66 | + "SEARCH_QUERY": "is:issue is:open repo:user/repo", |
| 67 | + "HIDE_ASSIGNEE": "true", |
| 68 | + "HIDE_AUTHOR": "true", |
| 69 | + }, |
| 70 | + clear=True, |
| 71 | + ) |
| 72 | + def test_get_non_hidden_columns_hides_both_assignee_and_author(self): |
| 73 | + """Test that both assignee and author can be hidden.""" |
| 74 | + columns = get_non_hidden_columns(labels=None) |
| 75 | + self.assertNotIn("Assignee", columns) |
| 76 | + self.assertNotIn("Author", columns) |
| 77 | + |
| 78 | + def test_assignee_column_position(self): |
| 79 | + """Test that assignee column appears before author column.""" |
| 80 | + with patch.dict( |
| 81 | + os.environ, |
| 82 | + { |
| 83 | + "GH_TOKEN": "test_token", |
| 84 | + "SEARCH_QUERY": "is:issue is:open repo:user/repo", |
| 85 | + "HIDE_ASSIGNEE": "false", |
| 86 | + "HIDE_AUTHOR": "false", |
| 87 | + }, |
| 88 | + clear=True, |
| 89 | + ): |
| 90 | + columns = get_non_hidden_columns(labels=None) |
| 91 | + assignee_index = columns.index("Assignee") |
| 92 | + author_index = columns.index("Author") |
| 93 | + self.assertLess( |
| 94 | + assignee_index, |
| 95 | + author_index, |
| 96 | + "Assignee column should appear before Author column", |
| 97 | + ) |
| 98 | + |
| 99 | + def test_multiple_assignees_rendering_logic(self): |
| 100 | + """Test that multiple assignees are rendered correctly in assignee column.""" |
| 101 | + |
| 102 | + # Test the assignee rendering logic directly |
| 103 | + endpoint = "github.com" |
| 104 | + columns = ["Title", "URL", "Assignee", "Author"] |
| 105 | + |
| 106 | + # Initialize variables |
| 107 | + multiple_output = "" |
| 108 | + single_output = "" |
| 109 | + none_output = "" |
| 110 | + |
| 111 | + # Test case 1: Multiple assignees |
| 112 | + issue_multiple = IssueWithMetrics( |
| 113 | + title="Test Issue with Multiple Assignees", |
| 114 | + html_url="https://github.com/test/repo/issues/1", |
| 115 | + author="testuser", |
| 116 | + assignee="alice", |
| 117 | + assignees=["alice", "bob", "charlie"], |
| 118 | + ) |
| 119 | + |
| 120 | + # Simulate the new rendering logic |
| 121 | + if "Assignee" in columns: |
| 122 | + if issue_multiple.assignees: |
| 123 | + assignee_links = [ |
| 124 | + f"[{assignee}](https://{endpoint}/{assignee})" |
| 125 | + for assignee in issue_multiple.assignees |
| 126 | + ] |
| 127 | + multiple_output = f" {', '.join(assignee_links)} |" |
| 128 | + else: |
| 129 | + multiple_output = " None |" |
| 130 | + |
| 131 | + expected_multiple = ( |
| 132 | + " [alice](https://github.com/alice), [bob](https://github.com/bob), " |
| 133 | + "[charlie](https://github.com/charlie) |" |
| 134 | + ) |
| 135 | + self.assertEqual( |
| 136 | + multiple_output, |
| 137 | + expected_multiple, |
| 138 | + "Multiple assignees should be rendered as comma-separated links", |
| 139 | + ) |
| 140 | + |
| 141 | + # Test case 2: Single assignee |
| 142 | + issue_single = IssueWithMetrics( |
| 143 | + title="Test Issue with Single Assignee", |
| 144 | + html_url="https://github.com/test/repo/issues/2", |
| 145 | + author="testuser", |
| 146 | + assignee="alice", |
| 147 | + assignees=["alice"], |
| 148 | + ) |
| 149 | + |
| 150 | + if "Assignee" in columns: |
| 151 | + if issue_single.assignees: |
| 152 | + assignee_links = [ |
| 153 | + f"[{assignee}](https://{endpoint}/{assignee})" |
| 154 | + for assignee in issue_single.assignees |
| 155 | + ] |
| 156 | + single_output = f" {', '.join(assignee_links)} |" |
| 157 | + else: |
| 158 | + single_output = " None |" |
| 159 | + |
| 160 | + expected_single = " [alice](https://github.com/alice) |" |
| 161 | + self.assertEqual( |
| 162 | + single_output, |
| 163 | + expected_single, |
| 164 | + "Single assignee should be rendered as a single link", |
| 165 | + ) |
| 166 | + |
| 167 | + # Test case 3: No assignees |
| 168 | + issue_none = IssueWithMetrics( |
| 169 | + title="Test Issue with No Assignees", |
| 170 | + html_url="https://github.com/test/repo/issues/3", |
| 171 | + author="testuser", |
| 172 | + assignee=None, |
| 173 | + assignees=[], |
| 174 | + ) |
| 175 | + |
| 176 | + if "Assignee" in columns: |
| 177 | + if issue_none.assignees: |
| 178 | + assignee_links = [ |
| 179 | + f"[{assignee}](https://{endpoint}/{assignee})" |
| 180 | + for assignee in issue_none.assignees |
| 181 | + ] |
| 182 | + none_output = f" {', '.join(assignee_links)} |" |
| 183 | + else: |
| 184 | + none_output = " None |" |
| 185 | + |
| 186 | + expected_none = " None |" |
| 187 | + self.assertEqual( |
| 188 | + none_output, expected_none, "No assignees should be rendered as 'None'" |
| 189 | + ) |
| 190 | + |
| 191 | + print(f"✅ Multiple assignees test: {expected_multiple}") |
| 192 | + print(f"✅ Single assignee test: {expected_single}") |
| 193 | + print(f"✅ No assignees test: {expected_none}") |
| 194 | + |
| 195 | + |
| 196 | +if __name__ == "__main__": |
| 197 | + unittest.main() |
0 commit comments