Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion superset/models/slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def get_query_context_factory(self) -> QueryContextFactory:

@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()


Expand Down
49 changes: 49 additions & 0 deletions tests/integration_tests/charts/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,55 @@ def test_get_chart_not_found(self):
rv = self.get_assert_metric(uri, "get")
assert rv.status_code == 404

def test_slice_get_by_id(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we merge these tests together and avoid duplication? both for the success and non existent ones

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you saying combine the id ones together and the uuid ones together? I normally prefer them separate in case if one use case fails and the other one passes, but can see that the uuid one might not actually be testing anything if we aren't in the if statement clause.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I mean, looking at the tests you can assert both things just with 1 extra assertion that's why I suggested merging them, but the ultimate goal is to DRY them because those tests are 95% the same

"""
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(str(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
"""
import uuid

# Use a valid UUID format that doesn't exist in database
nonexistent_uuid = str(uuid.uuid4())
result = Slice.get(nonexistent_uuid)
assert result is None

@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
def test_get_chart_no_data_access(self):
"""
Expand Down
120 changes: 120 additions & 0 deletions tests/unit_tests/models/slice_test.py
Original file line number Diff line number Diff line change
@@ -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:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment from above

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Antonio-RiveroMartnez good call. Parameterized it so I'd still know if one of the use cases failed but no duplicated code.

"""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

Copilot AI Aug 20, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment formatting is inconsistent with the period placement before the noqa comment. Consider moving the period after the noqa comment or removing it entirely for better readability.

Suggested change
# Success - no TypeError occurred, result can be None or a Slice. # noqa: E501
# Success - no TypeError occurred, result can be None or a Slice # noqa: E501.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The # no qa is not part of the comment instead it's so we don't get an error saying the length is too long.

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

Copilot AI Aug 20, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The same comment is duplicated in both test methods. Consider extracting this into a shared docstring or making the comments more specific to each test case.

Suggested change

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would we remove a line break here. That would leave 0 breaks between this and the next test.

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
Comment thread
sadpandajoe marked this conversation as resolved.
Outdated
Comment thread
sadpandajoe marked this conversation as resolved.
Outdated
Loading