tests.test_fake.Sample.__init__()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
"""Integration tests for the `yorm.settings.fake` option."""
2
3
# pylint: disable=missing-docstring,no-self-use,no-member,misplaced-comparison-constant
4
5
import os
6
from unittest.mock import patch
7
8
import yorm
9
10
from . import strip
11
12
13
# CLASSES #####################################################################
14
15
16
@yorm.attr(value=yorm.types.standard.Integer)
17
@yorm.sync("tmp/path/to/{self.name}.yml")
18
class Sample:
19
    """Sample class for fake mapping."""
20
21
    def __init__(self, name):
22
        self.name = name
23
        self.value = 0
24
25
    def __repr__(self):
26
        return "<sample {}>".format(id(self))
27
28
29
# TESTS #######################################################################
30
31
32
@patch('yorm.settings.fake', True)
33
class TestFake:
34
    """Integration tests with `yorm.settings.fake` enabled."""
35
36
    def test_no_file_create_when_fake(self, tmpdir):
37
        tmpdir.chdir()
38
        sample = Sample('sample')
39
40
        # ensure no file is created
41
        assert "tmp/path/to/sample.yml" == sample.__mapper__.path
42
        assert not os.path.exists(sample.__mapper__.path)
43
44
        # change object values
45
        sample.value = 42
46
47
        # check fake file
48
        assert strip("""
49
        value: 42
50
        """) == sample.__mapper__.text
51
52
        # ensure no file is created
53
        assert not os.path.exists(sample.__mapper__.path)
54
55
        # change fake file
56
        sample.__mapper__.text = "value: 0\n"
57
58
        # check object values
59
        assert 0 == sample.value
60
61
        # ensure no file is created
62
        assert not os.path.exists(sample.__mapper__.path)
63
64
    def test_fake_changes_indicate_modified(self, tmpdir):
65
        tmpdir.chdir()
66
        sample = Sample('sample')
67
68
        assert False is sample.__mapper__.modified
69
        assert 0 == sample.value
70
71
        sample.__mapper__.text = "value: 42\n"
72
73
        assert True is sample.__mapper__.modified
74
        assert 42 == sample.value
75
        assert False is sample.__mapper__.modified
76
77
78
class TestReal:
79
    """Integration tests with `yorm.settings.fake` disabled."""
80
81
    def test_setting_text_updates_attributes(self, tmpdir):
82
        tmpdir.chdir()
83
        sample = Sample('sample')
84
85
        sample.__mapper__.text = "value: 42"
86
87
        assert 42 == sample.value
88
89
    def test_setting_attributes_update_text(self, tmpdir):
90
        tmpdir.chdir()
91
        sample = Sample('sample')
92
93
        sample.value = 42
94
95
        assert strip("""
96
        value: 42
97
        """) == sample.__mapper__.text
98