def test_list_tags_from_creation(test_table_tags): got = test_table_tags.meta.client.describe_table( TableName=test_table_tags.name)['Table'] arn = got['TableArn'] got = test_table_tags.meta.client.list_tags_of_resource(ResourceArn=arn) assert multiset(got['Tags']) == multiset(PREDEFINED_TAGS)
def test_query_basic_restrictions(dynamodb, filled_test_table): test_table, items = filled_test_table paginator = dynamodb.meta.client.get_paginator('query') # EQ got_items = [] for page in paginator.paginate(TableName=test_table.name, KeyConditions={ 'p': { 'AttributeValueList': ['long'], 'ComparisonOperator': 'EQ' } }): got_items += page['Items'] print(got_items) assert multiset([item for item in items if item['p'] == 'long']) == multiset(got_items) # LT got_items = [] for page in paginator.paginate(TableName=test_table.name, KeyConditions={ 'p': { 'AttributeValueList': ['long'], 'ComparisonOperator': 'EQ' }, 'c': { 'AttributeValueList': ['12'], 'ComparisonOperator': 'LT' } }): got_items += page['Items'] print(got_items) assert multiset([ item for item in items if item['p'] == 'long' and item['c'] < '12' ]) == multiset(got_items) # LE got_items = [] for page in paginator.paginate(TableName=test_table.name, KeyConditions={ 'p': { 'AttributeValueList': ['long'], 'ComparisonOperator': 'EQ' }, 'c': { 'AttributeValueList': ['14'], 'ComparisonOperator': 'LE' } }): got_items += page['Items'] print(got_items) assert multiset([ item for item in items if item['p'] == 'long' and item['c'] <= '14' ]) == multiset(got_items) # GT got_items = [] for page in paginator.paginate(TableName=test_table.name, KeyConditions={ 'p': { 'AttributeValueList': ['long'], 'ComparisonOperator': 'EQ' }, 'c': { 'AttributeValueList': ['15'], 'ComparisonOperator': 'GT' } }): got_items += page['Items'] print(got_items) assert multiset([ item for item in items if item['p'] == 'long' and item['c'] > '15' ]) == multiset(got_items) # GE got_items = [] for page in paginator.paginate(TableName=test_table.name, KeyConditions={ 'p': { 'AttributeValueList': ['long'], 'ComparisonOperator': 'EQ' }, 'c': { 'AttributeValueList': ['14'], 'ComparisonOperator': 'GE' } }): got_items += page['Items'] print(got_items) assert multiset([ item for item in items if item['p'] == 'long' and item['c'] >= '14' ]) == multiset(got_items) # BETWEEN got_items = [] for page in paginator.paginate(TableName=test_table.name, KeyConditions={ 'p': { 'AttributeValueList': ['long'], 'ComparisonOperator': 'EQ' }, 'c': { 'AttributeValueList': ['155', '164'], 'ComparisonOperator': 'BETWEEN' } }): got_items += page['Items'] print(got_items) assert multiset([ item for item in items if item['p'] == 'long' and item['c'] >= '155' and item['c'] <= '164' ]) == multiset(got_items) # BEGINS_WITH got_items = [] for page in paginator.paginate(TableName=test_table.name, KeyConditions={ 'p': { 'AttributeValueList': ['long'], 'ComparisonOperator': 'EQ' }, 'c': { 'AttributeValueList': ['11'], 'ComparisonOperator': 'BEGINS_WITH' } }): print([ item for item in items if item['p'] == 'long' and item['c'].startswith('11') ]) got_items += page['Items'] print(got_items) assert multiset([ item for item in items if item['p'] == 'long' and item['c'].startswith('11') ]) == multiset(got_items)
def test_query_which_key(test_table): p = random_string() c = random_string() p2 = random_string() c2 = random_string() item1 = {'p': p, 'c': c} item2 = {'p': p, 'c': c2} item3 = {'p': p2, 'c': c} for i in [item1, item2, item3]: test_table.put_item(Item=i) # Query by hash key only: got_items = full_query(test_table, KeyConditions={ 'p': { 'AttributeValueList': [p], 'ComparisonOperator': 'EQ' } }) expected_items = [item1, item2] assert multiset(expected_items) == multiset(got_items) # Query by hash key *and* sort key (this is basically a GetItem): got_items = full_query(test_table, KeyConditions={ 'p': { 'AttributeValueList': [p], 'ComparisonOperator': 'EQ' }, 'c': { 'AttributeValueList': [c], 'ComparisonOperator': 'EQ' } }) expected_items = [item1] assert multiset(expected_items) == multiset(got_items) # Query by sort key alone is not allowed. DynamoDB reports: # "Query condition missed key schema element: p". with pytest.raises(ClientError, match='ValidationException'): full_query(test_table, KeyConditions={ 'c': { 'AttributeValueList': [c], 'ComparisonOperator': 'EQ' } }) # Query by a non-key isn't allowed, for the same reason - that the # actual hash key (p) is missing in the query: with pytest.raises(ClientError, match='ValidationException'): full_query(test_table, KeyConditions={ 'z': { 'AttributeValueList': [c], 'ComparisonOperator': 'EQ' } }) # If we try both p and a non-key we get a complaint that the sort # key is missing: "Query condition missed key schema element: c" with pytest.raises(ClientError, match='ValidationException'): full_query(test_table, KeyConditions={ 'p': { 'AttributeValueList': [p], 'ComparisonOperator': 'EQ' }, 'z': { 'AttributeValueList': [c], 'ComparisonOperator': 'EQ' } }) # If we try p, c and another key, we get an error that # "Conditions can be of length 1 or 2 only". with pytest.raises(ClientError, match='ValidationException'): full_query(test_table, KeyConditions={ 'p': { 'AttributeValueList': [p], 'ComparisonOperator': 'EQ' }, 'c': { 'AttributeValueList': [c], 'ComparisonOperator': 'EQ' }, 'z': { 'AttributeValueList': [c], 'ComparisonOperator': 'EQ' } })
def test_filter_expression_scan_partition_key(filled_test_table): table, items = filled_test_table got_items = full_scan(table, FilterExpression='p=:a', ExpressionAttributeValues={':a': '3'}) expected_items = [item for item in items if item['p'] == '3'] assert multiset(expected_items) == multiset(got_items)
def test_scan_with_key_equality_filtering(dynamodb, filled_test_table): table, items = filled_test_table scan_filter_p = { "p": { "AttributeValueList": ["7"], "ComparisonOperator": "EQ" } } scan_filter_c = { "c": { "AttributeValueList": ["9"], "ComparisonOperator": "EQ" } } scan_filter_p_and_attribute = { "p": { "AttributeValueList": ["7"], "ComparisonOperator": "EQ" }, "attribute": { "AttributeValueList": ["x" * 7], "ComparisonOperator": "EQ" } } scan_filter_c_and_another = { "c": { "AttributeValueList": ["9"], "ComparisonOperator": "EQ" }, "another": { "AttributeValueList": ["y" * 16], "ComparisonOperator": "EQ" } } # Filtering on the hash key got_items = full_scan(table, ScanFilter=scan_filter_p) expected_items = [ item for item in items if "p" in item.keys() and item["p"] == "7" ] assert multiset(expected_items) == multiset(got_items) # Filtering on the sort key got_items = full_scan(table, ScanFilter=scan_filter_c) expected_items = [ item for item in items if "c" in item.keys() and item["c"] == "9" ] assert multiset(expected_items) == multiset(got_items) # Filtering on the hash key and an attribute got_items = full_scan(table, ScanFilter=scan_filter_p_and_attribute) expected_items = [ item for item in items if "p" in item.keys() and "another" in item.keys() and item["p"] == "7" and item["another"] == "y" * 16 ] assert multiset(expected_items) == multiset(got_items) # Filtering on the sort key and an attribute got_items = full_scan(table, ScanFilter=scan_filter_c_and_another) expected_items = [ item for item in items if "c" in item.keys() and "another" in item.keys() and item["c"] == "9" and item["another"] == "y" * 16 ] assert multiset(expected_items) == multiset(got_items)
def assert_index_scan(table, index_name, expected_items, **kwargs): assert multiset(expected_items) == multiset( full_scan(table, IndexName=index_name, **kwargs))
def assert_index_query(table, index_name, expected_items, **kwargs): assert multiset(expected_items) == multiset( full_query(table, IndexName=index_name, ConsistentRead=True, **kwargs))