def test_assert_set():
    item = BaseItem("foo", "bar")
    item.partition_key = None
    with pytest.raises(DataModelException):
        item._assert_set()
    item.partition_key = "foo"
    item.sort_key = None
    with pytest.raises(DataModelException):
        item._assert_set()
예제 #2
0
def test_query_for_context_key(mocked_dbo, random_context_key_ddb_result):
    # There is some trickery for mocking out DDB here.
    # First there's a query for a specific context key, which will return a hash-and-
    # range of one unique context key and many UUIDs (sort keys). For the conceit
    # of the test, assume two queries are made in order: one to get a context key with
    # one matching item; and one to lookup that item and its context to map to a
    # pointer. This is a bit incomplete and contrived, but simulating DDB gets
    # complex fast and this is better covered by DDBLocal (integration).

    # Pull out the context key target from the 'results'
    test_context_target = random_context_key_ddb_result["Items"][0][
        BaseItem.sort_key_name()]
    # Pointer for this context key
    test_pointer = PointerItem(partition_key=test_context_target,
                               context=random_context(8))
    # Create a fake pointer lookup result to return for the chosen guid
    test_pointer_result = {"Items": [test_pointer.to_item()]}
    # Set up query objects
    q = ContextQuery(test_context_target)
    # Set up returning the fake DDB Item when we call query
    mock_query = mock.Mock()
    # Return first the "fake table", then the "fake pointer lookup result"
    mock_query.side_effect = [
        random_context_key_ddb_result, test_pointer_result
    ]
    mocked_dbo.table.query = mock_query
    # Query
    pointers = mocked_dbo._query_for_context_key(q)
    # Assert we got back a matching Pointer record
    assert test_pointer in pointers
예제 #3
0
def test_list_items(mocked_dbo, random_ddb_pointer_table):
    mocked_dbo.table.scan = mock.Mock(return_value=random_ddb_pointer_table)
    expected_guids = set()
    for item in random_ddb_pointer_table["Items"]:
        partition_key = item[BaseItem.partition_key_name()]
        if not partition_key.startswith(ContextItem._prefix()):
            expected_guids.add(partition_key)
    pointers = mocked_dbo.list()
    actual_guids = set()
    for p in pointers:
        # Ensure we filtered out context keys
        assert ContextItem._prefix() not in p.partition_key
        # Add this guid
        actual_guids.add(p.partition_key)
    assert expected_guids == actual_guids
def test_validate_reserved_ec_keys_sort():
    with pytest.raises(DataModelException):
        PointerItem._validate_reserved_ec_keys({BaseItem.sort_key_name(): "blammo"})
def test_duplicate_keys_throw():
    data = bytes.fromhex("badbadbadbad")
    bonkers_context = {BaseItem.partition_key_name(): "kaboom"}
    with pytest.raises(DataModelException):
        DocumentBundle.from_data_and_context(data, bonkers_context)