Coverage for mlos_bench/mlos_bench/tests/services/test_service_method_registering.py: 95%
21 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"""
6Unit tests for Service method registering.
7"""
9import pytest
11from mlos_bench.services.base_service import Service
13from mlos_bench.tests.services.mock_service import SupportsSomeMethod, MockServiceBase, MockServiceChild
16def test_service_method_register_without_constructor() -> None:
17 """
18 Test registering a method without a constructor.
19 """
20 # pylint: disable=protected-access
21 some_base_service = MockServiceBase()
22 some_child_service = MockServiceChild()
24 # create a mixin service that registers the base service methods
25 mixin_service = Service()
27 # Registering service methods inside a context should fail.
28 with pytest.raises(AssertionError):
29 with mixin_service as service_context:
30 service_context.register(some_base_service.export())
32 mixin_service.register(some_base_service.export())
33 # Make sure the base service instance got tracked registered
34 assert mixin_service._services == {some_base_service}
36 # pylint complains if we try to just assert this directly
37 # somehow having it in a different scope makes a difference
38 if isinstance(mixin_service, SupportsSomeMethod):
39 assert mixin_service.some_method() == f"{some_base_service}: base.some_method"
40 assert mixin_service.some_other_method() == f"{some_base_service}: base.some_other_method"
42 # register the child service
43 mixin_service.register(some_child_service.export())
44 # Make sure the child service instance got tracked registered
45 assert mixin_service._services == {some_child_service}
46 # check that the inheritance works as expected
47 assert mixin_service.some_method() == f"{some_child_service}: child.some_method"
48 assert mixin_service.some_other_method() == f"{some_child_service}: base.some_other_method"
49 else:
50 assert False