Browse Source
* [python] Add parameter schedule for process definition * Rebase and fix some code style * May schedule work on both string and datetime * Fix flaky test * Add comment about freeze time * Add edge test for schedule_json with None schedule * Fix test function name * Fix rebase error3.0.0/version-upgrade
Jiajie Zhong
3 years ago
committed by
GitHub
9 changed files with 485 additions and 42 deletions
@ -0,0 +1,81 @@ |
|||||||
|
# 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. |
||||||
|
|
||||||
|
"""Date util function collections.""" |
||||||
|
|
||||||
|
from datetime import datetime |
||||||
|
from pydolphinscheduler.constants import Delimiter, Time |
||||||
|
|
||||||
|
LEN_SUPPORT_DATETIME = ( |
||||||
|
15, |
||||||
|
19, |
||||||
|
) |
||||||
|
|
||||||
|
FMT_SHORT = f"{Time.FMT_SHORT_DATE} {Time.FMT_NO_COLON_TIME}" |
||||||
|
FMT_DASH = f"{Time.FMT_DASH_DATE} {Time.FMT_STD_TIME}" |
||||||
|
FMT_STD = f"{Time.FMT_STD_DATE} {Time.FMT_STD_TIME}" |
||||||
|
|
||||||
|
MAX_DATETIME = datetime(9999, 12, 31, 23, 59, 59) |
||||||
|
|
||||||
|
|
||||||
|
def conv_to_schedule(src: datetime) -> str: |
||||||
|
"""Convert given datetime to schedule date string.""" |
||||||
|
return datetime.strftime(src, FMT_STD) |
||||||
|
|
||||||
|
|
||||||
|
def conv_from_str(src: str) -> datetime: |
||||||
|
"""Convert given string to datetime. |
||||||
|
|
||||||
|
This function give an ability to convert string to datetime, and for now it could handle |
||||||
|
format like: |
||||||
|
- %Y-%m-%d |
||||||
|
- %Y/%m/%d |
||||||
|
- %Y%m%d |
||||||
|
- %Y-%m-%d %H:%M:%S |
||||||
|
- %Y/%m/%d %H:%M:%S |
||||||
|
- %Y%m%d %H%M%S |
||||||
|
If pattern not like above be given will raise NotImplementedError. |
||||||
|
""" |
||||||
|
len_ = len(src) |
||||||
|
if len_ == Time.LEN_SHORT_DATE: |
||||||
|
return datetime.strptime(src, Time.FMT_SHORT_DATE) |
||||||
|
elif len_ == Time.LEN_STD_DATE: |
||||||
|
if Delimiter.BAR in src: |
||||||
|
return datetime.strptime(src, Time.FMT_STD_DATE) |
||||||
|
elif Delimiter.DASH in src: |
||||||
|
return datetime.strptime(src, Time.FMT_DASH_DATE) |
||||||
|
else: |
||||||
|
raise NotImplementedError( |
||||||
|
"%s could not be convert to datetime for now.", src |
||||||
|
) |
||||||
|
elif len_ in LEN_SUPPORT_DATETIME: |
||||||
|
if Delimiter.BAR in src and Delimiter.COLON in src: |
||||||
|
return datetime.strptime(src, FMT_STD) |
||||||
|
elif Delimiter.DASH in src and Delimiter.COLON in src: |
||||||
|
return datetime.strptime(src, FMT_DASH) |
||||||
|
elif ( |
||||||
|
Delimiter.DASH not in src |
||||||
|
and Delimiter.BAR not in src |
||||||
|
and Delimiter.COLON not in src |
||||||
|
): |
||||||
|
return datetime.strptime(src, FMT_SHORT) |
||||||
|
else: |
||||||
|
raise NotImplementedError( |
||||||
|
"%s could not be convert to datetime for now.", src |
||||||
|
) |
||||||
|
else: |
||||||
|
raise NotImplementedError("%s could not be convert to datetime for now.", src) |
@ -0,0 +1,18 @@ |
|||||||
|
# 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. |
||||||
|
|
||||||
|
"""Init tests for utils package.""" |
@ -0,0 +1,71 @@ |
|||||||
|
# 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 utils.date module.""" |
||||||
|
|
||||||
|
import pytest |
||||||
|
from datetime import datetime |
||||||
|
from pydolphinscheduler.utils.date import ( |
||||||
|
conv_from_str, |
||||||
|
conv_to_schedule, |
||||||
|
FMT_STD, |
||||||
|
) |
||||||
|
|
||||||
|
curr_date = datetime.now() |
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize( |
||||||
|
"src,expect", |
||||||
|
[ |
||||||
|
(curr_date, curr_date.strftime(FMT_STD)), |
||||||
|
(datetime(2021, 1, 1), "2021-01-01 00:00:00"), |
||||||
|
(datetime(2021, 1, 1, 1), "2021-01-01 01:00:00"), |
||||||
|
(datetime(2021, 1, 1, 1, 1), "2021-01-01 01:01:00"), |
||||||
|
(datetime(2021, 1, 1, 1, 1, 1), "2021-01-01 01:01:01"), |
||||||
|
(datetime(2021, 1, 1, 1, 1, 1, 1), "2021-01-01 01:01:01"), |
||||||
|
], |
||||||
|
) |
||||||
|
def test_conv_to_schedule(src: datetime, expect: str) -> None: |
||||||
|
"""Test function conv_to_schedule.""" |
||||||
|
assert expect == conv_to_schedule(src) |
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize( |
||||||
|
"src,expect", |
||||||
|
[ |
||||||
|
("2021-01-01", datetime(2021, 1, 1)), |
||||||
|
("2021/01/01", datetime(2021, 1, 1)), |
||||||
|
("20210101", datetime(2021, 1, 1)), |
||||||
|
("2021-01-01 01:01:01", datetime(2021, 1, 1, 1, 1, 1)), |
||||||
|
("2021/01/01 01:01:01", datetime(2021, 1, 1, 1, 1, 1)), |
||||||
|
("20210101 010101", datetime(2021, 1, 1, 1, 1, 1)), |
||||||
|
], |
||||||
|
) |
||||||
|
def test_conv_from_str_success(src: str, expect: datetime) -> None: |
||||||
|
"""Test function conv_from_str success case.""" |
||||||
|
assert expect == conv_from_str( |
||||||
|
src |
||||||
|
), f"Function conv_from_str convert {src} not expect to {expect}." |
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize( |
||||||
|
"src", ["2021-01-01 010101", "2021:01:01", "202111", "20210101010101"] |
||||||
|
) |
||||||
|
def test_conv_from_str_not_impl(src: str) -> None: |
||||||
|
"""Test function conv_from_str fail case.""" |
||||||
|
with pytest.raises(NotImplementedError): |
||||||
|
conv_from_str(src) |
Loading…
Reference in new issue