| 
									
										
										
										
											2014-08-27 11:17:06 -04:00
										 |  |  | from __future__ import unicode_literals | 
					
						
							| 
									
										
										
										
											2014-11-27 08:03:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | from decimal import Decimal | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | import boto | 
					
						
							| 
									
										
										
										
											2015-08-01 19:32:33 -04:00
										 |  |  | import boto3 | 
					
						
							|  |  |  | from boto3.dynamodb.conditions import Key | 
					
						
							| 
									
										
										
										
											2017-09-12 21:42:29 -07:00
										 |  |  | from botocore.exceptions import ClientError | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | import sure  # noqa | 
					
						
							|  |  |  | from freezegun import freeze_time | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | from moto import mock_dynamodb2, mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | from boto.exception import JSONResponseError | 
					
						
							| 
									
										
										
										
											2014-11-27 08:03:47 -05:00
										 |  |  | from tests.helpers import requires_boto_gte | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-07 12:59:36 +02:00
										 |  |  | try: | 
					
						
							| 
									
										
										
										
											2016-01-14 11:30:50 -08:00
										 |  |  |     from boto.dynamodb2.fields import GlobalAllIndex, HashKey, RangeKey, AllIndex | 
					
						
							| 
									
										
										
										
											2015-03-14 15:02:43 -04:00
										 |  |  |     from boto.dynamodb2.table import Item, Table | 
					
						
							| 
									
										
										
										
											2016-01-14 14:25:04 -08:00
										 |  |  |     from boto.dynamodb2.types import STRING, NUMBER | 
					
						
							| 
									
										
										
										
											2014-01-07 12:59:36 +02:00
										 |  |  |     from boto.dynamodb2.exceptions import ValidationException | 
					
						
							| 
									
										
										
										
											2015-07-08 11:55:12 -04:00
										 |  |  |     from boto.dynamodb2.exceptions import ConditionalCheckFailedException | 
					
						
							| 
									
										
										
										
											2014-01-07 12:59:36 +02:00
										 |  |  | except ImportError: | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  |     pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | def create_table(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table = Table.create( | 
					
						
							|  |  |  |         "messages", | 
					
						
							|  |  |  |         schema=[HashKey("forum_name"), RangeKey("subject")], | 
					
						
							|  |  |  |         throughput={"read": 10, "write": 10}, | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     return table | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-14 11:30:50 -08:00
										 |  |  | def create_table_with_local_indexes(): | 
					
						
							|  |  |  |     table = Table.create( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "messages", | 
					
						
							|  |  |  |         schema=[HashKey("forum_name"), RangeKey("subject")], | 
					
						
							|  |  |  |         throughput={"read": 10, "write": 10}, | 
					
						
							| 
									
										
										
										
											2016-01-14 11:30:50 -08:00
										 |  |  |         indexes=[ | 
					
						
							|  |  |  |             AllIndex( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 "threads_index", | 
					
						
							| 
									
										
										
										
											2016-01-14 11:30:50 -08:00
										 |  |  |                 parts=[ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                     HashKey("forum_name", data_type=STRING), | 
					
						
							|  |  |  |                     RangeKey("threads", data_type=NUMBER), | 
					
						
							|  |  |  |                 ], | 
					
						
							| 
									
										
										
										
											2016-01-14 11:30:50 -08:00
										 |  |  |             ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ], | 
					
						
							| 
									
										
										
										
											2016-01-14 11:30:50 -08:00
										 |  |  |     ) | 
					
						
							|  |  |  |     return table | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | def iterate_results(res): | 
					
						
							|  |  |  |     for i in res: | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  |         pass | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-07 10:56:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | @requires_boto_gte("2.9") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2014-01-07 10:56:51 +02:00
										 |  |  | @freeze_time("2012-01-14") | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | def test_create_table(): | 
					
						
							|  |  |  |     table = create_table() | 
					
						
							|  |  |  |     expected = { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "Table": { | 
					
						
							|  |  |  |             "AttributeDefinitions": [ | 
					
						
							|  |  |  |                 {"AttributeName": "forum_name", "AttributeType": "S"}, | 
					
						
							|  |  |  |                 {"AttributeName": "subject", "AttributeType": "S"}, | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  |             ], | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "ProvisionedThroughput": { | 
					
						
							|  |  |  |                 "NumberOfDecreasesToday": 0, | 
					
						
							|  |  |  |                 "WriteCapacityUnits": 10, | 
					
						
							|  |  |  |                 "ReadCapacityUnits": 10, | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  |             }, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "TableSizeBytes": 0, | 
					
						
							|  |  |  |             "TableName": "messages", | 
					
						
							|  |  |  |             "TableStatus": "ACTIVE", | 
					
						
							|  |  |  |             "TableArn": "arn:aws:dynamodb:us-east-1:123456789011:table/messages", | 
					
						
							|  |  |  |             "KeySchema": [ | 
					
						
							|  |  |  |                 {"KeyType": "HASH", "AttributeName": "forum_name"}, | 
					
						
							|  |  |  |                 {"KeyType": "RANGE", "AttributeName": "subject"}, | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  |             ], | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "LocalSecondaryIndexes": [], | 
					
						
							|  |  |  |             "ItemCount": 0, | 
					
						
							|  |  |  |             "CreationDateTime": 1326499200.0, | 
					
						
							|  |  |  |             "GlobalSecondaryIndexes": [], | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     table.describe().should.equal(expected) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-14 11:30:50 -08:00
										 |  |  | @requires_boto_gte("2.9") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2016-01-14 11:30:50 -08:00
										 |  |  | @freeze_time("2012-01-14") | 
					
						
							| 
									
										
										
										
											2016-01-14 14:25:04 -08:00
										 |  |  | def test_create_table_with_local_index(): | 
					
						
							| 
									
										
										
										
											2016-01-14 11:30:50 -08:00
										 |  |  |     table = create_table_with_local_indexes() | 
					
						
							|  |  |  |     expected = { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "Table": { | 
					
						
							|  |  |  |             "AttributeDefinitions": [ | 
					
						
							|  |  |  |                 {"AttributeName": "forum_name", "AttributeType": "S"}, | 
					
						
							|  |  |  |                 {"AttributeName": "subject", "AttributeType": "S"}, | 
					
						
							|  |  |  |                 {"AttributeName": "threads", "AttributeType": "N"}, | 
					
						
							| 
									
										
										
										
											2016-01-14 11:30:50 -08:00
										 |  |  |             ], | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "ProvisionedThroughput": { | 
					
						
							|  |  |  |                 "NumberOfDecreasesToday": 0, | 
					
						
							|  |  |  |                 "WriteCapacityUnits": 10, | 
					
						
							|  |  |  |                 "ReadCapacityUnits": 10, | 
					
						
							| 
									
										
										
										
											2016-01-14 11:30:50 -08:00
										 |  |  |             }, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "TableSizeBytes": 0, | 
					
						
							|  |  |  |             "TableName": "messages", | 
					
						
							|  |  |  |             "TableStatus": "ACTIVE", | 
					
						
							|  |  |  |             "TableArn": "arn:aws:dynamodb:us-east-1:123456789011:table/messages", | 
					
						
							|  |  |  |             "KeySchema": [ | 
					
						
							|  |  |  |                 {"KeyType": "HASH", "AttributeName": "forum_name"}, | 
					
						
							|  |  |  |                 {"KeyType": "RANGE", "AttributeName": "subject"}, | 
					
						
							| 
									
										
										
										
											2016-01-14 11:30:50 -08:00
										 |  |  |             ], | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "LocalSecondaryIndexes": [ | 
					
						
							| 
									
										
										
										
											2016-01-14 11:30:50 -08:00
										 |  |  |                 { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                     "IndexName": "threads_index", | 
					
						
							|  |  |  |                     "KeySchema": [ | 
					
						
							|  |  |  |                         {"AttributeName": "forum_name", "KeyType": "HASH"}, | 
					
						
							|  |  |  |                         {"AttributeName": "threads", "KeyType": "RANGE"}, | 
					
						
							| 
									
										
										
										
											2016-01-14 11:30:50 -08:00
										 |  |  |                     ], | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                     "Projection": {"ProjectionType": "ALL"}, | 
					
						
							| 
									
										
										
										
											2016-01-14 11:30:50 -08:00
										 |  |  |                 } | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "ItemCount": 0, | 
					
						
							|  |  |  |             "CreationDateTime": 1326499200.0, | 
					
						
							|  |  |  |             "GlobalSecondaryIndexes": [], | 
					
						
							| 
									
										
										
										
											2016-01-14 11:30:50 -08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     table.describe().should.equal(expected) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-07 10:56:51 +02:00
										 |  |  | @requires_boto_gte("2.9") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | def test_delete_table(): | 
					
						
							|  |  |  |     conn = boto.dynamodb2.layer1.DynamoDBConnection() | 
					
						
							|  |  |  |     table = create_table() | 
					
						
							|  |  |  |     conn.list_tables()["TableNames"].should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     table.delete() | 
					
						
							|  |  |  |     conn.list_tables()["TableNames"].should.have.length_of(0) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn.delete_table.when.called_with("messages").should.throw(JSONResponseError) | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-07 10:56:51 +02:00
										 |  |  | @requires_boto_gte("2.9") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | def test_update_table_throughput(): | 
					
						
							|  |  |  |     table = create_table() | 
					
						
							|  |  |  |     table.throughput["read"].should.equal(10) | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  |     table.throughput["write"].should.equal(10) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table.update(throughput={"read": 5, "write": 15}) | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     table.throughput["read"].should.equal(5) | 
					
						
							|  |  |  |     table.throughput["write"].should.equal(15) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table.update(throughput={"read": 5, "write": 6}) | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     table.describe() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     table.throughput["read"].should.equal(5) | 
					
						
							|  |  |  |     table.throughput["write"].should.equal(6) | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-07 10:56:51 +02:00
										 |  |  | @requires_boto_gte("2.9") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | def test_item_add_and_describe_and_update(): | 
					
						
							|  |  |  |     table = create_table() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ok = table.put_item( | 
					
						
							|  |  |  |         data={ | 
					
						
							|  |  |  |             "forum_name": "LOLCat Forum", | 
					
						
							|  |  |  |             "subject": "Check this out!", | 
					
						
							|  |  |  |             "Body": "http://url_to_lolcat.gif", | 
					
						
							|  |  |  |             "SentBy": "User A", | 
					
						
							|  |  |  |             "ReceivedTime": "12/9/2011 11:36:03 PM", | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     ok.should.equal(True) | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table.get_item( | 
					
						
							|  |  |  |         forum_name="LOLCat Forum", subject="Check this out!" | 
					
						
							|  |  |  |     ).should_not.be.none | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     returned_item = table.get_item(forum_name="LOLCat Forum", subject="Check this out!") | 
					
						
							|  |  |  |     dict(returned_item).should.equal( | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "forum_name": "LOLCat Forum", | 
					
						
							|  |  |  |             "subject": "Check this out!", | 
					
						
							|  |  |  |             "Body": "http://url_to_lolcat.gif", | 
					
						
							|  |  |  |             "SentBy": "User A", | 
					
						
							|  |  |  |             "ReceivedTime": "12/9/2011 11:36:03 PM", | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     returned_item["SentBy"] = "User B" | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     returned_item.save(overwrite=True) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     returned_item = table.get_item(forum_name="LOLCat Forum", subject="Check this out!") | 
					
						
							|  |  |  |     dict(returned_item).should.equal( | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "forum_name": "LOLCat Forum", | 
					
						
							|  |  |  |             "subject": "Check this out!", | 
					
						
							|  |  |  |             "Body": "http://url_to_lolcat.gif", | 
					
						
							|  |  |  |             "SentBy": "User B", | 
					
						
							|  |  |  |             "ReceivedTime": "12/9/2011 11:36:03 PM", | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-31 10:17:08 -08:00
										 |  |  | @requires_boto_gte("2.9") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2015-12-31 10:17:08 -08:00
										 |  |  | def test_item_partial_save(): | 
					
						
							|  |  |  |     table = create_table() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     data = { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "forum_name": "LOLCat Forum", | 
					
						
							|  |  |  |         "subject": "The LOLz", | 
					
						
							|  |  |  |         "Body": "http://url_to_lolcat.gif", | 
					
						
							|  |  |  |         "SentBy": "User A", | 
					
						
							| 
									
										
										
										
											2015-12-31 10:17:08 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     table.put_item(data=data) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     returned_item = table.get_item(forum_name="LOLCat Forum", subject="The LOLz") | 
					
						
							| 
									
										
										
										
											2015-12-31 10:17:08 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     returned_item["SentBy"] = "User B" | 
					
						
							| 
									
										
										
										
											2015-12-31 10:17:08 -08:00
										 |  |  |     returned_item.partial_save() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     returned_item = table.get_item(forum_name="LOLCat Forum", subject="The LOLz") | 
					
						
							|  |  |  |     dict(returned_item).should.equal( | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "forum_name": "LOLCat Forum", | 
					
						
							|  |  |  |             "subject": "The LOLz", | 
					
						
							|  |  |  |             "Body": "http://url_to_lolcat.gif", | 
					
						
							|  |  |  |             "SentBy": "User B", | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-12-31 10:17:08 -08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-07 10:56:51 +02:00
										 |  |  | @requires_boto_gte("2.9") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | def test_item_put_without_table(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table = Table("undeclared-table") | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     item_data = { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "forum_name": "LOLCat Forum", | 
					
						
							|  |  |  |         "Body": "http://url_to_lolcat.gif", | 
					
						
							|  |  |  |         "SentBy": "User A", | 
					
						
							|  |  |  |         "ReceivedTime": "12/9/2011 11:36:03 PM", | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  |     item = Item(table, item_data) | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     item.save.when.called_with().should.throw(JSONResponseError) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-07 10:56:51 +02:00
										 |  |  | @requires_boto_gte("2.9") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | def test_get_missing_item(): | 
					
						
							|  |  |  |     table = create_table() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table.get_item.when.called_with(hash_key="tester", range_key="other").should.throw( | 
					
						
							|  |  |  |         ValidationException | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-07 10:56:51 +02:00
										 |  |  | @requires_boto_gte("2.9") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | def test_get_item_with_undeclared_table(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table = Table("undeclared-table") | 
					
						
							|  |  |  |     table.get_item.when.called_with(test_hash=3241526475).should.throw( | 
					
						
							|  |  |  |         JSONResponseError | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-07 10:56:51 +02:00
										 |  |  | @requires_boto_gte("2.9") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | def test_get_item_without_range_key(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table = Table.create( | 
					
						
							|  |  |  |         "messages", | 
					
						
							|  |  |  |         schema=[HashKey("test_hash"), RangeKey("test_range")], | 
					
						
							|  |  |  |         throughput={"read": 10, "write": 10}, | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     hash_key = 3241526475 | 
					
						
							|  |  |  |     range_key = 1234567890987 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table.put_item(data={"test_hash": hash_key, "test_range": range_key}) | 
					
						
							|  |  |  |     table.get_item.when.called_with(test_hash=hash_key).should.throw( | 
					
						
							|  |  |  |         ValidationException | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-22 21:53:54 -04:00
										 |  |  | @requires_boto_gte("2.30.0") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | def test_delete_item(): | 
					
						
							|  |  |  |     table = create_table() | 
					
						
							|  |  |  |     item_data = { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "forum_name": "LOLCat Forum", | 
					
						
							|  |  |  |         "Body": "http://url_to_lolcat.gif", | 
					
						
							|  |  |  |         "SentBy": "User A", | 
					
						
							|  |  |  |         "ReceivedTime": "12/9/2011 11:36:03 PM", | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  |     item = Item(table, item_data) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     item["subject"] = "Check this out!" | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     item.save() | 
					
						
							|  |  |  |     table.count().should.equal(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     response = item.delete() | 
					
						
							|  |  |  |     response.should.equal(True) | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     table.count().should.equal(0) | 
					
						
							| 
									
										
										
										
											2017-05-01 12:31:31 -07:00
										 |  |  |     # Deletes are idempotent | 
					
						
							|  |  |  |     item.delete().should.equal(True) | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-07 10:56:51 +02:00
										 |  |  | @requires_boto_gte("2.9") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | def test_delete_item_with_undeclared_table(): | 
					
						
							|  |  |  |     table = Table("undeclared-table") | 
					
						
							|  |  |  |     item_data = { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "forum_name": "LOLCat Forum", | 
					
						
							|  |  |  |         "Body": "http://url_to_lolcat.gif", | 
					
						
							|  |  |  |         "SentBy": "User A", | 
					
						
							|  |  |  |         "ReceivedTime": "12/9/2011 11:36:03 PM", | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  |     item = Item(table, item_data) | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     item.delete.when.called_with().should.throw(JSONResponseError) | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-07 10:56:51 +02:00
										 |  |  | @requires_boto_gte("2.9") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | def test_query(): | 
					
						
							|  |  |  |     table = create_table() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     item_data = { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "forum_name": "LOLCat Forum", | 
					
						
							|  |  |  |         "Body": "http://url_to_lolcat.gif", | 
					
						
							|  |  |  |         "SentBy": "User A", | 
					
						
							|  |  |  |         "ReceivedTime": "12/9/2011 11:36:03 PM", | 
					
						
							|  |  |  |         "subject": "Check this out!", | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  |     item = Item(table, item_data) | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     item.save(overwrite=True) | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     item["forum_name"] = "the-key" | 
					
						
							|  |  |  |     item["subject"] = "456" | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     item.save(overwrite=True) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     item["forum_name"] = "the-key" | 
					
						
							|  |  |  |     item["subject"] = "123" | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     item.save(overwrite=True) | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     item["forum_name"] = "the-key" | 
					
						
							|  |  |  |     item["subject"] = "789" | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     item.save(overwrite=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     table.count().should.equal(4) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     results = table.query_2(forum_name__eq="the-key", subject__gt="1", consistent=True) | 
					
						
							| 
									
										
										
										
											2014-02-23 23:38:04 -08:00
										 |  |  |     expected = ["123", "456", "789"] | 
					
						
							|  |  |  |     for index, item in enumerate(results): | 
					
						
							|  |  |  |         item["subject"].should.equal(expected[index]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     results = table.query_2(forum_name__eq="the-key", subject__gt="1", reverse=True) | 
					
						
							| 
									
										
										
										
											2014-03-03 13:49:08 -08:00
										 |  |  |     for index, item in enumerate(results): | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  |         item["subject"].should.equal(expected[len(expected) - 1 - index]) | 
					
						
							| 
									
										
										
										
											2014-03-03 13:49:08 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     results = table.query_2(forum_name__eq="the-key", subject__gt="1", consistent=True) | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     sum(1 for _ in results).should.equal(3) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     results = table.query_2( | 
					
						
							|  |  |  |         forum_name__eq="the-key", subject__gt="234", consistent=True | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     sum(1 for _ in results).should.equal(2) | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     results = table.query_2(forum_name__eq="the-key", subject__gt="9999") | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     sum(1 for _ in results).should.equal(0) | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     results = table.query_2(forum_name__eq="the-key", subject__beginswith="12") | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     sum(1 for _ in results).should.equal(1) | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     results = table.query_2(forum_name__eq="the-key", subject__beginswith="7") | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     sum(1 for _ in results).should.equal(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     results = table.query_2(forum_name__eq="the-key", subject__between=["567", "890"]) | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     sum(1 for _ in results).should.equal(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-07 10:56:51 +02:00
										 |  |  | @requires_boto_gte("2.9") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | def test_query_with_undeclared_table(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table = Table("undeclared") | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     results = table.query( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         forum_name__eq="Amazon DynamoDB", subject__beginswith="DynamoDB", limit=1 | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     ) | 
					
						
							|  |  |  |     iterate_results.when.called_with(results).should.throw(JSONResponseError) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-07 10:56:51 +02:00
										 |  |  | @requires_boto_gte("2.9") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | def test_scan(): | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  |     table = create_table() | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     item_data = { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "Body": "http://url_to_lolcat.gif", | 
					
						
							|  |  |  |         "SentBy": "User A", | 
					
						
							|  |  |  |         "ReceivedTime": "12/9/2011 11:36:03 PM", | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     item_data["forum_name"] = "the-key" | 
					
						
							|  |  |  |     item_data["subject"] = "456" | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     item = Item(table, item_data) | 
					
						
							|  |  |  |     item.save() | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     item["forum_name"] = "the-key" | 
					
						
							|  |  |  |     item["subject"] = "123" | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     item.save() | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     item_data = { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "Body": "http://url_to_lolcat.gif", | 
					
						
							|  |  |  |         "SentBy": "User B", | 
					
						
							|  |  |  |         "ReceivedTime": "12/9/2011 11:36:09 PM", | 
					
						
							|  |  |  |         "Ids": set([1, 2, 3]), | 
					
						
							|  |  |  |         "PK": 7, | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     item_data["forum_name"] = "the-key" | 
					
						
							|  |  |  |     item_data["subject"] = "789" | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     item = Item(table, item_data) | 
					
						
							|  |  |  |     item.save() | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     results = table.scan() | 
					
						
							|  |  |  |     sum(1 for _ in results).should.equal(3) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     results = table.scan(SentBy__eq="User B") | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     sum(1 for _ in results).should.equal(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     results = table.scan(Body__beginswith="http") | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     sum(1 for _ in results).should.equal(3) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     results = table.scan(Ids__null=False) | 
					
						
							|  |  |  |     sum(1 for _ in results).should.equal(1) | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     results = table.scan(Ids__null=True) | 
					
						
							|  |  |  |     sum(1 for _ in results).should.equal(2) | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     results = table.scan(PK__between=[8, 9]) | 
					
						
							|  |  |  |     sum(1 for _ in results).should.equal(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     results = table.scan(PK__between=[5, 8]) | 
					
						
							|  |  |  |     sum(1 for _ in results).should.equal(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-07 10:56:51 +02:00
										 |  |  | @requires_boto_gte("2.9") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | def test_scan_with_undeclared_table(): | 
					
						
							|  |  |  |     conn = boto.dynamodb2.layer1.DynamoDBConnection() | 
					
						
							|  |  |  |     conn.scan.when.called_with( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         table_name="undeclared-table", | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |         scan_filter={ | 
					
						
							|  |  |  |             "SentBy": { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 "AttributeValueList": [{"S": "User B"}], | 
					
						
							|  |  |  |                 "ComparisonOperator": "EQ", | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |     ).should.throw(JSONResponseError) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-07 10:56:51 +02:00
										 |  |  | @requires_boto_gte("2.9") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | def test_write_batch(): | 
					
						
							|  |  |  |     table = create_table() | 
					
						
							|  |  |  |     with table.batch_write() as batch: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         batch.put_item( | 
					
						
							|  |  |  |             data={ | 
					
						
							|  |  |  |                 "forum_name": "the-key", | 
					
						
							|  |  |  |                 "subject": "123", | 
					
						
							|  |  |  |                 "Body": "http://url_to_lolcat.gif", | 
					
						
							|  |  |  |                 "SentBy": "User A", | 
					
						
							|  |  |  |                 "ReceivedTime": "12/9/2011 11:36:03 PM", | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         batch.put_item( | 
					
						
							|  |  |  |             data={ | 
					
						
							|  |  |  |                 "forum_name": "the-key", | 
					
						
							|  |  |  |                 "subject": "789", | 
					
						
							|  |  |  |                 "Body": "http://url_to_lolcat.gif", | 
					
						
							|  |  |  |                 "SentBy": "User B", | 
					
						
							|  |  |  |                 "ReceivedTime": "12/9/2011 11:36:03 PM", | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     table.count().should.equal(2) | 
					
						
							|  |  |  |     with table.batch_write() as batch: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         batch.delete_item(forum_name="the-key", subject="789") | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     table.count().should.equal(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-07 10:56:51 +02:00
										 |  |  | @requires_boto_gte("2.9") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | def test_batch_read(): | 
					
						
							|  |  |  |     table = create_table() | 
					
						
							|  |  |  |     item_data = { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "Body": "http://url_to_lolcat.gif", | 
					
						
							|  |  |  |         "SentBy": "User A", | 
					
						
							|  |  |  |         "ReceivedTime": "12/9/2011 11:36:03 PM", | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     item_data["forum_name"] = "the-key" | 
					
						
							|  |  |  |     item_data["subject"] = "456" | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  |     item = Item(table, item_data) | 
					
						
							|  |  |  |     item.save() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     item = Item(table, item_data) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     item_data["forum_name"] = "the-key" | 
					
						
							|  |  |  |     item_data["subject"] = "123" | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  |     item.save() | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     item_data = { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "Body": "http://url_to_lolcat.gif", | 
					
						
							|  |  |  |         "SentBy": "User B", | 
					
						
							|  |  |  |         "ReceivedTime": "12/9/2011 11:36:03 PM", | 
					
						
							|  |  |  |         "Ids": set([1, 2, 3]), | 
					
						
							|  |  |  |         "PK": 7, | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  |     item = Item(table, item_data) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     item_data["forum_name"] = "another-key" | 
					
						
							|  |  |  |     item_data["subject"] = "789" | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  |     item.save() | 
					
						
							|  |  |  |     results = table.batch_get( | 
					
						
							|  |  |  |         keys=[ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             {"forum_name": "the-key", "subject": "123"}, | 
					
						
							|  |  |  |             {"forum_name": "another-key", "subject": "789"}, | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  |         ] | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Iterate through so that batch_item gets called | 
					
						
							|  |  |  |     count = len([x for x in results]) | 
					
						
							|  |  |  |     count.should.equal(2) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-22 21:47:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-07 10:56:51 +02:00
										 |  |  | @requires_boto_gte("2.9") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2013-12-05 13:16:56 +02:00
										 |  |  | def test_get_key_fields(): | 
					
						
							|  |  |  |     table = create_table() | 
					
						
							|  |  |  |     kf = table.get_key_fields() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     kf.should.equal(["forum_name", "subject"]) | 
					
						
							| 
									
										
										
										
											2015-03-14 15:02:43 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2015-03-14 15:02:43 -04:00
										 |  |  | def test_create_with_global_indexes(): | 
					
						
							|  |  |  |     conn = boto.dynamodb2.layer1.DynamoDBConnection() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     Table.create( | 
					
						
							|  |  |  |         "messages", | 
					
						
							|  |  |  |         schema=[HashKey("subject"), RangeKey("version")], | 
					
						
							|  |  |  |         global_indexes=[ | 
					
						
							|  |  |  |             GlobalAllIndex( | 
					
						
							|  |  |  |                 "topic-created_at-index", | 
					
						
							|  |  |  |                 parts=[HashKey("topic"), RangeKey("created_at", data_type="N")], | 
					
						
							|  |  |  |                 throughput={"read": 6, "write": 1}, | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2015-03-14 15:02:43 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     table_description = conn.describe_table("messages") | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table_description["Table"]["GlobalSecondaryIndexes"].should.equal( | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 "IndexName": "topic-created_at-index", | 
					
						
							|  |  |  |                 "KeySchema": [ | 
					
						
							|  |  |  |                     {"AttributeName": "topic", "KeyType": "HASH"}, | 
					
						
							|  |  |  |                     {"AttributeName": "created_at", "KeyType": "RANGE"}, | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |                 "Projection": {"ProjectionType": "ALL"}, | 
					
						
							|  |  |  |                 "ProvisionedThroughput": { | 
					
						
							|  |  |  |                     "ReadCapacityUnits": 6, | 
					
						
							|  |  |  |                     "WriteCapacityUnits": 1, | 
					
						
							| 
									
										
										
										
											2015-03-14 15:02:43 -04:00
										 |  |  |                 }, | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ] | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2015-03-14 15:02:43 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2015-03-14 15:02:43 -04:00
										 |  |  | def test_query_with_global_indexes(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table = Table.create( | 
					
						
							|  |  |  |         "messages", | 
					
						
							|  |  |  |         schema=[HashKey("subject"), RangeKey("version")], | 
					
						
							|  |  |  |         global_indexes=[ | 
					
						
							|  |  |  |             GlobalAllIndex( | 
					
						
							|  |  |  |                 "topic-created_at-index", | 
					
						
							|  |  |  |                 parts=[HashKey("topic"), RangeKey("created_at", data_type="N")], | 
					
						
							|  |  |  |                 throughput={"read": 6, "write": 1}, | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             GlobalAllIndex( | 
					
						
							|  |  |  |                 "status-created_at-index", | 
					
						
							|  |  |  |                 parts=[HashKey("status"), RangeKey("created_at", data_type="N")], | 
					
						
							|  |  |  |                 throughput={"read": 2, "write": 1}, | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2015-03-14 15:02:43 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     item_data = { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "subject": "Check this out!", | 
					
						
							|  |  |  |         "version": "1", | 
					
						
							|  |  |  |         "created_at": 0, | 
					
						
							|  |  |  |         "status": "inactive", | 
					
						
							| 
									
										
										
										
											2015-03-14 15:02:43 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     item = Item(table, item_data) | 
					
						
							|  |  |  |     item.save(overwrite=True) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     item["version"] = "2" | 
					
						
							| 
									
										
										
										
											2015-03-14 15:02:43 -04:00
										 |  |  |     item.save(overwrite=True) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     results = table.query(status__eq="active") | 
					
						
							| 
									
										
										
										
											2015-03-14 15:02:43 -04:00
										 |  |  |     list(results).should.have.length_of(0) | 
					
						
							| 
									
										
										
										
											2015-05-20 11:20:16 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2016-01-14 14:25:04 -08:00
										 |  |  | def test_query_with_local_indexes(): | 
					
						
							|  |  |  |     table = create_table_with_local_indexes() | 
					
						
							|  |  |  |     item_data = { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "forum_name": "Cool Forum", | 
					
						
							|  |  |  |         "subject": "Check this out!", | 
					
						
							|  |  |  |         "version": "1", | 
					
						
							|  |  |  |         "threads": 1, | 
					
						
							|  |  |  |         "status": "inactive", | 
					
						
							| 
									
										
										
										
											2016-01-14 14:25:04 -08:00
										 |  |  |     } | 
					
						
							|  |  |  |     item = Item(table, item_data) | 
					
						
							|  |  |  |     item.save(overwrite=True) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     item["version"] = "2" | 
					
						
							| 
									
										
										
										
											2016-01-14 14:25:04 -08:00
										 |  |  |     item.save(overwrite=True) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     results = table.query( | 
					
						
							|  |  |  |         forum_name__eq="Cool Forum", index="threads_index", threads__eq=1 | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-01-14 14:25:04 -08:00
										 |  |  |     list(results).should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  | @requires_boto_gte("2.9") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  | def test_query_filter_eq(): | 
					
						
							|  |  |  |     table = create_table_with_local_indexes() | 
					
						
							|  |  |  |     item_data = [ | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "forum_name": "Cool Forum", | 
					
						
							|  |  |  |             "subject": "Check this out!", | 
					
						
							|  |  |  |             "version": "1", | 
					
						
							|  |  |  |             "threads": 1, | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "forum_name": "Cool Forum", | 
					
						
							|  |  |  |             "subject": "Read this now!", | 
					
						
							|  |  |  |             "version": "1", | 
					
						
							|  |  |  |             "threads": 5, | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "forum_name": "Cool Forum", | 
					
						
							|  |  |  |             "subject": "Please read this... please", | 
					
						
							|  |  |  |             "version": "1", | 
					
						
							|  |  |  |             "threads": 0, | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  |     ] | 
					
						
							|  |  |  |     for data in item_data: | 
					
						
							|  |  |  |         item = Item(table, data) | 
					
						
							|  |  |  |         item.save(overwrite=True) | 
					
						
							|  |  |  |     results = table.query_2( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         forum_name__eq="Cool Forum", index="threads_index", threads__eq=5 | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  |     ) | 
					
						
							|  |  |  |     list(results).should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @requires_boto_gte("2.9") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  | def test_query_filter_lt(): | 
					
						
							|  |  |  |     table = create_table_with_local_indexes() | 
					
						
							|  |  |  |     item_data = [ | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "forum_name": "Cool Forum", | 
					
						
							|  |  |  |             "subject": "Check this out!", | 
					
						
							|  |  |  |             "version": "1", | 
					
						
							|  |  |  |             "threads": 1, | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "forum_name": "Cool Forum", | 
					
						
							|  |  |  |             "subject": "Read this now!", | 
					
						
							|  |  |  |             "version": "1", | 
					
						
							|  |  |  |             "threads": 5, | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "forum_name": "Cool Forum", | 
					
						
							|  |  |  |             "subject": "Please read this... please", | 
					
						
							|  |  |  |             "version": "1", | 
					
						
							|  |  |  |             "threads": 0, | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  |     ] | 
					
						
							|  |  |  |     for data in item_data: | 
					
						
							|  |  |  |         item = Item(table, data) | 
					
						
							|  |  |  |         item.save(overwrite=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     results = table.query( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         forum_name__eq="Cool Forum", index="threads_index", threads__lt=5 | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  |     ) | 
					
						
							|  |  |  |     results = list(results) | 
					
						
							|  |  |  |     results.should.have.length_of(2) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @requires_boto_gte("2.9") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  | def test_query_filter_gt(): | 
					
						
							|  |  |  |     table = create_table_with_local_indexes() | 
					
						
							|  |  |  |     item_data = [ | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "forum_name": "Cool Forum", | 
					
						
							|  |  |  |             "subject": "Check this out!", | 
					
						
							|  |  |  |             "version": "1", | 
					
						
							|  |  |  |             "threads": 1, | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "forum_name": "Cool Forum", | 
					
						
							|  |  |  |             "subject": "Read this now!", | 
					
						
							|  |  |  |             "version": "1", | 
					
						
							|  |  |  |             "threads": 5, | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "forum_name": "Cool Forum", | 
					
						
							|  |  |  |             "subject": "Please read this... please", | 
					
						
							|  |  |  |             "version": "1", | 
					
						
							|  |  |  |             "threads": 0, | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  |     ] | 
					
						
							|  |  |  |     for data in item_data: | 
					
						
							|  |  |  |         item = Item(table, data) | 
					
						
							|  |  |  |         item.save(overwrite=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     results = table.query( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         forum_name__eq="Cool Forum", index="threads_index", threads__gt=1 | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  |     ) | 
					
						
							|  |  |  |     list(results).should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @requires_boto_gte("2.9") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  | def test_query_filter_lte(): | 
					
						
							|  |  |  |     table = create_table_with_local_indexes() | 
					
						
							|  |  |  |     item_data = [ | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "forum_name": "Cool Forum", | 
					
						
							|  |  |  |             "subject": "Check this out!", | 
					
						
							|  |  |  |             "version": "1", | 
					
						
							|  |  |  |             "threads": 1, | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "forum_name": "Cool Forum", | 
					
						
							|  |  |  |             "subject": "Read this now!", | 
					
						
							|  |  |  |             "version": "1", | 
					
						
							|  |  |  |             "threads": 5, | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "forum_name": "Cool Forum", | 
					
						
							|  |  |  |             "subject": "Please read this... please", | 
					
						
							|  |  |  |             "version": "1", | 
					
						
							|  |  |  |             "threads": 0, | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  |     ] | 
					
						
							|  |  |  |     for data in item_data: | 
					
						
							|  |  |  |         item = Item(table, data) | 
					
						
							|  |  |  |         item.save(overwrite=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     results = table.query( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         forum_name__eq="Cool Forum", index="threads_index", threads__lte=5 | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  |     ) | 
					
						
							|  |  |  |     list(results).should.have.length_of(3) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @requires_boto_gte("2.9") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  | def test_query_filter_gte(): | 
					
						
							|  |  |  |     table = create_table_with_local_indexes() | 
					
						
							|  |  |  |     item_data = [ | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "forum_name": "Cool Forum", | 
					
						
							|  |  |  |             "subject": "Check this out!", | 
					
						
							|  |  |  |             "version": "1", | 
					
						
							|  |  |  |             "threads": 1, | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "forum_name": "Cool Forum", | 
					
						
							|  |  |  |             "subject": "Read this now!", | 
					
						
							|  |  |  |             "version": "1", | 
					
						
							|  |  |  |             "threads": 5, | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "forum_name": "Cool Forum", | 
					
						
							|  |  |  |             "subject": "Please read this... please", | 
					
						
							|  |  |  |             "version": "1", | 
					
						
							|  |  |  |             "threads": 0, | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  |     ] | 
					
						
							|  |  |  |     for data in item_data: | 
					
						
							|  |  |  |         item = Item(table, data) | 
					
						
							|  |  |  |         item.save(overwrite=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     results = table.query( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         forum_name__eq="Cool Forum", index="threads_index", threads__gte=1 | 
					
						
							| 
									
										
										
										
											2016-03-16 21:32:54 -07:00
										 |  |  |     ) | 
					
						
							|  |  |  |     list(results).should.have.length_of(2) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-21 14:55:25 -07:00
										 |  |  | @requires_boto_gte("2.9") | 
					
						
							|  |  |  | @mock_dynamodb2_deprecated | 
					
						
							|  |  |  | def test_query_non_hash_range_key(): | 
					
						
							|  |  |  |     table = create_table_with_local_indexes() | 
					
						
							|  |  |  |     item_data = [ | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "forum_name": "Cool Forum", | 
					
						
							|  |  |  |             "subject": "Check this out!", | 
					
						
							|  |  |  |             "version": "1", | 
					
						
							|  |  |  |             "threads": 1, | 
					
						
							| 
									
										
										
										
											2017-04-21 14:55:25 -07:00
										 |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "forum_name": "Cool Forum", | 
					
						
							|  |  |  |             "subject": "Read this now!", | 
					
						
							|  |  |  |             "version": "3", | 
					
						
							|  |  |  |             "threads": 5, | 
					
						
							| 
									
										
										
										
											2017-04-21 14:55:25 -07:00
										 |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "forum_name": "Cool Forum", | 
					
						
							|  |  |  |             "subject": "Please read this... please", | 
					
						
							|  |  |  |             "version": "2", | 
					
						
							|  |  |  |             "threads": 0, | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2017-04-21 14:55:25 -07:00
										 |  |  |     ] | 
					
						
							|  |  |  |     for data in item_data: | 
					
						
							|  |  |  |         item = Item(table, data) | 
					
						
							|  |  |  |         item.save(overwrite=True) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     results = table.query(forum_name__eq="Cool Forum", version__gt="2") | 
					
						
							| 
									
										
										
										
											2017-04-21 14:55:25 -07:00
										 |  |  |     results = list(results) | 
					
						
							|  |  |  |     results.should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     results = table.query(forum_name__eq="Cool Forum", version__lt="3") | 
					
						
							| 
									
										
										
										
											2017-04-21 14:55:25 -07:00
										 |  |  |     results = list(results) | 
					
						
							|  |  |  |     results.should.have.length_of(2) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2016-01-09 17:58:06 -08:00
										 |  |  | def test_reverse_query(): | 
					
						
							|  |  |  |     conn = boto.dynamodb2.layer1.DynamoDBConnection() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table = Table.create( | 
					
						
							|  |  |  |         "messages", schema=[HashKey("subject"), RangeKey("created_at", data_type="N")] | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-01-09 17:58:06 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for i in range(10): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         table.put_item({"subject": "Hi", "created_at": i}) | 
					
						
							| 
									
										
										
										
											2016-01-09 17:58:06 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     results = table.query_2(subject__eq="Hi", created_at__lt=6, limit=4, reverse=True) | 
					
						
							| 
									
										
										
										
											2016-01-09 17:58:06 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-09 18:29:21 -08:00
										 |  |  |     expected = [Decimal(5), Decimal(4), Decimal(3), Decimal(2)] | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     [r["created_at"] for r in results].should.equal(expected) | 
					
						
							| 
									
										
										
										
											2016-01-09 17:58:06 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2015-05-20 11:20:16 -04:00
										 |  |  | def test_lookup(): | 
					
						
							|  |  |  |     from decimal import Decimal | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     table = Table.create( | 
					
						
							|  |  |  |         "messages", | 
					
						
							|  |  |  |         schema=[HashKey("test_hash"), RangeKey("test_range")], | 
					
						
							|  |  |  |         throughput={"read": 10, "write": 10}, | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2015-05-20 11:20:16 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     hash_key = 3241526475 | 
					
						
							|  |  |  |     range_key = 1234567890987 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     data = {"test_hash": hash_key, "test_range": range_key} | 
					
						
							| 
									
										
										
										
											2015-05-20 11:20:16 -04:00
										 |  |  |     table.put_item(data=data) | 
					
						
							|  |  |  |     message = table.lookup(hash_key, range_key) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     message.get("test_hash").should.equal(Decimal(hash_key)) | 
					
						
							|  |  |  |     message.get("test_range").should.equal(Decimal(range_key)) | 
					
						
							| 
									
										
										
										
											2015-07-08 11:55:12 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2015-07-08 11:55:12 -04:00
										 |  |  | def test_failed_overwrite(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table = Table.create( | 
					
						
							|  |  |  |         "messages", | 
					
						
							|  |  |  |         schema=[HashKey("id"), RangeKey("range")], | 
					
						
							|  |  |  |         throughput={"read": 7, "write": 3}, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     data1 = {"id": "123", "range": "abc", "data": "678"} | 
					
						
							| 
									
										
										
										
											2015-07-08 11:55:12 -04:00
										 |  |  |     table.put_item(data=data1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     data2 = {"id": "123", "range": "abc", "data": "345"} | 
					
						
							| 
									
										
										
										
											2015-08-01 19:32:33 -04:00
										 |  |  |     table.put_item(data=data2, overwrite=True) | 
					
						
							| 
									
										
										
										
											2015-07-08 11:55:12 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     data3 = {"id": "123", "range": "abc", "data": "812"} | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     table.put_item.when.called_with(data=data3).should.throw( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ConditionalCheckFailedException | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2015-07-08 11:55:12 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     returned_item = table.lookup("123", "abc") | 
					
						
							| 
									
										
										
										
											2015-07-08 11:55:12 -04:00
										 |  |  |     dict(returned_item).should.equal(data2) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     data4 = {"id": "123", "range": "ghi", "data": 812} | 
					
						
							| 
									
										
										
										
											2015-07-08 11:55:12 -04:00
										 |  |  |     table.put_item(data=data4) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     returned_item = table.lookup("123", "ghi") | 
					
						
							| 
									
										
										
										
											2015-07-08 11:55:12 -04:00
										 |  |  |     dict(returned_item).should.equal(data4) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_dynamodb2_deprecated | 
					
						
							| 
									
										
										
										
											2015-07-08 11:55:12 -04:00
										 |  |  | def test_conflicting_writes(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table = Table.create("messages", schema=[HashKey("id"), RangeKey("range")]) | 
					
						
							| 
									
										
										
										
											2015-07-08 11:55:12 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     item_data = {"id": "123", "range": "abc", "data": "678"} | 
					
						
							| 
									
										
										
										
											2015-07-08 11:55:12 -04:00
										 |  |  |     item1 = Item(table, item_data) | 
					
						
							|  |  |  |     item2 = Item(table, item_data) | 
					
						
							|  |  |  |     item1.save() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     item1["data"] = "579" | 
					
						
							|  |  |  |     item2["data"] = "912" | 
					
						
							| 
									
										
										
										
											2015-07-08 11:55:12 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     item1.save() | 
					
						
							|  |  |  |     item2.save.when.called_with().should.throw(ConditionalCheckFailedException) | 
					
						
							| 
									
										
										
										
											2015-08-01 19:32:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-01 19:32:33 -04:00
										 |  |  | """
 | 
					
						
							|  |  |  | boto3 | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_dynamodb2 | 
					
						
							|  |  |  | def test_boto3_conditions(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     dynamodb = boto3.resource("dynamodb", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2015-08-01 19:32:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create the DynamoDB table. | 
					
						
							|  |  |  |     table = dynamodb.create_table( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         TableName="users", | 
					
						
							| 
									
										
										
										
											2015-08-01 19:32:33 -04:00
										 |  |  |         KeySchema=[ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             {"AttributeName": "forum_name", "KeyType": "HASH"}, | 
					
						
							|  |  |  |             {"AttributeName": "subject", "KeyType": "RANGE"}, | 
					
						
							| 
									
										
										
										
											2015-08-01 19:32:33 -04:00
										 |  |  |         ], | 
					
						
							|  |  |  |         AttributeDefinitions=[ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             {"AttributeName": "forum_name", "AttributeType": "S"}, | 
					
						
							|  |  |  |             {"AttributeName": "subject", "AttributeType": "S"}, | 
					
						
							| 
									
										
										
										
											2015-08-01 19:32:33 -04:00
										 |  |  |         ], | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ProvisionedThroughput={"ReadCapacityUnits": 5, "WriteCapacityUnits": 5}, | 
					
						
							| 
									
										
										
										
											2015-08-01 19:32:33 -04:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table = dynamodb.Table("users") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     table.put_item(Item={"forum_name": "the-key", "subject": "123"}) | 
					
						
							|  |  |  |     table.put_item(Item={"forum_name": "the-key", "subject": "456"}) | 
					
						
							|  |  |  |     table.put_item(Item={"forum_name": "the-key", "subject": "789"}) | 
					
						
							| 
									
										
										
										
											2015-08-01 19:32:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Test a query returning all items | 
					
						
							|  |  |  |     results = table.query( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         KeyConditionExpression=Key("forum_name").eq("the-key") & Key("subject").gt("1"), | 
					
						
							| 
									
										
										
										
											2015-08-01 19:32:33 -04:00
										 |  |  |         ScanIndexForward=True, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     expected = ["123", "456", "789"] | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     for index, item in enumerate(results["Items"]): | 
					
						
							| 
									
										
										
										
											2015-08-01 19:32:33 -04:00
										 |  |  |         item["subject"].should.equal(expected[index]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Return all items again, but in reverse | 
					
						
							|  |  |  |     results = table.query( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         KeyConditionExpression=Key("forum_name").eq("the-key") & Key("subject").gt("1"), | 
					
						
							| 
									
										
										
										
											2015-08-01 19:32:33 -04:00
										 |  |  |         ScanIndexForward=False, | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     for index, item in enumerate(reversed(results["Items"])): | 
					
						
							| 
									
										
										
										
											2015-08-01 19:32:33 -04:00
										 |  |  |         item["subject"].should.equal(expected[index]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Filter the subjects to only return some of the results | 
					
						
							|  |  |  |     results = table.query( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         KeyConditionExpression=Key("forum_name").eq("the-key") | 
					
						
							|  |  |  |         & Key("subject").gt("234"), | 
					
						
							| 
									
										
										
										
											2015-08-01 19:32:33 -04:00
										 |  |  |         ConsistentRead=True, | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     results["Count"].should.equal(2) | 
					
						
							| 
									
										
										
										
											2015-08-01 19:32:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Filter to return no results | 
					
						
							|  |  |  |     results = table.query( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         KeyConditionExpression=Key("forum_name").eq("the-key") | 
					
						
							|  |  |  |         & Key("subject").gt("9999") | 
					
						
							| 
									
										
										
										
											2015-08-01 19:32:33 -04:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     results["Count"].should.equal(0) | 
					
						
							| 
									
										
										
										
											2015-08-01 19:32:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     results = table.query( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         KeyConditionExpression=Key("forum_name").eq("the-key") | 
					
						
							|  |  |  |         & Key("subject").begins_with("12") | 
					
						
							| 
									
										
										
										
											2015-08-01 19:32:33 -04:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     results["Count"].should.equal(1) | 
					
						
							| 
									
										
										
										
											2015-08-01 19:32:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     results = table.query( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         KeyConditionExpression=Key("subject").begins_with("7") | 
					
						
							|  |  |  |         & Key("forum_name").eq("the-key") | 
					
						
							| 
									
										
										
										
											2015-08-01 19:32:33 -04:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     results["Count"].should.equal(1) | 
					
						
							| 
									
										
										
										
											2015-08-01 19:32:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     results = table.query( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         KeyConditionExpression=Key("forum_name").eq("the-key") | 
					
						
							|  |  |  |         & Key("subject").between("567", "890") | 
					
						
							| 
									
										
										
										
											2015-08-01 19:32:33 -04:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     results["Count"].should.equal(1) | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-04 12:24:26 +01:00
										 |  |  | @mock_dynamodb2 | 
					
						
							|  |  |  | def test_boto3_put_item_with_conditions(): | 
					
						
							|  |  |  |     import botocore | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     dynamodb = boto3.resource("dynamodb", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2016-11-04 12:24:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create the DynamoDB table. | 
					
						
							|  |  |  |     table = dynamodb.create_table( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         TableName="users", | 
					
						
							| 
									
										
										
										
											2016-11-04 12:24:26 +01:00
										 |  |  |         KeySchema=[ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             {"AttributeName": "forum_name", "KeyType": "HASH"}, | 
					
						
							|  |  |  |             {"AttributeName": "subject", "KeyType": "RANGE"}, | 
					
						
							| 
									
										
										
										
											2016-11-04 12:24:26 +01:00
										 |  |  |         ], | 
					
						
							|  |  |  |         AttributeDefinitions=[ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             {"AttributeName": "forum_name", "AttributeType": "S"}, | 
					
						
							|  |  |  |             {"AttributeName": "subject", "AttributeType": "S"}, | 
					
						
							| 
									
										
										
										
											2016-11-04 12:24:26 +01:00
										 |  |  |         ], | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ProvisionedThroughput={"ReadCapacityUnits": 5, "WriteCapacityUnits": 5}, | 
					
						
							| 
									
										
										
										
											2016-11-04 12:24:26 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table = dynamodb.Table("users") | 
					
						
							| 
									
										
										
										
											2016-11-04 12:24:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table.put_item(Item={"forum_name": "the-key", "subject": "123"}) | 
					
						
							| 
									
										
										
										
											2016-11-04 12:24:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     table.put_item( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         Item={"forum_name": "the-key-2", "subject": "1234"}, | 
					
						
							|  |  |  |         ConditionExpression="attribute_not_exists(forum_name) AND attribute_not_exists(subject)", | 
					
						
							| 
									
										
										
										
											2016-11-04 12:24:26 +01:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     table.put_item.when.called_with( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         Item={"forum_name": "the-key", "subject": "123"}, | 
					
						
							|  |  |  |         ConditionExpression="attribute_not_exists(forum_name) AND attribute_not_exists(subject)", | 
					
						
							| 
									
										
										
										
											2016-11-04 12:24:26 +01:00
										 |  |  |     ).should.throw(botocore.exceptions.ClientError) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-04 13:29:53 +01:00
										 |  |  |     table.put_item.when.called_with( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         Item={"forum_name": "bogus-key", "subject": "bogus", "test": "123"}, | 
					
						
							|  |  |  |         ConditionExpression="attribute_exists(forum_name) AND attribute_exists(subject)", | 
					
						
							| 
									
										
										
										
											2016-11-04 13:29:53 +01:00
										 |  |  |     ).should.throw(botocore.exceptions.ClientError) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-04 12:24:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-21 16:39:38 -07:00
										 |  |  | def _create_table_with_range_key(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     dynamodb = boto3.resource("dynamodb", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create the DynamoDB table. | 
					
						
							|  |  |  |     table = dynamodb.create_table( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         TableName="users", | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |         KeySchema=[ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             {"AttributeName": "forum_name", "KeyType": "HASH"}, | 
					
						
							|  |  |  |             {"AttributeName": "subject", "KeyType": "RANGE"}, | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |         ], | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         GlobalSecondaryIndexes=[ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 "IndexName": "TestGSI", | 
					
						
							|  |  |  |                 "KeySchema": [ | 
					
						
							|  |  |  |                     {"AttributeName": "username", "KeyType": "HASH"}, | 
					
						
							|  |  |  |                     {"AttributeName": "created", "KeyType": "RANGE"}, | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |                 "Projection": {"ProjectionType": "ALL"}, | 
					
						
							|  |  |  |                 "ProvisionedThroughput": { | 
					
						
							|  |  |  |                     "ReadCapacityUnits": 5, | 
					
						
							|  |  |  |                     "WriteCapacityUnits": 5, | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |                 }, | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ], | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |         AttributeDefinitions=[ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             {"AttributeName": "forum_name", "AttributeType": "S"}, | 
					
						
							|  |  |  |             {"AttributeName": "subject", "AttributeType": "S"}, | 
					
						
							|  |  |  |             {"AttributeName": "username", "AttributeType": "S"}, | 
					
						
							|  |  |  |             {"AttributeName": "created", "AttributeType": "N"}, | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |         ], | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ProvisionedThroughput={"ReadCapacityUnits": 5, "WriteCapacityUnits": 5}, | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     return dynamodb.Table("users") | 
					
						
							| 
									
										
										
										
											2015-12-21 16:39:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_dynamodb2 | 
					
						
							|  |  |  | def test_update_item_range_key_set(): | 
					
						
							|  |  |  |     table = _create_table_with_range_key() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table.put_item( | 
					
						
							|  |  |  |         Item={ | 
					
						
							|  |  |  |             "forum_name": "the-key", | 
					
						
							|  |  |  |             "subject": "123", | 
					
						
							|  |  |  |             "username": "johndoe", | 
					
						
							|  |  |  |             "created": Decimal("3"), | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     item_key = {"forum_name": "the-key", "subject": "123"} | 
					
						
							| 
									
										
										
										
											2015-12-21 16:39:38 -07:00
										 |  |  |     table.update_item( | 
					
						
							|  |  |  |         Key=item_key, | 
					
						
							|  |  |  |         AttributeUpdates={ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "username": {"Action": "PUT", "Value": "johndoe2"}, | 
					
						
							|  |  |  |             "created": {"Action": "PUT", "Value": Decimal("4")}, | 
					
						
							|  |  |  |             "mapfield": {"Action": "PUT", "Value": {"key": "value"}}, | 
					
						
							| 
									
										
										
										
											2015-12-21 16:39:38 -07:00
										 |  |  |         }, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     returned_item = dict( | 
					
						
							|  |  |  |         (k, str(v) if isinstance(v, Decimal) else v) | 
					
						
							|  |  |  |         for k, v in table.get_item(Key=item_key)["Item"].items() | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     dict(returned_item).should.equal( | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "username": "johndoe2", | 
					
						
							|  |  |  |             "forum_name": "the-key", | 
					
						
							|  |  |  |             "subject": "123", | 
					
						
							|  |  |  |             "created": "4", | 
					
						
							|  |  |  |             "mapfield": {"key": "value"}, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2015-12-21 16:39:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-14 15:46:05 -07:00
										 |  |  | @mock_dynamodb2 | 
					
						
							|  |  |  | def test_update_item_does_not_exist_is_created(): | 
					
						
							|  |  |  |     table = _create_table_with_range_key() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     item_key = {"forum_name": "the-key", "subject": "123"} | 
					
						
							| 
									
										
										
										
											2016-01-15 10:23:29 -07:00
										 |  |  |     result = table.update_item( | 
					
						
							| 
									
										
										
										
											2015-12-21 16:39:38 -07:00
										 |  |  |         Key=item_key, | 
					
						
							|  |  |  |         AttributeUpdates={ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "username": {"Action": "PUT", "Value": "johndoe2"}, | 
					
						
							|  |  |  |             "created": {"Action": "PUT", "Value": Decimal("4")}, | 
					
						
							|  |  |  |             "mapfield": {"Action": "PUT", "Value": {"key": "value"}}, | 
					
						
							| 
									
										
										
										
											2015-12-21 16:39:38 -07:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ReturnValues="ALL_OLD", | 
					
						
							| 
									
										
										
										
											2015-12-21 16:39:38 -07:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     assert not result.get("Attributes") | 
					
						
							| 
									
										
										
										
											2016-01-15 10:23:29 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     returned_item = dict( | 
					
						
							|  |  |  |         (k, str(v) if isinstance(v, Decimal) else v) | 
					
						
							|  |  |  |         for k, v in table.get_item(Key=item_key)["Item"].items() | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     dict(returned_item).should.equal( | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "username": "johndoe2", | 
					
						
							|  |  |  |             "forum_name": "the-key", | 
					
						
							|  |  |  |             "subject": "123", | 
					
						
							|  |  |  |             "created": "4", | 
					
						
							|  |  |  |             "mapfield": {"key": "value"}, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2015-12-21 16:39:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-14 15:44:28 -07:00
										 |  |  | @mock_dynamodb2 | 
					
						
							|  |  |  | def test_update_item_add_value(): | 
					
						
							|  |  |  |     table = _create_table_with_range_key() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table.put_item( | 
					
						
							|  |  |  |         Item={"forum_name": "the-key", "subject": "123", "numeric_field": Decimal("-1")} | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-01-14 15:44:28 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     item_key = {"forum_name": "the-key", "subject": "123"} | 
					
						
							| 
									
										
										
										
											2016-01-14 15:44:28 -07:00
										 |  |  |     table.update_item( | 
					
						
							|  |  |  |         Key=item_key, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         AttributeUpdates={"numeric_field": {"Action": "ADD", "Value": Decimal("2")}}, | 
					
						
							| 
									
										
										
										
											2016-01-14 15:44:28 -07:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     returned_item = dict( | 
					
						
							|  |  |  |         (k, str(v) if isinstance(v, Decimal) else v) | 
					
						
							|  |  |  |         for k, v in table.get_item(Key=item_key)["Item"].items() | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     dict(returned_item).should.equal( | 
					
						
							|  |  |  |         {"numeric_field": "1", "forum_name": "the-key", "subject": "123"} | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-01-14 15:44:28 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-11 12:06:24 -07:00
										 |  |  | @mock_dynamodb2 | 
					
						
							|  |  |  | def test_update_item_add_value_string_set(): | 
					
						
							|  |  |  |     table = _create_table_with_range_key() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table.put_item( | 
					
						
							|  |  |  |         Item={ | 
					
						
							|  |  |  |             "forum_name": "the-key", | 
					
						
							|  |  |  |             "subject": "123", | 
					
						
							|  |  |  |             "string_set": set(["str1", "str2"]), | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-09-11 12:06:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     item_key = {"forum_name": "the-key", "subject": "123"} | 
					
						
							| 
									
										
										
										
											2017-09-11 12:06:24 -07:00
										 |  |  |     table.update_item( | 
					
						
							|  |  |  |         Key=item_key, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         AttributeUpdates={"string_set": {"Action": "ADD", "Value": set(["str3"])}}, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     returned_item = dict( | 
					
						
							|  |  |  |         (k, str(v) if isinstance(v, Decimal) else v) | 
					
						
							|  |  |  |         for k, v in table.get_item(Key=item_key)["Item"].items() | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     dict(returned_item).should.equal( | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "string_set": set(["str1", "str2", "str3"]), | 
					
						
							|  |  |  |             "forum_name": "the-key", | 
					
						
							|  |  |  |             "subject": "123", | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-09-11 12:06:24 -07:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-22 02:47:02 -07:00
										 |  |  | @mock_dynamodb2 | 
					
						
							|  |  |  | def test_update_item_delete_value_string_set(): | 
					
						
							|  |  |  |     table = _create_table_with_range_key() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table.put_item( | 
					
						
							|  |  |  |         Item={ | 
					
						
							|  |  |  |             "forum_name": "the-key", | 
					
						
							|  |  |  |             "subject": "123", | 
					
						
							|  |  |  |             "string_set": set(["str1", "str2"]), | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-05-22 02:47:02 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     item_key = {"forum_name": "the-key", "subject": "123"} | 
					
						
							| 
									
										
										
										
											2019-05-22 02:47:02 -07:00
										 |  |  |     table.update_item( | 
					
						
							|  |  |  |         Key=item_key, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         AttributeUpdates={"string_set": {"Action": "DELETE", "Value": set(["str2"])}}, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     returned_item = dict( | 
					
						
							|  |  |  |         (k, str(v) if isinstance(v, Decimal) else v) | 
					
						
							|  |  |  |         for k, v in table.get_item(Key=item_key)["Item"].items() | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     dict(returned_item).should.equal( | 
					
						
							|  |  |  |         {"string_set": set(["str1"]), "forum_name": "the-key", "subject": "123"} | 
					
						
							| 
									
										
										
										
											2019-05-22 02:47:02 -07:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-11 12:06:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-14 15:44:28 -07:00
										 |  |  | @mock_dynamodb2 | 
					
						
							|  |  |  | def test_update_item_add_value_does_not_exist_is_created(): | 
					
						
							|  |  |  |     table = _create_table_with_range_key() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     item_key = {"forum_name": "the-key", "subject": "123"} | 
					
						
							| 
									
										
										
										
											2016-01-14 15:44:28 -07:00
										 |  |  |     table.update_item( | 
					
						
							|  |  |  |         Key=item_key, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         AttributeUpdates={"numeric_field": {"Action": "ADD", "Value": Decimal("2")}}, | 
					
						
							| 
									
										
										
										
											2016-01-14 15:44:28 -07:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     returned_item = dict( | 
					
						
							|  |  |  |         (k, str(v) if isinstance(v, Decimal) else v) | 
					
						
							|  |  |  |         for k, v in table.get_item(Key=item_key)["Item"].items() | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     dict(returned_item).should.equal( | 
					
						
							|  |  |  |         {"numeric_field": "2", "forum_name": "the-key", "subject": "123"} | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-01-14 15:44:28 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-11 23:04:14 +01:00
										 |  |  | @mock_dynamodb2 | 
					
						
							|  |  |  | def test_update_item_with_expression(): | 
					
						
							|  |  |  |     table = _create_table_with_range_key() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table.put_item(Item={"forum_name": "the-key", "subject": "123", "field": "1"}) | 
					
						
							| 
									
										
										
										
											2016-11-11 23:04:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     item_key = {"forum_name": "the-key", "subject": "123"} | 
					
						
							| 
									
										
										
										
											2016-11-11 23:04:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table.update_item(Key=item_key, UpdateExpression="SET field=2") | 
					
						
							|  |  |  |     dict(table.get_item(Key=item_key)["Item"]).should.equal( | 
					
						
							|  |  |  |         {"field": "2", "forum_name": "the-key", "subject": "123"} | 
					
						
							| 
									
										
										
										
											2016-11-11 23:04:14 +01:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table.update_item(Key=item_key, UpdateExpression="SET field  = 3") | 
					
						
							|  |  |  |     dict(table.get_item(Key=item_key)["Item"]).should.equal( | 
					
						
							|  |  |  |         {"field": "3", "forum_name": "the-key", "subject": "123"} | 
					
						
							| 
									
										
										
										
											2016-11-11 23:04:14 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-11 23:04:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-12 21:42:29 -07:00
										 |  |  | @mock_dynamodb2 | 
					
						
							|  |  |  | def test_update_item_add_with_expression(): | 
					
						
							|  |  |  |     table = _create_table_with_range_key() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     item_key = {"forum_name": "the-key", "subject": "123"} | 
					
						
							| 
									
										
										
										
											2017-09-12 21:42:29 -07:00
										 |  |  |     current_item = { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "forum_name": "the-key", | 
					
						
							|  |  |  |         "subject": "123", | 
					
						
							|  |  |  |         "str_set": {"item1", "item2", "item3"}, | 
					
						
							|  |  |  |         "num_set": {1, 2, 3}, | 
					
						
							|  |  |  |         "num_val": 6, | 
					
						
							| 
									
										
										
										
											2017-09-12 21:42:29 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Put an entry in the DB to play with | 
					
						
							|  |  |  |     table.put_item(Item=current_item) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Update item to add a string value to a string set | 
					
						
							|  |  |  |     table.update_item( | 
					
						
							|  |  |  |         Key=item_key, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         UpdateExpression="ADD str_set :v", | 
					
						
							|  |  |  |         ExpressionAttributeValues={":v": {"item4"}}, | 
					
						
							| 
									
										
										
										
											2017-09-12 21:42:29 -07:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     current_item["str_set"] = current_item["str_set"].union({"item4"}) | 
					
						
							|  |  |  |     dict(table.get_item(Key=item_key)["Item"]).should.equal(current_item) | 
					
						
							| 
									
										
										
										
											2017-09-12 21:42:29 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-27 12:36:42 +00:00
										 |  |  |     # Update item to add a string value to a non-existing set | 
					
						
							|  |  |  |     # Should just create the set in the background | 
					
						
							|  |  |  |     table.update_item( | 
					
						
							|  |  |  |         Key=item_key, | 
					
						
							|  |  |  |         UpdateExpression="ADD non_existing_str_set :v", | 
					
						
							|  |  |  |         ExpressionAttributeValues={":v": {"item4"}}, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     current_item["non_existing_str_set"] = {"item4"} | 
					
						
							|  |  |  |     dict(table.get_item(Key=item_key)["Item"]).should.equal(current_item) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-12 21:42:29 -07:00
										 |  |  |     # Update item to add a num value to a num set | 
					
						
							|  |  |  |     table.update_item( | 
					
						
							|  |  |  |         Key=item_key, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         UpdateExpression="ADD num_set :v", | 
					
						
							|  |  |  |         ExpressionAttributeValues={":v": {6}}, | 
					
						
							| 
									
										
										
										
											2017-09-12 21:42:29 -07:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     current_item["num_set"] = current_item["num_set"].union({6}) | 
					
						
							|  |  |  |     dict(table.get_item(Key=item_key)["Item"]).should.equal(current_item) | 
					
						
							| 
									
										
										
										
											2017-09-12 21:42:29 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Update item to add a value to a number value | 
					
						
							|  |  |  |     table.update_item( | 
					
						
							|  |  |  |         Key=item_key, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         UpdateExpression="ADD num_val :v", | 
					
						
							|  |  |  |         ExpressionAttributeValues={":v": 20}, | 
					
						
							| 
									
										
										
										
											2017-09-12 21:42:29 -07:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     current_item["num_val"] = current_item["num_val"] + 20 | 
					
						
							|  |  |  |     dict(table.get_item(Key=item_key)["Item"]).should.equal(current_item) | 
					
						
							| 
									
										
										
										
											2017-09-12 21:42:29 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Attempt to add a number value to a string set, should raise Client Error | 
					
						
							|  |  |  |     table.update_item.when.called_with( | 
					
						
							|  |  |  |         Key=item_key, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         UpdateExpression="ADD str_set :v", | 
					
						
							|  |  |  |         ExpressionAttributeValues={":v": 20}, | 
					
						
							| 
									
										
										
										
											2017-09-12 21:42:29 -07:00
										 |  |  |     ).should.have.raised(ClientError) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     dict(table.get_item(Key=item_key)["Item"]).should.equal(current_item) | 
					
						
							| 
									
										
										
										
											2017-09-12 21:42:29 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Attempt to add a number set to the string set, should raise a ClientError | 
					
						
							|  |  |  |     table.update_item.when.called_with( | 
					
						
							|  |  |  |         Key=item_key, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         UpdateExpression="ADD str_set :v", | 
					
						
							|  |  |  |         ExpressionAttributeValues={":v": {20}}, | 
					
						
							| 
									
										
										
										
											2017-09-12 21:42:29 -07:00
										 |  |  |     ).should.have.raised(ClientError) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     dict(table.get_item(Key=item_key)["Item"]).should.equal(current_item) | 
					
						
							| 
									
										
										
										
											2017-09-12 21:42:29 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-12 23:41:12 -07:00
										 |  |  |     # Attempt to update with a bad expression | 
					
						
							|  |  |  |     table.update_item.when.called_with( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         Key=item_key, UpdateExpression="ADD str_set bad_value" | 
					
						
							| 
									
										
										
										
											2017-09-12 23:41:12 -07:00
										 |  |  |     ).should.have.raised(ClientError) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Attempt to add a string value instead of a string set | 
					
						
							|  |  |  |     table.update_item.when.called_with( | 
					
						
							|  |  |  |         Key=item_key, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         UpdateExpression="ADD str_set :v", | 
					
						
							|  |  |  |         ExpressionAttributeValues={":v": "new_string"}, | 
					
						
							| 
									
										
										
										
											2017-09-12 23:41:12 -07:00
										 |  |  |     ).should.have.raised(ClientError) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-27 12:36:42 +00:00
										 |  |  | @mock_dynamodb2 | 
					
						
							| 
									
										
										
										
											2019-11-28 13:22:20 +00:00
										 |  |  | def test_update_item_add_with_nested_sets(): | 
					
						
							| 
									
										
										
										
											2019-11-27 12:36:42 +00:00
										 |  |  |     table = _create_table_with_range_key() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     item_key = {"forum_name": "the-key", "subject": "123"} | 
					
						
							|  |  |  |     current_item = { | 
					
						
							|  |  |  |         "forum_name": "the-key", | 
					
						
							|  |  |  |         "subject": "123", | 
					
						
							|  |  |  |         "nested": {"str_set": {"item1", "item2", "item3"}}, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Put an entry in the DB to play with | 
					
						
							|  |  |  |     table.put_item(Item=current_item) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Update item to add a string value to a nested string set | 
					
						
							|  |  |  |     table.update_item( | 
					
						
							|  |  |  |         Key=item_key, | 
					
						
							|  |  |  |         UpdateExpression="ADD nested.str_set :v", | 
					
						
							|  |  |  |         ExpressionAttributeValues={":v": {"item4"}}, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     current_item["nested"]["str_set"] = current_item["nested"]["str_set"].union( | 
					
						
							|  |  |  |         {"item4"} | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     dict(table.get_item(Key=item_key)["Item"]).should.equal(current_item) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Update item to add a string value to a non-existing set | 
					
						
							|  |  |  |     # Should just create the set in the background | 
					
						
							|  |  |  |     table.update_item( | 
					
						
							|  |  |  |         Key=item_key, | 
					
						
							|  |  |  |         UpdateExpression="ADD #ns.#ne :v", | 
					
						
							|  |  |  |         ExpressionAttributeNames={"#ns": "nested", "#ne": "non_existing_str_set"}, | 
					
						
							|  |  |  |         ExpressionAttributeValues={":v": {"new_item"}}, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     current_item["nested"]["non_existing_str_set"] = {"new_item"} | 
					
						
							|  |  |  |     dict(table.get_item(Key=item_key)["Item"]).should.equal(current_item) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-28 13:22:20 +00:00
										 |  |  | @mock_dynamodb2 | 
					
						
							|  |  |  | def test_update_item_delete_with_nested_sets(): | 
					
						
							|  |  |  |     table = _create_table_with_range_key() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     item_key = {"forum_name": "the-key", "subject": "123"} | 
					
						
							|  |  |  |     current_item = { | 
					
						
							|  |  |  |         "forum_name": "the-key", | 
					
						
							|  |  |  |         "subject": "123", | 
					
						
							|  |  |  |         "nested": {"str_set": {"item1", "item2", "item3"}}, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Put an entry in the DB to play with | 
					
						
							|  |  |  |     table.put_item(Item=current_item) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Update item to add a string value to a nested string set | 
					
						
							|  |  |  |     table.update_item( | 
					
						
							|  |  |  |         Key=item_key, | 
					
						
							|  |  |  |         UpdateExpression="DELETE nested.str_set :v", | 
					
						
							|  |  |  |         ExpressionAttributeValues={":v": {"item3"}}, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     current_item["nested"]["str_set"] = current_item["nested"]["str_set"].difference( | 
					
						
							|  |  |  |         {"item3"} | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     dict(table.get_item(Key=item_key)["Item"]).should.equal(current_item) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-12 21:42:29 -07:00
										 |  |  | @mock_dynamodb2 | 
					
						
							|  |  |  | def test_update_item_delete_with_expression(): | 
					
						
							|  |  |  |     table = _create_table_with_range_key() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     item_key = {"forum_name": "the-key", "subject": "123"} | 
					
						
							| 
									
										
										
										
											2017-09-12 21:42:29 -07:00
										 |  |  |     current_item = { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "forum_name": "the-key", | 
					
						
							|  |  |  |         "subject": "123", | 
					
						
							|  |  |  |         "str_set": {"item1", "item2", "item3"}, | 
					
						
							|  |  |  |         "num_set": {1, 2, 3}, | 
					
						
							|  |  |  |         "num_val": 6, | 
					
						
							| 
									
										
										
										
											2017-09-12 21:42:29 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Put an entry in the DB to play with | 
					
						
							|  |  |  |     table.put_item(Item=current_item) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Update item to delete a string value from a string set | 
					
						
							|  |  |  |     table.update_item( | 
					
						
							|  |  |  |         Key=item_key, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         UpdateExpression="DELETE str_set :v", | 
					
						
							|  |  |  |         ExpressionAttributeValues={":v": {"item2"}}, | 
					
						
							| 
									
										
										
										
											2017-09-12 21:42:29 -07:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     current_item["str_set"] = current_item["str_set"].difference({"item2"}) | 
					
						
							|  |  |  |     dict(table.get_item(Key=item_key)["Item"]).should.equal(current_item) | 
					
						
							| 
									
										
										
										
											2017-09-12 21:42:29 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Update item to delete  a num value from a num set | 
					
						
							|  |  |  |     table.update_item( | 
					
						
							|  |  |  |         Key=item_key, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         UpdateExpression="DELETE num_set :v", | 
					
						
							|  |  |  |         ExpressionAttributeValues={":v": {2}}, | 
					
						
							| 
									
										
										
										
											2017-09-12 21:42:29 -07:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     current_item["num_set"] = current_item["num_set"].difference({2}) | 
					
						
							|  |  |  |     dict(table.get_item(Key=item_key)["Item"]).should.equal(current_item) | 
					
						
							| 
									
										
										
										
											2017-09-12 21:42:29 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Try to delete on a number, this should fail | 
					
						
							|  |  |  |     table.update_item.when.called_with( | 
					
						
							|  |  |  |         Key=item_key, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         UpdateExpression="DELETE num_val :v", | 
					
						
							|  |  |  |         ExpressionAttributeValues={":v": 20}, | 
					
						
							| 
									
										
										
										
											2017-09-12 21:42:29 -07:00
										 |  |  |     ).should.have.raised(ClientError) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     dict(table.get_item(Key=item_key)["Item"]).should.equal(current_item) | 
					
						
							| 
									
										
										
										
											2017-09-12 21:42:29 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-12 23:41:12 -07:00
										 |  |  |     # Try to delete a string set from a number set | 
					
						
							|  |  |  |     table.update_item.when.called_with( | 
					
						
							|  |  |  |         Key=item_key, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         UpdateExpression="DELETE num_set :v", | 
					
						
							|  |  |  |         ExpressionAttributeValues={":v": {"del_str"}}, | 
					
						
							| 
									
										
										
										
											2017-09-12 23:41:12 -07:00
										 |  |  |     ).should.have.raised(ClientError) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     dict(table.get_item(Key=item_key)["Item"]).should.equal(current_item) | 
					
						
							| 
									
										
										
										
											2017-09-12 23:41:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Attempt to update with a bad expression | 
					
						
							|  |  |  |     table.update_item.when.called_with( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         Key=item_key, UpdateExpression="DELETE num_val badvalue" | 
					
						
							| 
									
										
										
										
											2017-09-12 23:41:12 -07:00
										 |  |  |     ).should.have.raised(ClientError) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-11 23:04:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-21 16:39:38 -07:00
										 |  |  | @mock_dynamodb2 | 
					
						
							|  |  |  | def test_boto3_query_gsi_range_comparison(): | 
					
						
							|  |  |  |     table = _create_table_with_range_key() | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table.put_item( | 
					
						
							|  |  |  |         Item={ | 
					
						
							|  |  |  |             "forum_name": "the-key", | 
					
						
							|  |  |  |             "subject": "123", | 
					
						
							|  |  |  |             "username": "johndoe", | 
					
						
							|  |  |  |             "created": 3, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     table.put_item( | 
					
						
							|  |  |  |         Item={ | 
					
						
							|  |  |  |             "forum_name": "the-key", | 
					
						
							|  |  |  |             "subject": "456", | 
					
						
							|  |  |  |             "username": "johndoe", | 
					
						
							|  |  |  |             "created": 1, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     table.put_item( | 
					
						
							|  |  |  |         Item={ | 
					
						
							|  |  |  |             "forum_name": "the-key", | 
					
						
							|  |  |  |             "subject": "789", | 
					
						
							|  |  |  |             "username": "johndoe", | 
					
						
							|  |  |  |             "created": 2, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     table.put_item( | 
					
						
							|  |  |  |         Item={ | 
					
						
							|  |  |  |             "forum_name": "the-key", | 
					
						
							|  |  |  |             "subject": "159", | 
					
						
							|  |  |  |             "username": "janedoe", | 
					
						
							|  |  |  |             "created": 2, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     table.put_item( | 
					
						
							|  |  |  |         Item={ | 
					
						
							|  |  |  |             "forum_name": "the-key", | 
					
						
							|  |  |  |             "subject": "601", | 
					
						
							|  |  |  |             "username": "janedoe", | 
					
						
							|  |  |  |             "created": 5, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Test a query returning all johndoe items | 
					
						
							|  |  |  |     results = table.query( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         KeyConditionExpression=Key("username").eq("johndoe") & Key("created").gt(0), | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |         ScanIndexForward=True, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         IndexName="TestGSI", | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |     ) | 
					
						
							|  |  |  |     expected = ["456", "789", "123"] | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     for index, item in enumerate(results["Items"]): | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |         item["subject"].should.equal(expected[index]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Return all johndoe items again, but in reverse | 
					
						
							|  |  |  |     results = table.query( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         KeyConditionExpression=Key("username").eq("johndoe") & Key("created").gt(0), | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |         ScanIndexForward=False, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         IndexName="TestGSI", | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     for index, item in enumerate(reversed(results["Items"])): | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |         item["subject"].should.equal(expected[index]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Filter the creation to only return some of the results | 
					
						
							|  |  |  |     # And reverse order of hash + range key | 
					
						
							|  |  |  |     results = table.query( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         KeyConditionExpression=Key("created").gt(1) & Key("username").eq("johndoe"), | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |         ConsistentRead=True, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         IndexName="TestGSI", | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     results["Count"].should.equal(2) | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Filter to return no results | 
					
						
							|  |  |  |     results = table.query( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         KeyConditionExpression=Key("username").eq("janedoe") & Key("created").gt(9), | 
					
						
							|  |  |  |         IndexName="TestGSI", | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     results["Count"].should.equal(0) | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     results = table.query( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         KeyConditionExpression=Key("username").eq("janedoe") & Key("created").eq(5), | 
					
						
							|  |  |  |         IndexName="TestGSI", | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     results["Count"].should.equal(1) | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Test range key sorting | 
					
						
							|  |  |  |     results = table.query( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         KeyConditionExpression=Key("username").eq("johndoe") & Key("created").gt(0), | 
					
						
							|  |  |  |         IndexName="TestGSI", | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     expected = [Decimal("1"), Decimal("2"), Decimal("3")] | 
					
						
							|  |  |  |     for index, item in enumerate(results["Items"]): | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |         item["created"].should.equal(expected[index]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_dynamodb2 | 
					
						
							| 
									
										
										
										
											2016-05-05 22:14:23 -04:00
										 |  |  | def test_boto3_update_table_throughput(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     dynamodb = boto3.resource("dynamodb", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create the DynamoDB table. | 
					
						
							|  |  |  |     table = dynamodb.create_table( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         TableName="users", | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |         KeySchema=[ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             {"AttributeName": "forum_name", "KeyType": "HASH"}, | 
					
						
							|  |  |  |             {"AttributeName": "subject", "KeyType": "RANGE"}, | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |         ], | 
					
						
							|  |  |  |         AttributeDefinitions=[ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             {"AttributeName": "forum_name", "AttributeType": "S"}, | 
					
						
							|  |  |  |             {"AttributeName": "subject", "AttributeType": "S"}, | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |         ], | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ProvisionedThroughput={"ReadCapacityUnits": 5, "WriteCapacityUnits": 6}, | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table = dynamodb.Table("users") | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table.provisioned_throughput["ReadCapacityUnits"].should.equal(5) | 
					
						
							|  |  |  |     table.provisioned_throughput["WriteCapacityUnits"].should.equal(6) | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table.update( | 
					
						
							|  |  |  |         ProvisionedThroughput={"ReadCapacityUnits": 10, "WriteCapacityUnits": 11} | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table = dynamodb.Table("users") | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table.provisioned_throughput["ReadCapacityUnits"].should.equal(10) | 
					
						
							|  |  |  |     table.provisioned_throughput["WriteCapacityUnits"].should.equal(11) | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_dynamodb2 | 
					
						
							| 
									
										
										
										
											2016-05-05 22:14:23 -04:00
										 |  |  | def test_boto3_update_table_gsi_throughput(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     dynamodb = boto3.resource("dynamodb", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create the DynamoDB table. | 
					
						
							|  |  |  |     table = dynamodb.create_table( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         TableName="users", | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |         KeySchema=[ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             {"AttributeName": "forum_name", "KeyType": "HASH"}, | 
					
						
							|  |  |  |             {"AttributeName": "subject", "KeyType": "RANGE"}, | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |         ], | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         GlobalSecondaryIndexes=[ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 "IndexName": "TestGSI", | 
					
						
							|  |  |  |                 "KeySchema": [ | 
					
						
							|  |  |  |                     {"AttributeName": "username", "KeyType": "HASH"}, | 
					
						
							|  |  |  |                     {"AttributeName": "created", "KeyType": "RANGE"}, | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |                 "Projection": {"ProjectionType": "ALL"}, | 
					
						
							|  |  |  |                 "ProvisionedThroughput": { | 
					
						
							|  |  |  |                     "ReadCapacityUnits": 3, | 
					
						
							|  |  |  |                     "WriteCapacityUnits": 4, | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |                 }, | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ], | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |         AttributeDefinitions=[ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             {"AttributeName": "forum_name", "AttributeType": "S"}, | 
					
						
							|  |  |  |             {"AttributeName": "subject", "AttributeType": "S"}, | 
					
						
							|  |  |  |             {"AttributeName": "username", "AttributeType": "S"}, | 
					
						
							|  |  |  |             {"AttributeName": "created", "AttributeType": "S"}, | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |         ], | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ProvisionedThroughput={"ReadCapacityUnits": 5, "WriteCapacityUnits": 6}, | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table = dynamodb.Table("users") | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     gsi_throughput = table.global_secondary_indexes[0]["ProvisionedThroughput"] | 
					
						
							|  |  |  |     gsi_throughput["ReadCapacityUnits"].should.equal(3) | 
					
						
							|  |  |  |     gsi_throughput["WriteCapacityUnits"].should.equal(4) | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table.provisioned_throughput["ReadCapacityUnits"].should.equal(5) | 
					
						
							|  |  |  |     table.provisioned_throughput["WriteCapacityUnits"].should.equal(6) | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table.update( | 
					
						
							|  |  |  |         GlobalSecondaryIndexUpdates=[ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 "Update": { | 
					
						
							|  |  |  |                     "IndexName": "TestGSI", | 
					
						
							|  |  |  |                     "ProvisionedThroughput": { | 
					
						
							|  |  |  |                         "ReadCapacityUnits": 10, | 
					
						
							|  |  |  |                         "WriteCapacityUnits": 11, | 
					
						
							|  |  |  |                     }, | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ] | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table = dynamodb.Table("users") | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Primary throughput has not changed | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table.provisioned_throughput["ReadCapacityUnits"].should.equal(5) | 
					
						
							|  |  |  |     table.provisioned_throughput["WriteCapacityUnits"].should.equal(6) | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     gsi_throughput = table.global_secondary_indexes[0]["ProvisionedThroughput"] | 
					
						
							|  |  |  |     gsi_throughput["ReadCapacityUnits"].should.equal(10) | 
					
						
							|  |  |  |     gsi_throughput["WriteCapacityUnits"].should.equal(11) | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_dynamodb2 | 
					
						
							|  |  |  | def test_update_table_gsi_create(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     dynamodb = boto3.resource("dynamodb", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create the DynamoDB table. | 
					
						
							|  |  |  |     table = dynamodb.create_table( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         TableName="users", | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |         KeySchema=[ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             {"AttributeName": "forum_name", "KeyType": "HASH"}, | 
					
						
							|  |  |  |             {"AttributeName": "subject", "KeyType": "RANGE"}, | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |         ], | 
					
						
							|  |  |  |         AttributeDefinitions=[ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             {"AttributeName": "forum_name", "AttributeType": "S"}, | 
					
						
							|  |  |  |             {"AttributeName": "subject", "AttributeType": "S"}, | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |         ], | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ProvisionedThroughput={"ReadCapacityUnits": 5, "WriteCapacityUnits": 6}, | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table = dynamodb.Table("users") | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     table.global_secondary_indexes.should.have.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table.update( | 
					
						
							|  |  |  |         GlobalSecondaryIndexUpdates=[ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 "Create": { | 
					
						
							|  |  |  |                     "IndexName": "TestGSI", | 
					
						
							|  |  |  |                     "KeySchema": [ | 
					
						
							|  |  |  |                         {"AttributeName": "username", "KeyType": "HASH"}, | 
					
						
							|  |  |  |                         {"AttributeName": "created", "KeyType": "RANGE"}, | 
					
						
							|  |  |  |                     ], | 
					
						
							|  |  |  |                     "Projection": {"ProjectionType": "ALL"}, | 
					
						
							|  |  |  |                     "ProvisionedThroughput": { | 
					
						
							|  |  |  |                         "ReadCapacityUnits": 3, | 
					
						
							|  |  |  |                         "WriteCapacityUnits": 4, | 
					
						
							|  |  |  |                     }, | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ] | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table = dynamodb.Table("users") | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |     table.global_secondary_indexes.should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     gsi_throughput = table.global_secondary_indexes[0]["ProvisionedThroughput"] | 
					
						
							|  |  |  |     assert gsi_throughput["ReadCapacityUnits"].should.equal(3) | 
					
						
							|  |  |  |     assert gsi_throughput["WriteCapacityUnits"].should.equal(4) | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Check update works | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table.update( | 
					
						
							|  |  |  |         GlobalSecondaryIndexUpdates=[ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 "Update": { | 
					
						
							|  |  |  |                     "IndexName": "TestGSI", | 
					
						
							|  |  |  |                     "ProvisionedThroughput": { | 
					
						
							|  |  |  |                         "ReadCapacityUnits": 10, | 
					
						
							|  |  |  |                         "WriteCapacityUnits": 11, | 
					
						
							|  |  |  |                     }, | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     table = dynamodb.Table("users") | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     gsi_throughput = table.global_secondary_indexes[0]["ProvisionedThroughput"] | 
					
						
							|  |  |  |     assert gsi_throughput["ReadCapacityUnits"].should.equal(10) | 
					
						
							|  |  |  |     assert gsi_throughput["WriteCapacityUnits"].should.equal(11) | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table.update(GlobalSecondaryIndexUpdates=[{"Delete": {"IndexName": "TestGSI"}}]) | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table = dynamodb.Table("users") | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |     table.global_secondary_indexes.should.have.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_dynamodb2 | 
					
						
							|  |  |  | def test_update_table_gsi_throughput(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     dynamodb = boto3.resource("dynamodb", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create the DynamoDB table. | 
					
						
							|  |  |  |     table = dynamodb.create_table( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         TableName="users", | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |         KeySchema=[ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             {"AttributeName": "forum_name", "KeyType": "HASH"}, | 
					
						
							|  |  |  |             {"AttributeName": "subject", "KeyType": "RANGE"}, | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |         ], | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         GlobalSecondaryIndexes=[ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 "IndexName": "TestGSI", | 
					
						
							|  |  |  |                 "KeySchema": [ | 
					
						
							|  |  |  |                     {"AttributeName": "username", "KeyType": "HASH"}, | 
					
						
							|  |  |  |                     {"AttributeName": "created", "KeyType": "RANGE"}, | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |                 "Projection": {"ProjectionType": "ALL"}, | 
					
						
							|  |  |  |                 "ProvisionedThroughput": { | 
					
						
							|  |  |  |                     "ReadCapacityUnits": 3, | 
					
						
							|  |  |  |                     "WriteCapacityUnits": 4, | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |                 }, | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ], | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |         AttributeDefinitions=[ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             {"AttributeName": "forum_name", "AttributeType": "S"}, | 
					
						
							|  |  |  |             {"AttributeName": "subject", "AttributeType": "S"}, | 
					
						
							|  |  |  |             {"AttributeName": "username", "AttributeType": "S"}, | 
					
						
							|  |  |  |             {"AttributeName": "created", "AttributeType": "S"}, | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |         ], | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ProvisionedThroughput={"ReadCapacityUnits": 5, "WriteCapacityUnits": 6}, | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table = dynamodb.Table("users") | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |     table.global_secondary_indexes.should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table.update(GlobalSecondaryIndexUpdates=[{"Delete": {"IndexName": "TestGSI"}}]) | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     table = dynamodb.Table("users") | 
					
						
							| 
									
										
										
										
											2015-12-14 17:07:12 -07:00
										 |  |  |     table.global_secondary_indexes.should.have.length_of(0) | 
					
						
							| 
									
										
										
										
											2016-03-01 11:30:35 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_dynamodb2 | 
					
						
							|  |  |  | def test_query_pagination(): | 
					
						
							|  |  |  |     table = _create_table_with_range_key() | 
					
						
							|  |  |  |     for i in range(10): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         table.put_item( | 
					
						
							|  |  |  |             Item={ | 
					
						
							|  |  |  |                 "forum_name": "the-key", | 
					
						
							|  |  |  |                 "subject": "{0}".format(i), | 
					
						
							|  |  |  |                 "username": "johndoe", | 
					
						
							|  |  |  |                 "created": Decimal("3"), | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2016-03-01 11:30:35 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     page1 = table.query(KeyConditionExpression=Key("forum_name").eq("the-key"), Limit=6) | 
					
						
							|  |  |  |     page1["Count"].should.equal(6) | 
					
						
							|  |  |  |     page1["Items"].should.have.length_of(6) | 
					
						
							|  |  |  |     page1.should.have.key("LastEvaluatedKey") | 
					
						
							| 
									
										
										
										
											2016-03-01 11:30:35 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     page2 = table.query( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         KeyConditionExpression=Key("forum_name").eq("the-key"), | 
					
						
							| 
									
										
										
										
											2016-03-01 11:30:35 -08:00
										 |  |  |         Limit=6, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ExclusiveStartKey=page1["LastEvaluatedKey"], | 
					
						
							| 
									
										
										
										
											2016-03-01 11:30:35 -08:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     page2["Count"].should.equal(4) | 
					
						
							|  |  |  |     page2["Items"].should.have.length_of(4) | 
					
						
							|  |  |  |     page2.should_not.have.key("LastEvaluatedKey") | 
					
						
							| 
									
										
										
										
											2016-03-01 11:30:35 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     results = page1["Items"] + page2["Items"] | 
					
						
							|  |  |  |     subjects = set([int(r["subject"]) for r in results]) | 
					
						
							| 
									
										
										
										
											2016-03-01 11:30:35 -08:00
										 |  |  |     subjects.should.equal(set(range(10))) | 
					
						
							| 
									
										
										
										
											2019-05-22 01:45:30 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_dynamodb2 | 
					
						
							|  |  |  | def test_scan_by_index(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     dynamodb = boto3.client("dynamodb", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2019-05-22 01:45:30 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |     dynamodb.create_table( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         TableName="test", | 
					
						
							| 
									
										
										
										
											2019-05-22 01:45:30 +09:00
										 |  |  |         KeySchema=[ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             {"AttributeName": "id", "KeyType": "HASH"}, | 
					
						
							|  |  |  |             {"AttributeName": "range_key", "KeyType": "RANGE"}, | 
					
						
							| 
									
										
										
										
											2019-05-22 01:45:30 +09:00
										 |  |  |         ], | 
					
						
							|  |  |  |         AttributeDefinitions=[ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             {"AttributeName": "id", "AttributeType": "S"}, | 
					
						
							|  |  |  |             {"AttributeName": "range_key", "AttributeType": "S"}, | 
					
						
							|  |  |  |             {"AttributeName": "gsi_col", "AttributeType": "S"}, | 
					
						
							|  |  |  |             {"AttributeName": "gsi_range_key", "AttributeType": "S"}, | 
					
						
							|  |  |  |             {"AttributeName": "lsi_range_key", "AttributeType": "S"}, | 
					
						
							| 
									
										
										
										
											2019-05-22 01:45:30 +09:00
										 |  |  |         ], | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ProvisionedThroughput={"ReadCapacityUnits": 1, "WriteCapacityUnits": 1}, | 
					
						
							| 
									
										
										
										
											2019-05-22 01:45:30 +09:00
										 |  |  |         GlobalSecondaryIndexes=[ | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 "IndexName": "test_gsi", | 
					
						
							|  |  |  |                 "KeySchema": [ | 
					
						
							|  |  |  |                     {"AttributeName": "gsi_col", "KeyType": "HASH"}, | 
					
						
							|  |  |  |                     {"AttributeName": "gsi_range_key", "KeyType": "RANGE"}, | 
					
						
							| 
									
										
										
										
											2019-05-22 01:45:30 +09:00
										 |  |  |                 ], | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 "Projection": {"ProjectionType": "ALL"}, | 
					
						
							|  |  |  |                 "ProvisionedThroughput": { | 
					
						
							|  |  |  |                     "ReadCapacityUnits": 1, | 
					
						
							|  |  |  |                     "WriteCapacityUnits": 1, | 
					
						
							| 
									
										
										
										
											2019-05-22 01:45:30 +09:00
										 |  |  |                 }, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-05-22 01:45:30 +09:00
										 |  |  |         ], | 
					
						
							|  |  |  |         LocalSecondaryIndexes=[ | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 "IndexName": "test_lsi", | 
					
						
							|  |  |  |                 "KeySchema": [ | 
					
						
							|  |  |  |                     {"AttributeName": "id", "KeyType": "HASH"}, | 
					
						
							|  |  |  |                     {"AttributeName": "lsi_range_key", "KeyType": "RANGE"}, | 
					
						
							| 
									
										
										
										
											2019-05-22 01:45:30 +09:00
										 |  |  |                 ], | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 "Projection": {"ProjectionType": "ALL"}, | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ], | 
					
						
							| 
									
										
										
										
											2019-05-22 01:45:30 +09:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     dynamodb.put_item( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         TableName="test", | 
					
						
							| 
									
										
										
										
											2019-05-22 01:45:30 +09:00
										 |  |  |         Item={ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "id": {"S": "1"}, | 
					
						
							|  |  |  |             "range_key": {"S": "1"}, | 
					
						
							|  |  |  |             "col1": {"S": "val1"}, | 
					
						
							|  |  |  |             "gsi_col": {"S": "1"}, | 
					
						
							|  |  |  |             "gsi_range_key": {"S": "1"}, | 
					
						
							|  |  |  |             "lsi_range_key": {"S": "1"}, | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2019-05-22 01:45:30 +09:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     dynamodb.put_item( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         TableName="test", | 
					
						
							| 
									
										
										
										
											2019-05-22 01:45:30 +09:00
										 |  |  |         Item={ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "id": {"S": "1"}, | 
					
						
							|  |  |  |             "range_key": {"S": "2"}, | 
					
						
							|  |  |  |             "col1": {"S": "val2"}, | 
					
						
							|  |  |  |             "gsi_col": {"S": "1"}, | 
					
						
							|  |  |  |             "gsi_range_key": {"S": "2"}, | 
					
						
							|  |  |  |             "lsi_range_key": {"S": "2"}, | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2019-05-22 01:45:30 +09:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     dynamodb.put_item( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         TableName="test", | 
					
						
							|  |  |  |         Item={"id": {"S": "3"}, "range_key": {"S": "1"}, "col1": {"S": "val3"}}, | 
					
						
							| 
									
										
										
										
											2019-05-22 01:45:30 +09:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     res = dynamodb.scan(TableName="test") | 
					
						
							|  |  |  |     assert res["Count"] == 3 | 
					
						
							|  |  |  |     assert len(res["Items"]) == 3 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res = dynamodb.scan(TableName="test", IndexName="test_gsi") | 
					
						
							|  |  |  |     assert res["Count"] == 2 | 
					
						
							|  |  |  |     assert len(res["Items"]) == 2 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res = dynamodb.scan(TableName="test", IndexName="test_gsi", Limit=1) | 
					
						
							|  |  |  |     assert res["Count"] == 1 | 
					
						
							|  |  |  |     assert len(res["Items"]) == 1 | 
					
						
							|  |  |  |     last_eval_key = res["LastEvaluatedKey"] | 
					
						
							|  |  |  |     assert last_eval_key["id"]["S"] == "1" | 
					
						
							|  |  |  |     assert last_eval_key["gsi_col"]["S"] == "1" | 
					
						
							|  |  |  |     assert last_eval_key["gsi_range_key"]["S"] == "1" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res = dynamodb.scan(TableName="test", IndexName="test_lsi") | 
					
						
							|  |  |  |     assert res["Count"] == 2 | 
					
						
							|  |  |  |     assert len(res["Items"]) == 2 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res = dynamodb.scan(TableName="test", IndexName="test_lsi", Limit=1) | 
					
						
							|  |  |  |     assert res["Count"] == 1 | 
					
						
							|  |  |  |     assert len(res["Items"]) == 1 | 
					
						
							|  |  |  |     last_eval_key = res["LastEvaluatedKey"] | 
					
						
							|  |  |  |     assert last_eval_key["id"]["S"] == "1" | 
					
						
							|  |  |  |     assert last_eval_key["range_key"]["S"] == "1" | 
					
						
							|  |  |  |     assert last_eval_key["lsi_range_key"]["S"] == "1" |