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
def test_unset_partition_raises_uuid(): with pytest.raises(DataModelException): PointerItem(None, "sort_key")
def test_invalid_uuid_throws(): with pytest.raises(ValueError): PointerItem("garbage")
def test_uuid_item_happy_case_str(suuid): item = PointerItem(suuid) assert suuid in item.partition_key
def test_pointer_key_hash(suuid, sample_context): pointer1 = PointerItem(suuid) pointer2 = PointerItem(partition_key=suuid, context=sample_context) assert hash(pointer1) == hash(pointer2) assert pointer1 == pointer2
def test_setting_pointer_sort_key_throws(suuid, sample_context): with pytest.raises(DataModelException): PointerItem(suuid, sample_context)