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

38 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-10-07 01:52 +0000

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5"""Unit tests for checking tunable comparisons.""" 

6 

7import pytest 

8 

9from mlos_bench.tunables.covariant_group import CovariantTunableGroup 

10from mlos_bench.tunables.tunable import Tunable 

11from mlos_bench.tunables.tunable_groups import TunableGroups 

12 

13 

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

15 """Tests that the __lt__ operator works as expected.""" 

16 tunable_int_2 = tunable_int.copy() 

17 tunable_int_2.numerical_value += 1 

18 assert tunable_int.numerical_value < tunable_int_2.numerical_value 

19 assert tunable_int < tunable_int_2 

20 

21 

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

23 """Tests that the __lt__ operator works as expected.""" 

24 tunable_int_2 = tunable_int.copy() 

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

26 assert tunable_int_2 < tunable_int 

27 

28 

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

30 """Tests that the __lt__ operator works as expected.""" 

31 tunable_categorical_2 = tunable_categorical.copy() 

32 new_value = [ 

33 x 

34 for x in tunable_categorical.categories 

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

36 ][0] 

37 assert tunable_categorical.category is not None 

38 tunable_categorical_2.category = new_value 

39 if tunable_categorical.category < new_value: 

40 assert tunable_categorical < tunable_categorical_2 

41 elif tunable_categorical.category > new_value: 

42 assert tunable_categorical > tunable_categorical_2 

43 

44 

45def test_tunable_categorical_lt_null() -> None: 

46 """Tests that the __lt__ operator works as expected.""" 

47 tunable_cat = Tunable( 

48 name="same-name", 

49 config={ 

50 "type": "categorical", 

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

52 "default": "floof", 

53 }, 

54 ) 

55 tunable_dog = Tunable( 

56 name="same-name", 

57 config={ 

58 "type": "categorical", 

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

60 "default": None, 

61 }, 

62 ) 

63 assert tunable_dog < tunable_cat 

64 

65 

66def test_tunable_lt_same_name_different_type() -> None: 

67 """Tests that the __lt__ operator works as expected.""" 

68 tunable_cat = Tunable( 

69 name="same-name", 

70 config={ 

71 "type": "categorical", 

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

73 "default": "floof", 

74 }, 

75 ) 

76 tunable_int = Tunable( 

77 name="same-name", 

78 config={ 

79 "type": "int", 

80 "range": [1, 3], 

81 "default": 2, 

82 }, 

83 ) 

84 assert tunable_cat < tunable_int 

85 

86 

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

88 """Tests that the __lt__ operator works as expected.""" 

89 assert (tunable_int < "foo") is False 

90 with pytest.raises(TypeError): 

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

92 

93 

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

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

96 assert tunable_groups != "foo" 

97 

98 

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

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

101 assert covariant_group != "foo"