Coverage for mlos_bench/mlos_bench/tests/environments/__init__.py: 100%
25 statements
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-06 00:35 +0000
« 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 helpers for mlos_bench.environments.
7"""
9from datetime import datetime
10from typing import Any, Dict, List, Optional, Tuple
12import pytest
14from mlos_bench.environments.base_environment import Environment
16from mlos_bench.tunables.tunable import TunableValue
17from mlos_bench.tunables.tunable_groups import TunableGroups
20def check_env_success(env: Environment,
21 tunable_groups: TunableGroups,
22 expected_results: Dict[str, TunableValue],
23 expected_telemetry: List[Tuple[datetime, str, Any]],
24 global_config: Optional[dict] = None) -> None:
25 """
26 Set up an environment and run a test experiment there.
28 Parameters
29 ----------
30 tunable_groups : TunableGroups
31 Tunable parameters (usually come from a fixture).
32 env : Environment
33 An environment to query for the results.
34 expected_results : Dict[str, float]
35 Expected results of the benchmark.
36 expected_telemetry : List[Tuple[datetime, str, Any]]
37 Expected telemetry data of the benchmark.
38 global_config : dict
39 Global params.
40 """
41 with env as env_context:
43 assert env_context.setup(tunable_groups, global_config)
45 (status, _ts, data) = env_context.run()
46 assert status.is_succeeded()
47 assert data == pytest.approx(expected_results, nan_ok=True)
49 (status, _ts, telemetry) = env_context.status()
50 assert status.is_good()
51 assert telemetry == pytest.approx(expected_telemetry, nan_ok=True)
53 env_context.teardown()
54 assert not env_context._is_ready # pylint: disable=protected-access
57def check_env_fail_telemetry(env: Environment, tunable_groups: TunableGroups) -> None:
58 """
59 Set up a local environment and run a test experiment there;
60 Make sure the environment `.status()` call fails.
62 Parameters
63 ----------
64 tunable_groups : TunableGroups
65 Tunable parameters (usually come from a fixture).
66 env : Environment
67 An environment to query for the results.
68 """
69 with env as env_context:
71 assert env_context.setup(tunable_groups)
72 (status, _ts, _data) = env_context.run()
73 assert status.is_succeeded()
75 with pytest.raises(ValueError):
76 env_context.status()