Completed
Push — develop ( b4a21c...b1df34 )
by Jace
02:37
created

tests.Sample   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 11
Duplicated Lines 0 %
Metric Value
dl 0
loc 11
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Sample.__repr__() 0 2 1
A Sample.__init__() 0 3 1
1
"""Integration tests for the `yorm.settings.fake` option."""
2
# pylint: disable=missing-docstring,no-self-use,no-member,misplaced-comparison-constant
0 ignored issues
show
introduced by
Bad option value 'misplaced-comparison-constant'
Loading history...
3
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("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 "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