bito-code-review[bot] commented on code in PR #40397: URL: https://github.com/apache/superset/pull/40397#discussion_r3293884909
########## tests/unit_tests/daos/test_base_relationship_filters.py: ########## @@ -0,0 +1,93 @@ +# 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. + +"""Tests for BaseDAO.apply_column_operators with relationship columns. + +`apply_column_operators` historically only worked on scalar columns. When a +caller asked for `{col: "<m2m_relationship>", opr: "eq", value: <id>}` the +SQLAlchemy backend raised "Can't compare a collection to an object." + +The behavior now: detect the relationship attribute and dispatch to +`column.any(<related_pk> == value)`, so callers can use the natural shape +for both scalar columns and m2m relationships. +""" + +from unittest.mock import MagicMock + +import pytest + +from superset.daos.base import BaseDAO, ColumnOperator, ColumnOperatorEnum +from superset.models.slice import Slice + + +class _SliceDAO(BaseDAO[Slice]): + """Tiny concrete DAO so we can exercise the BaseDAO logic against the + real Slice model + its m2m `dashboards` relationship without standing up + a full database.""" + + model_cls = Slice + + +class TestApplyColumnOperatorsRelationship: + """`apply_column_operators` handles m2m relationship columns via .any().""" + + def test_eq_on_relationship_dispatches_to_any(self): + """`{col: 'dashboards', opr: 'eq', value: 42}` calls .any() on the + relationship and compares the related model's PK to 42.""" + mock_query = MagicMock() + mock_query.filter.return_value = mock_query + + result = _SliceDAO.apply_column_operators( + mock_query, + [ColumnOperator(col="dashboards", opr=ColumnOperatorEnum.eq, value=42)], + ) + + assert result is mock_query + # We applied exactly one filter + assert mock_query.filter.call_count == 1 + # And the argument is a SQLAlchemy BinaryExpression that survives + # being passed to .filter() — we can't easily inspect its structure + # without rendering SQL, but we can verify the dispatch happened + # without raising. Review Comment: <div> <div id="suggestion"> <div id="issue"><b>Weak test assertions</b></div> <div id="fix"> The test only asserts `call_count == 1` without verifying the actual filter expression. Per rule 6262, tests should validate behavior directly — verify the filter receives the expected BinaryExpression with related_pk == value. </div> </div> <div id="suggestion"> <div id="issue"><b>Non-existent model attribute in test</b></div> <div id="fix"> The `col="dashboards"` references an attribute that doesn't exist on `Slice`. The `dashboards` backref is on the `Dashboard` model (via `Dashboard.slices`), not `Slice`. This test will fail with `ValueError: Invalid filter: column 'dashboards' does not exist on Slice`. </div> </div> <small><i>Code Review Run #80e0f2</i></small> </div> --- Should Bito avoid suggestions like this for future reviews? (<a href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>) - [ ] Yes, avoid them -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
