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

16 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-10-07 01:52 +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, 

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 ) 

49 

50 def some_method(self) -> str: 

51 """some_method.""" 

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

53 

54 def some_other_method(self) -> str: 

55 """some_other_method.""" 

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

57 

58 

59class MockServiceChild(MockServiceBase, SupportsSomeMethod): 

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

61 

62 # Intentionally includes no constructor. 

63 

64 def some_method(self) -> str: 

65 """some_method.""" 

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