aminghadersohi commented on code in PR #35890:
URL: https://github.com/apache/superset/pull/35890#discussion_r2524800995


##########
tests/unit_tests/models/test_time_filter_double_application.py:
##########
@@ -0,0 +1,365 @@
+# 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.
+"""Test for double time filter application in virtual datasets (Issue 
#34894)"""
+
+from __future__ import annotations
+
+from datetime import datetime
+
+from flask import Flask
+from freezegun import freeze_time
+from pytest_mock import MockerFixture
+
+from superset.connectors.sqla.models import SqlaTable, SqlMetric, TableColumn
+from superset.models.core import Database
+
+
+def test_time_filter_applied_once_with_remove_filter_in_virtual_dataset(
+    mocker: MockerFixture, app: Flask
+) -> None:
+    """
+    Test that time filter is applied only once when using get_time_filter with
+    remove_filter=True in a virtual dataset.
+
+    This test verifies the fix for GitHub issue #34894 where time filters were
+    being applied twice - once in the inner query (virtual dataset) and again 
in
+    the outer query.
+
+    Expected behavior: When remove_filter=True is used in the virtual dataset,
+    the time filter should only be applied in the inner query, not in the 
outer query.
+    """
+    # Mock the database connection
+    database = Database(
+        id=1,
+        database_name="test_db",
+        sqlalchemy_uri="postgresql://test",
+    )
+
+    # Create a virtual dataset that uses get_time_filter with
+    # remove_filter=True
+    virtual_dataset_sql = """
+    SELECT *
+    FROM my_table
+    WHERE
+        dttm >= {{
+            get_time_filter('dttm', remove_filter=True, target_type='DATE')
+                .from_expr
+        }}
+        AND dttm < {{
+            get_time_filter('dttm', remove_filter=True, target_type='DATE')
+                .to_expr
+        }}
+    """
+
+    columns = [
+        TableColumn(column_name="dttm", is_dttm=1, type="TIMESTAMP"),
+        TableColumn(column_name="value", type="INTEGER"),
+    ]
+
+    virtual_dataset = SqlaTable(
+        table_name="virtual_dataset",
+        sql=virtual_dataset_sql,
+        columns=columns,
+        main_dttm_col="dttm",
+        database=database,
+    )
+
+    # Mock the security manager to avoid RLS checks
+    mocker.patch(
+        
"superset.connectors.sqla.models.security_manager.get_guest_rls_filters",
+        return_value=[],
+    )
+    mocker.patch(
+        "superset.connectors.sqla.models.security_manager.is_guest_user",
+        return_value=False,
+    )
+
+    # Create a query object with a time filter
+    query_obj = {
+        "granularity": "dttm",
+        "from_dttm": datetime(2024, 1, 1),

Review Comment:
   do we need timezone these in tests? zinfo=timezone.utc



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

Reply via email to