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

1"""Tests the shared `wonka` functions.""" 

2 

3from __future__ import annotations 

4 

5from typing import Any 

6 

7from conftest import Adder, Thing 

8 

9import wonka 

10 

11 

12class Duck: 

13 """Class with a `create` class method that is not a `wonka` class.""" 

14 

15 @classmethod 

16 def create(cls, item: Any) -> Any: 

17 """Returns `item`. 

18 

19 Args: 

20 item: item to return. 

21 

22 Returns: 

23 `item`, unchanged. 

24 

25 """ 

26 return item 

27 

28 

29class InstanceDuck: 

30 """Class with a `create` instance method.""" 

31 

32 def create(self, item: Any) -> Any: 

33 """Returns `item`. 

34 

35 Args: 

36 item: item to return. 

37 

38 Returns: 

39 `item`, unchanged. 

40 

41 """ 

42 return item 

43 

44 

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 

48 

49 

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 

56 

57 

58def test_finalize_without_parameters() -> None: 

59 """Tests that an item is returned unchanged without `parameters`.""" 

60 assert wonka.finalize(Thing) is Thing 

61 

62 

63def test_finalize_uses_produce_of_item() -> None: 

64 """Tests that the `produce` method of `item` is used.""" 

65 

66 class Made(wonka.Classer): 

67 """`Classer` subclass that is its own item.""" 

68 

69 assert wonka.finalize(Made(), {'ignored': True}) is Made 

70 

71 

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() 

76 

77 

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 

87 

88 

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 

99 

100 

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) 

108 

109 

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)