Coverage for /pythoncovmergedfiles/medio/medio/src/airflow/tests/macros/test_macros.py: 0%

26 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:35 +0000

1# 

2# Licensed to the Apache Software Foundation (ASF) under one 

3# or more contributor license agreements. See the NOTICE file 

4# distributed with this work for additional information 

5# regarding copyright ownership. The ASF licenses this file 

6# to you under the Apache License, Version 2.0 (the 

7# "License"); you may not use this file except in compliance 

8# with the License. You may obtain a copy of the License at 

9# 

10# http://www.apache.org/licenses/LICENSE-2.0 

11# 

12# Unless required by applicable law or agreed to in writing, 

13# software distributed under the License is distributed on an 

14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 

15# KIND, either express or implied. See the License for the 

16# specific language governing permissions and limitations 

17# under the License. 

18from __future__ import annotations 

19 

20import lazy_object_proxy 

21import pendulum 

22import pytest 

23 

24from airflow import macros 

25from airflow.utils import timezone 

26 

27 

28@pytest.mark.parametrize( 

29 "ds, days, expected", 

30 [ 

31 ("2015-01-01", 5, "2015-01-06"), 

32 ("2015-01-02", 0, "2015-01-02"), 

33 ("2015-01-06", -5, "2015-01-01"), 

34 (lazy_object_proxy.Proxy(lambda: "2015-01-01"), 5, "2015-01-06"), 

35 (lazy_object_proxy.Proxy(lambda: "2015-01-02"), 0, "2015-01-02"), 

36 (lazy_object_proxy.Proxy(lambda: "2015-01-06"), -5, "2015-01-01"), 

37 ], 

38) 

39def test_ds_add(ds, days, expected): 

40 result = macros.ds_add(ds, days) 

41 assert result == expected 

42 

43 

44@pytest.mark.parametrize( 

45 "ds, input_format, output_format, expected", 

46 [ 

47 ("2015-01-02", "%Y-%m-%d", "%m-%d-%y", "01-02-15"), 

48 ("2015-01-02", "%Y-%m-%d", "%Y-%m-%d", "2015-01-02"), 

49 ("1/5/2015", "%m/%d/%Y", "%m-%d-%y", "01-05-15"), 

50 ("1/5/2015", "%m/%d/%Y", "%Y-%m-%d", "2015-01-05"), 

51 (lazy_object_proxy.Proxy(lambda: "2015-01-02"), "%Y-%m-%d", "%m-%d-%y", "01-02-15"), 

52 (lazy_object_proxy.Proxy(lambda: "2015-01-02"), "%Y-%m-%d", "%Y-%m-%d", "2015-01-02"), 

53 (lazy_object_proxy.Proxy(lambda: "1/5/2015"), "%m/%d/%Y", "%m-%d-%y", "01-05-15"), 

54 (lazy_object_proxy.Proxy(lambda: "1/5/2015"), "%m/%d/%Y", "%Y-%m-%d", "2015-01-05"), 

55 ], 

56) 

57def test_ds_format(ds, input_format, output_format, expected): 

58 result = macros.ds_format(ds, input_format, output_format) 

59 assert result == expected 

60 

61 

62@pytest.mark.parametrize( 

63 "dt, since, expected", 

64 [ 

65 ( 

66 timezone.datetime(2017, 1, 2), 

67 None, 

68 pendulum.instance(timezone.datetime(2017, 1, 2)).diff_for_humans(), 

69 ), 

70 (timezone.datetime(2017, 1, 2), timezone.datetime(2017, 1, 3), "1 day before"), 

71 (timezone.datetime(2017, 1, 2), timezone.datetime(2017, 1, 1), "1 day after"), 

72 ( 

73 lazy_object_proxy.Proxy(lambda: timezone.datetime(2017, 1, 2)), 

74 None, 

75 pendulum.instance(timezone.datetime(2017, 1, 2)).diff_for_humans(), 

76 ), 

77 ( 

78 lazy_object_proxy.Proxy(lambda: timezone.datetime(2017, 1, 2)), 

79 timezone.datetime(2017, 1, 3), 

80 "1 day before", 

81 ), 

82 ( 

83 lazy_object_proxy.Proxy(lambda: timezone.datetime(2017, 1, 2)), 

84 timezone.datetime(2017, 1, 1), 

85 "1 day after", 

86 ), 

87 ], 

88) 

89def test_datetime_diff_for_humans(dt, since, expected): 

90 result = macros.datetime_diff_for_humans(dt, since) 

91 assert result == expected 

92 

93 

94@pytest.mark.parametrize( 

95 "input_value, expected", 

96 [ 

97 ('{"field1":"value1", "field2":4, "field3":true}', {"field1": "value1", "field2": 4, "field3": True}), 

98 ( 

99 '{"field1": [ 1, 2, 3, 4, 5 ], "field2" : {"mini1" : 1, "mini2" : "2"}}', 

100 {"field1": [1, 2, 3, 4, 5], "field2": {"mini1": 1, "mini2": "2"}}, 

101 ), 

102 ], 

103) 

104def test_json_loads(input_value, expected): 

105 result = macros.json.loads(input_value) 

106 assert result == expected 

107 

108 

109@pytest.mark.parametrize( 

110 "input_value, expected", 

111 [ 

112 ('{"field1":"value1", "field2":4, "field3":true}', {"field1": "value1", "field2": 4, "field3": True}), 

113 ("field1: value1\nfield2: value2", {"field1": "value1", "field2": "value2"}), 

114 ( 

115 'field1: [ 1, 2, 3, 4, 5 ]\nfield2: {"mini1" : 1, "mini2" : "2"}', 

116 {"field1": [1, 2, 3, 4, 5], "field2": {"mini1": 1, "mini2": "2"}}, 

117 ), 

118 ], 

119) 

120def test_yaml_loads(input_value, expected): 

121 result = macros.yaml.safe_load(input_value) 

122 assert result == expected