Coverage for mlos_bench/mlos_bench/tests/tunables/tunable_comparison_test.py: 97%

39 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 checking tunable comparisons. 

7""" 

8 

9import pytest 

10 

11from mlos_bench.tunables.covariant_group import CovariantTunableGroup 

12from mlos_bench.tunables.tunable import Tunable 

13from mlos_bench.tunables.tunable_groups import TunableGroups 

14 

15 

16def test_tunable_int_value_lt(tunable_int: Tunable) -> None: 

17 """ 

18 Tests that the __lt__ operator works as expected. 

19 """ 

20 tunable_int_2 = tunable_int.copy() 

21 tunable_int_2.numerical_value += 1 

22 assert tunable_int.numerical_value < tunable_int_2.numerical_value 

23 assert tunable_int < tunable_int_2 

24 

25 

26def test_tunable_int_name_lt(tunable_int: Tunable) -> None: 

27 """ 

28 Tests that the __lt__ operator works as expected. 

29 """ 

30 tunable_int_2 = tunable_int.copy() 

31 tunable_int_2._name = "aaa" # pylint: disable=protected-access 

32 assert tunable_int_2 < tunable_int 

33 

34 

35def test_tunable_categorical_value_lt(tunable_categorical: Tunable) -> None: 

36 """ 

37 Tests that the __lt__ operator works as expected. 

38 """ 

39 tunable_categorical_2 = tunable_categorical.copy() 

40 new_value = [ 

41 x for x in tunable_categorical.categories 

42 if x != tunable_categorical.category and x is not None 

43 ][0] 

44 assert tunable_categorical.category is not None 

45 tunable_categorical_2.category = new_value 

46 if tunable_categorical.category < new_value: 

47 assert tunable_categorical < tunable_categorical_2 

48 elif tunable_categorical.category > new_value: 

49 assert tunable_categorical > tunable_categorical_2 

50 

51 

52def test_tunable_categorical_lt_null() -> None: 

53 """ 

54 Tests that the __lt__ operator works as expected. 

55 """ 

56 tunable_cat = Tunable( 

57 name="same-name", 

58 config={ 

59 "type": "categorical", 

60 "values": ["floof", "fuzz"], 

61 "default": "floof", 

62 } 

63 ) 

64 tunable_dog = Tunable( 

65 name="same-name", 

66 config={ 

67 "type": "categorical", 

68 "values": [None, "doggo"], 

69 "default": None, 

70 } 

71 ) 

72 assert tunable_dog < tunable_cat 

73 

74 

75def test_tunable_lt_same_name_different_type() -> None: 

76 """ 

77 Tests that the __lt__ operator works as expected. 

78 """ 

79 tunable_cat = Tunable( 

80 name="same-name", 

81 config={ 

82 "type": "categorical", 

83 "values": ["floof", "fuzz"], 

84 "default": "floof", 

85 } 

86 ) 

87 tunable_int = Tunable( 

88 name="same-name", 

89 config={ 

90 "type": "int", 

91 "range": [1, 3], 

92 "default": 2, 

93 } 

94 ) 

95 assert tunable_cat < tunable_int 

96 

97 

98def test_tunable_lt_different_object(tunable_int: Tunable) -> None: 

99 """ 

100 Tests that the __lt__ operator works as expected. 

101 """ 

102 assert (tunable_int < "foo") is False 

103 with pytest.raises(TypeError): 

104 assert "foo" < tunable_int # type: ignore[operator] 

105 

106 

107def test_tunable_group_ne_object(tunable_groups: TunableGroups) -> None: 

108 """ 

109 Tests that the __eq__ operator works as expected with other objects. 

110 """ 

111 assert tunable_groups != "foo" 

112 

113 

114def test_covariant_group_ne_object(covariant_group: CovariantTunableGroup) -> None: 

115 """ 

116 Tests that the __eq__ operator works as expected with other objects. 

117 """ 

118 assert covariant_group != "foo"