Coverage for tests/test_base.py: 100%
32 statements
« prev ^ index » next coverage.py v7.14.1, created at 2026-10-30 22:54 +0000
« prev ^ index » next coverage.py v7.14.1, created at 2026-10-30 22:54 +0000
1"""Tests the `wonka` base classes."""
3from __future__ import annotations
5import dataclasses
6from typing import Any
8import pytest
9from conftest import Adder
11import wonka
12from wonka import base
15@dataclasses.dataclass
16class Counter(wonka.Manager):
17 """Manager that counts its constructors.
19 Args:
20 contents: stored constructors. Defaults to an empty list.
22 """
24 contents: list[Any] = dataclasses.field(default_factory = list)
26 def manage(self, item: Any, **kwargs: Any) -> Any:
27 """Returns `item` and the number of stored constructors.
29 Args:
30 item: item to return.
31 **kwargs: ignored keyword arguments.
33 Returns:
34 A `tuple` of `item` and the length of `contents`.
36 """
37 return item, len(self.contents)
40def test_base_classes_are_abstract() -> None:
41 """Tests that the base classes cannot be instanced."""
42 with pytest.raises(TypeError):
43 base.Factory() # type: ignore[abstract]
44 with pytest.raises(TypeError):
45 base.Manager([]) # type: ignore[abstract]
46 with pytest.raises(TypeError):
47 base.Producer() # type: ignore[abstract]
48 with pytest.raises(TypeError):
49 base.Cluster() # type: ignore[abstract]
52def test_manager_create_calls_manage() -> None:
53 """Tests that `Manager.create` is an alias for `manage`."""
54 counter = Counter([Adder, Adder])
55 assert counter.create('x') == ('x', 2)
56 assert counter.manage('x') == ('x', 2)
59def test_manager_iterates_contents() -> None:
60 """Tests that iterating a `Manager` iterates its `contents`."""
61 assert list(Counter([Adder])) == [Adder]
64def test_manager_is_constructor() -> None:
65 """Tests that a `Manager` instance is a `wonka` constructor."""
66 assert wonka.is_constructor(Counter())
67 assert not wonka.is_constructor(Counter)
70def test_manufacturer_is_cluster() -> None:
71 """Tests that `Manufacturer` is a `Cluster` subclass."""
72 assert issubclass(wonka.Manufacturer, wonka.Cluster)