1
|
|
|
"""Integration tests using YORM as a persistence model.""" |
2
|
|
|
# pylint: disable=missing-docstring,no-self-use,misplaced-comparison-constant |
|
|
|
|
3
|
|
|
|
4
|
|
|
import os |
5
|
|
|
|
6
|
|
|
from expecter import expect |
|
|
|
|
7
|
|
|
|
8
|
|
|
import yorm |
9
|
|
|
from yorm.types import String |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
# CLASSES ###################################################################### |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
class Config: |
16
|
|
|
"""Domain model.""" |
17
|
|
|
|
18
|
|
|
def __init__(self, key, name=None, root=None): |
19
|
|
|
self.key = key |
20
|
|
|
self.name = name or "" |
21
|
|
|
self.root = root or "" |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
@yorm.attr(key=String) |
25
|
|
|
@yorm.attr(name=String) |
26
|
|
|
@yorm.sync("{self.root}/{self.key}/config.yml", |
27
|
|
|
auto_create=False, auto_save=False) |
28
|
|
|
class ConfigModel: |
29
|
|
|
"""Persistence model.""" |
30
|
|
|
|
31
|
|
|
def __init__(self, key, root): |
32
|
|
|
self.key = key |
33
|
|
|
self.root = root |
34
|
|
|
print(self.key) |
35
|
|
|
self.unmapped = 0 |
36
|
|
|
|
37
|
|
|
@staticmethod |
38
|
|
|
def pm_to_dm(model): |
39
|
|
|
config = Config(model.key) |
40
|
|
|
config.name = model.name |
41
|
|
|
config.root = model.root |
42
|
|
|
return config |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
class ConfigStore: |
46
|
|
|
|
47
|
|
|
def __init__(self, root): |
48
|
|
|
self.root = root |
49
|
|
|
|
50
|
|
|
def read(self, key): |
51
|
|
|
return yorm.find(ConfigModel, self.root, key) |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
# TESTS ######################################################################## |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
class TestPersistanceMapping: # pylint: disable=no-member |
58
|
|
|
|
59
|
|
|
root = os.path.join(os.path.dirname(__file__), 'files') |
60
|
|
|
|
61
|
|
|
def test_load_pm(self): |
62
|
|
|
model = ConfigModel('my_key', self.root) |
63
|
|
|
|
64
|
|
|
print(model.__dict__) |
65
|
|
|
assert model.key == "my_key" |
66
|
|
|
assert model.root == self.root |
67
|
|
|
assert model.name == "my_name" |
68
|
|
|
|
69
|
|
|
def test_create_dm_from_pm(self): |
70
|
|
|
model = ConfigModel('my_key', self.root) |
71
|
|
|
config = ConfigModel.pm_to_dm(model) |
72
|
|
|
|
73
|
|
|
print(config.__dict__) |
74
|
|
|
assert config.key == "my_key" |
75
|
|
|
assert config.root == self.root |
76
|
|
|
assert config.name == "my_name" |
77
|
|
|
|
78
|
|
|
def test_nonmapped_attribute_is_kept(self): |
79
|
|
|
model = ConfigModel('my_key', self.root) |
80
|
|
|
model.unmapped = 42 |
81
|
|
|
assert 42 == model.unmapped |
82
|
|
|
|
83
|
|
|
def test_missing_files_are_handled(self): |
84
|
|
|
model = ConfigModel('my_key_manual', self.root) |
85
|
|
|
|
86
|
|
|
with expect.raises(yorm.exceptions.MissingFileError): |
87
|
|
|
print(model.name) |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
class TestStore: |
91
|
|
|
|
92
|
|
|
def test_read_missing(self, tmpdir): |
93
|
|
|
store = ConfigStore(str(tmpdir)) |
94
|
|
|
assert None is store.read('unknown') |
95
|
|
|
|