-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_dynamodb.py
147 lines (111 loc) · 2.86 KB
/
test_dynamodb.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
import os
import boto3 # type: ignore
from moto import mock_aws # type: ignore
import pytest
import abnosql.exceptions as ex
from abnosql.mocks import mock_dynamodbx
from abnosql import table
from tests import common as cmn
def create_table(name, rk=True):
dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
key_schema = [
{'AttributeName': 'hk', 'KeyType': 'HASH'}
]
attr_defs = [
{'AttributeName': 'hk', 'AttributeType': 'S'}
]
if rk is True:
key_schema.append({'AttributeName': 'rk', 'KeyType': 'RANGE'})
attr_defs.append({'AttributeName': 'rk', 'AttributeType': 'S'})
params = {
'TableName': name,
'KeySchema': key_schema,
'AttributeDefinitions': attr_defs,
'ProvisionedThroughput': {
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
}
}
dynamodb.create_table(**params)
def setup_dynamodb(set_region=False):
os.environ['ABNOSQL_KEY_ATTRS'] = 'hk,rk'
if set_region is True:
os.environ['AWS_DEFAULT_REGION'] = 'us-east-1'
os.environ.pop('ABNOSQL_DB', None)
else:
os.environ['ABNOSQL_DB'] = 'dynamodb'
create_table('hash_range', True)
create_table('hash_only', False)
@mock_aws
def test_exceptions():
setup_dynamodb(set_region=True)
tb = table('notfound', {'key_attrs': ['hk']})
with pytest.raises(ex.NotFoundException) as e:
tb.get_item(hk='1')
assert 'not found' in str(e.value)
assert e.value.to_problem() == {
'title': 'not found',
'detail': None,
'status': 404,
'type': None
}
@mock_aws
def test_get_item():
# test inferring ABNOSQL_DB / database via region env var
setup_dynamodb(set_region=True)
cmn.test_get_item()
@mock_aws
def test_check_exists():
setup_dynamodb()
cmn.test_check_exists()
@mock_aws
def test_validate_item():
setup_dynamodb()
cmn.test_validate_item()
@mock_aws
def test_put_item():
setup_dynamodb()
cmn.test_put_item()
@mock_aws
def test_put_item_audit():
setup_dynamodb()
cmn.test_put_item_audit()
@mock_aws
def test_update_item():
setup_dynamodb()
cmn.test_update_item()
@mock_aws
def test_put_items():
setup_dynamodb()
cmn.test_put_items()
@mock_aws
def test_delete_item():
setup_dynamodb()
cmn.test_delete_item()
@mock_aws
def test_hooks():
setup_dynamodb()
cmn.test_hooks()
@mock_aws
def test_audit_callback():
setup_dynamodb()
cmn.test_audit_callback()
@mock_aws
def test_query():
setup_dynamodb()
cmn.test_query()
@mock_dynamodbx
@mock_aws
def test_query_sql():
setup_dynamodb()
cmn.test_query_sql()
@mock_dynamodbx
@mock_aws
def test_query_scan():
setup_dynamodb()
cmn.test_query_scan()
@mock_dynamodbx
@mock_aws
def test_query_pagination():
setup_dynamodb()
cmn.test_query_pagination()