| 
									
										
										
										
											2014-08-27 11:17:06 -04:00
										 |  |  | from __future__ import unicode_literals | 
					
						
							| 
									
										
										
										
											2014-11-27 08:03:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  | import boto | 
					
						
							| 
									
										
										
										
											2013-08-03 17:21:25 -04:00
										 |  |  | import sure  # noqa | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  | from freezegun import freeze_time | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | from moto import mock_dynamodb_deprecated | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | from boto.dynamodb import condition | 
					
						
							| 
									
										
										
										
											2013-05-16 22:24:26 -04:00
										 |  |  | from boto.dynamodb.exceptions import DynamoDBKeyNotFoundError, DynamoDBValidationError | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  | from boto.exception import DynamoDBResponseError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def create_table(conn): | 
					
						
							|  |  |  |     message_table_schema = conn.create_schema( | 
					
						
							|  |  |  |         hash_key_name='forum_name', | 
					
						
							|  |  |  |         hash_key_proto_value=str, | 
					
						
							|  |  |  |         range_key_name='subject', | 
					
						
							|  |  |  |         range_key_proto_value=str | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     table = conn.create_table( | 
					
						
							|  |  |  |         name='messages', | 
					
						
							|  |  |  |         schema=message_table_schema, | 
					
						
							|  |  |  |         read_units=10, | 
					
						
							|  |  |  |         write_units=10 | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     return table | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @freeze_time("2012-01-14") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb_deprecated | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  | def test_create_table(): | 
					
						
							|  |  |  |     conn = boto.connect_dynamodb() | 
					
						
							|  |  |  |     create_table(conn) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expected = { | 
					
						
							|  |  |  |         'Table': { | 
					
						
							|  |  |  |             'CreationDateTime': 1326499200.0, | 
					
						
							|  |  |  |             'ItemCount': 0, | 
					
						
							|  |  |  |             'KeySchema': { | 
					
						
							|  |  |  |                 'HashKeyElement': { | 
					
						
							|  |  |  |                     'AttributeName': 'forum_name', | 
					
						
							|  |  |  |                     'AttributeType': 'S' | 
					
						
							|  |  |  |                 }, | 
					
						
							|  |  |  |                 'RangeKeyElement': { | 
					
						
							|  |  |  |                     'AttributeName': 'subject', | 
					
						
							|  |  |  |                     'AttributeType': 'S' | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             'ProvisionedThroughput': { | 
					
						
							|  |  |  |                 'ReadCapacityUnits': 10, | 
					
						
							|  |  |  |                 'WriteCapacityUnits': 10 | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             'TableName': 'messages', | 
					
						
							|  |  |  |             'TableSizeBytes': 0, | 
					
						
							|  |  |  |             'TableStatus': 'ACTIVE' | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     conn.describe_table('messages').should.equal(expected) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb_deprecated | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  | def test_delete_table(): | 
					
						
							|  |  |  |     conn = boto.connect_dynamodb() | 
					
						
							|  |  |  |     create_table(conn) | 
					
						
							|  |  |  |     conn.list_tables().should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     conn.layer1.delete_table('messages') | 
					
						
							|  |  |  |     conn.list_tables().should.have.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     conn.layer1.delete_table.when.called_with( | 
					
						
							|  |  |  |         'messages').should.throw(DynamoDBResponseError) | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb_deprecated | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  | def test_update_table_throughput(): | 
					
						
							|  |  |  |     conn = boto.connect_dynamodb() | 
					
						
							|  |  |  |     table = create_table(conn) | 
					
						
							|  |  |  |     table.read_units.should.equal(10) | 
					
						
							|  |  |  |     table.write_units.should.equal(10) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     table.update_throughput(5, 6) | 
					
						
							|  |  |  |     table.refresh() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     table.read_units.should.equal(5) | 
					
						
							|  |  |  |     table.write_units.should.equal(6) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb_deprecated | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  | def test_item_add_and_describe_and_update(): | 
					
						
							|  |  |  |     conn = boto.connect_dynamodb() | 
					
						
							|  |  |  |     table = create_table(conn) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     item_data = { | 
					
						
							|  |  |  |         'Body': 'http://url_to_lolcat.gif', | 
					
						
							|  |  |  |         'SentBy': 'User A', | 
					
						
							|  |  |  |         'ReceivedTime': '12/9/2011 11:36:03 PM', | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     item = table.new_item( | 
					
						
							|  |  |  |         hash_key='LOLCat Forum', | 
					
						
							|  |  |  |         range_key='Check this out!', | 
					
						
							|  |  |  |         attrs=item_data, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     item.put() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-07 00:19:04 -04:00
										 |  |  |     table.has_item("LOLCat Forum", "Check this out!").should.equal(True) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  |     returned_item = table.get_item( | 
					
						
							|  |  |  |         hash_key='LOLCat Forum', | 
					
						
							|  |  |  |         range_key='Check this out!', | 
					
						
							|  |  |  |         attributes_to_get=['Body', 'SentBy'] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     dict(returned_item).should.equal({ | 
					
						
							|  |  |  |         'forum_name': 'LOLCat Forum', | 
					
						
							|  |  |  |         'subject': 'Check this out!', | 
					
						
							|  |  |  |         'Body': 'http://url_to_lolcat.gif', | 
					
						
							|  |  |  |         'SentBy': 'User A', | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     item['SentBy'] = 'User B' | 
					
						
							|  |  |  |     item.put() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     returned_item = table.get_item( | 
					
						
							|  |  |  |         hash_key='LOLCat Forum', | 
					
						
							|  |  |  |         range_key='Check this out!', | 
					
						
							|  |  |  |         attributes_to_get=['Body', 'SentBy'] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     dict(returned_item).should.equal({ | 
					
						
							|  |  |  |         'forum_name': 'LOLCat Forum', | 
					
						
							|  |  |  |         'subject': 'Check this out!', | 
					
						
							|  |  |  |         'Body': 'http://url_to_lolcat.gif', | 
					
						
							|  |  |  |         'SentBy': 'User B', | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb_deprecated | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  | def test_item_put_without_table(): | 
					
						
							|  |  |  |     conn = boto.connect_dynamodb() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     conn.layer1.put_item.when.called_with( | 
					
						
							|  |  |  |         table_name='undeclared-table', | 
					
						
							| 
									
										
										
										
											2013-03-15 17:14:14 -04:00
										 |  |  |         item=dict( | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  |             hash_key='LOLCat Forum', | 
					
						
							|  |  |  |             range_key='Check this out!', | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |     ).should.throw(DynamoDBResponseError) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb_deprecated | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  | def test_get_missing_item(): | 
					
						
							|  |  |  |     conn = boto.connect_dynamodb() | 
					
						
							|  |  |  |     table = create_table(conn) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     table.get_item.when.called_with( | 
					
						
							|  |  |  |         hash_key='tester', | 
					
						
							|  |  |  |         range_key='other', | 
					
						
							| 
									
										
										
										
											2013-05-07 00:19:04 -04:00
										 |  |  |     ).should.throw(DynamoDBKeyNotFoundError) | 
					
						
							| 
									
										
										
										
											2013-05-16 22:24:26 -04:00
										 |  |  |     table.has_item("foobar", "more").should.equal(False) | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb_deprecated | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  | def test_get_item_with_undeclared_table(): | 
					
						
							|  |  |  |     conn = boto.connect_dynamodb() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     conn.layer1.get_item.when.called_with( | 
					
						
							|  |  |  |         table_name='undeclared-table', | 
					
						
							|  |  |  |         key={ | 
					
						
							|  |  |  |             'HashKeyElement': {'S': 'tester'}, | 
					
						
							|  |  |  |             'RangeKeyElement': {'S': 'test-range'}, | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2013-05-07 00:19:04 -04:00
										 |  |  |     ).should.throw(DynamoDBKeyNotFoundError) | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb_deprecated | 
					
						
							| 
									
										
										
										
											2013-05-16 22:24:26 -04:00
										 |  |  | def test_get_item_without_range_key(): | 
					
						
							|  |  |  |     conn = boto.connect_dynamodb() | 
					
						
							|  |  |  |     message_table_schema = conn.create_schema( | 
					
						
							|  |  |  |         hash_key_name="test_hash", | 
					
						
							|  |  |  |         hash_key_proto_value=int, | 
					
						
							|  |  |  |         range_key_name="test_range", | 
					
						
							|  |  |  |         range_key_proto_value=int, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     table = conn.create_table( | 
					
						
							|  |  |  |         name='messages', | 
					
						
							|  |  |  |         schema=message_table_schema, | 
					
						
							|  |  |  |         read_units=10, | 
					
						
							|  |  |  |         write_units=10 | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     hash_key = 3241526475 | 
					
						
							|  |  |  |     range_key = 1234567890987 | 
					
						
							|  |  |  |     new_item = table.new_item(hash_key=hash_key, range_key=range_key) | 
					
						
							|  |  |  |     new_item.put() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     table.get_item.when.called_with( | 
					
						
							|  |  |  |         hash_key=hash_key).should.throw(DynamoDBValidationError) | 
					
						
							| 
									
										
										
										
											2013-05-16 22:24:26 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb_deprecated | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  | def test_delete_item(): | 
					
						
							|  |  |  |     conn = boto.connect_dynamodb() | 
					
						
							|  |  |  |     table = create_table(conn) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     item_data = { | 
					
						
							|  |  |  |         'Body': 'http://url_to_lolcat.gif', | 
					
						
							|  |  |  |         'SentBy': 'User A', | 
					
						
							|  |  |  |         'ReceivedTime': '12/9/2011 11:36:03 PM', | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     item = table.new_item( | 
					
						
							|  |  |  |         hash_key='LOLCat Forum', | 
					
						
							|  |  |  |         range_key='Check this out!', | 
					
						
							|  |  |  |         attrs=item_data, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     item.put() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     table.refresh() | 
					
						
							|  |  |  |     table.item_count.should.equal(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     response = item.delete() | 
					
						
							|  |  |  |     response.should.equal({u'Attributes': [], u'ConsumedCapacityUnits': 0.5}) | 
					
						
							|  |  |  |     table.refresh() | 
					
						
							|  |  |  |     table.item_count.should.equal(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     item.delete.when.called_with().should.throw(DynamoDBResponseError) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb_deprecated | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  | def test_delete_item_with_attribute_response(): | 
					
						
							|  |  |  |     conn = boto.connect_dynamodb() | 
					
						
							|  |  |  |     table = create_table(conn) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     item_data = { | 
					
						
							|  |  |  |         'Body': 'http://url_to_lolcat.gif', | 
					
						
							|  |  |  |         'SentBy': 'User A', | 
					
						
							|  |  |  |         'ReceivedTime': '12/9/2011 11:36:03 PM', | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     item = table.new_item( | 
					
						
							|  |  |  |         hash_key='LOLCat Forum', | 
					
						
							|  |  |  |         range_key='Check this out!', | 
					
						
							|  |  |  |         attrs=item_data, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     item.put() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     table.refresh() | 
					
						
							|  |  |  |     table.item_count.should.equal(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     response = item.delete(return_values='ALL_OLD') | 
					
						
							|  |  |  |     response.should.equal({ | 
					
						
							| 
									
										
										
										
											2014-08-26 13:25:50 -04:00
										 |  |  |         'Attributes': { | 
					
						
							|  |  |  |             'Body': 'http://url_to_lolcat.gif', | 
					
						
							|  |  |  |             'forum_name': 'LOLCat Forum', | 
					
						
							|  |  |  |             'ReceivedTime': '12/9/2011 11:36:03 PM', | 
					
						
							|  |  |  |             'SentBy': 'User A', | 
					
						
							|  |  |  |             'subject': 'Check this out!' | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2014-08-26 13:25:50 -04:00
										 |  |  |         'ConsumedCapacityUnits': 0.5 | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  |     }) | 
					
						
							|  |  |  |     table.refresh() | 
					
						
							|  |  |  |     table.item_count.should.equal(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     item.delete.when.called_with().should.throw(DynamoDBResponseError) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb_deprecated | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  | def test_delete_item_with_undeclared_table(): | 
					
						
							|  |  |  |     conn = boto.connect_dynamodb() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     conn.layer1.delete_item.when.called_with( | 
					
						
							|  |  |  |         table_name='undeclared-table', | 
					
						
							|  |  |  |         key={ | 
					
						
							|  |  |  |             'HashKeyElement': {'S': 'tester'}, | 
					
						
							|  |  |  |             'RangeKeyElement': {'S': 'test-range'}, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |     ).should.throw(DynamoDBResponseError) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb_deprecated | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  | def test_query(): | 
					
						
							|  |  |  |     conn = boto.connect_dynamodb() | 
					
						
							|  |  |  |     table = create_table(conn) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     item_data = { | 
					
						
							|  |  |  |         'Body': 'http://url_to_lolcat.gif', | 
					
						
							|  |  |  |         'SentBy': 'User A', | 
					
						
							|  |  |  |         'ReceivedTime': '12/9/2011 11:36:03 PM', | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     item = table.new_item( | 
					
						
							|  |  |  |         hash_key='the-key', | 
					
						
							|  |  |  |         range_key='456', | 
					
						
							|  |  |  |         attrs=item_data, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     item.put() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     item = table.new_item( | 
					
						
							|  |  |  |         hash_key='the-key', | 
					
						
							|  |  |  |         range_key='123', | 
					
						
							|  |  |  |         attrs=item_data, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     item.put() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     item = table.new_item( | 
					
						
							|  |  |  |         hash_key='the-key', | 
					
						
							|  |  |  |         range_key='789', | 
					
						
							|  |  |  |         attrs=item_data, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     item.put() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     results = table.query(hash_key='the-key', | 
					
						
							|  |  |  |                           range_key_condition=condition.GT('1')) | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  |     results.response['Items'].should.have.length_of(3) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     results = table.query(hash_key='the-key', | 
					
						
							|  |  |  |                           range_key_condition=condition.GT('234')) | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  |     results.response['Items'].should.have.length_of(2) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     results = table.query(hash_key='the-key', | 
					
						
							|  |  |  |                           range_key_condition=condition.GT('9999')) | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  |     results.response['Items'].should.have.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     results = table.query(hash_key='the-key', | 
					
						
							|  |  |  |                           range_key_condition=condition.CONTAINS('12')) | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  |     results.response['Items'].should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     results = table.query(hash_key='the-key', | 
					
						
							|  |  |  |                           range_key_condition=condition.BEGINS_WITH('7')) | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  |     results.response['Items'].should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     results = table.query(hash_key='the-key', | 
					
						
							|  |  |  |                           range_key_condition=condition.BETWEEN('567', '890')) | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  |     results.response['Items'].should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb_deprecated | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  | def test_query_with_undeclared_table(): | 
					
						
							|  |  |  |     conn = boto.connect_dynamodb() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     conn.layer1.query.when.called_with( | 
					
						
							|  |  |  |         table_name='undeclared-table', | 
					
						
							|  |  |  |         hash_key_value={'S': 'the-key'}, | 
					
						
							|  |  |  |         range_key_conditions={ | 
					
						
							| 
									
										
										
										
											2013-03-15 17:14:14 -04:00
										 |  |  |             "AttributeValueList": [{ | 
					
						
							|  |  |  |                 "S": "User B" | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  |             }], | 
					
						
							| 
									
										
										
										
											2013-03-15 17:14:14 -04:00
										 |  |  |             "ComparisonOperator": "EQ", | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  |         }, | 
					
						
							|  |  |  |     ).should.throw(DynamoDBResponseError) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb_deprecated | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  | def test_scan(): | 
					
						
							|  |  |  |     conn = boto.connect_dynamodb() | 
					
						
							|  |  |  |     table = create_table(conn) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     item_data = { | 
					
						
							|  |  |  |         'Body': 'http://url_to_lolcat.gif', | 
					
						
							|  |  |  |         'SentBy': 'User A', | 
					
						
							|  |  |  |         'ReceivedTime': '12/9/2011 11:36:03 PM', | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     item = table.new_item( | 
					
						
							|  |  |  |         hash_key='the-key', | 
					
						
							|  |  |  |         range_key='456', | 
					
						
							|  |  |  |         attrs=item_data, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     item.put() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     item = table.new_item( | 
					
						
							|  |  |  |         hash_key='the-key', | 
					
						
							|  |  |  |         range_key='123', | 
					
						
							|  |  |  |         attrs=item_data, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     item.put() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     item_data = { | 
					
						
							|  |  |  |         'Body': 'http://url_to_lolcat.gif', | 
					
						
							|  |  |  |         'SentBy': 'User B', | 
					
						
							|  |  |  |         'ReceivedTime': '12/9/2011 11:36:03 PM', | 
					
						
							| 
									
										
										
										
											2013-10-03 20:34:13 -04:00
										 |  |  |         'Ids': set([1, 2, 3]), | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  |         'PK': 7, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     item = table.new_item( | 
					
						
							|  |  |  |         hash_key='the-key', | 
					
						
							|  |  |  |         range_key='789', | 
					
						
							|  |  |  |         attrs=item_data, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     item.put() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-15 17:14:14 -04:00
										 |  |  |     results = table.scan() | 
					
						
							|  |  |  |     results.response['Items'].should.have.length_of(3) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  |     results = table.scan(scan_filter={'SentBy': condition.EQ('User B')}) | 
					
						
							|  |  |  |     results.response['Items'].should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     results = table.scan(scan_filter={'Body': condition.BEGINS_WITH('http')}) | 
					
						
							|  |  |  |     results.response['Items'].should.have.length_of(3) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     results = table.scan(scan_filter={'Ids': condition.CONTAINS(2)}) | 
					
						
							|  |  |  |     results.response['Items'].should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     results = table.scan(scan_filter={'Ids': condition.NOT_NULL()}) | 
					
						
							|  |  |  |     results.response['Items'].should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     results = table.scan(scan_filter={'Ids': condition.NULL()}) | 
					
						
							|  |  |  |     results.response['Items'].should.have.length_of(2) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     results = table.scan(scan_filter={'PK': condition.BETWEEN(8, 9)}) | 
					
						
							|  |  |  |     results.response['Items'].should.have.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     results = table.scan(scan_filter={'PK': condition.BETWEEN(5, 8)}) | 
					
						
							|  |  |  |     results.response['Items'].should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb_deprecated | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  | def test_scan_with_undeclared_table(): | 
					
						
							|  |  |  |     conn = boto.connect_dynamodb() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     conn.layer1.scan.when.called_with( | 
					
						
							|  |  |  |         table_name='undeclared-table', | 
					
						
							|  |  |  |         scan_filter={ | 
					
						
							|  |  |  |             "SentBy": { | 
					
						
							| 
									
										
										
										
											2013-03-15 17:14:14 -04:00
										 |  |  |                 "AttributeValueList": [{ | 
					
						
							|  |  |  |                     "S": "User B"} | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  |                 ], | 
					
						
							| 
									
										
										
										
											2013-03-15 17:14:14 -04:00
										 |  |  |                 "ComparisonOperator": "EQ" | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  |             } | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |     ).should.throw(DynamoDBResponseError) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb_deprecated | 
					
						
							| 
									
										
										
										
											2016-10-25 17:09:39 +01:00
										 |  |  | def test_scan_after_has_item(): | 
					
						
							|  |  |  |     conn = boto.connect_dynamodb() | 
					
						
							|  |  |  |     table = create_table(conn) | 
					
						
							|  |  |  |     list(table.scan()).should.equal([]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     table.has_item(hash_key='the-key', range_key='123') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     list(table.scan()).should.equal([]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb_deprecated | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  | def test_write_batch(): | 
					
						
							|  |  |  |     conn = boto.connect_dynamodb() | 
					
						
							|  |  |  |     table = create_table(conn) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     batch_list = conn.new_batch_write_list() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     items = [] | 
					
						
							|  |  |  |     items.append(table.new_item( | 
					
						
							|  |  |  |         hash_key='the-key', | 
					
						
							|  |  |  |         range_key='123', | 
					
						
							|  |  |  |         attrs={ | 
					
						
							|  |  |  |             'Body': 'http://url_to_lolcat.gif', | 
					
						
							|  |  |  |             'SentBy': 'User A', | 
					
						
							|  |  |  |             'ReceivedTime': '12/9/2011 11:36:03 PM', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |     )) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     items.append(table.new_item( | 
					
						
							|  |  |  |         hash_key='the-key', | 
					
						
							|  |  |  |         range_key='789', | 
					
						
							|  |  |  |         attrs={ | 
					
						
							|  |  |  |             'Body': 'http://url_to_lolcat.gif', | 
					
						
							|  |  |  |             'SentBy': 'User B', | 
					
						
							|  |  |  |             'ReceivedTime': '12/9/2011 11:36:03 PM', | 
					
						
							| 
									
										
										
										
											2013-10-03 20:34:13 -04:00
										 |  |  |             'Ids': set([1, 2, 3]), | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  |             'PK': 7, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |     )) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     batch_list.add_batch(table, puts=items) | 
					
						
							|  |  |  |     conn.batch_write_item(batch_list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     table.refresh() | 
					
						
							|  |  |  |     table.item_count.should.equal(2) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     batch_list = conn.new_batch_write_list() | 
					
						
							|  |  |  |     batch_list.add_batch(table, deletes=[('the-key', '789')]) | 
					
						
							|  |  |  |     conn.batch_write_item(batch_list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     table.refresh() | 
					
						
							|  |  |  |     table.item_count.should.equal(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb_deprecated | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  | def test_batch_read(): | 
					
						
							|  |  |  |     conn = boto.connect_dynamodb() | 
					
						
							|  |  |  |     table = create_table(conn) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     item_data = { | 
					
						
							|  |  |  |         'Body': 'http://url_to_lolcat.gif', | 
					
						
							|  |  |  |         'SentBy': 'User A', | 
					
						
							|  |  |  |         'ReceivedTime': '12/9/2011 11:36:03 PM', | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     item = table.new_item( | 
					
						
							|  |  |  |         hash_key='the-key', | 
					
						
							|  |  |  |         range_key='456', | 
					
						
							|  |  |  |         attrs=item_data, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     item.put() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     item = table.new_item( | 
					
						
							|  |  |  |         hash_key='the-key', | 
					
						
							|  |  |  |         range_key='123', | 
					
						
							|  |  |  |         attrs=item_data, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     item.put() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     item_data = { | 
					
						
							|  |  |  |         'Body': 'http://url_to_lolcat.gif', | 
					
						
							|  |  |  |         'SentBy': 'User B', | 
					
						
							|  |  |  |         'ReceivedTime': '12/9/2011 11:36:03 PM', | 
					
						
							| 
									
										
										
										
											2013-10-03 20:34:13 -04:00
										 |  |  |         'Ids': set([1, 2, 3]), | 
					
						
							| 
									
										
										
										
											2013-03-14 22:52:32 -04:00
										 |  |  |         'PK': 7, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     item = table.new_item( | 
					
						
							|  |  |  |         hash_key='another-key', | 
					
						
							|  |  |  |         range_key='789', | 
					
						
							|  |  |  |         attrs=item_data, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     item.put() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     items = table.batch_get_item([('the-key', '123'), ('another-key', '789')]) | 
					
						
							| 
									
										
										
										
											2013-08-03 17:27:34 -04:00
										 |  |  |     # Iterate through so that batch_item gets called | 
					
						
							|  |  |  |     count = len([x for x in items]) | 
					
						
							|  |  |  |     count.should.equal(2) |