def test_query_filter_paging(test_table_sn_with_data): table, p, items = test_table_sn_with_data # Filter on the "bool" attribute so the filter will match roughly half of # the items. (prefilter_count, postfilter_count, pages, got_items) = full_query_and_counts(table, KeyConditions={ 'p': { 'AttributeValueList': [p], 'ComparisonOperator': 'EQ' } }, QueryFilter={ 'bool': { 'AttributeValueList': [True], 'ComparisonOperator': 'EQ' } }, Limit=1) expected_items = [item for item in items if item['bool'] == True] assert (got_items == expected_items) # We expect the number of pages to be len(items)+1. The "+1" is because # the 20th page found one last item to consider (it may or may not have # passed the filter), but doesn't know it is really the last item - it # takes one more query to discover there are no more. assert (pages == len(items) + 1)
def test_query_filter_paging(test_table_sn_with_data): table, p, items = test_table_sn_with_data # Filter on the "bool" attribute so the filter will match roughly half of # the items. (prefilter_count, postfilter_count, pages, got_items) = full_query_and_counts(table, KeyConditions={ 'p': { 'AttributeValueList': [p], 'ComparisonOperator': 'EQ' } }, QueryFilter={ 'bool': { 'AttributeValueList': [True], 'ComparisonOperator': 'EQ' } }, Limit=1) expected_items = [item for item in items if item['bool'] == True] assert (got_items == expected_items) # The total number of pages may be len(items) or len(items)+1, depending # on the implementation: The "+1" can happen if the 20th page found one # last item to consider (it may or may not have passed the filter), but # doesn't know it is really the last item - it may take one more query to # discover there are no more. Currently, Alternator returns len(items)+1 # while DynamoDB returns len(items), but neither is more correct than the # other - nor should any user case about this difference, as empty pages # are a documented possibility. assert (pages == len(items) or pages == len(items) + 1)
def test_query_filter_and_select_count(test_table): p = random_string() test_table.put_item(Item={'p': p, 'c': 'hi', 'x': 'dog', 'y': 'cat'}) test_table.put_item(Item={'p': p, 'c': 'yo', 'x': 'mouse', 'y': 'horse'}) (prefilter_count, postfilter_count, pages, got_items) = full_query_and_counts(test_table, KeyConditions={ 'p': { 'AttributeValueList': [p], 'ComparisonOperator': 'EQ' } }, QueryFilter={ 'x': { 'AttributeValueList': ['mouse'], 'ComparisonOperator': 'EQ' } }, Select='COUNT') # Exactly one item matches the filter on x. But because of Select=COUNT, # we shouldn't get an item back - just the count. assert postfilter_count == 1 assert prefilter_count == 2 assert got_items == []
def test_filter_expression_and_select_count(test_table): p = random_string() test_table.put_item(Item={'p': p, 'c': 'hi', 'x': 'dog', 'y': 'cat'}) test_table.put_item(Item={'p': p, 'c': 'yo', 'x': 'mouse', 'y': 'horse'}) (prefilter_count, postfilter_count, pages, got_items) = full_query_and_counts(test_table, KeyConditionExpression='p=:p', FilterExpression='x=:x', Select='COUNT', ExpressionAttributeValues={':p': p, ':x': 'mouse'}) # Exactly one item matches the filter on x. But because of Select=COUNT, # we shouldn't get an item back - just the count. assert postfilter_count == 1 assert prefilter_count == 2 assert got_items == []
def test_query_filter_counts(test_table_sn_with_data): table, p, items = test_table_sn_with_data # First test without a filter - both Count (postfilter_count) and # ScannedCount (prefilter_count) should return the same count of items. (prefilter_count, postfilter_count, pages, got_items) = full_query_and_counts(table, KeyConditions={ 'p': { 'AttributeValueList': [p], 'ComparisonOperator': 'EQ' } }) assert (got_items == items) assert (prefilter_count == len(items)) assert (postfilter_count == len(items)) # Now use a filter, on the "bool" attribute so the filter will match # roughly half of the items. ScannedCount (prefilter_count) should still # returns the full number of items, but Count (postfilter_count) returns # should return just the number of matched items. (prefilter_count, postfilter_count, pages, got_items) = full_query_and_counts(table, KeyConditions={ 'p': { 'AttributeValueList': [p], 'ComparisonOperator': 'EQ' } }, QueryFilter={ 'bool': { 'AttributeValueList': [True], 'ComparisonOperator': 'EQ' } }) expected_items = [item for item in items if item['bool'] == True] assert (got_items == expected_items) assert (prefilter_count == len(items)) assert (postfilter_count == len(expected_items))