Coverage for mlos_bench/mlos_bench/tests/services/test_service_method_registering.py: 95%
20 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 00:44 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 00:44 +0000
1#
2# Copyright (c) Microsoft Corporation.
3# Licensed under the MIT License.
4#
5"""Unit tests for Service method registering."""
7import pytest
9from mlos_bench.services.base_service import Service
10from mlos_bench.tests.services.mock_service import (
11 MockServiceBase,
12 MockServiceChild,
13 SupportsSomeMethod,
14)
17def test_service_method_register_without_constructor() -> None:
18 """Test registering a method without a constructor."""
19 # pylint: disable=protected-access
20 some_base_service = MockServiceBase()
21 some_child_service = MockServiceChild()
23 # create a mixin service that registers the base service methods
24 mixin_service = Service()
26 # Registering service methods inside a context should fail.
27 with pytest.raises(AssertionError):
28 with mixin_service as service_context:
29 service_context.register(some_base_service.export())
31 mixin_service.register(some_base_service.export())
32 # Make sure the base service instance got tracked registered
33 assert mixin_service._services == {some_base_service}
35 # pylint complains if we try to just assert this directly
36 # somehow having it in a different scope makes a difference
37 if isinstance(mixin_service, SupportsSomeMethod):
38 assert mixin_service.some_method() == f"{some_base_service}: base.some_method"
39 assert mixin_service.some_other_method() == f"{some_base_service}: base.some_other_method"
41 # register the child service
42 mixin_service.register(some_child_service.export())
43 # Make sure the child service instance got tracked registered
44 assert mixin_service._services == {some_child_service}
45 # check that the inheritance works as expected
46 assert mixin_service.some_method() == f"{some_child_service}: child.some_method"
47 assert mixin_service.some_other_method() == f"{some_child_service}: base.some_other_method"
48 else:
49 assert False