From 80c11d676cdf86a09ed6a9e6edf390a8c730a2b6 Mon Sep 17 00:00:00 2001 From: Xu Liu Date: Tue, 23 Apr 2019 11:53:00 -0400 Subject: [PATCH 1/2] Use cast_value when comparing DynamoType --- moto/dynamodb2/models.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/moto/dynamodb2/models.py b/moto/dynamodb2/models.py index 0f4594aa..10fe75c0 100644 --- a/moto/dynamodb2/models.py +++ b/moto/dynamodb2/models.py @@ -45,16 +45,16 @@ class DynamoType(object): ) def __lt__(self, other): - return self.value < other.value + return self.cast_value < other.cast_value def __le__(self, other): - return self.value <= other.value + return self.cast_value <= other.cast_value def __gt__(self, other): - return self.value > other.value + return self.cast_value > other.cast_value def __ge__(self, other): - return self.value >= other.value + return self.cast_value >= other.cast_value def __repr__(self): return "DynamoType: {0}".format(self.to_json()) From cf899eecff6a8b1471ef454bba9bcbdd8ed68fa0 Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Thu, 10 Oct 2019 09:33:11 +0100 Subject: [PATCH 2/2] #2384 - Add test case --- tests/test_dynamodb2/test_dynamodb.py | 29 +++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/test_dynamodb2/test_dynamodb.py b/tests/test_dynamodb2/test_dynamodb.py index c4cf05c5..ef5da005 100644 --- a/tests/test_dynamodb2/test_dynamodb.py +++ b/tests/test_dynamodb2/test_dynamodb.py @@ -2295,6 +2295,35 @@ def test_index_with_unknown_attributes_should_fail(): ex.exception.response['Error']['Message'].should.contain(expected_exception) +@mock_dynamodb2 +def test_sorted_query_with_numerical_sort_key(): + dynamodb = boto3.resource('dynamodb', region_name='us-east-1') + dynamodb.create_table(TableName="CarCollection", + KeySchema=[{ 'AttributeName': "CarModel", 'KeyType': 'HASH'}, + {'AttributeName': "CarPrice", 'KeyType': 'RANGE'}], + AttributeDefinitions=[{'AttributeName': "CarModel", 'AttributeType': "S"}, + {'AttributeName': "CarPrice", 'AttributeType': "N"}], + ProvisionedThroughput={'ReadCapacityUnits': 1, 'WriteCapacityUnits': 1}) + + def create_item(price): + return {"CarModel": "M", "CarPrice": price} + + table = dynamodb.Table('CarCollection') + items = list(map(create_item, [2, 1, 10, 3])) + for item in items: + table.put_item(Item=item) + + response = table.query(KeyConditionExpression=Key('CarModel').eq("M")) + + response_items = response['Items'] + assert len(items) == len(response_items) + assert all(isinstance(item["CarPrice"], Decimal) for item in response_items) + response_prices = [item["CarPrice"] for item in response_items] + expected_prices = [Decimal(item["CarPrice"]) for item in items] + expected_prices.sort() + assert expected_prices == response_prices, "result items are not sorted by numerical value" + + def _create_user_table(): client = boto3.client('dynamodb', region_name='us-east-1') client.create_table(