Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hynek committed Aug 2, 2023
1 parent 9cefc13 commit 403f991
Show file tree
Hide file tree
Showing 6 changed files with 489 additions and 460 deletions.
11 changes: 11 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from sybil import Sybil
from sybil.parsers import myst, rest
from tests.ifaces import Service

import svcs

Expand All @@ -32,6 +33,16 @@
pytest_collect_file = (markdown_examples + rest_examples).pytest()


@pytest.fixture(name="svc")
def _svc():
return Service()


@pytest.fixture(name="rs")
def _rs(svc):
return svcs.RegisteredService(Service, Service, False, False, None)


@pytest.fixture(name="registry")
def _registry():
return svcs.Registry()
Expand Down
25 changes: 25 additions & 0 deletions tests/ifaces.py
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
188 changes: 0 additions & 188 deletions tests/test_async.py

This file was deleted.

96 changes: 96 additions & 0 deletions tests/test_container.py
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
Loading

0 comments on commit 403f991

Please sign in to comment.