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

19 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-21 01:50 +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 collections.abc import Callable 

12from typing import Protocol, runtime_checkable 

13 

14from mlos_bench.services.base_service import Service 

15 

16 

17@runtime_checkable 

18class SupportsSomeMethod(Protocol): 

19 """Protocol for some_method.""" 

20 

21 # pylint: disable=unnecessary-ellipsis 

22 

23 def some_method(self) -> str: 

24 """some_method.""" 

25 ... 

26 

27 def some_other_method(self) -> str: 

28 """some_other_method.""" 

29 ... 

30 

31 

32class MockServiceBase(Service, SupportsSomeMethod): 

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

34 

35 def __init__( 

36 self, 

37 config: dict | None = None, 

38 global_config: dict | None = None, 

39 parent: Service | None = None, 

40 methods: dict[str, Callable] | list[Callable] | None = None, 

41 ) -> None: 

42 super().__init__( 

43 config, 

44 global_config, 

45 parent, 

46 self.merge_methods( 

47 methods, 

48 [ 

49 self.some_method, 

50 self.some_other_method, 

51 ], 

52 ), 

53 ) 

54 

55 def some_method(self) -> str: 

56 """some_method.""" 

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

58 

59 def some_other_method(self) -> str: 

60 """some_other_method.""" 

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

62 

63 

64class MockServiceChild(MockServiceBase, SupportsSomeMethod): 

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

66 

67 # Intentionally includes no constructor. 

68 

69 def some_method(self) -> str: 

70 """some_method.""" 

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