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 pointer_item(): return PointerItem.generate(standard_context())
def random_pointer_item(): return PointerItem.generate(random_context(random.randint(0, 10)))
def get_pointer_key(): return str(PointerItem._generate_key())
def test_invalid_uuid_throws(): with pytest.raises(ValueError): PointerItem("garbage")
def test_unset_partition_raises_uuid(): with pytest.raises(DataModelException): PointerItem(None, "sort_key")
def test_uuid_item_happy_case_from_key_and_context(suuid, sample_context): test_item = PointerItem.from_key_and_context(suuid, sample_context) assert suuid in test_item.partition_key assert sample_context.items() <= test_item.context.items()
def test_uuid_item_happy_case_str(suuid): item = PointerItem(suuid) assert suuid in item.partition_key
def test_uuid_item_happy_case(sample_context): test_item = PointerItem.generate(sample_context) assert sample_context.items() <= test_item.context.items()
def test_object_type_neq(sample_context): pointer = PointerItem.generate(sample_context) context = ContextItem("stuff", pointer.partition_key) assert pointer != context
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)
def test_validate_reserved_ec_keys_sort(): with pytest.raises(DataModelException): PointerItem._validate_reserved_ec_keys({BaseItem.sort_key_name(): "blammo"})