Coverage for tests/test_shared.py: 96%
55 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 shared `wonka` functions."""
3from __future__ import annotations
5from typing import Any
7from conftest import Adder, Thing
9import wonka
12class Duck:
13 """Class with a `create` class method that is not a `wonka` class."""
15 @classmethod
16 def create(cls, item: Any) -> Any:
17 """Returns `item`.
19 Args:
20 item: item to return.
22 Returns:
23 `item`, unchanged.
25 """
26 return item
29class InstanceDuck:
30 """Class with a `create` instance method."""
32 def create(self, item: Any) -> Any:
33 """Returns `item`.
35 Args:
36 item: item to return.
38 Returns:
39 `item`, unchanged.
41 """
42 return item
45def test_finalize_class_with_parameters() -> None:
46 """Tests that a class is instanced with `parameters`."""
47 assert wonka.finalize(Thing, {'value': 4}).value == 4
50def test_finalize_instance_with_parameters() -> None:
51 """Tests that `parameters` are injected into an instance."""
52 thing = Thing()
53 result = wonka.finalize(thing, {'value': 9})
54 assert result is thing
55 assert thing.value == 9
58def test_finalize_without_parameters() -> None:
59 """Tests that an item is returned unchanged without `parameters`."""
60 assert wonka.finalize(Thing) is Thing
63def test_finalize_uses_produce_of_item() -> None:
64 """Tests that the `produce` method of `item` is used."""
66 class Made(wonka.Classer):
67 """`Classer` subclass that is its own item."""
69 assert wonka.finalize(Made(), {'ignored': True}) is Made
72def test_finalize_uses_produce_of_factory() -> None:
73 """Tests that the `produce` method of `factory` is used."""
74 result = wonka.finalize(Thing, factory = wonka.Instancer)
75 assert result == Thing()
78def test_inject_attributes_overwrite_rules() -> None:
79 """Tests the `overwrite` argument and empty `parameters`."""
80 thing = Thing(value = 1)
81 wonka.inject_attributes(thing, {'value': 2}, overwrite = False)
82 assert thing.value == 1
83 wonka.inject_attributes(thing, {'value': 2}, overwrite = True)
84 assert thing.value == 2
85 assert wonka.inject_attributes(thing, None) is thing
86 assert wonka.inject_attributes(thing, {}) is thing
89def test_inject_attributes_uses_global_overwrite_rule() -> None:
90 """Tests that the global overwrite rule is used by default."""
91 thing = Thing(value = 1)
92 wonka.set_overwrite_rule(False)
93 wonka.inject_attributes(thing, {'value': 2, 'extra': 3})
94 assert thing.value == 1
95 assert thing.extra == 3 # type: ignore[attr-defined]
96 wonka.set_overwrite_rule(True)
97 wonka.inject_attributes(thing, {'value': 2})
98 assert thing.value == 2
101def test_is_constructor_strict() -> None:
102 """Tests strict compatibility, which requires `wonka` classes."""
103 assert wonka.is_constructor(wonka.Scribe)
104 assert wonka.is_constructor(Adder())
105 assert wonka.is_constructor(wonka.Assembler())
106 assert not wonka.is_constructor(Duck)
107 assert not wonka.is_constructor(3)
110def test_is_constructor_relaxed() -> None:
111 """Tests relaxed compatibility, which allows any `create` method."""
112 wonka.set_compatibility_rule(False)
113 assert wonka.is_constructor(Duck)
114 assert wonka.is_constructor(InstanceDuck())
115 assert not wonka.is_constructor(InstanceDuck)
116 assert not wonka.is_constructor(3)