Coverage for pyfields/tests/test_helpers.py: 100%

24 statements  

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

1import pytest 

2 

3from pyfields import field, get_field_values, get_fields, copy_field 

4from pyfields.core import PY36 

5 

6 

7@pytest.mark.parametrize("a_first", [False, True], ids="ancestor_first={}".format) 

8@pytest.mark.parametrize("public_only", [False, True], ids="public_only={}".format) 

9def test_get_fields(a_first, public_only): 

10 class A(object): 

11 a = field() 

12 _d = field(default=5) 

13 

14 class B(object): 

15 b = field() 

16 

17 class C(B, A): 

18 a = field(default=None) 

19 c = field(default_factory=copy_field('b')) 

20 

21 fields = get_fields(C, include_inherited=True, ancestors_first=a_first, 

22 _auto_fix_fields=not PY36, public_only=public_only) 

23 field_names = [f.name for f in fields] 

24 if a_first: 

25 assert field_names == ['a', 'b', 'c'] if public_only else ['a', '_d', 'b', 'c'] 

26 else: 

27 assert field_names == ['a', 'c', 'b'] if public_only else ['a', 'c', 'b', '_d'] 

28 

29 obj = C() 

30 obj.b = 2 

31 

32 fields = get_field_values(obj, ancestors_first=a_first if a_first is not None else True, _auto_fix_fields=not PY36, 

33 container_type=list, public_only=public_only) 

34 if a_first is None or a_first: 

35 assert fields == [('a', None), ('b', 2), ('c', 2)] if public_only else [('a', None), ('_d', 5), ('b', 2), ('c', 2)] 

36 else: 

37 assert fields == [('a', None), ('c', 2), ('b', 2)] if public_only else [('a', None), ('c', 2), ('b', 2), ('_d', 5)]