1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
18 from aria.modeling import models
19 from aria.storage import collection_instrumentation
22 class MockActor(object):
28 class MockMAPI(object):
33 def put(self, *args, **kwargs):
36 def update(self, *args, **kwargs):
40 class CollectionInstrumentation(object):
51 def dict_(self, actor, model):
52 return collection_instrumentation._InstrumentedDict(model, actor, 'dict_', models.Attribute)
55 def list_(self, actor, model):
56 return collection_instrumentation._InstrumentedList(model, actor, 'list_', models.Attribute)
59 class TestDict(CollectionInstrumentation):
61 def test_keys(self, actor, dict_):
64 'key1': models.Attribute.wrap('key1', 'value1'),
65 'key2': models.Attribute.wrap('key2', 'value2')
68 assert sorted(dict_.keys()) == sorted(['key1', 'key2']) == sorted(actor.dict_.keys())
70 def test_values(self, actor, dict_):
72 'key1': models.Attribute.wrap('key1', 'value1'),
73 'key2': models.Attribute.wrap('key1', 'value2')
75 assert (sorted(dict_.values()) ==
76 sorted(['value1', 'value2']) ==
77 sorted(v.value for v in actor.dict_.values()))
79 def test_items(self, dict_):
81 'key1': models.Attribute.wrap('key1', 'value1'),
82 'key2': models.Attribute.wrap('key1', 'value2')
84 assert sorted(dict_.items()) == sorted([('key1', 'value1'), ('key2', 'value2')])
86 def test_iter(self, actor, dict_):
88 'key1': models.Attribute.wrap('key1', 'value1'),
89 'key2': models.Attribute.wrap('key1', 'value2')
91 assert sorted(list(dict_)) == sorted(['key1', 'key2']) == sorted(actor.dict_.keys())
93 def test_bool(self, dict_):
96 'key1': models.Attribute.wrap('key1', 'value1'),
97 'key2': models.Attribute.wrap('key1', 'value2')
101 def test_set_item(self, actor, dict_):
102 dict_['key1'] = models.Attribute.wrap('key1', 'value1')
103 assert dict_['key1'] == 'value1' == actor.dict_['key1'].value
104 assert isinstance(actor.dict_['key1'], models.Attribute)
106 def test_nested(self, actor, dict_):
108 assert isinstance(actor.dict_['key'], models.Attribute)
109 assert dict_['key'] == actor.dict_['key'].value == {}
111 dict_['key']['inner_key'] = 'value'
113 assert len(dict_) == 1
114 assert 'inner_key' in dict_['key']
115 assert dict_['key']['inner_key'] == 'value'
116 assert dict_['key'].keys() == ['inner_key']
117 assert dict_['key'].values() == ['value']
118 assert dict_['key'].items() == [('inner_key', 'value')]
119 assert isinstance(actor.dict_['key'], models.Attribute)
120 assert isinstance(dict_['key'], collection_instrumentation._InstrumentedDict)
122 dict_['key'].update({'updated_key': 'updated_value'})
123 assert len(dict_) == 1
124 assert 'updated_key' in dict_['key']
125 assert dict_['key']['updated_key'] == 'updated_value'
126 assert sorted(dict_['key'].keys()) == sorted(['inner_key', 'updated_key'])
127 assert sorted(dict_['key'].values()) == sorted(['value', 'updated_value'])
128 assert sorted(dict_['key'].items()) == sorted([('inner_key', 'value'),
129 ('updated_key', 'updated_value')])
130 assert isinstance(actor.dict_['key'], models.Attribute)
131 assert isinstance(dict_['key'], collection_instrumentation._InstrumentedDict)
133 dict_.update({'key': 'override_value'})
134 assert len(dict_) == 1
135 assert 'key' in dict_
136 assert dict_['key'] == 'override_value'
137 assert len(actor.dict_) == 1
138 assert isinstance(actor.dict_['key'], models.Attribute)
139 assert actor.dict_['key'].value == 'override_value'
141 def test_get_item(self, actor, dict_):
142 dict_['key1'] = models.Attribute.wrap('key1', 'value1')
143 assert isinstance(actor.dict_['key1'], models.Attribute)
145 def test_update(self, actor, dict_):
146 dict_['key1'] = 'value1'
148 new_dict = {'key2': 'value2'}
149 dict_.update(new_dict)
150 assert len(dict_) == 2
151 assert dict_['key2'] == 'value2'
152 assert isinstance(actor.dict_['key2'], models.Attribute)
155 new_dict.update(dict_)
156 assert new_dict['key1'] == dict_['key1']
158 def test_copy(self, dict_):
159 dict_['key1'] = 'value1'
161 new_dict = dict_.copy()
162 assert new_dict is not dict_
163 assert new_dict == dict_
165 dict_['key1'] = 'value2'
166 assert new_dict['key1'] == 'value1'
167 assert dict_['key1'] == 'value2'
169 def test_clear(self, dict_):
170 dict_['key1'] = 'value1'
173 assert len(dict_) == 0
176 class TestList(CollectionInstrumentation):
178 def test_append(self, actor, list_):
179 list_.append(models.Attribute.wrap('name', 'value1'))
180 list_.append('value2')
181 assert len(actor.list_) == 2
182 assert len(list_) == 2
183 assert isinstance(actor.list_[0], models.Attribute)
184 assert list_[0] == 'value1'
186 assert isinstance(actor.list_[1], models.Attribute)
187 assert list_[1] == 'value2'
189 list_[0] = 'new_value1'
190 list_[1] = 'new_value2'
191 assert isinstance(actor.list_[1], models.Attribute)
192 assert isinstance(actor.list_[1], models.Attribute)
193 assert list_[0] == 'new_value1'
194 assert list_[1] == 'new_value2'
196 def test_iter(self, list_):
197 list_.append('value1')
198 list_.append('value2')
199 assert sorted(list_) == sorted(['value1', 'value2'])
201 def test_insert(self, actor, list_):
202 list_.append('value1')
203 list_.insert(0, 'value2')
204 list_.insert(2, 'value3')
205 list_.insert(10, 'value4')
206 assert sorted(list_) == sorted(['value1', 'value2', 'value3', 'value4'])
207 assert len(actor.list_) == 4
209 def test_set(self, list_):
210 list_.append('value1')
211 list_.append('value2')
214 assert len(list_) == 2
215 assert sorted(list_) == sorted(['value1', 'value3'])
217 def test_insert_into_nested(self, actor, list_):
220 list_[0].append('inner_item')
221 assert isinstance(actor.list_[0], models.Attribute)
222 assert len(list_) == 1
223 assert list_[0][0] == 'inner_item'
225 list_[0].append('new_item')
226 assert isinstance(actor.list_[0], models.Attribute)
227 assert len(list_) == 1
228 assert list_[0][1] == 'new_item'
230 assert list_[0] == ['inner_item', 'new_item']
231 assert ['inner_item', 'new_item'] == list_[0]
234 class TestDictList(CollectionInstrumentation):
235 def test_dict_in_list(self, actor, list_):
237 assert len(list_) == 1
238 assert isinstance(actor.list_[0], models.Attribute)
239 assert actor.list_[0].value == {}
241 list_[0]['key'] = 'value'
242 assert list_[0]['key'] == 'value'
243 assert len(actor.list_) == 1
244 assert isinstance(actor.list_[0], models.Attribute)
245 assert actor.list_[0].value['key'] == 'value'
247 def test_list_in_dict(self, actor, dict_):
249 assert len(dict_) == 1
250 assert isinstance(actor.dict_['key'], models.Attribute)
251 assert actor.dict_['key'].value == []
253 dict_['key'].append('value')
254 assert dict_['key'][0] == 'value'
255 assert len(actor.dict_) == 1
256 assert isinstance(actor.dict_['key'], models.Attribute)
257 assert actor.dict_['key'].value[0] == 'value'