Coverage for mlos_bench/mlos_bench/tests/dict_templater_test.py: 100%

26 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-05 00:36 +0000

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5""" 

6Unit tests for DictTemplater class. 

7""" 

8 

9from copy import deepcopy 

10from typing import Any, Dict 

11 

12import pytest 

13 

14from mlos_bench.dict_templater import DictTemplater 

15from mlos_bench.os_environ import environ 

16 

17 

18@pytest.fixture 

19def source_template_dict() -> Dict[str, Any]: 

20 """A source dictionary with template variables.""" 

21 return { 

22 "extra_str-ref": "$extra_str-ref", 

23 "str": "string", 

24 "str_ref": "$str-ref", 

25 "secondary_expansion": "${str_ref}", 

26 "tertiary_expansion": "$secondary_expansion", 

27 "int": 1, 

28 "int_ref": "$int-ref", 

29 "float": 1.0, 

30 "float_ref": "$float-ref", 

31 "bool": True, 

32 "bool_ref": "$bool-ref", 

33 "list": [ 

34 "$str", 

35 "$int", 

36 "$float", 

37 ], 

38 "dict": { 

39 "nested-str-ref": "nested-$str-ref", 

40 "nested-extra-str-ref": "nested-$extra_str-ref", 

41 }, 

42 } 

43 

44 

45# pylint: disable=redefined-outer-name 

46 

47 

48def test_no_side_effects(source_template_dict: Dict[str, Any]) -> None: 

49 """ 

50 Test that the templater does not modify the source dictionary. 

51 """ 

52 source_template_dict_copy = deepcopy(source_template_dict) 

53 results = DictTemplater(source_template_dict_copy).expand_vars() 

54 assert results 

55 assert source_template_dict_copy == source_template_dict 

56 

57 

58def test_secondary_expansion(source_template_dict: Dict[str, Any]) -> None: 

59 """ 

60 Test that internal expansions work as expected. 

61 """ 

62 results = DictTemplater(source_template_dict).expand_vars() 

63 assert results == { 

64 "extra_str-ref": "$extra_str-ref", 

65 "str": "string", 

66 "str_ref": "string-ref", 

67 "secondary_expansion": "string-ref", 

68 "tertiary_expansion": "string-ref", 

69 "int": 1, 

70 "int_ref": "1-ref", 

71 "float": 1.0, 

72 "float_ref": "1.0-ref", 

73 "bool": True, 

74 "bool_ref": "True-ref", 

75 "list": [ 

76 "string", 

77 "1", 

78 "1.0", 

79 ], 

80 "dict": { 

81 "nested-str-ref": "nested-string-ref", 

82 "nested-extra-str-ref": "nested-$extra_str-ref", 

83 }, 

84 } 

85 

86 

87def test_os_env_expansion(source_template_dict: Dict[str, Any]) -> None: 

88 """ 

89 Test that expansions from OS env work as expected. 

90 """ 

91 environ["extra_str"] = "os-env-extra_str" 

92 environ["string"] = "shouldn't be used" 

93 

94 results = DictTemplater(source_template_dict).expand_vars(use_os_env=True) 

95 assert results == { 

96 "extra_str-ref": f"{environ['extra_str']}-ref", 

97 "str": "string", 

98 "str_ref": "string-ref", 

99 "secondary_expansion": "string-ref", 

100 "tertiary_expansion": "string-ref", 

101 "int": 1, 

102 "int_ref": "1-ref", 

103 "float": 1.0, 

104 "float_ref": "1.0-ref", 

105 "bool": True, 

106 "bool_ref": "True-ref", 

107 "list": [ 

108 "string", 

109 "1", 

110 "1.0", 

111 ], 

112 "dict": { 

113 "nested-str-ref": "nested-string-ref", 

114 "nested-extra-str-ref": f"nested-{environ['extra_str']}-ref", 

115 }, 

116 } 

117 

118 

119def test_from_extras_expansion(source_template_dict: Dict[str, Any]) -> None: 

120 """ 

121 Test that 

122 """ 

123 extra_source_dict = { 

124 "extra_str": "str-from-extras", 

125 "string": "shouldn't be used", 

126 } 

127 results = DictTemplater(source_template_dict).expand_vars(extra_source_dict=extra_source_dict) 

128 assert results == { 

129 "extra_str-ref": f"{extra_source_dict['extra_str']}-ref", 

130 "str": "string", 

131 "str_ref": "string-ref", 

132 "secondary_expansion": "string-ref", 

133 "tertiary_expansion": "string-ref", 

134 "int": 1, 

135 "int_ref": "1-ref", 

136 "float": 1.0, 

137 "float_ref": "1.0-ref", 

138 "bool": True, 

139 "bool_ref": "True-ref", 

140 "list": [ 

141 "string", 

142 "1", 

143 "1.0", 

144 ], 

145 "dict": { 

146 "nested-str-ref": "nested-string-ref", 

147 "nested-extra-str-ref": f"nested-{extra_source_dict['extra_str']}-ref", 

148 }, 

149 }