|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from superset.views.datasource.utils import replace_verbose_with_column |
| 21 | + |
| 22 | + |
| 23 | +class Column: |
| 24 | + def __init__(self, column_name, verbose_name): |
| 25 | + self.column_name = column_name |
| 26 | + self.verbose_name = verbose_name |
| 27 | + |
| 28 | + |
| 29 | +class IncompleteColumn: |
| 30 | + """A column missing required attributes.""" |
| 31 | + |
| 32 | + def __init__(self, only_name): |
| 33 | + self.only_name = only_name |
| 34 | + |
| 35 | + |
| 36 | +# Test dataset and filters |
| 37 | +columns = [ |
| 38 | + Column("col1", "Column 1"), |
| 39 | + Column("col3", "Column 3"), |
| 40 | +] |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.parametrize( |
| 44 | + "filters, expected", |
| 45 | + [ |
| 46 | + # Normal match, should be replaced with the actual column_name |
| 47 | + ([{"col": "Column 1"}], [{"col": "col1"}]), |
| 48 | + # Multiple filters, should correctly replace all matching columns |
| 49 | + ( |
| 50 | + [{"col": "Column 1"}, {"col": "Column 3"}], |
| 51 | + [{"col": "col1"}, {"col": "col3"}], |
| 52 | + ), |
| 53 | + # No matching case, the original value should remain unchanged |
| 54 | + ([{"col": "Non-existent"}], [{"col": "Non-existent"}]), |
| 55 | + # Empty filters, no changes should be made |
| 56 | + ([], []), |
| 57 | + ], |
| 58 | +) |
| 59 | +def test_replace_verbose_with_column(filters, expected): |
| 60 | + filters_copy = [dict(f) for f in filters] |
| 61 | + replace_verbose_with_column(filters_copy, columns) |
| 62 | + assert filters_copy == expected |
| 63 | + |
| 64 | + |
| 65 | +def test_replace_verbose_with_column_missing_col_key(caplog): |
| 66 | + """Filter dict missing 'col' should trigger a warning and be skipped.""" |
| 67 | + filters = [{"op": "=="}] # missing "col" |
| 68 | + with caplog.at_level("WARNING"): |
| 69 | + replace_verbose_with_column(filters, columns) |
| 70 | + assert "Filter missing 'col' key:" in caplog.text |
| 71 | + # filter should remain unchanged |
| 72 | + assert filters == [{"op": "=="}] |
| 73 | + |
| 74 | + |
| 75 | +def test_replace_verbose_with_column_missing_column_attrs(caplog): |
| 76 | + """Column missing expected attributes should trigger a warning.""" |
| 77 | + filters = [{"col": "whatever"}] |
| 78 | + bad_columns = [IncompleteColumn("broken")] |
| 79 | + with caplog.at_level("WARNING"): |
| 80 | + replace_verbose_with_column(filters, bad_columns) |
| 81 | + assert "missing expected attributes" in caplog.text |
| 82 | + # filter should remain unchanged |
| 83 | + assert filters == [{"col": "whatever"}] |
0 commit comments