From ad53d6a230fedca58889fa7ff4600a3de2c693d0 Mon Sep 17 00:00:00 2001 From: Nadav Har'El Date: Sat, 13 May 2023 13:00:00 +0300 Subject: [PATCH] test/alternator: test invalid key types for GSI This patch adds a test that types which are not allowed for GSI keys - basically any type except S(tring), B(ytes) or N(number), are rejected as expected - an error path that we didn't cover in existing tests. The new test passes - Alternator doesn't have a bug in this area, and as usual, also passes on DynamoDB. Signed-off-by: Nadav Har'El --- test/alternator/test_gsi.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/alternator/test_gsi.py b/test/alternator/test_gsi.py index d215dfa394..38cdb78045 100644 --- a/test/alternator/test_gsi.py +++ b/test/alternator/test_gsi.py @@ -1933,3 +1933,28 @@ def test_gsi_and_lsi_same_key(dynamodb): KeyConditions={'p': {'AttributeValueList': [p], 'ComparisonOperator': 'EQ'}, 'x': {'AttributeValueList': [x], 'ComparisonOperator': 'EQ'}}) assert_index_query(table, 'gsi', [item], KeyConditions={'x': {'AttributeValueList': [x], 'ComparisonOperator': 'EQ'}}) + +# Check that any type besides S(tring), B(ytes) or N(umber)s is *not* NOT +# allowed as the type of a GSI key attribute. We don't check here that these +# three types *are* allowed, because we already checked this in other tests +# (see test_gsi_6_*). +def test_gsi_invalid_key_types(dynamodb): + # The following are all the types that DynamoDB supports, as documented in + # https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.LowLevelAPI.html + # also the non-existent type "junk" yields the same error message. + for key_type in ['BOOL', 'NULL', 'M', 'L', 'SS', 'NS', 'BS', 'junk']: + # DynamDB's and Alternator's error messages are different, but both + # include the invalid type's name in single quotes. + with pytest.raises(ClientError, match=f"ValidationException.*'{key_type}'"): + with new_test_table(dynamodb, + KeySchema=[{ 'AttributeName': 'p', 'KeyType': 'HASH' }], + AttributeDefinitions=[ + { 'AttributeName': 'p', 'AttributeType': 'S' }, + { 'AttributeName': 'x', 'AttributeType': key_type }, + ], + GlobalSecondaryIndexes=[{ + 'IndexName': 'gsi', + 'KeySchema': [{ 'AttributeName': 'x', 'KeyType': 'HASH' }], + 'Projection': { 'ProjectionType': 'ALL' } + }]) as table: + pass