|
| 1 | +import os |
| 2 | +from unittest import mock |
| 3 | + |
| 4 | +import responses # type: ignore |
| 5 | + |
| 6 | +from smart_tests.utils.http_client import get_base_url |
| 7 | +from tests.cli_test_case import CliTestCase |
| 8 | + |
| 9 | + |
| 10 | +class DetectFlakeTest(CliTestCase): |
| 11 | + @responses.activate |
| 12 | + @mock.patch.dict(os.environ, {"SMART_TESTS_TOKEN": CliTestCase.smart_tests_token}) |
| 13 | + def test_detect_flakes_success(self): |
| 14 | + mock_json_response = { |
| 15 | + "testPaths": [ |
| 16 | + [{"type": "file", "name": "test_flaky_1.py"}], |
| 17 | + [{"type": "file", "name": "test_flaky_2.py"}], |
| 18 | + ] |
| 19 | + } |
| 20 | + responses.add( |
| 21 | + responses.GET, |
| 22 | + f"{get_base_url()}/intake/organizations/{self.organization}/workspaces/{self.workspace}/detect-flake", |
| 23 | + json=mock_json_response, |
| 24 | + status=200, |
| 25 | + ) |
| 26 | + result = self.cli( |
| 27 | + "detect-flakes", |
| 28 | + "file", |
| 29 | + "--session", self.session, |
| 30 | + "--retry-threshold", "high", |
| 31 | + mix_stderr=False, |
| 32 | + ) |
| 33 | + self.assert_success(result) |
| 34 | + self.assertIn("test_flaky_1.py", result.stdout) |
| 35 | + self.assertIn("test_flaky_2.py", result.stdout) |
| 36 | + |
| 37 | + @responses.activate |
| 38 | + @mock.patch.dict(os.environ, {"SMART_TESTS_TOKEN": CliTestCase.smart_tests_token}) |
| 39 | + def test_detect_flakes_without_retry_threshold_success(self): |
| 40 | + mock_json_response = { |
| 41 | + "testPaths": [ |
| 42 | + [{"type": "file", "name": "test_flaky_1.py"}], |
| 43 | + [{"type": "file", "name": "test_flaky_2.py"}], |
| 44 | + ] |
| 45 | + } |
| 46 | + responses.add( |
| 47 | + responses.GET, |
| 48 | + f"{get_base_url()}/intake/organizations/{self.organization}/workspaces/{self.workspace}/detect-flake", |
| 49 | + json=mock_json_response, |
| 50 | + status=200, |
| 51 | + ) |
| 52 | + result = self.cli( |
| 53 | + "detect-flakes", |
| 54 | + "file", |
| 55 | + "--session", self.session, |
| 56 | + mix_stderr=False, |
| 57 | + ) |
| 58 | + self.assert_success(result) |
| 59 | + self.assertIn("test_flaky_1.py", result.stdout) |
| 60 | + self.assertIn("test_flaky_2.py", result.stdout) |
| 61 | + |
| 62 | + @responses.activate |
| 63 | + @mock.patch.dict(os.environ, {"SMART_TESTS_TOKEN": CliTestCase.smart_tests_token}) |
| 64 | + def test_detect_flakes_no_flakes(self): |
| 65 | + mock_json_response = {"testPaths": []} |
| 66 | + responses.add( |
| 67 | + responses.GET, |
| 68 | + f"{get_base_url()}/intake/organizations/{self.organization}/workspaces/{self.workspace}/detect-flake", |
| 69 | + json=mock_json_response, |
| 70 | + status=200, |
| 71 | + ) |
| 72 | + result = self.cli( |
| 73 | + "detect-flakes", |
| 74 | + "file", |
| 75 | + "--session", self.session, |
| 76 | + "--retry-threshold", "low", |
| 77 | + mix_stderr=False, |
| 78 | + ) |
| 79 | + self.assert_success(result) |
| 80 | + self.assertEqual(result.stdout, "") |
| 81 | + |
| 82 | + @responses.activate |
| 83 | + @mock.patch.dict(os.environ, {"SMART_TESTS_TOKEN": CliTestCase.smart_tests_token}) |
| 84 | + def test_flake_detection_api_error(self): |
| 85 | + responses.add( |
| 86 | + responses.GET, |
| 87 | + f"{get_base_url()}/intake/organizations/{self.organization}/workspaces/{self.workspace}/detect-flake", |
| 88 | + status=500, |
| 89 | + ) |
| 90 | + result = self.cli( |
| 91 | + "detect-flakes", |
| 92 | + "file", |
| 93 | + "--session", self.session, |
| 94 | + "--retry-threshold", "medium", |
| 95 | + mix_stderr=False, |
| 96 | + ) |
| 97 | + self.assert_exit_code(result, 0) |
| 98 | + self.assertIn("Error", result.stderr) |
| 99 | + self.assertEqual(result.stdout, "") |
0 commit comments