You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2022/02/18 12:07:15 UTC

[GitHub] [superset] zhaoyongjie commented on a change in pull request #18810: refactor: move date_parser to unittest

zhaoyongjie commented on a change in pull request #18810:
URL: https://github.com/apache/superset/pull/18810#discussion_r809940789



##########
File path: tests/unit_tests/utils/date_parser_tests.py
##########
@@ -0,0 +1,358 @@
+# 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 re
+from datetime import date, datetime, timedelta
+from typing import Optional, Tuple
+from unittest.mock import Mock, patch
+
+import pytest
+from dateutil.relativedelta import relativedelta
+
+from superset.charts.commands.exceptions import (
+    TimeRangeAmbiguousError,
+    TimeRangeParseFailError,
+)
+from superset.utils.date_parser import (
+    DateRangeMigration,
+    datetime_eval,
+    get_past_or_future,
+    get_since_until,
+    parse_human_datetime,
+    parse_human_timedelta,
+    parse_past_timedelta,
+)
+
+
+def mock_parse_human_datetime(s: str) -> Optional[datetime]:
+    if s == "now":
+        return datetime(2016, 11, 7, 9, 30, 10)
+    elif s == "2018":
+        return datetime(2018, 1, 1)
+    elif s == "2018-9":
+        return datetime(2018, 9, 1)
+    elif s == "today":
+        return datetime(2016, 11, 7)
+    elif s == "yesterday":
+        return datetime(2016, 11, 6)
+    elif s == "tomorrow":
+        return datetime(2016, 11, 8)
+    elif s == "Last year":
+        return datetime(2015, 11, 7)
+    elif s == "Last week":
+        return datetime(2015, 10, 31)
+    elif s == "Last 5 months":
+        return datetime(2016, 6, 7)
+    elif s == "Next 5 months":
+        return datetime(2017, 4, 7)
+    elif s in ["5 days", "5 days ago"]:
+        return datetime(2016, 11, 2)
+    elif s == "2018-01-01T00:00:00":
+        return datetime(2018, 1, 1)
+    elif s == "2018-12-31T23:59:59":
+        return datetime(2018, 12, 31, 23, 59, 59)
+    else:
+        return None

Review comment:
       Thanks, Ville! Unfortunately, I'm stuck when migrate to `freeze_time`. I leave some notes for this.
   
   ```
   @freeze_time(datetime(2016, 11, 7, 9, 30, 10))
   def test_get_since_until() -> None:
       result: Tuple[Optional[datetime], Optional[datetime]]
       expected: Tuple[Optional[datetime], Optional[datetime]]
   
       # result = get_since_until()
       # expected = None, datetime(2016, 11, 7, tzinfo=pytz.utc)
       # assert result == expected
   
       result = get_since_until(" : now")
       # expected = None, datetime(2016, 11, 7, 9, 30, 10)
       print(result)
       # print(expected)
   
   (superset) yongjie.zhao@:incubator-superset$ pytest -s --disable-warnings tests/unit_tests/utils/date_parser_tests.py::test_get_since_until
   ============================================================================= test session starts ==============================================================================
   platform darwin -- Python 3.8.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
   rootdir: /Users/yongjie.zhao/workspace/warehouse/superset/incubator-superset, configfile: pytest.ini
   plugins: cov-2.12.1, anyio-2.2.0, celery-4.4.7, mock-3.6.1, pyfakefs-4.5.0
   collected 1 item
   
   tests/unit_tests/utils/date_parser_tests.py now
   2016-11-07 17:30:10+00:00
   (None, FakeDatetime(2016, 11, 7, 17, 30, 10))
   ```
   
   




-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org