Permalink
Browse files

Merge branch 'master' of https://github.com/milancermak/boto into mil…

…ancermak-master
  • Loading branch information...
2 parents aac9fba + fd40444 commit 423c9fc7ff0882280858e3d419353a6aa28a05ab @garnaat garnaat committed Jan 25, 2012
Showing with 24 additions and 3 deletions.
  1. +12 −2 boto/dynamodb/layer2.py
  2. +12 −1 tests/dynamodb/test_layer2.py
View
14 boto/dynamodb/layer2.py
@@ -32,7 +32,7 @@
"""
def is_num(n):
- return (isinstance(n, (int, long, float)))
+ return isinstance(n, (int, long, float, bool))
def is_str(n):
return isinstance(n, basestring)
@@ -145,9 +145,19 @@ def dynamize_value(self, val):
needs to be sent to Amazon DynamoDB. If the type of the value
is not supported, raise a TypeError
"""
+ def _str(val):
+ """
+ DynamoDB stores booleans as numbers. True is 1, False is 0.
+ This function converts Python booleans into DynamoDB friendly
+ representation.
+ """
+ if isinstance(val, bool):
+ return str(int(val))
+ return str(val)
+
dynamodb_type = self.get_dynamodb_type(val)
if dynamodb_type == 'N':
- val = {dynamodb_type : str(val)}
+ val = {dynamodb_type : _str(val)}
elif dynamodb_type == 'S':
val = {dynamodb_type : val}
elif dynamodb_type == 'NS':
View
13 tests/dynamodb/test_layer2.py
@@ -81,11 +81,16 @@ def test_layer2_basic(self):
'Views': 0,
'Replies': 0,
'Answered': 0,
+ 'Public': True,
'Tags': set(['index', 'primarykey', 'table']),
'LastPostDateTime': '12/9/2011 11:36:03 PM'}
item1 = table.new_item(item1_key, item1_range, item1_attrs)
- item1.put()
+ # make sure the put() succeeds
+ try:
+ item1.put()
+ except c.layer1.ResponseError, e:
+ raise Exception("Item put failed: %s" % e)
# Try to get an item that does not exist.
self.assertRaises(DynamoDBKeyNotFoundError,
@@ -187,6 +192,10 @@ def test_layer2_basic(self):
item3['IntAttr'] = integer_value
item3['FloatAttr'] = float_value
+ # Test booleans
+ item3['TrueBoolean'] = True
+ item3['FalseBoolean'] = False
+
# Test some set values
integer_set = set([1,2,3,4,5])
float_set = set([1.1, 2.2, 3.3, 4.4, 5.5])
@@ -202,6 +211,8 @@ def test_layer2_basic(self):
item4 = table.get_item(item3_key, item3_range, consistent_read=True)
assert item4['IntAttr'] == integer_value
assert item4['FloatAttr'] == float_value
+ assert item4['TrueBoolean'] == True
+ assert item4['FalseBoolean'] == False
# The values will not necessarily be in the same order as when
# we wrote them to the DB.
for i in item4['IntSetAttr']:

0 comments on commit 423c9fc

Please sign in to comment.