summaryrefslogtreecommitdiff
path: root/tests/integration/dynamodb2/test_layer1.py
blob: 98e8a3cb1e31d6241333aaad0070b2f2b4e54462 (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# Copyright (c) 2013 Amazon.com, Inc. or its affiliates.
# All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

"""
Tests for Layer1 of DynamoDB v2
"""
import time

from tests.unit import unittest
from boto.dynamodb2 import exceptions
from boto.dynamodb2.layer1 import DynamoDBConnection


class DynamoDBv2Layer1Test(unittest.TestCase):
    dynamodb = True

    def setUp(self):
        self.dynamodb = DynamoDBConnection()
        self.table_name = 'test-%d' % int(time.time())
        self.hash_key_name = 'username'
        self.hash_key_type = 'S'
        self.range_key_name = 'date_joined'
        self.range_key_type = 'N'
        self.read_units = 5
        self.write_units = 5
        self.attributes = [
            {
                'AttributeName': self.hash_key_name,
                'AttributeType': self.hash_key_type,
            },
            {
                'AttributeName': self.range_key_name,
                'AttributeType': self.range_key_type,
            }
        ]
        self.schema = [
            {
                'AttributeName': self.hash_key_name,
                'KeyType': 'HASH',
            },
            {
                'AttributeName': self.range_key_name,
                'KeyType': 'RANGE',
            },
        ]
        self.provisioned_throughput = {
            'ReadCapacityUnits': self.read_units,
            'WriteCapacityUnits': self.write_units,
        }
        self.lsi = [
            {
                'IndexName': 'MostRecentIndex',
                'KeySchema': [
                    {
                        'AttributeName': self.hash_key_name,
                        'KeyType': 'HASH',
                    },
                    {
                        'AttributeName': self.range_key_name,
                        'KeyType': 'RANGE',
                    },
                ],
                'Projection': {
                    'ProjectionType': 'KEYS_ONLY',
                }
            }
        ]

    def create_table(self, table_name, attributes, schema,
                     provisioned_throughput, lsi=None, wait=True):
        # Note: This is a slightly different ordering that makes less sense.
        result = self.dynamodb.create_table(
            attributes,
            table_name,
            schema,
            provisioned_throughput,
            local_secondary_indexes=lsi
        )
        self.addCleanup(self.dynamodb.delete_table, table_name)
        if wait:
            while True:
                description = self.dynamodb.describe_table(table_name)
                if description['Table']['TableStatus'].lower() == 'active':
                    return result
                else:
                    time.sleep(5)
        else:
            return result

    def test_integrated(self):
        result = self.create_table(
            self.table_name,
            self.attributes,
            self.schema,
            self.provisioned_throughput,
            self.lsi
        )
        self.assertEqual(
            result['TableDescription']['TableName'],
            self.table_name
        )

        description = self.dynamodb.describe_table(self.table_name)
        self.assertEqual(description['Table']['ItemCount'], 0)

        # Create some records.
        record_1_data = {
            'username': {'S': 'johndoe'},
            'first_name': {'S': 'John'},
            'last_name': {'S': 'Doe'},
            'date_joined': {'N': '1366056668'},
            'friend_count': {'N': '3'},
            'friends': {'SS': ['alice', 'bob', 'jane']},
        }
        r1_result = self.dynamodb.put_item(self.table_name, record_1_data)

        # Get the data.
        record_1 = self.dynamodb.get_item(self.table_name, key={
            'username': {'S': 'johndoe'},
            'date_joined': {'N': '1366056668'},
        }, consistent_read=True)
        self.assertEqual(record_1['Item']['username']['S'], 'johndoe')
        self.assertEqual(record_1['Item']['first_name']['S'], 'John')
        self.assertEqual(record_1['Item']['friends']['SS'], [
            'alice', 'bob', 'jane'
        ])

        # Now in a batch.
        self.dynamodb.batch_write_item({
            self.table_name: [
                {
                    'PutRequest': {
                        'Item': {
                            'username': {'S': 'jane'},
                            'first_name': {'S': 'Jane'},
                            'last_name': {'S': 'Doe'},
                            'date_joined': {'N': '1366056789'},
                            'friend_count': {'N': '1'},
                            'friends': {'SS': ['johndoe']},
                        },
                    },
                },
            ]
        })

        # Now a query.
        lsi_results = self.dynamodb.query(
            self.table_name,
            index_name='MostRecentIndex',
            key_conditions={
                'username': {
                    'AttributeValueList': [
                        {'S': 'johndoe'},
                    ],
                    'ComparisonOperator': 'EQ',
                },
            },
            consistent_read=True
        )
        self.assertEqual(lsi_results['Count'], 1)

        results = self.dynamodb.query(self.table_name, key_conditions={
            'username': {
                'AttributeValueList': [
                    {'S': 'jane'},
                ],
                'ComparisonOperator': 'EQ',
            },
            'date_joined': {
                'AttributeValueList': [
                    {'N': '1366050000'}
                ],
                'ComparisonOperator': 'GT',
            }
        }, consistent_read=True)
        self.assertEqual(results['Count'], 1)

        # Now a scan.
        results = self.dynamodb.scan(self.table_name)
        self.assertEqual(results['Count'], 2)
        s_items = sorted([res['username']['S'] for res in results['Items']])
        self.assertEqual(s_items, ['jane', 'johndoe'])

        self.dynamodb.delete_item(self.table_name, key={
            'username': {'S': 'johndoe'},
            'date_joined': {'N': '1366056668'},
        })

        results = self.dynamodb.scan(self.table_name)
        self.assertEqual(results['Count'], 1)

        # Parallel scan (minus client-side threading).
        self.dynamodb.batch_write_item({
            self.table_name: [
                {
                    'PutRequest': {
                        'Item': {
                            'username': {'S': 'johndoe'},
                            'first_name': {'S': 'Johann'},
                            'last_name': {'S': 'Does'},
                            'date_joined': {'N': '1366058000'},
                            'friend_count': {'N': '1'},
                            'friends': {'SS': ['jane']},
                        },
                    },
                    'PutRequest': {
                        'Item': {
                            'username': {'S': 'alice'},
                            'first_name': {'S': 'Alice'},
                            'last_name': {'S': 'Expert'},
                            'date_joined': {'N': '1366056800'},
                            'friend_count': {'N': '2'},
                            'friends': {'SS': ['johndoe', 'jane']},
                        },
                    },
                },
            ]
        })
        time.sleep(20)
        results = self.dynamodb.scan(self.table_name, segment=0, total_segments=2)
        self.assertTrue(results['Count'] in [1, 2])
        results = self.dynamodb.scan(self.table_name, segment=1, total_segments=2)
        self.assertTrue(results['Count'] in [1, 2])

    def test_without_range_key(self):
        result = self.create_table(
            self.table_name,
            [
                {
                    'AttributeName': self.hash_key_name,
                    'AttributeType': self.hash_key_type,
                },
            ],
            [
                {
                    'AttributeName': self.hash_key_name,
                    'KeyType': 'HASH',
                },
            ],
            self.provisioned_throughput
        )
        self.assertEqual(
            result['TableDescription']['TableName'],
            self.table_name
        )

        description = self.dynamodb.describe_table(self.table_name)
        self.assertEqual(description['Table']['ItemCount'], 0)

        # Create some records.
        record_1_data = {
            'username': {'S': 'johndoe'},
            'first_name': {'S': 'John'},
            'last_name': {'S': 'Doe'},
            'date_joined': {'N': '1366056668'},
            'friend_count': {'N': '3'},
            'friends': {'SS': ['alice', 'bob', 'jane']},
        }
        r1_result = self.dynamodb.put_item(self.table_name, record_1_data)

        # Now try a range-less get.
        johndoe = self.dynamodb.get_item(self.table_name, key={
            'username': {'S': 'johndoe'},
        }, consistent_read=True)
        self.assertEqual(johndoe['Item']['username']['S'], 'johndoe')
        self.assertEqual(johndoe['Item']['first_name']['S'], 'John')
        self.assertEqual(johndoe['Item']['friends']['SS'], [
            'alice', 'bob', 'jane'
        ])

    def test_throughput_exceeded_regression(self):
        tiny_tablename = 'TinyThroughput'
        tiny = self.create_table(
            tiny_tablename,
            self.attributes,
            self.schema,
            {
                'ReadCapacityUnits': 1,
                'WriteCapacityUnits': 1,
            }
        )

        self.dynamodb.put_item(tiny_tablename, {
            'username': {'S': 'johndoe'},
            'first_name': {'S': 'John'},
            'last_name': {'S': 'Doe'},
            'date_joined': {'N': '1366056668'},
        })
        self.dynamodb.put_item(tiny_tablename, {
            'username': {'S': 'jane'},
            'first_name': {'S': 'Jane'},
            'last_name': {'S': 'Doe'},
            'date_joined': {'N': '1366056669'},
        })
        self.dynamodb.put_item(tiny_tablename, {
            'username': {'S': 'alice'},
            'first_name': {'S': 'Alice'},
            'last_name': {'S': 'Expert'},
            'date_joined': {'N': '1366057000'},
        })
        time.sleep(20)

        for i in range(100):
            # This would cause an exception due to a non-existant instance variable.
            self.dynamodb.scan(tiny_tablename)

    def test_recursive(self):
        result = self.create_table(
            self.table_name,
            self.attributes,
            self.schema,
            self.provisioned_throughput,
            self.lsi
        )
        self.assertEqual(
            result['TableDescription']['TableName'],
            self.table_name
        )

        description = self.dynamodb.describe_table(self.table_name)
        self.assertEqual(description['Table']['ItemCount'], 0)

        # Create some records with one being a recursive shape.
        record_1_data = {
            'username': {'S': 'johndoe'},
            'first_name': {'S': 'John'},
            'last_name': {'S': 'Doe'},
            'date_joined': {'N': '1366056668'},
            'friend_count': {'N': '3'},
            'friend_data': {'M': {'username': {'S': 'alice'},
                                  'friend_count': {'N': '4'}}}
        }
        r1_result = self.dynamodb.put_item(self.table_name, record_1_data)

        # Get the data.
        record_1 = self.dynamodb.get_item(self.table_name, key={
            'username': {'S': 'johndoe'},
            'date_joined': {'N': '1366056668'},
        }, consistent_read=True)
        self.assertEqual(record_1['Item']['username']['S'], 'johndoe')
        self.assertEqual(record_1['Item']['first_name']['S'], 'John')
        recursive_data = record_1['Item']['friend_data']['M']
        self.assertEqual(recursive_data['username']['S'], 'alice')
        self.assertEqual(recursive_data['friend_count']['N'], '4')