-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_cosmos.py
182 lines (143 loc) · 4.16 KB
/
test_cosmos.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
from base64 import b64encode
import os
import time
from azure.identity import EnvironmentCredential # type: ignore
import pytest
import responses # type: ignore
import abnosql.exceptions as ex
from abnosql.mocks import mock_cosmos
from abnosql.mocks.mock_cosmos import set_keyattrs
from abnosql.plugins.table import cosmos
from abnosql.plugins.table.memory import clear_tables
from tests import common as cmn
def setup_cosmos():
clear_tables()
os.environ['ABNOSQL_KEY_ATTRS'] = 'hk,rk'
set_keyattrs({
'hash_range': ['hk', 'rk'],
'hash_only': ['hk']
})
os.environ['ABNOSQL_DB'] = 'cosmos'
os.environ['ABNOSQL_COSMOS_ACCOUNT'] = 'foo'
os.environ['ABNOSQL_COSMOS_CREDENTIAL'] = b64encode(
'mycredential'.encode('utf-8')
).decode()
os.environ['ABNOSQL_COSMOS_DATABASE'] = 'bar'
os.environ['ABNOSQL_DISABLE_GLOBAL_CACHE'] = 'TRUE'
@mock_cosmos
@responses.activate
def test_config_exception():
setup_cosmos()
os.environ['ABNOSQL_DISABLE_GLOBAL_CACHE'] = 'FALSE'
with pytest.raises(ex.ConfigException) as e:
cmn.test_get_item()
os.environ['ABNOSQL_DISABLE_GLOBAL_CACHE'] = 'TRUE'
assert str(e.value) == 'missing config: endpoint, database'
@mock_cosmos
@responses.activate
def test_config_exception1():
setup_cosmos()
os.environ['ABNOSQL_DISABLE_GLOBAL_CACHE'] = 'FALSE'
from importlib import reload
reload(cosmos)
cmn.test_get_item()
os.environ['ABNOSQL_DISABLE_GLOBAL_CACHE'] = 'TRUE'
@mock_cosmos
@responses.activate
def test_exceptions():
setup_cosmos()
os.environ.pop('ABNOSQL_COSMOS_CREDENTIAL', None)
vars = ['AZURE_CLIENT_ID', 'AZURE_TENANT_ID', 'AZURE_CLIENT_SECRET']
orig = {var: os.environ.pop(var, None) for var in vars}
with pytest.raises(ex.PluginException) as e:
cmn.test_get_item(config={
'credential': EnvironmentCredential()
})
assert 'plugin exception' in str(e.value)
assert e.value.to_problem() == {
'title': 'plugin exception',
'detail': 'EnvironmentCredential authentication unavailable. Environment variables are not fully configured.\nVisit https://aka.ms/azsdk/python/identity/environmentcredential/troubleshoot to troubleshoot this issue.', # noqa E501
'status': 500,
'type': None
}
# restore (prob don't need to do this)
for var, val in orig.items():
if val is not None:
os.environ[var] = val
@mock_cosmos
@responses.activate
def test_get_item():
setup_cosmos()
cmn.test_get_item()
@mock_cosmos
@responses.activate
def test_check_exists():
setup_cosmos()
cmn.test_check_exists()
@mock_cosmos
@responses.activate
def test_validate_item():
setup_cosmos()
cmn.test_validate_item()
@mock_cosmos
@responses.activate
def test_put_item():
setup_cosmos()
# test with DefaultAzureCredential
# os.environ.pop('ABNOSQL_COSMOS_CREDENTIAL', None)
cmn.test_put_item()
@mock_cosmos
@responses.activate
def test_put_item_audit():
setup_cosmos()
cmn.test_put_item_audit()
@mock_cosmos
@responses.activate
def test_update_item():
setup_cosmos()
cmn.test_update_item()
@mock_cosmos
@responses.activate
def test_put_items():
setup_cosmos()
cmn.test_put_items()
@mock_cosmos
@responses.activate
def test_delete_item():
setup_cosmos()
start_secs = time.time()
os.environ['ABNOSQL_COSMOS_CHANGE_META'] = 'TRUE'
cmn.test_delete_item()
os.environ.pop('ABNOSQL_COSMOS_CHANGE_META', None)
# validate that ABNOSQL_COSMOS_CHANGE_META_SLEEPSECS defaulted to 5
assert time.time() >= start_secs + 5
@mock_cosmos
@responses.activate
def test_hooks():
setup_cosmos()
cmn.test_hooks()
@mock_cosmos
@responses.activate
def test_audit_callback():
setup_cosmos()
cmn.test_audit_callback()
@mock_cosmos
@responses.activate
def test_query():
setup_cosmos()
cmn.test_query()
@mock_cosmos
@responses.activate
def test_query_sql():
setup_cosmos()
cmn.test_query_sql()
@mock_cosmos
@responses.activate
def test_query_scan():
setup_cosmos()
cmn.test_query_scan()
@mock_cosmos
@responses.activate
def test_query_pagination():
setup_cosmos()
cmn.test_query_pagination()