summaryrefslogtreecommitdiffstats
path: root/azure/aria/aria-extension-cloudify/src/aria/tests/storage/test_collection_instrumentation.py
blob: e9154213f86ed5598fc49582d1b56efdd28e262d (plain)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# 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

from aria.modeling import models
from aria.storage import collection_instrumentation


class MockActor(object):
    def __init__(self):
        self.dict_ = {}
        self.list_ = []


class MockMAPI(object):

    def __init__(self):
        pass

    def put(self, *args, **kwargs):
        pass

    def update(self, *args, **kwargs):
        pass


class CollectionInstrumentation(object):

    @pytest.fixture
    def actor(self):
        return MockActor()

    @pytest.fixture
    def model(self):
        return MockMAPI()

    @pytest.fixture
    def dict_(self, actor, model):
        return collection_instrumentation._InstrumentedDict(model, actor, 'dict_', models.Attribute)

    @pytest.fixture
    def list_(self, actor, model):
        return collection_instrumentation._InstrumentedList(model, actor, 'list_', models.Attribute)


class TestDict(CollectionInstrumentation):

    def test_keys(self, actor, dict_):
        dict_.update(
            {
                'key1': models.Attribute.wrap('key1', 'value1'),
                'key2': models.Attribute.wrap('key2', 'value2')
            }
        )
        assert sorted(dict_.keys()) == sorted(['key1', 'key2']) == sorted(actor.dict_.keys())

    def test_values(self, actor, dict_):
        dict_.update({
            'key1': models.Attribute.wrap('key1', 'value1'),
            'key2': models.Attribute.wrap('key1', 'value2')
        })
        assert (sorted(dict_.values()) ==
                sorted(['value1', 'value2']) ==
                sorted(v.value for v in actor.dict_.values()))

    def test_items(self, dict_):
        dict_.update({
            'key1': models.Attribute.wrap('key1', 'value1'),
            'key2': models.Attribute.wrap('key1', 'value2')
        })
        assert sorted(dict_.items()) == sorted([('key1', 'value1'), ('key2', 'value2')])

    def test_iter(self, actor, dict_):
        dict_.update({
            'key1': models.Attribute.wrap('key1', 'value1'),
            'key2': models.Attribute.wrap('key1', 'value2')
        })
        assert sorted(list(dict_)) == sorted(['key1', 'key2']) == sorted(actor.dict_.keys())

    def test_bool(self, dict_):
        assert not dict_
        dict_.update({
            'key1': models.Attribute.wrap('key1', 'value1'),
            'key2': models.Attribute.wrap('key1', 'value2')
        })
        assert dict_

    def test_set_item(self, actor, dict_):
        dict_['key1'] = models.Attribute.wrap('key1', 'value1')
        assert dict_['key1'] == 'value1' == actor.dict_['key1'].value
        assert isinstance(actor.dict_['key1'], models.Attribute)

    def test_nested(self, actor, dict_):
        dict_['key'] = {}
        assert isinstance(actor.dict_['key'], models.Attribute)
        assert dict_['key'] == actor.dict_['key'].value == {}

        dict_['key']['inner_key'] = 'value'

        assert len(dict_) == 1
        assert 'inner_key' in dict_['key']
        assert dict_['key']['inner_key'] == 'value'
        assert dict_['key'].keys() == ['inner_key']
        assert dict_['key'].values() == ['value']
        assert dict_['key'].items() == [('inner_key', 'value')]
        assert isinstance(actor.dict_['key'], models.Attribute)
        assert isinstance(dict_['key'], collection_instrumentation._InstrumentedDict)

        dict_['key'].update({'updated_key': 'updated_value'})
        assert len(dict_) == 1
        assert 'updated_key' in dict_['key']
        assert dict_['key']['updated_key'] == 'updated_value'
        assert sorted(dict_['key'].keys()) == sorted(['inner_key', 'updated_key'])
        assert sorted(dict_['key'].values()) == sorted(['value', 'updated_value'])
        assert sorted(dict_['key'].items()) == sorted([('inner_key', 'value'),
                                                       ('updated_key', 'updated_value')])
        assert isinstance(actor.dict_['key'], models.Attribute)
        assert isinstance(dict_['key'], collection_instrumentation._InstrumentedDict)

        dict_.update({'key': 'override_value'})
        assert len(dict_) == 1
        assert 'key' in dict_
        assert dict_['key'] == 'override_value'
        assert len(actor.dict_) == 1
        assert isinstance(actor.dict_['key'], models.Attribute)
        assert actor.dict_['key'].value == 'override_value'

    def test_get_item(self, actor, dict_):
        dict_['key1'] = models.Attribute.wrap('key1', 'value1')
        assert isinstance(actor.dict_['key1'], models.Attribute)

    def test_update(self, actor, dict_):
        dict_['key1'] = 'value1'

        new_dict = {'key2': 'value2'}
        dict_.update(new_dict)
        assert len(dict_) == 2
        assert dict_['key2'] == 'value2'
        assert isinstance(actor.dict_['key2'], models.Attribute)

        new_dict = {}
        new_dict.update(dict_)
        assert new_dict['key1'] == dict_['key1']

    def test_copy(self, dict_):
        dict_['key1'] = 'value1'

        new_dict = dict_.copy()
        assert new_dict is not dict_
        assert new_dict == dict_

        dict_['key1'] = 'value2'
        assert new_dict['key1'] == 'value1'
        assert dict_['key1'] == 'value2'

    def test_clear(self, dict_):
        dict_['key1'] = 'value1'
        dict_.clear()

        assert len(dict_) == 0


class TestList(CollectionInstrumentation):

    def test_append(self, actor, list_):
        list_.append(models.Attribute.wrap('name', 'value1'))
        list_.append('value2')
        assert len(actor.list_) == 2
        assert len(list_) == 2
        assert isinstance(actor.list_[0], models.Attribute)
        assert list_[0] == 'value1'

        assert isinstance(actor.list_[1], models.Attribute)
        assert list_[1] == 'value2'

        list_[0] = 'new_value1'
        list_[1] = 'new_value2'
        assert isinstance(actor.list_[1], models.Attribute)
        assert isinstance(actor.list_[1], models.Attribute)
        assert list_[0] == 'new_value1'
        assert list_[1] == 'new_value2'

    def test_iter(self, list_):
        list_.append('value1')
        list_.append('value2')
        assert sorted(list_) == sorted(['value1', 'value2'])

    def test_insert(self, actor, list_):
        list_.append('value1')
        list_.insert(0, 'value2')
        list_.insert(2, 'value3')
        list_.insert(10, 'value4')
        assert sorted(list_) == sorted(['value1', 'value2', 'value3', 'value4'])
        assert len(actor.list_) == 4

    def test_set(self, list_):
        list_.append('value1')
        list_.append('value2')

        list_[1] = 'value3'
        assert len(list_) == 2
        assert sorted(list_) == sorted(['value1', 'value3'])

    def test_insert_into_nested(self, actor, list_):
        list_.append([])

        list_[0].append('inner_item')
        assert isinstance(actor.list_[0], models.Attribute)
        assert len(list_) == 1
        assert list_[0][0] == 'inner_item'

        list_[0].append('new_item')
        assert isinstance(actor.list_[0], models.Attribute)
        assert len(list_) == 1
        assert list_[0][1] == 'new_item'

        assert list_[0] == ['inner_item', 'new_item']
        assert ['inner_item', 'new_item'] == list_[0]


class TestDictList(CollectionInstrumentation):
    def test_dict_in_list(self, actor, list_):
        list_.append({})
        assert len(list_) == 1
        assert isinstance(actor.list_[0], models.Attribute)
        assert actor.list_[0].value == {}

        list_[0]['key'] = 'value'
        assert list_[0]['key'] == 'value'
        assert len(actor.list_) == 1
        assert isinstance(actor.list_[0], models.Attribute)
        assert actor.list_[0].value['key'] == 'value'

    def test_list_in_dict(self, actor, dict_):
        dict_['key'] = []
        assert len(dict_) == 1
        assert isinstance(actor.dict_['key'], models.Attribute)
        assert actor.dict_['key'].value == []

        dict_['key'].append('value')
        assert dict_['key'][0] == 'value'
        assert len(actor.dict_) == 1
        assert isinstance(actor.dict_['key'], models.Attribute)
        assert actor.dict_['key'].value[0] == 'value'