Merge pull request #2469 from bblommers/bugfix/2384

DynamoDB - Fixes query sorting on numerical values
This commit is contained in:
Mike Grima 2019-10-12 15:37:56 -07:00 committed by GitHub
commit d0a6c4380b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 4 deletions

View File

@ -53,16 +53,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())

View File

@ -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"
# https://github.com/spulec/moto/issues/1874
@mock_dynamodb2
def test_item_size_is_under_400KB():