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