Coverage for pyfields/tests/issues/test_issue_84.py: 94%

49 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-11-06 16:35 +0000

1import sys 

2 

3import pytest 

4 

5try: 

6 from abc import ABC 

7except ImportError: 

8 from abc import ABCMeta 

9 

10 class ABC: 

11 __metaclass__ = ABCMeta 

12 

13 

14from pyfields import autofields, field, copy_value, autoclass 

15 

16 

17@pytest.mark.skipif(sys.version_info < (3,), reason="This test does not yet reproduce the exception in python 2") 

18@pytest.mark.parametrize("auto,deep", [(False, False), (False, True), (True, None)]) 

19def test_issue_deepcopy_autofields(auto, deep): 

20 """Make sure that """ 

21 

22 class NotCopiable(object): 

23 def __deepcopy__(self, memodict={}): 

24 raise NotImplementedError() 

25 

26 def __copy__(self): 

27 raise NotImplementedError() 

28 

29 default_value = NotCopiable() 

30 

31 if auto: 

32 with pytest.raises(ValueError) as exc_info: 

33 @autofields 

34 class Foo: 

35 a = default_value 

36 assert str(exc_info.value).startswith("The provided default value for field 'a'=%r can not be deep-copied" 

37 % (default_value, )) 

38 else: 

39 with pytest.raises(ValueError) as exc_info: 

40 class Foo: 

41 a = field(default_factory=copy_value(default_value, deep=deep)) 

42 

43 extra = "deep-" if deep else "" 

44 assert str(exc_info.value).startswith("The provided default value %r can not be %scopied" 

45 % (default_value, extra)) 

46 

47 

48def test_issue_84_autofields(): 

49 """Make sure that the _abc_impl field from ABC is excluded automatically""" 

50 

51 @autofields 

52 class Foo(ABC): 

53 a = 0 

54 

55 g = Foo() 

56 assert g.a == 0 

57 

58 if sys.version_info < (3, 7): 58 ↛ 60line 58 didn't jump to line 60, because the condition on line 58 was never true

59 # errors below wont be raised anyway 

60 return 

61 

62 with pytest.raises(ValueError) as exc_info: 

63 @autofields(exclude=()) 

64 class Foo(ABC): 

65 a = 0 

66 

67 assert str(exc_info.value).startswith("The provided default value for field '_abc_impl'=") 

68 

69 

70def test_issue_84_autoclass(): 

71 """Make sure that the _abc_impl field from ABC is excluded automatically""" 

72 

73 @autoclass 

74 class Foo(ABC): 

75 a = 0 

76 

77 f = Foo() 

78 assert str(f) == "Foo(a=0)" 

79 

80 if sys.version_info < (3, 7): 80 ↛ 82line 80 didn't jump to line 82, because the condition on line 80 was never true

81 # errors below wont be raised anyway 

82 return 

83 

84 with pytest.raises(ValueError) as exc_info: 

85 @autoclass(af_exclude=()) 

86 class Foo(ABC): 

87 a = 0 

88 

89 assert str(exc_info.value).startswith("The provided default value for field '_abc_impl'=")