def test_dynamodb_scan_table_as_dict(): dynamodb_access = DynamoDBAccess(profile_name=test_awsimple_str, table_name=test_awsimple_str, cache_dir=Path("cache"), cache_life=timedelta(seconds=10).total_seconds()) dynamodb_access.create_table(id_str) dynamodb_access.put_item({id_str: "b", "value": 1}) # will be sorted in a different order than we're inputting dynamodb_access.put_item({id_str: "c", "value": 3}) dynamodb_access.put_item({id_str: "a", "value": 2}) expected_contents = {"a": {"id": "a", "value": Decimal("2")}, "b": {"id": "b", "value": Decimal("1")}, "c": {"id": "c", "value": Decimal("3")}} table_contents = dynamodb_access.scan_table_as_dict() check_scan_table(table_contents, expected_contents) table_contents = dynamodb_access.scan_table_cached_as_dict() check_scan_table(table_contents, expected_contents) table_contents = dynamodb_access.scan_table_cached_as_dict() assert dynamodb_access.cache_hit check_scan_table(table_contents, expected_contents) table_contents = dynamodb_access.scan_table_cached_as_dict(sort_key=lambda x: x[id_str]) # test sort_key check_scan_table(table_contents, expected_contents)
def test_dynamodb_secondary_index(): table_name = f"{test_awsimple_str}2" table = DynamoDBAccess(table_name) sort_key = "id2" secondary_index = "id3" table.create_table(id_str, sort_key, secondary_index) item = {id_str: "me", sort_key: "myself", secondary_index: "i"} table.put_item(item) item2 = deepcopy(item) item2[sort_key] = "moi même" # also test unicode! item2[secondary_index] = "je" table.put_item(item2) query_results = table.query(id_str, "me") print(f"{query_results=}") assert len( query_results ) == 2 # just the partition key should provide us with both rows assert table.query(secondary_index, "je") == [ item2 ] # with (only) the secondary index (in DynamoDB you can't mix primary and secondary indexes) expected_contents = { DictKey(partition="me", sort="moi même"): { "id": "me", "id2": "moi même", "id3": "je" }, DictKey(partition="me", sort="myself"): { "id": "me", "id2": "myself", "id3": "i" }, } contents = table.scan_table_cached_as_dict() assert contents == expected_contents assert list(contents.keys()) == [ DictKey(partition="me", sort="moi même"), DictKey(partition="me", sort="myself") ] table.delete_table()