|
1 | 1 | import sys, os, unittest, asyncio |
2 | 2 | from unittest.mock import MagicMock |
3 | 3 | sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) |
4 | | -from pystackql import StackQL |
5 | | -from pystackql.stackql_magic import StackqlMagic, load_ipython_extension |
| 4 | +from pystackql import StackQL, load_non_server_magic, load_server_magic, StackqlMagic, StackqlServerMagic |
6 | 5 | from .test_params import * |
7 | 6 |
|
8 | 7 | def pystackql_test_setup(**kwargs): |
@@ -275,48 +274,61 @@ def instance(): |
275 | 274 | """Return a mock instance of the shell.""" |
276 | 275 | return MockInteractiveShell() |
277 | 276 |
|
278 | | -class StackQLMagicTests(PyStackQLTestsBase): |
279 | | - |
| 277 | +class BaseStackQLMagicTests: |
| 278 | + MAGIC_CLASS = None # To be overridden by child classes |
| 279 | + server_mode = None # To be overridden by child classes |
280 | 280 | def setUp(self): |
281 | 281 | """Set up for the magic tests.""" |
| 282 | + assert self.MAGIC_CLASS, "MAGIC_CLASS should be set by child classes" |
282 | 283 | self.shell = MockInteractiveShell.instance() |
283 | | - load_ipython_extension(self.shell) |
284 | | - self.stackql_magic = StackqlMagic(shell=self.shell) |
| 284 | + if self.server_mode: |
| 285 | + load_server_magic(self.shell) |
| 286 | + else: |
| 287 | + load_non_server_magic(self.shell) |
| 288 | + self.stackql_magic = self.MAGIC_CLASS(shell=self.shell) |
285 | 289 | self.query = "SELECT 1 as fred" |
286 | 290 | self.expected_result = pd.DataFrame({"fred": [1]}) |
287 | 291 |
|
288 | | - def test_23_line_magic_query(self): |
289 | | - # Mock the run_query method to return a known DataFrame. |
290 | | - self.stackql_magic.run_query = MagicMock(return_value=self.expected_result) |
291 | | - # Execute the line magic with our query. |
292 | | - result = self.stackql_magic.stackql(line=self.query, cell=None) |
293 | | - # Check if the result is as expected and if 'stackql_df' is set in the namespace. |
294 | | - self.assertTrue(result.equals(self.expected_result)) |
295 | | - self.assertTrue('stackql_df' in self.shell.user_ns) |
296 | | - self.assertTrue(self.shell.user_ns['stackql_df'].equals(self.expected_result)) |
297 | | - print_test_result(f"""Line magic test""", True, True, True) |
298 | | - |
299 | | - def test_24_cell_magic_query(self): |
300 | | - # Mock the run_query method to return a known DataFrame. |
301 | | - self.stackql_magic.run_query = MagicMock(return_value=self.expected_result) |
302 | | - # Execute the cell magic with our query. |
303 | | - result = self.stackql_magic.stackql(line="", cell=self.query) |
304 | | - # Validate the outcome. |
305 | | - self.assertTrue(result.equals(self.expected_result)) |
306 | | - self.assertTrue('stackql_df' in self.shell.user_ns) |
307 | | - self.assertTrue(self.shell.user_ns['stackql_df'].equals(self.expected_result)) |
308 | | - print_test_result(f"""Cell magic test""", True, True, True) |
| 292 | + def print_test_result(self, test_name, *checks): |
| 293 | + all_passed = all(checks) |
| 294 | + print_test_result(f"{test_name}, server_mode: {self.server_mode}", all_passed, True, True) |
309 | 295 |
|
310 | | - def test_25_cell_magic_query_no_output(self): |
| 296 | + def run_magic_test(self, line, cell, expect_none=False): |
311 | 297 | # Mock the run_query method to return a known DataFrame. |
312 | 298 | self.stackql_magic.run_query = MagicMock(return_value=self.expected_result) |
313 | | - # Execute the cell magic with our query and the no-display argument. |
314 | | - result = self.stackql_magic.stackql(line="--no-display", cell=self.query) |
| 299 | + # Execute the magic with our query. |
| 300 | + result = self.stackql_magic.stackql(line=line, cell=cell) |
315 | 301 | # Validate the outcome. |
316 | | - self.assertIsNone(result) |
317 | | - self.assertTrue('stackql_df' in self.shell.user_ns) |
318 | | - self.assertTrue(self.shell.user_ns['stackql_df'].equals(self.expected_result)) |
319 | | - print_test_result(f"""Cell magic test (with --no-display)""", True, True, True) |
| 302 | + checks = [] |
| 303 | + if expect_none: |
| 304 | + checks.append(result is None) |
| 305 | + else: |
| 306 | + checks.append(result.equals(self.expected_result)) |
| 307 | + checks.append('stackql_df' in self.shell.user_ns) |
| 308 | + checks.append(self.shell.user_ns['stackql_df'].equals(self.expected_result)) |
| 309 | + return checks |
| 310 | + |
| 311 | + def test_line_magic_query(self): |
| 312 | + checks = self.run_magic_test(line=self.query, cell=None) |
| 313 | + self.print_test_result("Line magic test", *checks) |
| 314 | + |
| 315 | + def test_cell_magic_query(self): |
| 316 | + checks = self.run_magic_test(line="", cell=self.query) |
| 317 | + self.print_test_result("Cell magic test", *checks) |
| 318 | + |
| 319 | + def test_cell_magic_query_no_output(self): |
| 320 | + checks = self.run_magic_test(line="--no-display", cell=self.query, expect_none=True) |
| 321 | + self.print_test_result("Cell magic test (with --no-display)", *checks) |
| 322 | + |
| 323 | + |
| 324 | +class StackQLMagicTests(BaseStackQLMagicTests, unittest.TestCase): |
| 325 | + |
| 326 | + MAGIC_CLASS = StackqlMagic |
| 327 | + server_mode = False |
| 328 | + |
| 329 | +class StackQLServerMagicTests(BaseStackQLMagicTests, unittest.TestCase): |
| 330 | + MAGIC_CLASS = StackqlServerMagic |
| 331 | + server_mode = True |
320 | 332 |
|
321 | 333 | def main(): |
322 | 334 | unittest.main(verbosity=0) |
|
0 commit comments