john-bodley commented on code in PR #25510:
URL: https://github.com/apache/superset/pull/25510#discussion_r1396303474


##########
superset/datasets/schemas.py:
##########
@@ -43,18 +43,12 @@
 
 
 def validate_python_date_format(value: str) -> None:
-    regex = re.compile(
-        r"""
-        ^(
-            epoch_s|epoch_ms|
-            
(?P<date>%Y([-/]%m([-/]%d)?)?)([\sT](?P<time>%H(:%M(:%S(\.%f)?)?)?))?
-        )$
-        """,
-        re.VERBOSE,
-    )
-    match = regex.match(value or "")
-    if not match:
-        raise ValidationError([_("Invalid date/timestamp format")])
+    if value in ("epoch_s", "epoch_ms"):
+        return
+    try:
+        datetime.now().strftime(value or "")

Review Comment:
   @mapledan would you mind using the 
[dateutil.parser.isoparse](https://dateutil.readthedocs.io/en/stable/parser.html#dateutil.parser.isoparse)
 method instead?



##########
tests/unit_tests/datasets/schema_tests.py:
##########
@@ -0,0 +1,77 @@
+# 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.
+
+# pylint: disable=import-outside-toplevel, invalid-name, unused-argument, 
redefined-outer-name
+import pytest
+from marshmallow import ValidationError
+
+
+# pylint: disable=too-few-public-methods
+@pytest.mark.parametrize(
+    "payload",
+    [
+        (
+            {
+                "column_name": "insert_time",
+                "filterable": True,
+                "groupby": True,
+                "python_date_format": None,
+            }
+        ),
+        (
+            {
+                "column_name": "insert_time",
+                "filterable": True,
+                "groupby": True,
+                "python_date_format": "epoch_ms",
+            }
+        ),
+        (
+            {
+                "column_name": "insert_time",
+                "filterable": True,
+                "groupby": True,
+                "python_date_format": "epoch_s",
+            }
+        ),
+        (
+            {
+                "column_name": "insert_time",
+                "filterable": True,
+                "groupby": True,
+                "python_date_format": "%Y/%m/%dT%H:%M:%S.%f",
+            }
+        ),
+        (
+            {
+                "column_name": "insert_time",
+                "filterable": True,
+                "groupby": True,
+                "python_date_format": "%Y%m%d",
+            }
+        ),
+    ],
+)
+def test_datasets_schema_update_column_datetime_format(payload) -> None:
+    from superset.datasets.schemas import DatasetColumnsPutSchema
+
+    schema = DatasetColumnsPutSchema()
+
+    try:
+        schema.load(payload)
+    except ValidationError as err:
+        assert False, err.messages

Review Comment:
   Thanks for adding  the tests. I'm a little perplexed as how this works or is 
intended to work. Ideally these tests should be split into two: i) those where 
the schema are valid and those which the schema is valid. The logic would be of 
the form:
   
   ```python
   def test_datasets_schema_update_column_datetime_format(payload) -> None:
       DatasetColumnsPutSchema().load(payload)
   ```
   
   and
   
   ```python
   def test_datasets_schema_update_column_datetime_format_raises(payload) -> 
None:
       with pytest.raises(ValidationError):
           DatasetColumnsPutSchema().load(payload)
   ```



-- 
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-unsubscr...@superset.apache.org

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


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

Reply via email to