Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2APIUtil test module 

3""" 

4import copy 

5import logging 

6from datetime import datetime, timedelta 

7from unittest import TestCase 

8 

9import pytest 

10from sdc_etl_libs.api_helpers import APIUtils 

11from sdc_etl_libs.api_helpers.SDCAPIExceptions import \ 

12 DeltaTimeExpressionParserException 

13 

14 

15class TestAPIUtils(TestCase): 

16 """ 

17 Test suite for Module APIUtils 

18 """ 

19 

20 @classmethod 

21 def setup_class(cls): 

22 """ 

23 Code block executed once on instance creation time 

24 """ 

25 logging.info("setup class: %s execution", cls.__name__) 

26 

27 @classmethod 

28 def teardown_class(cls): 

29 """ 

30 Code block executed once on instance destruction time 

31 """ 

32 logging.info("teardown class: %s execution", cls.__name__) 

33 

34 # pylint: disable=R0201 

35 def setup_method(self, method): 

36 """ 

37 Code block executed before every test execution 

38 """ 

39 logging.info("setup execution of test case: %s", method.__name__) 

40 

41 # pylint: disable=R0201 

42 def teardown_method(self, method): 

43 """ 

44 Code block executed after every test execution 

45 """ 

46 logging.info("teardown execution of test case: %s", method.__name__) 

47 

48 # pylint: disable=R0201 

49 def test_parse_mult_value_time_delta(self): 

50 """ 

51 Validate logic related to date ranges calculated based on a given delta, and validate parsing logic of delta 

52 input string 

53 """ 

54 granularity_map1 = APIUtils.parse_mult_value_time_delta("23h59m59s") 

55 granularity_map2 = APIUtils.parse_mult_value_time_delta("86399s") 

56 granularity_map3 = APIUtils.parse_mult_value_time_delta("59s59m23h0d") 

57 

58 assert granularity_map1 == {"d": 0, "h": 23, "m": 59, "s": 59} 

59 assert granularity_map2 == {"d": 0, "h": 0, "m": 0, "s": 86399} 

60 assert granularity_map3 == {"d": 0, "h": 23, "m": 59, "s": 59} 

61 

62 execution_datetime = datetime(2020, 6, 1, 23, 59, 59, 0) 

63 expected_datetime = datetime(2020, 6, 1, 0, 0, 0, 0) 

64 

65 start_datetime = ( 

66 execution_datetime - timedelta( 

67 days=granularity_map1["d"], 

68 hours=granularity_map1["h"], 

69 minutes=granularity_map1["m"], 

70 seconds=granularity_map1["s"])) 

71 alternative1_start_date_time = ( 

72 execution_datetime - timedelta( 

73 days=granularity_map2["d"], 

74 hours=granularity_map2["h"], 

75 minutes=granularity_map2["m"], 

76 seconds=granularity_map2["s"])) 

77 alternative2_start_date_time = ( 

78 execution_datetime - timedelta( 

79 days=granularity_map3["d"], 

80 hours=granularity_map3["h"], 

81 minutes=granularity_map3["m"], 

82 seconds=granularity_map3["s"])) 

83 

84 assert expected_datetime == start_datetime == alternative1_start_date_time == alternative2_start_date_time, \ 

85 "Expected datetime and calculated datetimes are different!!!" 

86 

87 # pylint: disable=R0201 

88 def test_inv_input_parse_time_delta(self): 

89 """ 

90 Validate proper behavior if invalid input delta string is found. 

91 """ 

92 with pytest.raises(DeltaTimeExpressionParserException) as exc_info: 

93 granularity_map1 = APIUtils.parse_mult_value_time_delta("23h2t59m59s") 

94 logging.error("Should never print this: %s", granularity_map1) 

95 # Validate accessException message + debug_message 

96 assert "Expression has invalid tokens:" in str(exc_info.value) 

97