Coverage for mlos_bench/mlos_bench/tests/tunables/tunable_group_indexing_test.py: 100%

31 statements  

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

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5""" 

6Tests for checking the indexing rules for tunable groups. 

7""" 

8 

9from mlos_bench.tunables.tunable import Tunable 

10from mlos_bench.tunables.tunable_groups import TunableGroups 

11 

12 

13def test_tunable_group_indexing(tunable_groups: TunableGroups, tunable_categorical: Tunable) -> None: 

14 """ 

15 Check that various types of indexing work for the tunable group. 

16 """ 

17 # Check that the "in" operator works. 

18 assert tunable_categorical in tunable_groups 

19 assert tunable_categorical.name in tunable_groups 

20 

21 # NOTE: we reassign the tunable_categorical here since they come from 

22 # different fixtures so are technically different objects. 

23 (tunable_categorical, covariant_group) = tunable_groups.get_tunable(tunable_categorical.name) 

24 assert tunable_groups.get_tunable(tunable_categorical)[0] == tunable_categorical 

25 

26 assert tunable_categorical in covariant_group 

27 assert tunable_categorical.name in covariant_group 

28 

29 # Check that we can lookup that tunable by name or tunable object in the covariant group. 

30 assert covariant_group.get_tunable(tunable_categorical) == tunable_categorical 

31 assert covariant_group.get_tunable(tunable_categorical.name) == tunable_categorical 

32 

33 # Reset the value on the tunable using the tunable. 

34 tunable_categorical.value = tunable_categorical.default 

35 

36 # Check that we can index by name or tunable object. 

37 assert tunable_groups[tunable_categorical] == tunable_categorical.value 

38 assert tunable_groups[tunable_categorical.name] == tunable_categorical.value 

39 assert covariant_group[tunable_categorical] == tunable_categorical.value 

40 assert covariant_group[tunable_categorical.name] == tunable_categorical.value 

41 

42 # Check that we can assign a new value by index. 

43 new_value = [x for x in tunable_categorical.categories if x != tunable_categorical.value][0] 

44 tunable_groups[tunable_categorical] = new_value 

45 assert tunable_groups[tunable_categorical] == new_value 

46 assert tunable_groups[tunable_categorical.name] == new_value 

47 assert covariant_group[tunable_categorical] == new_value 

48 assert covariant_group[tunable_categorical.name] == new_value 

49 assert tunable_categorical.value == new_value 

50 assert tunable_categorical.value != tunable_categorical.default 

51 

52 # Check that we can assign a new value by name. 

53 tunable_groups[tunable_categorical] = tunable_categorical.default 

54 assert tunable_categorical.value == tunable_categorical.default 

55 assert tunable_groups[tunable_categorical] == tunable_categorical.value 

56 assert tunable_groups[tunable_categorical.name] == tunable_categorical.value 

57 assert covariant_group[tunable_categorical] == tunable_categorical.value 

58 assert covariant_group[tunable_categorical.name] == tunable_categorical.value