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

30 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"""Tests for checking the indexing rules for tunable groups.""" 

6 

7from mlos_bench.tunables.tunable import Tunable 

8from mlos_bench.tunables.tunable_groups import TunableGroups 

9 

10 

11def test_tunable_group_indexing( 

12 tunable_groups: TunableGroups, 

13 tunable_categorical: Tunable, 

14) -> None: 

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

16 # Check that the "in" operator works. 

17 assert tunable_categorical in tunable_groups 

18 assert tunable_categorical.name in tunable_groups 

19 

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

21 # different fixtures so are technically different objects. 

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

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

24 

25 assert tunable_categorical in covariant_group 

26 assert tunable_categorical.name in covariant_group 

27 

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

29 assert covariant_group.get_tunable(tunable_categorical) == tunable_categorical 

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

31 

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

33 tunable_categorical.value = tunable_categorical.default 

34 

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

36 assert tunable_groups[tunable_categorical] == tunable_categorical.value 

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

38 assert covariant_group[tunable_categorical] == tunable_categorical.value 

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

40 

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

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

43 tunable_groups[tunable_categorical] = new_value 

44 assert tunable_groups[tunable_categorical] == new_value 

45 assert tunable_groups[tunable_categorical.name] == new_value 

46 assert covariant_group[tunable_categorical] == new_value 

47 assert covariant_group[tunable_categorical.name] == new_value 

48 assert tunable_categorical.value == new_value 

49 assert tunable_categorical.value != tunable_categorical.default 

50 

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

52 tunable_groups[tunable_categorical] = tunable_categorical.default 

53 assert tunable_categorical.value == tunable_categorical.default 

54 assert tunable_groups[tunable_categorical] == tunable_categorical.value 

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

56 assert covariant_group[tunable_categorical] == tunable_categorical.value 

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