test_date.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. """Test utils.date module."""
  18. from datetime import datetime
  19. import pytest
  20. from pydolphinscheduler.utils.date import FMT_STD, conv_from_str, conv_to_schedule
  21. curr_date = datetime.now()
  22. @pytest.mark.parametrize(
  23. "src,expect",
  24. [
  25. (curr_date, curr_date.strftime(FMT_STD)),
  26. (datetime(2021, 1, 1), "2021-01-01 00:00:00"),
  27. (datetime(2021, 1, 1, 1), "2021-01-01 01:00:00"),
  28. (datetime(2021, 1, 1, 1, 1), "2021-01-01 01:01:00"),
  29. (datetime(2021, 1, 1, 1, 1, 1), "2021-01-01 01:01:01"),
  30. (datetime(2021, 1, 1, 1, 1, 1, 1), "2021-01-01 01:01:01"),
  31. ],
  32. )
  33. def test_conv_to_schedule(src: datetime, expect: str) -> None:
  34. """Test function conv_to_schedule."""
  35. assert expect == conv_to_schedule(src)
  36. @pytest.mark.parametrize(
  37. "src,expect",
  38. [
  39. ("2021-01-01", datetime(2021, 1, 1)),
  40. ("2021/01/01", datetime(2021, 1, 1)),
  41. ("20210101", datetime(2021, 1, 1)),
  42. ("2021-01-01 01:01:01", datetime(2021, 1, 1, 1, 1, 1)),
  43. ("2021/01/01 01:01:01", datetime(2021, 1, 1, 1, 1, 1)),
  44. ("20210101 010101", datetime(2021, 1, 1, 1, 1, 1)),
  45. ],
  46. )
  47. def test_conv_from_str_success(src: str, expect: datetime) -> None:
  48. """Test function conv_from_str success case."""
  49. assert expect == conv_from_str(
  50. src
  51. ), f"Function conv_from_str convert {src} not expect to {expect}."
  52. @pytest.mark.parametrize(
  53. "src",
  54. [
  55. "2021-01-01 010101",
  56. "2021:01:01",
  57. "202111",
  58. "20210101010101",
  59. "2021:01:01 01:01:01",
  60. ],
  61. )
  62. def test_conv_from_str_not_impl(src: str) -> None:
  63. """Test function conv_from_str fail case."""
  64. with pytest.raises(
  65. NotImplementedError, match=".*? could not be convert to datetime for now."
  66. ):
  67. conv_from_str(src)