From fef748b1c14541eb8753ffb1dbc6ffd1a1a2be51 Mon Sep 17 00:00:00 2001 From: Paul Craciunoiu Date: Mon, 21 Dec 2015 16:39:38 -0700 Subject: [PATCH 1/5] Add test. --- .../test_dynamodb_table_with_range_key.py | 41 +++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/tests/test_dynamodb2/test_dynamodb_table_with_range_key.py b/tests/test_dynamodb2/test_dynamodb_table_with_range_key.py index 282599bad..661c6c977 100644 --- a/tests/test_dynamodb2/test_dynamodb_table_with_range_key.py +++ b/tests/test_dynamodb2/test_dynamodb_table_with_range_key.py @@ -735,9 +735,7 @@ def test_boto3_conditions(): results['Count'].should.equal(1) - -@mock_dynamodb2 -def test_boto3_query_gsi_range_comparison(): +def _create_table_with_range_key(): dynamodb = boto3.resource('dynamodb', region_name='us-east-1') # Create the DynamoDB table. @@ -788,7 +786,42 @@ def test_boto3_query_gsi_range_comparison(): 'WriteCapacityUnits': 5 } ) - table = dynamodb.Table('users') + return dynamodb.Table('users') + + +@mock_dynamodb2 +def test_update_item_range_key_set(): + table = _create_table_with_range_key() + table.put_item(Item={ + 'forum_name': 'the-key', + 'subject': '123', + 'username': 'johndoe', + 'created': Decimal('3'), + }) + + item_key = {'forum_name': 'the-key', 'subject': '123'} + table.update_item( + Key=item_key, + AttributeUpdates={ + 'username': { + 'Action': u'PUT', + 'Value': 'johndoe2' + }, + }, + ) + + returned_item = table.get_item(Key=item_key)['Item'] + dict(returned_item).should.equal({ + 'username': "johndoe2", + 'forum_name': 'the-key', + 'subject': '123', + 'created': Decimal('3'), + }) + + +@mock_dynamodb2 +def test_boto3_query_gsi_range_comparison(): + table = _create_table_with_range_key() table.put_item(Item={ 'forum_name': 'the-key', From f1099dd0063334502253b2cdf2f3ff138606205e Mon Sep 17 00:00:00 2001 From: Paul Craciunoiu Date: Mon, 21 Dec 2015 16:45:08 -0700 Subject: [PATCH 2/5] Support update_item with map and numeric types. --- moto/dynamodb2/models.py | 4 ++++ .../test_dynamodb_table_with_range_key.py | 11 ++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/moto/dynamodb2/models.py b/moto/dynamodb2/models.py index 99ace6da0..5aa870b48 100644 --- a/moto/dynamodb2/models.py +++ b/moto/dynamodb2/models.py @@ -129,6 +129,10 @@ class Item(object): # TODO deal with other types if isinstance(new_value, list) or isinstance(new_value, set): self.attrs[attribute_name] = DynamoType({"SS": new_value}) + elif isinstance(new_value, dict): + self.attrs[attribute_name] = DynamoType({"M": new_value}) + elif update_action['Value'].keys() == ['N']: + self.attrs[attribute_name] = DynamoType({"N": new_value}) else: self.attrs[attribute_name] = DynamoType({"S": new_value}) diff --git a/tests/test_dynamodb2/test_dynamodb_table_with_range_key.py b/tests/test_dynamodb2/test_dynamodb_table_with_range_key.py index 661c6c977..639fc0687 100644 --- a/tests/test_dynamodb2/test_dynamodb_table_with_range_key.py +++ b/tests/test_dynamodb2/test_dynamodb_table_with_range_key.py @@ -807,6 +807,14 @@ def test_update_item_range_key_set(): 'Action': u'PUT', 'Value': 'johndoe2' }, + 'created': { + 'Action': u'PUT', + 'Value': Decimal('4'), + }, + 'mapfield': { + 'Action': u'PUT', + 'Value': {'key': 'value'}, + } }, ) @@ -815,7 +823,8 @@ def test_update_item_range_key_set(): 'username': "johndoe2", 'forum_name': 'the-key', 'subject': '123', - 'created': Decimal('3'), + 'created': Decimal('4'), + 'mapfield': {'key': 'value'}, }) From f5406ad212953d07d70f8d56cb4ae62c43007697 Mon Sep 17 00:00:00 2001 From: Paul Craciunoiu Date: Mon, 4 Jan 2016 16:29:02 -0700 Subject: [PATCH 3/5] Handle delete updates. --- moto/dynamodb2/models.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/moto/dynamodb2/models.py b/moto/dynamodb2/models.py index 5aa870b48..94f726bc2 100644 --- a/moto/dynamodb2/models.py +++ b/moto/dynamodb2/models.py @@ -124,6 +124,9 @@ class Item(object): def update_with_attribute_updates(self, attribute_updates): for attribute_name, update_action in attribute_updates.items(): action = update_action['Action'] + if action == 'DELETE' and not 'Value' in update_action: + del self.attrs[attribute_name] + continue new_value = list(update_action['Value'].values())[0] if action == 'PUT': # TODO deal with other types @@ -133,6 +136,8 @@ class Item(object): self.attrs[attribute_name] = DynamoType({"M": new_value}) elif update_action['Value'].keys() == ['N']: self.attrs[attribute_name] = DynamoType({"N": new_value}) + elif update_action['Value'].keys() == ['NULL']: + del self.attrs[attribute_name] else: self.attrs[attribute_name] = DynamoType({"S": new_value}) From c9f43c885a42db97f975f02171a4617039a09180 Mon Sep 17 00:00:00 2001 From: Paul Craciunoiu Date: Thu, 7 Jan 2016 12:39:18 -0700 Subject: [PATCH 4/5] Support Select=COUNT when querying. --- moto/dynamodb2/responses.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/moto/dynamodb2/responses.py b/moto/dynamodb2/responses.py index 20c45560d..901ae8e92 100644 --- a/moto/dynamodb2/responses.py +++ b/moto/dynamodb2/responses.py @@ -330,9 +330,10 @@ class DynamoHandler(BaseResponse): result = { "Count": len(items), - "Items": [item.attrs for item in items], "ConsumedCapacityUnits": 1, } + if self.body.get('Select', '').upper() != 'COUNT': + result["Items"] = [item.attrs for item in items] # Implement this when we do pagination # if not last_page: From 7a6e85517ad5f4d78283659ffac47cd25fe4d4ed Mon Sep 17 00:00:00 2001 From: Paul Craciunoiu Date: Fri, 8 Jan 2016 17:46:55 -0700 Subject: [PATCH 5/5] Quick fix for test in python3.3 --- tests/test_dynamodb2/test_dynamodb_table_with_range_key.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_dynamodb2/test_dynamodb_table_with_range_key.py b/tests/test_dynamodb2/test_dynamodb_table_with_range_key.py index 639fc0687..9dddbadc4 100644 --- a/tests/test_dynamodb2/test_dynamodb_table_with_range_key.py +++ b/tests/test_dynamodb2/test_dynamodb_table_with_range_key.py @@ -818,12 +818,13 @@ def test_update_item_range_key_set(): }, ) - returned_item = table.get_item(Key=item_key)['Item'] + 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': Decimal('4'), + 'created': '4', 'mapfield': {'key': 'value'}, })