-
Notifications
You must be signed in to change notification settings - Fork 17.9k
fix: Fix TypeError in Slice.get() method when using filter_by() with BinaryExpression #34769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
34e1ae2
54c0953
1a9997a
f55041a
348d836
6107970
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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: | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment from above
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||
|
||||||
| # 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. |
There was a problem hiding this comment.
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.
Copilot
AI
Aug 20, 2025
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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