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

25 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-20 00:44 +0000

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5"""Unit tests for DictTemplater class.""" 

6 

7from copy import deepcopy 

8from typing import Any, Dict 

9 

10import pytest 

11 

12from mlos_bench.dict_templater import DictTemplater 

13from mlos_bench.os_environ import environ 

14 

15 

16@pytest.fixture 

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

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

19 return { 

20 "extra_str-ref": "$extra_str-ref", 

21 "str": "string", 

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

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

24 "tertiary_expansion": "$secondary_expansion", 

25 "int": 1, 

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

27 "float": 1.0, 

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

29 "bool": True, 

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

31 "list": [ 

32 "$str", 

33 "$int", 

34 "$float", 

35 ], 

36 "dict": { 

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

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

39 }, 

40 } 

41 

42 

43# pylint: disable=redefined-outer-name 

44 

45 

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

47 """Test that the templater does not modify the source dictionary.""" 

48 source_template_dict_copy = deepcopy(source_template_dict) 

49 results = DictTemplater(source_template_dict_copy).expand_vars() 

50 assert results 

51 assert source_template_dict_copy == source_template_dict 

52 

53 

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

55 """Test that internal expansions work as expected.""" 

56 results = DictTemplater(source_template_dict).expand_vars() 

57 assert results == { 

58 "extra_str-ref": "$extra_str-ref", 

59 "str": "string", 

60 "str_ref": "string-ref", 

61 "secondary_expansion": "string-ref", 

62 "tertiary_expansion": "string-ref", 

63 "int": 1, 

64 "int_ref": "1-ref", 

65 "float": 1.0, 

66 "float_ref": "1.0-ref", 

67 "bool": True, 

68 "bool_ref": "True-ref", 

69 "list": [ 

70 "string", 

71 "1", 

72 "1.0", 

73 ], 

74 "dict": { 

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

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

77 }, 

78 } 

79 

80 

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

82 """Test that expansions from OS env work as expected.""" 

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

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

85 

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

87 assert results == { 

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

89 "str": "string", 

90 "str_ref": "string-ref", 

91 "secondary_expansion": "string-ref", 

92 "tertiary_expansion": "string-ref", 

93 "int": 1, 

94 "int_ref": "1-ref", 

95 "float": 1.0, 

96 "float_ref": "1.0-ref", 

97 "bool": True, 

98 "bool_ref": "True-ref", 

99 "list": [ 

100 "string", 

101 "1", 

102 "1.0", 

103 ], 

104 "dict": { 

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

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

107 }, 

108 } 

109 

110 

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

112 """Test that.""" 

113 extra_source_dict = { 

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

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

116 } 

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

118 assert results == { 

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

120 "str": "string", 

121 "str_ref": "string-ref", 

122 "secondary_expansion": "string-ref", 

123 "tertiary_expansion": "string-ref", 

124 "int": 1, 

125 "int_ref": "1-ref", 

126 "float": 1.0, 

127 "float_ref": "1.0-ref", 

128 "bool": True, 

129 "bool_ref": "True-ref", 

130 "list": [ 

131 "string", 

132 "1", 

133 "1.0", 

134 ], 

135 "dict": { 

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

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

138 }, 

139 }