-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
489 additions
and
460 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# SPDX-FileCopyrightText: 2023 Hynek Schlawack <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
""" | ||
Interfaces used throughout the tests. They're dataclasses so they have a | ||
predicatable repr. | ||
""" | ||
|
||
import dataclasses | ||
|
||
|
||
@dataclasses.dataclass | ||
class Service: | ||
pass | ||
|
||
|
||
@dataclasses.dataclass | ||
class AnotherService: | ||
pass | ||
|
||
|
||
@dataclasses.dataclass | ||
class YetAnotherService: | ||
pass |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# SPDX-FileCopyrightText: 2023 Hynek Schlawack <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
|
||
from unittest.mock import Mock | ||
|
||
import pytest | ||
|
||
import svcs | ||
|
||
from .ifaces import AnotherService, Service | ||
|
||
|
||
class TestContainer: | ||
def test_get_pings_empty(self, container): | ||
""" | ||
get_pings returns an empty list if there are no pings. | ||
""" | ||
assert [] == container.get_pings() | ||
|
||
def test_forget_about_nothing_registered(self, container): | ||
""" | ||
forget_about does nothing if nothing has been registered. | ||
""" | ||
container.forget_about(Service) | ||
|
||
def test_forget_about_no_cleanup(self, container, rs, svc): | ||
""" | ||
forget_about removes the registered service from the container. | ||
""" | ||
container._instantiated[rs.svc_type] = (rs, svc) | ||
|
||
container.forget_about(Service) | ||
|
||
assert {} == container._instantiated | ||
assert [] == container._on_close | ||
|
||
@pytest.mark.asyncio() | ||
async def test_repr(self, registry, container): | ||
""" | ||
The repr counts correctly. | ||
""" | ||
|
||
def factory(): | ||
yield 42 | ||
|
||
async def async_factory(): | ||
yield 42 | ||
|
||
registry.register_factory(Service, factory) | ||
registry.register_factory(AnotherService, async_factory) | ||
|
||
container.get(Service) | ||
await container.aget(AnotherService) | ||
|
||
assert "<Container(instantiated=2, cleanups=2)>" == repr(container) | ||
|
||
|
||
class TestServicePing: | ||
def test_name(self, rs): | ||
""" | ||
The name property proxies the correct class name. | ||
""" | ||
|
||
assert "tests.ifaces.Service" == svcs.ServicePing(None, rs).name | ||
|
||
def test_ping(self, registry, container): | ||
""" | ||
Calling ping instantiates the service using its factory, appends it to | ||
the cleanup list, and calls the service's ping method. | ||
""" | ||
|
||
cleaned_up = False | ||
|
||
def factory(): | ||
nonlocal cleaned_up | ||
yield Service() | ||
cleaned_up = True | ||
|
||
ping = Mock(spec_set=["__call__"]) | ||
registry.register_factory(Service, factory, ping=ping) | ||
|
||
(svc_ping,) = container.get_pings() | ||
|
||
svc_ping.ping() | ||
|
||
ping.assert_called_once() | ||
|
||
assert not cleaned_up | ||
|
||
container.close() | ||
|
||
assert cleaned_up | ||
assert not container._instantiated | ||
assert not container._on_close |
Oops, something went wrong.