You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by jl...@apache.org on 2025/08/19 23:23:51 UTC

(superset) branch fix/slice-get-filter-by-bug created (now 34e1ae24a3)

This is an automated email from the ASF dual-hosted git repository.

jli pushed a change to branch fix/slice-get-filter-by-bug
in repository https://gitbox.apache.org/repos/asf/superset.git


      at 34e1ae24a3 fix(models): Change Slice.get() to use filter() instead of filter_by()

This branch includes the following new commits:

     new 34e1ae24a3 fix(models): Change Slice.get() to use filter() instead of filter_by()

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(superset) 01/01: fix(models): Change Slice.get() to use filter() instead of filter_by()

Posted by jl...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jli pushed a commit to branch fix/slice-get-filter-by-bug
in repository https://gitbox.apache.org/repos/asf/superset.git

commit 34e1ae24a367dd8dd24fd4c3712bd6dccea142f2
Author: Joe Li <jo...@preset.io>
AuthorDate: Tue Aug 19 15:58:22 2025 -0700

    fix(models): Change Slice.get() to use filter() instead of filter_by()
    
    Fix TypeError in Slice.get() method introduced in PR #29573. The method was
    incorrectly using filter_by() with a BinaryExpression from id_or_uuid_filter(),
    which expects keyword arguments. Changed to use filter() which accepts
    BinaryExpressions.
    
    This fixes:
    - Chart thumbnail generation failures
    - 9 failing tests (8 event logger, 1 multi-tenant)
    - All calls to Slice.get() with ID or UUID strings
    
    Added comprehensive test coverage:
    - Unit tests for Slice.get() method with mock verification
    - Integration tests for both ID and UUID lookup scenarios
    - Tests to prevent regression of filter_by usage
    
    🤖 Generated with [Claude Code](https://claude.ai/code)
    
    Co-Authored-By: Claude <no...@anthropic.com>
---
 superset/models/slice.py                    |   2 +-
 tests/integration_tests/charts/api_tests.py |  45 +++++++++++
 tests/unit_tests/models/slice_test.py       | 120 ++++++++++++++++++++++++++++
 3 files changed, 166 insertions(+), 1 deletion(-)

diff --git a/superset/models/slice.py b/superset/models/slice.py
index f47f424fc3..f6379f0e77 100644
--- a/superset/models/slice.py
+++ b/superset/models/slice.py
@@ -363,7 +363,7 @@ class Slice(  # pylint: disable=too-many-public-methods
 
     @classmethod
     def get(cls, id_or_uuid: str) -> Slice:
-        qry = db.session.query(Slice).filter_by(id_or_uuid_filter(id_or_uuid))
+        qry = db.session.query(Slice).filter(id_or_uuid_filter(id_or_uuid))
         return qry.one_or_none()
 
 
diff --git a/tests/integration_tests/charts/api_tests.py b/tests/integration_tests/charts/api_tests.py
index 1b95ed1f63..84882c36c4 100644
--- a/tests/integration_tests/charts/api_tests.py
+++ b/tests/integration_tests/charts/api_tests.py
@@ -1076,6 +1076,51 @@ class TestChartApi(ApiOwnersTestCaseMixin, InsertChartMixin, SupersetTestCase):
         rv = self.get_assert_metric(uri, "get")
         assert rv.status_code == 404
 
+    def test_slice_get_by_id(self):
+        """
+        Chart API: Test Slice.get() method with numeric ID
+        """
+        admin = self.get_user("admin")
+        chart = self.insert_chart("test_slice_get_by_id", [admin.id], 1)
+
+        result = Slice.get(str(chart.id))
+        assert result is not None
+        assert result.id == chart.id
+        assert result.slice_name == "test_slice_get_by_id"
+
+        db.session.delete(chart)
+        db.session.commit()
+
+    def test_slice_get_by_uuid(self):
+        """
+        Chart API: Test Slice.get() method with UUID
+        """
+        admin = self.get_user("admin")
+        chart = self.insert_chart("test_slice_get_by_uuid", [admin.id], 1)
+
+        if chart.uuid:
+            result = Slice.get(chart.uuid)
+            assert result is not None
+            assert result.id == chart.id
+            assert result.uuid == chart.uuid
+
+        db.session.delete(chart)
+        db.session.commit()
+
+    def test_slice_get_nonexistent_id(self):
+        """
+        Chart API: Test Slice.get() with non-existent ID returns None
+        """
+        result = Slice.get("999999")
+        assert result is None
+
+    def test_slice_get_nonexistent_uuid(self):
+        """
+        Chart API: Test Slice.get() with non-existent UUID returns None
+        """
+        result = Slice.get("non-existent-uuid-123")
+        assert result is None
+
     @pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
     def test_get_chart_no_data_access(self):
         """
diff --git a/tests/unit_tests/models/slice_test.py b/tests/unit_tests/models/slice_test.py
new file mode 100644
index 0000000000..2c5d4b4e43
--- /dev/null
+++ b/tests/unit_tests/models/slice_test.py
@@ -0,0 +1,120 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import uuid
+from unittest.mock import MagicMock, patch
+
+import pytest
+
+from superset.models.slice import id_or_uuid_filter, Slice
+
+
+class TestSlice:
+    """Test cases for Slice model functionality."""
+
+    def test_slice_get_by_id_calls_filter_correctly(self):
+        """
+        Test that Slice.get() with numeric ID calls the correct SQLAlchemy
+        filter method.
+        """
+        with patch("superset.models.slice.db") as mock_db:
+            # Set up the mock chain properly
+            mock_query = MagicMock()
+            mock_filtered_query = MagicMock()
+            mock_db.session.query.return_value = mock_query
+            mock_query.filter.return_value = mock_filtered_query
+            mock_filtered_query.one_or_none.return_value = None
+
+            # This should not raise TypeError if filter() is used correctly
+            result = Slice.get("123")
+
+            # Verify that query() was called with Slice
+            mock_db.session.query.assert_called_once_with(Slice)
+
+            # Verify that filter() was called (not filter_by)
+            mock_query.filter.assert_called_once()
+            mock_filtered_query.one_or_none.assert_called_once()
+
+            # Result should be None (mocked return value)
+            assert result is None
+
+    def test_slice_get_by_uuid_calls_filter_correctly(self):
+        """
+        Test that Slice.get() with UUID calls the correct SQLAlchemy
+        filter method.
+        """
+        test_uuid = str(uuid.uuid4())
+
+        with patch("superset.models.slice.db") as mock_db:
+            # Set up the mock chain properly
+            mock_query = MagicMock()
+            mock_filtered_query = MagicMock()
+            mock_db.session.query.return_value = mock_query
+            mock_query.filter.return_value = mock_filtered_query
+            mock_filtered_query.one_or_none.return_value = None
+
+            # This should not raise TypeError if filter() is used correctly
+            result = Slice.get(test_uuid)
+
+            # Verify that query() was called with Slice
+            mock_db.session.query.assert_called_once_with(Slice)
+
+            # Verify that filter() was called (not filter_by)
+            mock_query.filter.assert_called_once()
+            mock_filtered_query.one_or_none.assert_called_once()
+
+            # Result should be None (mocked return value)
+            assert result is None
+
+    def test_slice_get_no_type_error(self):
+        """
+        Integration test - verify Slice.get() doesn't raise TypeError
+        for ID or UUID.
+        """
+        test_cases = ["1", "999", str(uuid.uuid4())]
+
+        for test_input in test_cases:
+            try:
+                result = Slice.get(test_input)
+                # Success - no TypeError occurred, result can be None or a Slice  # noqa: E501
+                assert result is None or hasattr(result, "id")
+            except TypeError as e:
+                if "filter_by() takes 1 positional argument but 2 were given" in str(e):
+                    pytest.fail(
+                        f"filter_by() bug exists: Slice.get('{test_input}') failed with {e}"  # noqa: E501
+                    )
+                else:
+                    # Some other TypeError - re-raise for investigation
+                    raise
+
+    def test_id_or_uuid_filter_with_numeric_id(self):
+        """Test that id_or_uuid_filter works with numeric ID strings."""
+        # Simple test - just verify it doesn't crash and returns something
+        result = id_or_uuid_filter("123")
+        # Should return a BinaryExpression that can be used with filter()
+        assert result is not None
+        # The important thing is it doesn't crash and returns a filter expression  # noqa: E501
+
+    def test_id_or_uuid_filter_with_uuid(self):
+        """Test that id_or_uuid_filter works with UUID strings."""
+        test_uuid = "abc-def-123-456"
+
+        # Simple test - just verify it doesn't crash and returns something
+        result = id_or_uuid_filter(test_uuid)
+        # Should return a BinaryExpression that can be used with filter()
+        assert result is not None
+        # The important thing is it doesn't crash and returns a filter expression. # noqa: E501