|
12 | 12 | else:
|
13 | 13 | from typing_extensions import assert_type
|
14 | 14 |
|
| 15 | +from unittest.mock import patch |
| 16 | + |
15 | 17 | import luigi
|
16 | 18 | import luigi.mock
|
17 | 19 |
|
@@ -196,5 +198,61 @@ def test_build_expo_backoff_when_luigi_failed_due_to_locked_task(self):
|
196 | 198 | gokart.build(_FailThreeTimesAndSuccessTask(), reset_register=False)
|
197 | 199 |
|
198 | 200 |
|
| 201 | +class TestBuildFailedAndSchedulingFailed(unittest.TestCase): |
| 202 | + def test_build_raises_exception_on_failed_and_scheduling_failed(self): |
| 203 | + """Test that build() raises GokartBuildError when FAILED_AND_SCHEDULING_FAILED occurs""" |
| 204 | + |
| 205 | + # Create a mock result object with FAILED_AND_SCHEDULING_FAILED status |
| 206 | + class MockResult: |
| 207 | + def __init__(self): |
| 208 | + self.status = luigi.LuigiStatusCode.FAILED_AND_SCHEDULING_FAILED |
| 209 | + self.summary_text = 'Task failed and scheduling failed' |
| 210 | + |
| 211 | + # Mock luigi.build to return FAILED_AND_SCHEDULING_FAILED status |
| 212 | + with patch('luigi.build') as mock_luigi_build: |
| 213 | + mock_luigi_build.return_value = MockResult() |
| 214 | + |
| 215 | + # This should now raise GokartBuildError after the fix |
| 216 | + with self.assertRaises(GokartBuildError): |
| 217 | + gokart.build(_DummyTask(param='test'), reset_register=False, log_level=logging.CRITICAL) |
| 218 | + |
| 219 | + def test_build_not_raises_exception_when_success_with_retry(self): |
| 220 | + """Test that build() does not raise GokartBuildError when task succeeds with retry""" |
| 221 | + |
| 222 | + # Create a mock result object with SUCCESS_WITH_RETRY status |
| 223 | + class MockResult: |
| 224 | + def __init__(self): |
| 225 | + self.status = luigi.LuigiStatusCode.SUCCESS_WITH_RETRY |
| 226 | + self.summary_text = 'Task completed successfully after retries' |
| 227 | + |
| 228 | + # Mock _build_task to return a test value directly |
| 229 | + with patch('luigi.build') as mock_luigi_build: |
| 230 | + mock_luigi_build.return_value = MockResult() |
| 231 | + |
| 232 | + # Create a mock task that will be used by build() |
| 233 | + mock_task = _DummyTask(param='test') |
| 234 | + |
| 235 | + # This should not raise GokartBuildError |
| 236 | + # The test output will be whatever the mock returns |
| 237 | + gokart.build(mock_task, reset_register=False, return_value=False, log_level=logging.CRITICAL) |
| 238 | + |
| 239 | + def test_build_not_raises_exception_on_scheduling_failed_only(self): |
| 240 | + """Test that build() raises GokartBuildError when SCHEDULING_FAILED occurs""" |
| 241 | + |
| 242 | + # Create a mock result object with SCHEDULING_FAILED status |
| 243 | + class MockResult: |
| 244 | + def __init__(self): |
| 245 | + self.status = luigi.LuigiStatusCode.SCHEDULING_FAILED |
| 246 | + self.summary_text = 'Task scheduling failed' |
| 247 | + |
| 248 | + # Mock luigi.build to return SCHEDULING_FAILED status |
| 249 | + with patch('luigi.build') as mock_luigi_build: |
| 250 | + mock_luigi_build.return_value = MockResult() |
| 251 | + |
| 252 | + # This should raise GokartBuildError after the fix |
| 253 | + with self.assertRaises(GokartBuildError): |
| 254 | + gokart.build(_DummyTask(param='test'), reset_register=False, log_level=logging.CRITICAL) |
| 255 | + |
| 256 | + |
199 | 257 | if __name__ == '__main__':
|
200 | 258 | unittest.main()
|
0 commit comments