Fix dynamodb2 comparisons. Closes #601.

This commit is contained in:
Steve Pulec 2016-05-05 23:39:10 -04:00
parent 3dd44245f7
commit 196e5a7d8d
2 changed files with 15 additions and 8 deletions

View File

@ -53,6 +53,13 @@ class DynamoType(object):
def __repr__(self):
return "DynamoType: {0}".format(self.to_json())
@property
def cast_value(self):
if self.type == 'N':
return int(self.value)
else:
return self.value
def to_json(self):
return {self.type: self.value}
@ -60,9 +67,9 @@ class DynamoType(object):
"""
Compares this type against comparison filters
"""
range_values = [obj.value for obj in range_objs]
range_values = [obj.cast_value for obj in range_objs]
comparison_func = get_comparison_func(range_comparison)
return comparison_func(self.value, *range_values)
return comparison_func(self.cast_value, *range_values)
class Item(object):

View File

@ -1238,7 +1238,7 @@ def test_boto3_query_gsi_range_comparison():
# Test a query returning all johndoe items
results = table.query(
KeyConditionExpression=Key('username').eq('johndoe') & Key("created").gt('0'),
KeyConditionExpression=Key('username').eq('johndoe') & Key("created").gt(0),
ScanIndexForward=True,
IndexName='TestGSI',
)
@ -1248,7 +1248,7 @@ def test_boto3_query_gsi_range_comparison():
# Return all johndoe items again, but in reverse
results = table.query(
KeyConditionExpression=Key('username').eq('johndoe') & Key("created").gt('0'),
KeyConditionExpression=Key('username').eq('johndoe') & Key("created").gt(0),
ScanIndexForward=False,
IndexName='TestGSI',
)
@ -1258,7 +1258,7 @@ def test_boto3_query_gsi_range_comparison():
# Filter the creation to only return some of the results
# And reverse order of hash + range key
results = table.query(
KeyConditionExpression=Key("created").gt('1') & Key('username').eq('johndoe'),
KeyConditionExpression=Key("created").gt(1) & Key('username').eq('johndoe'),
ConsistentRead=True,
IndexName='TestGSI',
)
@ -1266,20 +1266,20 @@ def test_boto3_query_gsi_range_comparison():
# Filter to return no results
results = table.query(
KeyConditionExpression=Key('username').eq('janedoe') & Key("created").gt('9'),
KeyConditionExpression=Key('username').eq('janedoe') & Key("created").gt(9),
IndexName='TestGSI',
)
results['Count'].should.equal(0)
results = table.query(
KeyConditionExpression=Key('username').eq('janedoe') & Key("created").eq('5'),
KeyConditionExpression=Key('username').eq('janedoe') & Key("created").eq(5),
IndexName='TestGSI',
)
results['Count'].should.equal(1)
# Test range key sorting
results = table.query(
KeyConditionExpression=Key('username').eq('johndoe') & Key("created").gt('0'),
KeyConditionExpression=Key('username').eq('johndoe') & Key("created").gt(0),
IndexName='TestGSI',
)
expected = [Decimal('1'), Decimal('2'), Decimal('3')]