This repository has been archived by the owner on Jul 17, 2018. It is now read-only.
/
test_mixins.py
222 lines (168 loc) · 6.73 KB
/
test_mixins.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest
import sqlalchemy
from aria.storage import (
ModelStorage,
sql_mapi
)
from aria import modeling
from aria.modeling.exceptions import ValueFormatException
from ..storage import (
release_sqlite_storage,
init_inmemory_model_storage
)
from . import MockModel
from ..mock import (
models,
context as mock_context
)
@pytest.fixture
def storage():
base_storage = ModelStorage(sql_mapi.SQLAlchemyModelAPI,
initiator=init_inmemory_model_storage)
base_storage.register(MockModel)
yield base_storage
release_sqlite_storage(base_storage)
@pytest.fixture(scope='module', autouse=True)
def module_cleanup():
modeling.models.aria_declarative_base.metadata.remove(MockModel.__table__) # pylint: disable=no-member
@pytest.fixture
def context(tmpdir):
ctx = mock_context.simple(str(tmpdir))
yield ctx
release_sqlite_storage(ctx.model)
def test_inner_dict_update(storage):
inner_dict = {'inner_value': 1}
mock_model = MockModel(model_dict={'inner_dict': inner_dict, 'value': 0})
storage.mock_model.put(mock_model)
storage_mm = storage.mock_model.get(mock_model.id)
assert storage_mm == mock_model
storage_mm.model_dict['inner_dict']['inner_value'] = 2
storage_mm.model_dict['value'] = -1
storage.mock_model.update(storage_mm)
storage_mm = storage.mock_model.get(storage_mm.id)
assert storage_mm.model_dict['inner_dict']['inner_value'] == 2
assert storage_mm.model_dict['value'] == -1
def test_inner_list_update(storage):
mock_model = MockModel(model_list=[0, [1]])
storage.mock_model.put(mock_model)
storage_mm = storage.mock_model.get(mock_model.id)
assert storage_mm == mock_model
storage_mm.model_list[1][0] = 'new_inner_value'
storage_mm.model_list[0] = 'new_value'
storage.mock_model.update(storage_mm)
storage_mm = storage.mock_model.get(storage_mm.id)
assert storage_mm.model_list[1][0] == 'new_inner_value'
assert storage_mm.model_list[0] == 'new_value'
def test_model_to_dict(context):
service = context.service
service = service.to_dict()
expected_keys = [
'description',
'created_at',
'permalink',
'scaling_groups',
'updated_at'
]
for expected_key in expected_keys:
assert expected_key in service
def test_relationship_model_ordering(context):
service = context.model.service.get_by_name(models.SERVICE_NAME)
source_node = context.model.node.get_by_name(models.DEPENDENT_NODE_NAME)
target_node = context.model.node.get_by_name(models.DEPENDENCY_NODE_NAME)
new_node_template = modeling.models.NodeTemplate(
name='new_node_template',
type=source_node.type,
default_instances=1,
min_instances=1,
max_instances=1,
service_template=service.service_template
)
new_node = modeling.models.Node(
name='new_node',
type=source_node.type,
runtime_properties={},
service=service,
version=None,
node_template=new_node_template,
state=modeling.models.Node.INITIAL,
scaling_groups=[]
)
source_node.outbound_relationships.append(modeling.models.Relationship(
source_node=source_node,
target_node=new_node,
))
new_node.outbound_relationships.append(modeling.models.Relationship( # pylint: disable=no-member
source_node=new_node,
target_node=target_node,
))
context.model.node_template.put(new_node_template)
context.model.node.put(new_node)
context.model.node.refresh(source_node)
context.model.node.refresh(target_node)
def flip_and_assert(node, direction):
"""
Reversed the order of relationships and assert effects took place.
:param node: the node instance to operate on
:param direction: the type of relationships to flip (inbound/outbound)
:return:
"""
assert direction in ('inbound', 'outbound')
def get_relationships():
return getattr(node, direction + '_relationships')
relationships = get_relationships()
assert len(relationships) == 2
reversed_relationship = list(reversed(relationships))
assert relationships != reversed_relationship
relationships[:] = reversed_relationship
context.model.node.update(node)
assert get_relationships() == reversed_relationship
flip_and_assert(source_node, 'outbound')
flip_and_assert(target_node, 'inbound')
class StrictClass(modeling.models.aria_declarative_base, modeling.mixins.ModelMixin):
__tablename__ = 'strict_class'
strict_dict = sqlalchemy.Column(modeling.types.StrictDict(basestring, basestring))
strict_list = sqlalchemy.Column(modeling.types.StrictList(basestring))
def test_strict_dict():
strict_class = StrictClass()
def assert_strict(sc):
with pytest.raises(ValueFormatException):
sc.strict_dict = {'key': 1}
with pytest.raises(ValueFormatException):
sc.strict_dict = {1: 'value'}
with pytest.raises(ValueFormatException):
sc.strict_dict = {1: 1}
assert_strict(strict_class)
strict_class.strict_dict = {'key': 'value'}
assert strict_class.strict_dict == {'key': 'value'}
assert_strict(strict_class)
with pytest.raises(ValueFormatException):
strict_class.strict_dict['key'] = 1
with pytest.raises(ValueFormatException):
strict_class.strict_dict[1] = 'value'
with pytest.raises(ValueFormatException):
strict_class.strict_dict[1] = 1
def test_strict_list():
strict_class = StrictClass()
def assert_strict(sc):
with pytest.raises(ValueFormatException):
sc.strict_list = [1]
assert_strict(strict_class)
strict_class.strict_list = ['item']
assert strict_class.strict_list == ['item']
assert_strict(strict_class)
with pytest.raises(ValueFormatException):
strict_class.strict_list[0] = 1