def test_create_should_fail_if_user_is_same( cosmos_db_repository: CosmosDBRepository, sample_item: dict, event_context: EventContext, ): try: cosmos_db_repository.create(sample_item, event_context) fail('It should have failed') except Exception as e: assert type(e) is CosmosResourceExistsError assert e.status_code == 409
def test_create_with_diff_id_but_same_unique_field_should_fail( cosmos_db_repository: CosmosDBRepository, sample_item: dict, event_context: EventContext, ): try: new_data = sample_item.copy() new_data.update({'id': fake.uuid4()}) cosmos_db_repository.create(new_data, event_context) fail('It should have failed') except Exception as e: assert type(e) is CosmosResourceExistsError assert e.status_code == 409
def test_create_with_mapper_should_provide_calculated_fields( cosmos_db_repository: CosmosDBRepository, event_context: EventContext, tenant_id: str, ): new_item = dict( id=fake.uuid4(), name=fake.name(), email=fake.safe_email(), age=fake.pyint(min_value=10, max_value=80), tenant_id=tenant_id, ) created_item: Person = cosmos_db_repository.create( new_item, event_context, mapper=Person ) assert created_item is not None assert all( item in created_item.__dict__.items() for item in new_item.items() ) assert ( type(created_item) is Person ), "The result should be wrapped with a class" assert created_item.is_adult() is (new_item["age"] >= 18)
def another_item( cosmos_db_repository: CosmosDBRepository, tenant_id: str, event_context: EventContext, ) -> dict: sample_item_data = dict( id=fake.uuid4(), name=fake.name(), email=fake.safe_email(), age=fake.pyint(min_value=10, max_value=80), tenant_id=tenant_id, ) return cosmos_db_repository.create(sample_item_data, event_context)
def test_create_with_diff_unique_data_but_same_tenant_should_succeed( cosmos_db_repository: CosmosDBRepository, sample_item: dict, event_context: EventContext, ): new_data = sample_item.copy() new_data.update( { 'id': fake.uuid4(), 'email': fake.safe_email(), } ) result = cosmos_db_repository.create(new_data, event_context) assert result["id"] != sample_item["id"], 'It should be a new element'
def test_create_with_same_id_but_diff_partition_key_attrib_should_succeed( cosmos_db_repository: CosmosDBRepository, another_event_context: EventContext, sample_item: dict, another_tenant_id: str, ): new_data = sample_item.copy() new_data.update( { 'tenant_id': another_tenant_id, } ) result = cosmos_db_repository.create(new_data, another_event_context) assert result["id"] == sample_item["id"], "Should have allowed same id"
def test_create_should_succeed( cosmos_db_repository: CosmosDBRepository, tenant_id: str, event_context: EventContext, ): sample_item = dict( id=fake.uuid4(), name=fake.name(), email=fake.safe_email(), age=fake.pyint(min_value=10, max_value=80), tenant_id=tenant_id, ) created_item = cosmos_db_repository.create(sample_item, event_context) assert created_item is not None assert all(item in created_item.items() for item in sample_item.items())