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
|
|
View Code Duplication |
def describe_create(): |
|
|
|
|
39
|
|
|
|
40
|
|
|
def it_creates_files(model_class): |
41
|
|
|
instance = utilities.create(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.create(model_class, 'foo', 'bar') |
50
|
|
|
|
51
|
|
|
def it_can_overwrite_files(model_class, instance): |
52
|
|
|
instance.__mapper__.create() |
53
|
|
|
|
54
|
|
|
utilities.create(model_class, 'foo', 'bar', overwrite=True) |
55
|
|
|
|
56
|
|
|
def it_can_also_be_called_with_an_instance(instance): |
57
|
|
|
expect(yorm.create(instance)) == instance |
58
|
|
|
|
59
|
|
|
def it_requires_a_mapped_class_or_instance(): |
60
|
|
|
with expect.raises(TypeError): |
61
|
|
|
utilities.create(Mock) |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
def describe_find(): |
65
|
|
|
|
66
|
|
|
def it_returns_object_when_found(model_class, instance): |
67
|
|
|
instance.__mapper__.create() |
68
|
|
|
|
69
|
|
|
expect(utilities.find(model_class, 'foo', 'bar')) == instance |
70
|
|
|
|
71
|
|
|
def it_returns_none_when_no_match(model_class): |
72
|
|
|
expect(utilities.find(model_class, 'not', 'here')) == None |
73
|
|
|
|
74
|
|
|
def it_allows_objects_to_be_created(model_class): |
75
|
|
|
expect(utilities.find(model_class, 'new', 'one', create=True)) == \ |
76
|
|
|
model_class('new', 'one') |
77
|
|
|
|
78
|
|
|
def it_can_also_be_called_with_an_instance(instance): |
79
|
|
|
expect(yorm.find(instance, create=True)) == instance |
80
|
|
|
|
81
|
|
|
def it_requires_a_mapped_class_or_instance(): |
82
|
|
|
with expect.raises(TypeError): |
83
|
|
|
utilities.find(Mock) |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
def describe_match(): |
87
|
|
|
|
88
|
|
|
def it_is_not_yet_implemented(): |
89
|
|
|
with expect.raises(NotImplementedError): |
90
|
|
|
utilities.match(Mock) |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
def describe_load(): |
94
|
|
|
|
95
|
|
|
def it_marks_files_as_unmodified(instance): |
96
|
|
|
instance.__mapper__.create() |
97
|
|
|
instance.__mapper__.modified = True |
98
|
|
|
|
99
|
|
|
utilities.load(instance) |
100
|
|
|
|
101
|
|
|
expect(instance.__mapper__.modified) == False |
102
|
|
|
|
103
|
|
|
def it_requires_a_mapped_instance(): |
104
|
|
|
with expect.raises(TypeError): |
105
|
|
|
utilities.load(Mock) |
106
|
|
|
|
107
|
|
|
|
108
|
|
View Code Duplication |
def describe_save(): |
|
|
|
|
109
|
|
|
|
110
|
|
|
def it_creates_files(instance): |
111
|
|
|
utilities.save(instance) |
112
|
|
|
|
113
|
|
|
expect(instance.__mapper__.exists) == True |
114
|
|
|
|
115
|
|
|
def it_marks_files_as_modified(instance): |
116
|
|
|
instance.__mapper__.create() |
117
|
|
|
instance.__mapper__.modified = False |
118
|
|
|
|
119
|
|
|
utilities.save(instance) |
120
|
|
|
|
121
|
|
|
expect(instance.__mapper__.modified) == True |
122
|
|
|
|
123
|
|
|
def it_expects_the_file_to_not_be_deleted(instance): |
124
|
|
|
instance.__mapper__.delete() |
125
|
|
|
|
126
|
|
|
with expect.raises(exceptions.DeletedFileError): |
127
|
|
|
utilities.save(instance) |
128
|
|
|
|
129
|
|
|
def it_requires_a_mapped_instance(): |
130
|
|
|
with expect.raises(TypeError): |
131
|
|
|
utilities.save(Mock) |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
def describe_delete(): |
135
|
|
|
|
136
|
|
|
def it_deletes_files(instance): |
137
|
|
|
utilities.delete(instance) |
138
|
|
|
|
139
|
|
|
expect(instance.__mapper__.exists) == False |
140
|
|
|
expect(instance.__mapper__.deleted) == True |
141
|
|
|
|
142
|
|
|
def it_requires_a_mapped_instance(): |
143
|
|
|
with expect.raises(TypeError): |
144
|
|
|
utilities.delete(Mock) |
145
|
|
|
|