|
| 1 | +import unittest |
| 2 | +from v2_utils import remove_unmatched_tags |
| 3 | +from app import app |
| 4 | +import json,random |
| 5 | + |
| 6 | + |
| 7 | +class CustomTestResult(unittest.TextTestResult): |
| 8 | + def addSuccess(self, test): |
| 9 | + super().addSuccess(test) |
| 10 | + print(f"{test._testMethodName} - passed") |
| 11 | + |
| 12 | + |
| 13 | +class CustomTestRunner(unittest.TextTestRunner): |
| 14 | + resultclass = CustomTestResult |
| 15 | + |
| 16 | + |
| 17 | +class TestRemoveUnmatchedTags(unittest.TestCase): |
| 18 | + """ |
| 19 | + Static test case input & output for check markdown handler function |
| 20 | + """ |
| 21 | + def test_remove_unmatched_tags_basic(self): |
| 22 | + input_text = "<div>Test content</p></div>" |
| 23 | + expected_output = "<div>Test content</div>" |
| 24 | + self.assertEqual(remove_unmatched_tags(input_text), expected_output) |
| 25 | + |
| 26 | + def test_remove_unmatched_tags_unmatched_opening(self): |
| 27 | + input_text = "<div>Test content" |
| 28 | + expected_output = "<div>Test content</div>" |
| 29 | + self.assertEqual(remove_unmatched_tags(input_text), expected_output) |
| 30 | + |
| 31 | + def test_remove_unmatched_tags_unmatched_closing(self): |
| 32 | + input_text = "<div><span><p>Test content</div>" |
| 33 | + expected_output = "<div><span><p>Test content</p></span></div>" |
| 34 | + self.assertEqual(remove_unmatched_tags(input_text), expected_output) |
| 35 | + |
| 36 | + def test_remove_unmatched_tags_nested_tags(self): |
| 37 | + input_text = "<div><p>Test content</p></p></div>" |
| 38 | + expected_output = "<div><p>Test content</p></div>" |
| 39 | + self.assertEqual(remove_unmatched_tags(input_text), expected_output) |
| 40 | + |
| 41 | + def test_remove_unmatched_tags_unmatched_nested_opening(self): |
| 42 | + input_text = "<div><p>Test content</div>" |
| 43 | + expected_output = "<div><p>Test content</p></div>" |
| 44 | + self.assertEqual(remove_unmatched_tags(input_text), expected_output) |
| 45 | + |
| 46 | + def test_remove_unmatched_tags_unmatched_nested_closing(self): |
| 47 | + input_text = "<div>Test content</p></div>" |
| 48 | + expected_output = "<div>Test content</div>" |
| 49 | + self.assertEqual(remove_unmatched_tags(input_text), expected_output) |
| 50 | + |
| 51 | + def test_remove_unmatched_tags_multiple_unmatched_tags(self): |
| 52 | + input_text = "<div>Test</div><p>Content</p><span>Here" |
| 53 | + expected_output = "<div>Test</div><p>Content</p><span>Here</span>" |
| 54 | + self.assertEqual(remove_unmatched_tags(input_text), expected_output) |
| 55 | + |
| 56 | + def test_remove_unmatched_tags_text_with_no_tags(self): |
| 57 | + input_text = "Plain text with no tags" |
| 58 | + expected_output = "Plain text with no tags" |
| 59 | + self.assertEqual(remove_unmatched_tags(input_text), expected_output) |
| 60 | + |
| 61 | + def test_remove_unmatched_tags_empty_string(self): |
| 62 | + input_text = "" |
| 63 | + expected_output = "" |
| 64 | + self.assertEqual(len(remove_unmatched_tags(input_text)),len(expected_output)) |
| 65 | + |
| 66 | + |
| 67 | +class TestIssuesEndpoints(unittest.TestCase): |
| 68 | + |
| 69 | + def setUp(self): |
| 70 | + self.app = app.test_client() |
| 71 | + self.app.testing = True |
| 72 | + self.issues_data = None # To store issues data for use in subsequent tests |
| 73 | + |
| 74 | + # Fetch issues data during setup |
| 75 | + self._fetch_issues_data() |
| 76 | + |
| 77 | + def _fetch_issues_data(self): |
| 78 | + # Validate the /issues endpoint and store the issues data |
| 79 | + response = self.app.get('/issues') |
| 80 | + self.assertEqual(response.status_code, 200) |
| 81 | + |
| 82 | + data = json.loads(response.data) |
| 83 | + self.issues_data = data.get('issues', []) |
| 84 | + self.assertTrue(len(self.issues_data) > 0, "No issues found in response") |
| 85 | + |
| 86 | + def test_get_issues_success(self): |
| 87 | + # Check if issues data is correctly fetched |
| 88 | + self.assertIsNotNone(self.issues_data, "Issues data is not populated") |
| 89 | + |
| 90 | + def test_get_issues_detail_success(self): |
| 91 | + # Ensure the /issues endpoint was successfully called and issues data is available |
| 92 | + if not self.issues_data: |
| 93 | + self.skipTest("Skipping detail test as /issues endpoint did not return data") |
| 94 | + |
| 95 | + # Use first data from /issues response to form the endpoint URL |
| 96 | + |
| 97 | + index = random.randrange(1,len(self.issues_data)-1) |
| 98 | + sample_issue = self.issues_data[index]['issues'][0] |
| 99 | + issue_id = sample_issue['id'] |
| 100 | + orgname = self.issues_data[index]['org_name'] |
| 101 | + |
| 102 | + endpoint = f'/v2/issues/{orgname}/{issue_id}' |
| 103 | + |
| 104 | + response = self.app.get(endpoint) |
| 105 | + self.assertEqual(response.status_code, 200) |
| 106 | + |
| 107 | + def test_get_repo_detail_success(self): |
| 108 | + # Ensure the /issues endpoint was successfully called and issues data is available |
| 109 | + if not self.issues_data: |
| 110 | + self.skipTest("Skipping detail test as /issues endpoint did not return data") |
| 111 | + |
| 112 | + # Use first data from /issues response to form the endpoint URL |
| 113 | + index = random.randrange(1,len(self.issues_data)-1) |
| 114 | + orgname = self.issues_data[index]['org_name'] |
| 115 | + endpoint = f'/issues/{orgname}' |
| 116 | + response = self.app.get(endpoint) |
| 117 | + self.assertEqual(response.status_code, 200) |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == '__main__': |
| 122 | + unittest.main(testRunner=CustomTestRunner()) |
0 commit comments