TestPersistanceMapping.test_missing_files_are_handled()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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