Issues (15)

tests/test_ordering.py (1 issue)

Severity
1
# pylint: disable=redefined-outer-name,expression-not-assigned,attribute-defined-outside-init,no-member
2
3
from expecter import expect
0 ignored issues
show
Unable to import 'expecter'
Loading history...
4
5
import yorm
6
7
from . import strip
8
9
10
@yorm.attr(status=yorm.types.Boolean)
11
@yorm.attr(label=yorm.types.String)
12
class StatusDictionary(yorm.types.Dictionary):
13
    """Sample dictionary converter with ordered attributes."""
14
15
16
@yorm.attr(string=yorm.types.String)
17
@yorm.attr(number_int=yorm.types.Integer)
18
@yorm.attr(dictionary=StatusDictionary)
19
@yorm.attr(number_real=yorm.types.Float)
20
@yorm.attr(truthy=yorm.types.Boolean)
21
@yorm.attr(falsey=yorm.types.Boolean)
22
@yorm.sync("sample.yml")
23
class Sample:
24
    """Sample class with ordered attributes."""
25
26
27
def test_attribute_order_is_maintained(tmpdir):
28
    tmpdir.chdir()
29
    sample = Sample()
30
    sample.string = "Hello, world!"
31
    sample.number_int = 42
32
    sample.number_real = 4.2
33
    # pylint: disable=duplicate-code
34
    sample.truthy = False
35
    sample.falsey = True
36
    sample.dictionary['status'] = 1
37
38
    expect(sample.__mapper__.text) == strip("""
39
    string: Hello, world!
40
    number_int: 42
41
    dictionary:
42
      status: true
43
      label: ''
44
    number_real: 4.2
45
    truthy: false
46
    falsey: true
47
    """)
48
49
50
def test_existing_files_are_reorderd(tmpdir):
51
    tmpdir.chdir()
52
    with open("sample.yml", 'w') as stream:
53
        stream.write(strip("""
54
        falsey: 1
55
        number_int: 2
56
        number_real: 3
57
        string: 4
58
        truthy: 5
59
        dictionary: {label: foo}
60
        """))
61
    sample = Sample()
62
    sample.falsey = 0
63
64
    expect(sample.__mapper__.text) == strip("""
65
    string: 4
66
    number_int: 2
67
    dictionary:
68
      status: false
69
      label: foo
70
    number_real: 3.0
71
    truthy: true
72
    falsey: false
73
    """)
74