DynamoDB: key_condition_expression cannot contain a literal value (#6784)

This commit is contained in:
Bert Blommers 2023-09-07 20:45:04 +00:00 committed by GitHub
parent 870f0ad22d
commit 3545d38595
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 30 deletions

View File

@ -150,11 +150,11 @@ def parse_expression(
# hashkey = :id and sortkey = :sk
# ^
if current_stage == EXPRESSION_STAGES.KEY_VALUE:
key_values.append(
expression_attribute_values.get(
current_phrase, {"S": current_phrase}
if current_phrase not in expression_attribute_values:
raise MockValidationException(
"Invalid condition in KeyConditionExpression: Multiple attribute names used in one condition"
)
)
key_values.append(expression_attribute_values[current_phrase])
results.append((key_name, comparison, key_values))
break
if crnt_char == "(":

View File

@ -1094,3 +1094,22 @@ def test_query_with_empty_filter_expression():
assert (
err["Message"] == "Invalid FilterExpression: The expression can not be empty;"
)
@mock_dynamodb
def test_query_with_missing_expression_attribute():
ddb = boto3.resource("dynamodb", region_name="us-west-2")
ddb.create_table(TableName="test", BillingMode="PAY_PER_REQUEST", **table_schema)
client = boto3.client("dynamodb", region_name="us-west-2")
with pytest.raises(ClientError) as exc:
client.query(
TableName="test",
KeyConditionExpression="#part_key=some_value",
ExpressionAttributeNames={"#part_key": "partitionKey"},
)
err = exc.value.response["Error"]
assert err["Code"] == "ValidationException"
assert (
err["Message"]
== "Invalid condition in KeyConditionExpression: Multiple attribute names used in one condition"
)

View File

@ -31,21 +31,6 @@ class TestHashKey:
)
assert exc.value.message == "Query condition missed key schema element: job_id"
def test_unknown_hash_value(self):
# TODO: is this correct? I'd assume that this should throw an error instead
# Revisit after test in exceptions.py passes
kce = "job_id = :unknown"
eav = {":id": {"S": "asdasdasd"}}
desired_hash_key, comparison, range_values = parse_expression(
expression_attribute_values=eav,
key_condition_expression=kce,
schema=self.schema,
expression_attribute_names=dict(),
)
assert desired_hash_key == {"S": ":unknown"}
assert comparison is None
assert range_values == []
class TestHashAndRangeKey:
schema = [
@ -185,9 +170,8 @@ class TestHashAndRangeKey:
],
)
def test_brackets(self, expr):
eav = {":id": "pk", ":sk1": "19", ":sk2": "21"}
desired_hash_key, comparison, range_values = parse_expression(
expression_attribute_values=eav,
expression_attribute_values={":id": "pk", ":sk": "19"},
key_condition_expression=expr,
schema=self.schema,
expression_attribute_names=dict(),

View File

@ -1977,15 +1977,6 @@ def test_query_missing_expr_names():
assert resp["Count"] == 1
assert resp["Items"][0]["client"]["S"] == "test1"
resp = client.query(
TableName="test1",
KeyConditionExpression=":name=test2",
ExpressionAttributeNames={":name": "client"},
)
assert resp["Count"] == 1
assert resp["Items"][0]["client"]["S"] == "test2"
# https://github.com/getmoto/moto/issues/2328
@mock_dynamodb