-
Notifications
You must be signed in to change notification settings - Fork 427
/
base_integration_test.py
169 lines (156 loc) · 4.71 KB
/
base_integration_test.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
"""
Runs tests against dynamodb
"""
import time
from pynamodb.connection import Connection
from pynamodb.constants import PROVISIONED_THROUGHPUT, READ_CAPACITY_UNITS
from pynamodb.expressions.condition import BeginsWith, NotExists
from pynamodb.expressions.operand import Path, Value
from pynamodb.exceptions import TableDoesNotExist
from pynamodb.types import STRING, HASH, RANGE, NUMBER
import pytest
@pytest.mark.ddblocal
def test_connection_integration(ddb_url):
table_name = 'pynamodb-ci-connection'
# For use with a fake dynamodb connection
# See: http://aws.amazon.com/dynamodb/developer-resources/
conn = Connection(host=ddb_url)
print(conn)
print("conn.describe_table...")
table = None
try:
table = conn.describe_table(table_name)
except TableDoesNotExist:
params = {
'read_capacity_units': 1,
'write_capacity_units': 1,
'attribute_definitions': [
{
'attribute_type': STRING,
'attribute_name': 'Forum'
},
{
'attribute_type': STRING,
'attribute_name': 'Thread'
},
{
'attribute_type': STRING,
'attribute_name': 'AltKey'
},
{
'attribute_type': NUMBER,
'attribute_name': 'number'
}
],
'key_schema': [
{
'key_type': HASH,
'attribute_name': 'Forum'
},
{
'key_type': RANGE,
'attribute_name': 'Thread'
}
],
'global_secondary_indexes': [
{
'index_name': 'alt-index',
'key_schema': [
{
'KeyType': 'HASH',
'AttributeName': 'AltKey'
}
],
'projection': {
'ProjectionType': 'KEYS_ONLY'
},
'provisioned_throughput': {
'ReadCapacityUnits': 1,
'WriteCapacityUnits': 1,
}
}
],
'local_secondary_indexes': [
{
'index_name': 'view-index',
'key_schema': [
{
'KeyType': 'HASH',
'AttributeName': 'Forum'
},
{
'KeyType': 'RANGE',
'AttributeName': 'AltKey'
}
],
'projection': {
'ProjectionType': 'KEYS_ONLY'
}
}
]
}
print("conn.create_table...")
conn.create_table(table_name, **params)
while table is None:
time.sleep(1)
table = conn.describe_table(table_name)
while table['TableStatus'] == 'CREATING':
time.sleep(2)
table = conn.describe_table(table_name)
print("conn.list_tables")
conn.list_tables()
print("conn.update_table...")
conn.update_table(
table_name,
read_capacity_units=table.get(PROVISIONED_THROUGHPUT).get(READ_CAPACITY_UNITS) + 1,
write_capacity_units=2
)
table = conn.describe_table(table_name)
while table['TableStatus'] != 'ACTIVE':
time.sleep(2)
table = conn.describe_table(table_name)
print("conn.put_item")
conn.put_item(
table_name,
'item1-hash',
range_key='item1-range',
attributes={'foo': {'S': 'bar'}},
condition=NotExists(Path('Forum')),
)
conn.get_item(
table_name,
'item1-hash',
range_key='item1-range'
)
conn.delete_item(
table_name,
'item1-hash',
range_key='item1-range'
)
items = []
for i in range(10):
items.append(
{"Forum": "FooForum", "Thread": "thread-{}".format(i)}
)
print("conn.batch_write_items...")
conn.batch_write_item(
table_name,
put_items=items
)
print("conn.batch_get_items...")
data = conn.batch_get_item(
table_name,
items
)
print("conn.query...")
conn.query(
table_name,
"FooForum",
range_key_condition=(BeginsWith(Path('Thread'), Value('thread'))),
)
print("conn.scan...")
conn.scan(
table_name,
)
print("conn.delete_table...")
conn.delete_table(table_name)