|
| 1 | +"""Tests for backward-compatible fetch() method.""" |
| 2 | + |
| 3 | +import warnings |
| 4 | +from unittest.mock import MagicMock |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +import pytest |
| 8 | + |
| 9 | + |
| 10 | +class TestFetchBackwardCompat: |
| 11 | + """Test backward-compatible fetch() emits deprecation warning and delegates correctly.""" |
| 12 | + |
| 13 | + @pytest.fixture |
| 14 | + def mock_expression(self): |
| 15 | + """Create a mock QueryExpression with mocked output methods.""" |
| 16 | + from datajoint.expression import QueryExpression |
| 17 | + |
| 18 | + expr = MagicMock(spec=QueryExpression) |
| 19 | + # Make fetch() callable by using the real implementation |
| 20 | + expr.fetch = QueryExpression.fetch.__get__(expr, QueryExpression) |
| 21 | + |
| 22 | + # Mock the output methods |
| 23 | + expr.to_arrays = MagicMock(return_value=np.array([(1, "a"), (2, "b")])) |
| 24 | + expr.to_dicts = MagicMock(return_value=[{"id": 1, "name": "a"}, {"id": 2, "name": "b"}]) |
| 25 | + expr.to_pandas = MagicMock() |
| 26 | + expr.proj = MagicMock(return_value=expr) |
| 27 | + |
| 28 | + return expr |
| 29 | + |
| 30 | + def test_fetch_emits_deprecation_warning(self, mock_expression): |
| 31 | + """fetch() should emit a DeprecationWarning.""" |
| 32 | + with warnings.catch_warnings(record=True) as w: |
| 33 | + warnings.simplefilter("always") |
| 34 | + mock_expression.fetch() |
| 35 | + |
| 36 | + assert len(w) == 1 |
| 37 | + assert issubclass(w[0].category, DeprecationWarning) |
| 38 | + assert "fetch() is deprecated" in str(w[0].message) |
| 39 | + |
| 40 | + def test_fetch_default_returns_arrays(self, mock_expression): |
| 41 | + """fetch() with no args should call to_arrays().""" |
| 42 | + with warnings.catch_warnings(): |
| 43 | + warnings.simplefilter("ignore", DeprecationWarning) |
| 44 | + mock_expression.fetch() |
| 45 | + |
| 46 | + mock_expression.to_arrays.assert_called_once_with(order_by=None, limit=None, offset=None, squeeze=False) |
| 47 | + |
| 48 | + def test_fetch_as_dict_true(self, mock_expression): |
| 49 | + """fetch(as_dict=True) should call to_dicts().""" |
| 50 | + with warnings.catch_warnings(): |
| 51 | + warnings.simplefilter("ignore", DeprecationWarning) |
| 52 | + mock_expression.fetch(as_dict=True) |
| 53 | + |
| 54 | + mock_expression.to_dicts.assert_called_once_with(order_by=None, limit=None, offset=None, squeeze=False) |
| 55 | + |
| 56 | + def test_fetch_with_attrs_returns_dicts(self, mock_expression): |
| 57 | + """fetch('col1', 'col2') should call proj().to_dicts().""" |
| 58 | + with warnings.catch_warnings(): |
| 59 | + warnings.simplefilter("ignore", DeprecationWarning) |
| 60 | + mock_expression.fetch("col1", "col2") |
| 61 | + |
| 62 | + mock_expression.proj.assert_called_once_with("col1", "col2") |
| 63 | + mock_expression.to_dicts.assert_called_once() |
| 64 | + |
| 65 | + def test_fetch_with_attrs_as_dict_false(self, mock_expression): |
| 66 | + """fetch('col1', 'col2', as_dict=False) should call to_arrays('col1', 'col2').""" |
| 67 | + with warnings.catch_warnings(): |
| 68 | + warnings.simplefilter("ignore", DeprecationWarning) |
| 69 | + mock_expression.fetch("col1", "col2", as_dict=False) |
| 70 | + |
| 71 | + mock_expression.to_arrays.assert_called_once_with( |
| 72 | + "col1", "col2", order_by=None, limit=None, offset=None, squeeze=False |
| 73 | + ) |
| 74 | + |
| 75 | + def test_fetch_format_frame(self, mock_expression): |
| 76 | + """fetch(format='frame') should call to_pandas().""" |
| 77 | + with warnings.catch_warnings(): |
| 78 | + warnings.simplefilter("ignore", DeprecationWarning) |
| 79 | + mock_expression.fetch(format="frame") |
| 80 | + |
| 81 | + mock_expression.to_pandas.assert_called_once_with(order_by=None, limit=None, offset=None, squeeze=False) |
| 82 | + |
| 83 | + def test_fetch_format_frame_with_attrs_raises(self, mock_expression): |
| 84 | + """fetch(format='frame') with attrs should raise error.""" |
| 85 | + from datajoint.errors import DataJointError |
| 86 | + |
| 87 | + with warnings.catch_warnings(): |
| 88 | + warnings.simplefilter("ignore", DeprecationWarning) |
| 89 | + with pytest.raises(DataJointError, match="format='frame' cannot be combined"): |
| 90 | + mock_expression.fetch("col1", format="frame") |
| 91 | + |
| 92 | + def test_fetch_passes_order_by_limit_offset(self, mock_expression): |
| 93 | + """fetch() should pass order_by, limit, offset to output methods.""" |
| 94 | + with warnings.catch_warnings(): |
| 95 | + warnings.simplefilter("ignore", DeprecationWarning) |
| 96 | + mock_expression.fetch(order_by="id", limit=10, offset=5) |
| 97 | + |
| 98 | + mock_expression.to_arrays.assert_called_once_with(order_by="id", limit=10, offset=5, squeeze=False) |
| 99 | + |
| 100 | + def test_fetch_passes_squeeze(self, mock_expression): |
| 101 | + """fetch(squeeze=True) should pass squeeze to output methods.""" |
| 102 | + with warnings.catch_warnings(): |
| 103 | + warnings.simplefilter("ignore", DeprecationWarning) |
| 104 | + mock_expression.fetch(squeeze=True) |
| 105 | + |
| 106 | + mock_expression.to_arrays.assert_called_once_with(order_by=None, limit=None, offset=None, squeeze=True) |
0 commit comments