Coverage for mlos_bench/mlos_bench/tests/services/mock_service.py: 100%
16 statements
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-22 01:18 +0000
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-22 01:18 +0000
1#
2# Copyright (c) Microsoft Corporation.
3# Licensed under the MIT License.
4#
5"""
6Basic MockService for testing.
8See Also: test_service_method_registering.py
9"""
11from typing import Callable, Dict, List, Optional, Protocol, Union, runtime_checkable
13from mlos_bench.services.base_service import Service
16@runtime_checkable
17class SupportsSomeMethod(Protocol):
18 """Protocol for some_method."""
20 def some_method(self) -> str:
21 """some_method."""
23 def some_other_method(self) -> str:
24 """some_other_method."""
27class MockServiceBase(Service, SupportsSomeMethod):
28 """A base service class for testing."""
30 def __init__(
31 self,
32 config: Optional[dict] = None,
33 global_config: Optional[dict] = None,
34 parent: Optional[Service] = None,
35 methods: Optional[Union[Dict[str, Callable], List[Callable]]] = None,
36 ) -> None:
37 super().__init__(
38 config,
39 global_config,
40 parent,
41 self.merge_methods(
42 methods,
43 [
44 self.some_method,
45 self.some_other_method,
46 ],
47 ),
48 )
50 def some_method(self) -> str:
51 """some_method."""
52 return f"{self}: base.some_method"
54 def some_other_method(self) -> str:
55 """some_other_method."""
56 return f"{self}: base.some_other_method"
59class MockServiceChild(MockServiceBase, SupportsSomeMethod):
60 """A child service class for testing."""
62 # Intentionally includes no constructor.
64 def some_method(self) -> str:
65 """some_method."""
66 return f"{self}: child.some_method"