Coverage for pyfields/tests/_test_py36.py: 98%

130 statements  

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

1# Authors: Sylvain Marie <sylvain.marie@se.com> 

2# 

3# Copyright (c) Schneider Electric Industries, 2019. All right reserved. 

4 

5import pytest 

6 

7from typing import List, Optional 

8from pyfields import field, inject_fields, MandatoryFieldInitError, make_init, autofields, autoclass 

9 

10 

11def _test_class_annotations(): 

12 class Foo: 

13 field_with_validate_type: int = field(check_type=True) 

14 field_with_defaults: str = field() 

15 

16 return Foo 

17 

18 

19def _test_readme_type_validation(): 

20 class Wall(object): 

21 height: int = field(check_type=True, doc="Height of the wall in mm.") 

22 color: str = field(check_type=True, default='white', doc="Color of the wall.") 

23 

24 return Wall 

25 

26 

27def _test_readme_value_validation(colors): 

28 from mini_lambda import x 

29 from valid8.validation_lib import is_in 

30 

31 class Wall(object): 

32 height: int = field(validators={'should be a positive number': x > 0, 

33 'should be a multiple of 100': x % 100 == 0}, 

34 doc="Height of the wall in mm.") 

35 color: str = field(validators=is_in(colors), 

36 default='white', 

37 doc="Color of the wall.") 

38 

39 return Wall 

40 

41 

42def test_value_validation_advanced(validate_width): 

43 class Wall(object): 

44 height: int = field(doc="Height of the wall in mm.") 

45 width: str = field(validators=validate_width, 

46 doc="Width of the wall in mm.") 

47 return Wall 

48 

49 

50def _test_readme_constructor(explicit_fields_list, init_type, native): 

51 if init_type == 'inject_fields': 

52 if explicit_fields_list: 

53 class Wall: 

54 height: int = field(doc="Height of the wall in mm.", native=native) 

55 color: str = field(default='white', doc="Color of the wall.", native=native) 

56 

57 @inject_fields(height, color) 

58 def __init__(self, fields): 

59 with pytest.raises(MandatoryFieldInitError): 

60 print(self.height) 

61 

62 # initialize all fields received 

63 fields.init(self) 

64 else: 

65 class Wall: 

66 height: int = field(doc="Height of the wall in mm.", native=native) 

67 color: str = field(default='white', doc="Color of the wall.", native=native) 

68 

69 @inject_fields 

70 def __init__(self, fields): 

71 with pytest.raises(MandatoryFieldInitError): 

72 print(self.height) 

73 

74 # initialize all fields received 

75 fields.init(self) 

76 elif init_type == 'make_init': 

77 if explicit_fields_list: 

78 class Wall(object): 

79 height: int = field(doc="Height of the wall in mm.", native=native) 

80 color: str = field(default='white', doc="Color of the wall.", native=native) 

81 __init__ = make_init(height, color) 

82 else: 

83 class Wall(object): 

84 height: int = field(doc="Height of the wall in mm.", native=native) 

85 color: str = field(default='white', doc="Color of the wall.", native=native) 

86 __init__ = make_init() 

87 elif init_type == 'make_init_with_postinit': 87 ↛ 108line 87 didn't jump to line 108, because the condition on line 87 was never false

88 if explicit_fields_list: 

89 class Wall(object): 

90 height: int = field(doc="Height of the wall in mm.", native=native) 

91 color: str = field(default='white', doc="Color of the wall.", native=native) 

92 

93 def post_init(self, foo: str = 'bar'): 

94 print(self.height) 

95 print("post init man !") 

96 __init__ = make_init(height, color, post_init_fun=post_init) 

97 else: 

98 class Wall(object): 

99 height: int = field(doc="Height of the wall in mm.", native=native) 

100 color: str = field(default='white', doc="Color of the wall.", native=native) 

101 

102 def post_init(self, foo: str = 'bar'): 

103 print(self.height) 

104 print("post init man !") 

105 

106 __init__ = make_init(post_init_fun=post_init) 

107 else: 

108 raise ValueError(init_type) 

109 

110 return Wall 

111 

112 

113def _test_autofields(type_check): 

114 if type_check: 

115 _deco = autofields(check_types=True) 

116 else: 

117 _deco = autofields 

118 

119 @_deco 

120 class Foo: 

121 CONSTANT: str = 's' 

122 __a__: int = 0 

123 

124 foo: int 

125 bar = 0 

126 barcls = int 

127 barfunc = lambda x: x 127 ↛ exitline 127 didn't run the lambda on line 127

128 barbar: str 

129 

130 class cls: 

131 pass 

132 

133 def fct(self): 

134 return 1 

135 

136 return Foo 

137 

138 

139def _test_autofields_readme(): 

140 

141 @autofields(make_init=True) 

142 class Item: 

143 name: str 

144 

145 @autofields 

146 class Pocket: 

147 size: int 

148 items: List[Item] = [] 

149 

150 @autofields 

151 class Pocket2: 

152 size: int 

153 items: List[Item] = [] 

154 def __init__(self, who): 

155 print("hello, %s" % who) 

156 

157 return Pocket, Item, Pocket2 

158 

159 

160def _test_autofields_vtypes_readme(): 

161 from vtypes import VType 

162 

163 class PositiveInt(int, VType): 

164 __validators__ = {'should be positive': lambda x: x >= 0} 

165 

166 @autofields(check_types=True) 

167 class Rectangle: 

168 x: PositiveInt 

169 y: PositiveInt 

170 

171 return Rectangle 

172 

173 

174def test_issue_74(): 

175 @autofields 

176 class City: 

177 name: Optional[str] 

178 buildings: List[str] = [] 

179 

180 return City 

181 

182 

183def test_issue_76(): 

184 @autofields 

185 class Foo: 

186 c: int 

187 b: str = "hello" 

188 a: int = field(default=50) 

189 

190 return Foo 

191 

192 

193def _test_autoclass2(): 

194 @autoclass() 

195 class Foo: 

196 msg: str 

197 age: int = 12 

198 height: int = field(default=50) 

199 

200 return Foo 

201 

202 

203def _test_autoclass3(): 

204 

205 @autoclass(typecheck=True, dict=False) 

206 class Foo: 

207 msg: str 

208 age: int = 12 

209 height: int = field(default=50) 

210 

211 return Foo