#2627 - Change comparison to differentiate between 0 and None
This commit is contained in:
parent
8c920cce10
commit
6f02782624
@ -977,7 +977,7 @@ class OpLessThan(Op):
|
|||||||
lhs = self.lhs.expr(item)
|
lhs = self.lhs.expr(item)
|
||||||
rhs = self.rhs.expr(item)
|
rhs = self.rhs.expr(item)
|
||||||
# In python3 None is not a valid comparator when using < or > so must be handled specially
|
# In python3 None is not a valid comparator when using < or > so must be handled specially
|
||||||
if lhs and rhs:
|
if lhs is not None and rhs is not None:
|
||||||
return lhs < rhs
|
return lhs < rhs
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
@ -990,7 +990,7 @@ class OpGreaterThan(Op):
|
|||||||
lhs = self.lhs.expr(item)
|
lhs = self.lhs.expr(item)
|
||||||
rhs = self.rhs.expr(item)
|
rhs = self.rhs.expr(item)
|
||||||
# In python3 None is not a valid comparator when using < or > so must be handled specially
|
# In python3 None is not a valid comparator when using < or > so must be handled specially
|
||||||
if lhs and rhs:
|
if lhs is not None and rhs is not None:
|
||||||
return lhs > rhs
|
return lhs > rhs
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
@ -1021,7 +1021,7 @@ class OpLessThanOrEqual(Op):
|
|||||||
lhs = self.lhs.expr(item)
|
lhs = self.lhs.expr(item)
|
||||||
rhs = self.rhs.expr(item)
|
rhs = self.rhs.expr(item)
|
||||||
# In python3 None is not a valid comparator when using < or > so must be handled specially
|
# In python3 None is not a valid comparator when using < or > so must be handled specially
|
||||||
if lhs and rhs:
|
if lhs is not None and rhs is not None:
|
||||||
return lhs <= rhs
|
return lhs <= rhs
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
@ -1034,7 +1034,7 @@ class OpGreaterThanOrEqual(Op):
|
|||||||
lhs = self.lhs.expr(item)
|
lhs = self.lhs.expr(item)
|
||||||
rhs = self.rhs.expr(item)
|
rhs = self.rhs.expr(item)
|
||||||
# In python3 None is not a valid comparator when using < or > so must be handled specially
|
# In python3 None is not a valid comparator when using < or > so must be handled specially
|
||||||
if lhs and rhs:
|
if lhs is not None and rhs is not None:
|
||||||
return lhs >= rhs
|
return lhs >= rhs
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
@ -2531,6 +2531,48 @@ def test_condition_expressions():
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@mock_dynamodb2
|
||||||
|
def test_condition_expression_numerical_attribute():
|
||||||
|
dynamodb = boto3.resource("dynamodb")
|
||||||
|
dynamodb.create_table(
|
||||||
|
TableName="my-table",
|
||||||
|
KeySchema=[{"AttributeName": "partitionKey", "KeyType": "HASH"}],
|
||||||
|
AttributeDefinitions=[{"AttributeName": "partitionKey", "AttributeType": "S"}],
|
||||||
|
)
|
||||||
|
table = dynamodb.Table("my-table")
|
||||||
|
table.put_item(Item={"partitionKey": "pk-pos", "myAttr": 5})
|
||||||
|
table.put_item(Item={"partitionKey": "pk-neg", "myAttr": -5})
|
||||||
|
|
||||||
|
# try to update the item we put in the table using numerical condition expression
|
||||||
|
# Specifically, verify that we can compare with a zero-value
|
||||||
|
# First verify that > and >= work on positive numbers
|
||||||
|
update_numerical_con_expr(
|
||||||
|
key="pk-pos", con_expr="myAttr > :zero", res="6", table=table
|
||||||
|
)
|
||||||
|
update_numerical_con_expr(
|
||||||
|
key="pk-pos", con_expr="myAttr >= :zero", res="7", table=table
|
||||||
|
)
|
||||||
|
# Second verify that < and <= work on negative numbers
|
||||||
|
update_numerical_con_expr(
|
||||||
|
key="pk-neg", con_expr="myAttr < :zero", res="-4", table=table
|
||||||
|
)
|
||||||
|
update_numerical_con_expr(
|
||||||
|
key="pk-neg", con_expr="myAttr <= :zero", res="-3", table=table
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def update_numerical_con_expr(key, con_expr, res, table):
|
||||||
|
table.update_item(
|
||||||
|
Key={"partitionKey": key},
|
||||||
|
UpdateExpression="ADD myAttr :one",
|
||||||
|
ExpressionAttributeValues={":zero": 0, ":one": 1},
|
||||||
|
ConditionExpression=con_expr,
|
||||||
|
)
|
||||||
|
table.get_item(Key={"partitionKey": key})["Item"]["myAttr"].should.equal(
|
||||||
|
Decimal(res)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@mock_dynamodb2
|
@mock_dynamodb2
|
||||||
def test_condition_expression__attr_doesnt_exist():
|
def test_condition_expression__attr_doesnt_exist():
|
||||||
client = boto3.client("dynamodb", region_name="us-east-1")
|
client = boto3.client("dynamodb", region_name="us-east-1")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user