Coverage for mlos_bench/mlos_bench/tests/services/mock_service.py: 100%

17 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""" 

6Basic MockService for testing. 

7 

8See Also: test_service_method_registering.py 

9""" 

10 

11from typing import Callable, Dict, List, Optional, Protocol, Union, runtime_checkable 

12 

13from mlos_bench.services.base_service import Service 

14 

15 

16@runtime_checkable 

17class SupportsSomeMethod(Protocol): 

18 """Protocol for some_method""" 

19 

20 def some_method(self) -> str: 

21 """some_method""" 

22 

23 def some_other_method(self) -> str: 

24 """some_other_method""" 

25 

26 

27class MockServiceBase(Service, SupportsSomeMethod): 

28 """A base service class for testing.""" 

29 

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) -> None: 

36 super().__init__( 

37 config, 

38 global_config, 

39 parent, 

40 self.merge_methods(methods, [ 

41 self.some_method, 

42 self.some_other_method, 

43 ])) 

44 

45 def some_method(self) -> str: 

46 """some_method""" 

47 return f"{self}: base.some_method" 

48 

49 def some_other_method(self) -> str: 

50 """some_other_method""" 

51 return f"{self}: base.some_other_method" 

52 

53 

54class MockServiceChild(MockServiceBase, SupportsSomeMethod): 

55 """A child service class for testing.""" 

56 

57 # Intentionally includes no constructor. 

58 

59 def some_method(self) -> str: 

60 """some_method""" 

61 return f"{self}: child.some_method"