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): def __repr__(self):
return "DynamoType: {0}".format(self.to_json()) 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): def to_json(self):
return {self.type: self.value} return {self.type: self.value}
@ -60,9 +67,9 @@ class DynamoType(object):
""" """
Compares this type against comparison filters 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) 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): class Item(object):

View File

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