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

tests.SampleStandardDecorated   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 26
Duplicated Lines 0 %
Metric Value
dl 0
loc 26
rs 10
wmc 2
1
"""Integration tests for file IO."""
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 logging
6
7
import pytest
8
9
import yorm
10
from yorm.types import Integer, String, Float, Boolean, Dictionary, List
11
12
from . import refresh_file_modification_times
13
14
15
# CLASSES ######################################################################
16
17
18
class EmptyDictionary(Dictionary):
19
    """Sample dictionary container."""
20
21
22
@yorm.attr(all=Integer)
23
class IntegerList(List):
24
    """Sample list container."""
25
26
27 View Code Duplication
@yorm.attr(array=IntegerList)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
28
@yorm.attr(false=Boolean)
29
@yorm.attr(number_int=Integer)
30
@yorm.attr(number_real=Float)
31
@yorm.attr(object=EmptyDictionary)
32
@yorm.attr(string=String)
33
@yorm.attr(true=Boolean)
34
@yorm.sync("path/to/{self.category}/{self.name}.yml")
35
class SampleStandardDecorated:
36
    """Sample class using standard attribute types."""
37
38
    def __init__(self, name, category='default'):
39
        self.name = name
40
        self.category = category
41
        # https://docs.python.org/3.4/library/json.html#json.JSONDecoder
42
        self.object = {}
43
        self.array = []
44
        self.string = ""
45
        self.number_int = 0
46
        self.number_real = 0.0
47
        self.true = True
48
        self.false = False
49
        self.null = None
50
51
    def __repr__(self):
52
        return "<decorated {}>".format(id(self))
53
54
55
# TESTS ########################################################################
56
57
58
class TestCreate:
59
    """Integration tests for creating mapped classes."""
60
61
    def test_fetch_from_existing(self, tmpdir):
62
        """Verify attributes are updated from an existing file."""
63
        tmpdir.chdir()
64
        sample = SampleStandardDecorated('sample')
65
        sample2 = SampleStandardDecorated('sample')
66
        assert sample2.__mapper__.path == sample.__mapper__.path
67
68
        refresh_file_modification_times()
69
70
        logging.info("Changing values in object 1...")
71
        sample.array = [0, 1, 2]
72
        sample.string = "Hello, world!"
73
        sample.number_int = 42
74
        sample.number_real = 4.2
75
        sample.true = True
76
        sample.false = False
77
78
        logging.info("Reading changed values in object 2...")
79
        assert [0, 1, 2] == sample2.array
80
        assert "Hello, world!" == sample2.string
81
        assert 42 == sample2.number_int
82
        assert 4.2 == sample2.number_real
83
        assert True is sample2.true
84
        assert False is sample2.false
85
86
87
class TestDelete:
88
    """Integration tests for deleting files."""
89
90
    def test_read(self, tmpdir):
91
        """Verify a deleted file cannot be read from."""
92
        tmpdir.chdir()
93
        sample = SampleStandardDecorated('sample')
94
        sample.__mapper__.delete()
95
96
        with pytest.raises(FileNotFoundError):
97
            print(sample.string)
98
99
        with pytest.raises(FileNotFoundError):
100
            sample.string = "def456"
101
102
    def test_write(self, tmpdir):
103
        """Verify a deleted file cannot be written to."""
104
        tmpdir.chdir()
105
        sample = SampleStandardDecorated('sample')
106
        sample.__mapper__.delete()
107
108
        with pytest.raises(FileNotFoundError):
109
            sample.string = "def456"
110
111
    def test_multiple(self, tmpdir):
112
        """Verify a deleted file can be deleted again."""
113
        tmpdir.chdir()
114
        sample = SampleStandardDecorated('sample')
115
        sample.__mapper__.delete()
116
        sample.__mapper__.delete()
117
118
119
class TestUpdate:
120
    """Integration tests for updating files/object."""
121
122
    def test_automatic_store_after_first_modification(self, tmpdir):
123
        tmpdir.chdir()
124
        sample = SampleStandardDecorated('sample')
125
        assert "number_int: 0\n" in sample.__mapper__.text
126
127
        sample.number_int = 42
128
        assert "number_int: 42\n" in sample.__mapper__.text
129
130
        sample.__mapper__.text = "number_int: true\n"
131
        assert 1 is sample.number_int
132
        assert "number_int: 1\n" in sample.__mapper__.text
133
134
    def test_automatic_store_after_first_modification_on_list(self, tmpdir):
135
        tmpdir.chdir()
136
        sample = SampleStandardDecorated('sample')
137
        assert "array: []\n" in sample.__mapper__.text
138
139
        sample.array.append(42)
140
        assert "array:\n- 42\n" in sample.__mapper__.text
141
142
        sample.__mapper__.text = "array: [true]\n"
143
        iter(sample.array)
144
        assert "array:\n- 1\n" in sample.__mapper__.text
145