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

1"""Tests the `wonka` base classes.""" 

2 

3from __future__ import annotations 

4 

5import dataclasses 

6from typing import Any 

7 

8import pytest 

9from conftest import Adder 

10 

11import wonka 

12from wonka import base 

13 

14 

15@dataclasses.dataclass 

16class Counter(wonka.Manager): 

17 """Manager that counts its constructors. 

18 

19 Args: 

20 contents: stored constructors. Defaults to an empty list. 

21 

22 """ 

23 

24 contents: list[Any] = dataclasses.field(default_factory = list) 

25 

26 def manage(self, item: Any, **kwargs: Any) -> Any: 

27 """Returns `item` and the number of stored constructors. 

28 

29 Args: 

30 item: item to return. 

31 **kwargs: ignored keyword arguments. 

32 

33 Returns: 

34 A `tuple` of `item` and the length of `contents`. 

35 

36 """ 

37 return item, len(self.contents) 

38 

39 

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] 

50 

51 

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) 

57 

58 

59def test_manager_iterates_contents() -> None: 

60 """Tests that iterating a `Manager` iterates its `contents`.""" 

61 assert list(Counter([Adder])) == [Adder] 

62 

63 

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) 

68 

69 

70def test_manufacturer_is_cluster() -> None: 

71 """Tests that `Manufacturer` is a `Cluster` subclass.""" 

72 assert issubclass(wonka.Manufacturer, wonka.Cluster)