Exemplo n.º 1
0
def test_dynamodb_query():
    table_name = "testawsimpleps"  # ps = both partition and sort

    dynamodb_access = DynamoDBAccess(profile_name=test_awsimple_str,
                                     table_name=table_name)
    dynamodb_access.create_table("id", "name")

    # three entries for "me"
    dynamodb_access.put_item({
        "id": "me",
        "name": "james",
        "answer": 13
    })  # this will be the "first" one
    dynamodb_access.put_item({"id": "me", "name": "james abel", "answer": 1})
    dynamodb_access.put_item({
        "id": "me",
        "name": "zzz",
        "answer": 99
    })  # this will be the "last" one

    dynamodb_access.put_item({"id": "notme", "name": "notjames", "answer": 42})

    response = dynamodb_access.query("id", "me")  # partition only
    assert len(response) == 3

    response = dynamodb_access.query("id", "me", "name",
                                     "james")  # partition and sort
    assert len(response) == 1

    response = dynamodb_access.query_begins_with("id", "me", "name",
                                                 "james a")  # begins with
    assert len(response) == 1
    response = dynamodb_access.query_begins_with("id", "me", "name", "jame")
    assert len(response) == 2

    response = dynamodb_access.query("id", "idonotexist")  # does not exist
    assert len(response) == 0

    response = dynamodb_access.query_one("id", "me", QuerySelection.highest)
    assert response["answer"] == 99
    assert response["name"] == "zzz"  # the "last" entry, as sorted by sort key

    response = dynamodb_access.query_one("id", "me", QuerySelection.lowest)
    assert response["answer"] == 13
    assert response[
        "name"] == "james"  # the "first" entry, as sorted by sort key

    response = dynamodb_access.query_one("id", "idonotexist",
                                         QuerySelection.lowest)
    assert response is None
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()
def test_dynamodb_partition_as_number():
    dynamodb_access = DynamoDBAccess(
        profile_name=test_awsimple_str,
        table_name=f"{test_awsimple_str}_partition_as_number",
        cache_dir=Path("cache"))
    dynamodb_access.create_table(
        "year", "id", partition_key_type=int)  # partition key as number
    input_item = {"id": "me", "year": 1999, "out_of_time": False}
    dynamodb_access.put_item(input_item)
    item = dynamodb_access.get_item("id", "me", "year", 1999)
    pprint(item)
    assert input_item == dynamodb_to_dict(item)

    item = dynamodb_access.query(
        "year", 1999)[0]  # only use the partition key (no sort key)
    pprint(item)
    assert input_item == dynamodb_to_dict(item)

    dynamodb_access.delete_table()
Exemplo n.º 4
0
def musical_instruments_example():

    """
    This example shows how to use DynamoDB to keep a table of musical instruments.

    """

    dynamodb_access = DynamoDBAccess("musical_instruments_example", profile_name="testawsimple", cache_life=60)  # short cache life for development

    # Our primary key is a composite of partition (manufacturer) and sort (serial_number).
    # For a particular manufacturer, serial numbers define exactly one instrument (for this example we are assuming a serial number can be represented as an
    # integer and doesn't have to be a string).
    dynamodb_access.create_table("manufacturer", "serial_number", sort_key_type=int)

    # we have to convert float to a Decimal for DynamoDB
    dynamodb_access.put_item(dict_to_dynamodb({"manufacturer": "Gibson", "serial_number": 1234, "model": "Ripper", "year": 1983, "price": 1299.50}))
    dynamodb_access.put_item(dict_to_dynamodb({"manufacturer": "Gibson", "serial_number": 5678, "model": "Thunderbird", "year": 1977, "price": 2399.50}))
    dynamodb_access.put_item(
        dict_to_dynamodb(
            {
                "manufacturer": "Fender",
                "serial_number": 1234,
                "model": "Precision",
                "year": 2008,
                "price": 1800.0,
            }  # same serial number as the Gibson Ripper, but that's OK since this is Fender
        )
    )

    # get all the Gibson instruments
    start = time.time()
    item = dynamodb_access.query("manufacturer", "Gibson")  # this can (and will in this case) be multiple items
    end = time.time()
    pprint(item)
    print(f"query took {end-start} seconds")  # nominal 0.1 to 0.15 seconds
    print()

    # get the entire inventory
    start = time.time()
    all_items = dynamodb_access.scan_table_cached()  # use cached if the table is large and *only* if we know our table is slowly or never changing
    end = time.time()
    pprint(all_items)
    print(f"scan took {end-start} seconds ({dynamodb_access.cache_hit=})")  # always fast for this small data set, but caching can offer a speedup for large tables
def test_dynamodb_secondary_index_int():

    table_name = f"{test_awsimple_str}3"
    table = DynamoDBAccess(table_name)

    sort_key = "id2"
    secondary_index = "num"
    table.create_table(id_str,
                       sort_key,
                       secondary_index,
                       secondary_key_type=int)  # secondary index as an int

    table.put_item({id_str: "me", sort_key: "myself", secondary_index: 1})
    table.put_item({id_str: "me", sort_key: "moi", secondary_index: 2})

    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
    table.delete_table()