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
« 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.
5import pytest
7from typing import List, Optional
8from pyfields import field, inject_fields, MandatoryFieldInitError, make_init, autofields, autoclass
11def _test_class_annotations():
12 class Foo:
13 field_with_validate_type: int = field(check_type=True)
14 field_with_defaults: str = field()
16 return Foo
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.")
24 return Wall
27def _test_readme_value_validation(colors):
28 from mini_lambda import x
29 from valid8.validation_lib import is_in
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.")
39 return Wall
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
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)
57 @inject_fields(height, color)
58 def __init__(self, fields):
59 with pytest.raises(MandatoryFieldInitError):
60 print(self.height)
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)
69 @inject_fields
70 def __init__(self, fields):
71 with pytest.raises(MandatoryFieldInitError):
72 print(self.height)
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)
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)
102 def post_init(self, foo: str = 'bar'):
103 print(self.height)
104 print("post init man !")
106 __init__ = make_init(post_init_fun=post_init)
107 else:
108 raise ValueError(init_type)
110 return Wall
113def _test_autofields(type_check):
114 if type_check:
115 _deco = autofields(check_types=True)
116 else:
117 _deco = autofields
119 @_deco
120 class Foo:
121 CONSTANT: str = 's'
122 __a__: int = 0
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
130 class cls:
131 pass
133 def fct(self):
134 return 1
136 return Foo
139def _test_autofields_readme():
141 @autofields(make_init=True)
142 class Item:
143 name: str
145 @autofields
146 class Pocket:
147 size: int
148 items: List[Item] = []
150 @autofields
151 class Pocket2:
152 size: int
153 items: List[Item] = []
154 def __init__(self, who):
155 print("hello, %s" % who)
157 return Pocket, Item, Pocket2
160def _test_autofields_vtypes_readme():
161 from vtypes import VType
163 class PositiveInt(int, VType):
164 __validators__ = {'should be positive': lambda x: x >= 0}
166 @autofields(check_types=True)
167 class Rectangle:
168 x: PositiveInt
169 y: PositiveInt
171 return Rectangle
174def test_issue_74():
175 @autofields
176 class City:
177 name: Optional[str]
178 buildings: List[str] = []
180 return City
183def test_issue_76():
184 @autofields
185 class Foo:
186 c: int
187 b: str = "hello"
188 a: int = field(default=50)
190 return Foo
193def _test_autoclass2():
194 @autoclass()
195 class Foo:
196 msg: str
197 age: int = 12
198 height: int = field(default=50)
200 return Foo
203def _test_autoclass3():
205 @autoclass(typecheck=True, dict=False)
206 class Foo:
207 msg: str
208 age: int = 12
209 height: int = field(default=50)
211 return Foo