1
|
|
|
# pylint: disable=unused-variable,redefined-outer-name,expression-not-assigned,singleton-comparison |
|
|
|
|
2
|
|
|
|
3
|
|
|
import logging |
4
|
|
|
from unittest.mock import Mock |
5
|
|
|
|
6
|
|
|
import pytest |
7
|
|
|
from expecter import expect |
|
|
|
|
8
|
|
|
|
9
|
|
|
import yorm |
10
|
|
|
from yorm import exceptions |
11
|
|
|
from yorm import utilities |
12
|
|
|
|
13
|
|
|
log = logging.getLogger(__name__) |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
@pytest.fixture |
17
|
|
|
def model_class(tmpdir): |
18
|
|
|
tmpdir.chdir() |
19
|
|
|
|
20
|
|
|
@yorm.sync("data/{self.kind}/{self.key}.yml", auto_create=False) |
21
|
|
|
class Model: |
22
|
|
|
|
23
|
|
|
def __init__(self, kind, key): |
24
|
|
|
self.kind = kind |
25
|
|
|
self.key = key |
26
|
|
|
|
27
|
|
|
def __eq__(self, other): |
28
|
|
|
return (self.kind, self.key) == (other.kind, other.key) |
29
|
|
|
|
30
|
|
|
return Model |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
@pytest.fixture |
34
|
|
|
def instance(model_class): |
35
|
|
|
return model_class('foo', 'bar') |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
def describe_new(): |
39
|
|
|
|
40
|
|
|
def it_creates_files(model_class): |
41
|
|
|
instance = utilities.new(model_class, 'foo', 'bar') |
42
|
|
|
|
43
|
|
|
expect(instance.__mapper__.exists) == True |
44
|
|
|
|
45
|
|
|
def it_requires_files_to_not_yet_exist(model_class, instance): |
46
|
|
|
instance.__mapper__.create() |
47
|
|
|
|
48
|
|
|
with expect.raises(exceptions.DuplicateMappingError): |
49
|
|
|
utilities.new(model_class, 'foo', 'bar') |
50
|
|
|
|
51
|
|
|
def it_requires_a_mapped_object(): |
52
|
|
|
with expect.raises(TypeError): |
53
|
|
|
utilities.new(Mock) |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
def describe_find(): |
57
|
|
|
|
58
|
|
|
def it_returns_object_when_found(model_class, instance): |
59
|
|
|
instance.__mapper__.create() |
60
|
|
|
|
61
|
|
|
expect(utilities.find(model_class, 'foo', 'bar')) == instance |
62
|
|
|
|
63
|
|
|
def it_returns_none_when_no_match(model_class): |
64
|
|
|
expect(utilities.find(model_class, 'not', 'here')) == None |
65
|
|
|
|
66
|
|
|
def it_allows_objects_to_be_created(model_class): |
67
|
|
|
expect(utilities.find(model_class, 'new', 'one', create=True)) == \ |
68
|
|
|
model_class('new', 'one') |
69
|
|
|
|
70
|
|
|
def it_requires_a_mapped_object(): |
71
|
|
|
with expect.raises(TypeError): |
72
|
|
|
utilities.find(Mock) |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
def describe_load(): |
76
|
|
|
|
77
|
|
|
def it_is_not_yet_implemented(): |
78
|
|
|
with expect.raises(NotImplementedError): |
79
|
|
|
utilities.load(Mock) |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
def describe_save(): |
83
|
|
|
|
84
|
|
|
def it_creates_files(instance): |
85
|
|
|
utilities.save(instance) |
86
|
|
|
|
87
|
|
|
expect(instance.__mapper__.exists) == True |
88
|
|
|
|
89
|
|
|
def it_marks_files_as_modified(instance): |
90
|
|
|
instance.__mapper__.create() |
91
|
|
|
instance.__mapper__.modified = False |
92
|
|
|
|
93
|
|
|
utilities.save(instance) |
94
|
|
|
|
95
|
|
|
expect(instance.__mapper__.modified) == True |
96
|
|
|
|
97
|
|
|
def it_expects_the_file_to_not_be_deleted(instance): |
98
|
|
|
instance.__mapper__.delete() |
99
|
|
|
|
100
|
|
|
with expect.raises(exceptions.DeletedFileError): |
101
|
|
|
utilities.save(instance) |
102
|
|
|
|
103
|
|
|
def it_requires_a_mapped_object(): |
104
|
|
|
with expect.raises(TypeError): |
105
|
|
|
utilities.save(Mock) |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
def describe_delete(): |
109
|
|
|
|
110
|
|
|
def it_deletes_files(instance): |
111
|
|
|
utilities.delete(instance) |
112
|
|
|
|
113
|
|
|
expect(instance.__mapper__.exists) == False |
114
|
|
|
expect(instance.__mapper__.deleted) == True |
115
|
|
|
|
116
|
|
|
def it_requires_a_mapped_object(): |
117
|
|
|
with expect.raises(TypeError): |
118
|
|
|
utilities.delete(Mock) |
119
|
|
|
|