|
19 | 19 | serialize_env, |
20 | 20 | ) |
21 | 21 |
|
| 22 | + |
| 23 | +def test_print_exception(mocker: MockerFixture): |
| 24 | + out_mock = mocker.Mock() |
| 25 | + |
| 26 | + test_env = { |
| 27 | + "test_fun": Executable( |
| 28 | + name="test_func", |
| 29 | + payload="""def test_fun(): |
| 30 | + raise RuntimeError("error")""", |
| 31 | + path="/test/path.py", |
| 32 | + ), |
| 33 | + } |
| 34 | + env: t.Dict[str, t.Any] = {} |
| 35 | + prepare_env(env, test_env) |
| 36 | + try: |
| 37 | + eval("test_fun()", env) |
| 38 | + except Exception as ex: |
| 39 | + print_exception(ex, test_env, out_mock) |
| 40 | + |
| 41 | + expected_message = f"""Traceback (most recent call last): |
| 42 | +
|
| 43 | + File "{__file__}", line 37, in test_print_exception |
| 44 | + eval("test_fun()", env) |
| 45 | +
|
| 46 | + File "<string>", line 1, in <module> |
| 47 | +
|
| 48 | + File '/test/path.py' (or imported file), line 2, in test_fun |
| 49 | + def test_fun(): |
| 50 | + raise RuntimeError("error") |
| 51 | +
|
| 52 | +
|
| 53 | +RuntimeError: error |
| 54 | +""" |
| 55 | + out_mock.write.assert_called_once_with(expected_message) |
| 56 | + |
| 57 | + |
22 | 58 | X = 1 |
23 | 59 | Y = 2 |
24 | 60 | Z = 3 |
@@ -114,6 +150,7 @@ def closure(z): |
114 | 150 |
|
115 | 151 | def test_serialize_env_error() -> None: |
116 | 152 | with pytest.raises(SQLMeshError) as e: |
| 153 | + # pretend to be the module pandas |
117 | 154 | serialize_env({"pandas": pd}, module="pandas") |
118 | 155 |
|
119 | 156 |
|
@@ -188,38 +225,3 @@ def baz(self): |
188 | 225 | return X + a""", |
189 | 226 | ), |
190 | 227 | } |
191 | | - |
192 | | - |
193 | | -def test_print_exception(mocker: MockerFixture): |
194 | | - out_mock = mocker.Mock() |
195 | | - |
196 | | - test_env = { |
197 | | - "test_fun": Executable( |
198 | | - name="test_func", |
199 | | - payload="""def test_fun(): |
200 | | - raise RuntimeError("error")""", |
201 | | - path="/test/path.py", |
202 | | - ), |
203 | | - } |
204 | | - env: t.Dict[str, t.Any] = {} |
205 | | - prepare_env(env, test_env) |
206 | | - try: |
207 | | - eval("test_fun()", env) |
208 | | - except Exception as ex: |
209 | | - print_exception(ex, test_env, out_mock) |
210 | | - |
211 | | - expected_message = f"""Traceback (most recent call last): |
212 | | -
|
213 | | - File "{__file__}", line 200, in test_print_exception |
214 | | - eval("test_fun()", env) |
215 | | -
|
216 | | - File "<string>", line 1, in <module> |
217 | | -
|
218 | | - File '/test/path.py' (or imported file), line 2, in test_fun |
219 | | - def test_fun(): |
220 | | - raise RuntimeError("error") |
221 | | -
|
222 | | -
|
223 | | -RuntimeError: error |
224 | | -""" |
225 | | - out_mock.write.assert_called_once_with(expected_message) |
|
0 commit comments