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