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