def test_it_logs_a_warning(self, log): """ It should warn on document mismatches. It should warn if there's an existing DocumentMeta with a different Document. """ session = mock_db_session() document_one = mock_document() document_two = mock_document() existing_document_meta = mock_document_meta(document=document_one) session.query.return_value.filter.return_value.one_or_none\ .return_value = existing_document_meta document.create_or_update_document_meta( session=session, claimant='http://example.com/claimant', type='title', value='new value', document=document_two, created=yesterday(), updated=now(), ) assert log.warn.call_count == 1
def test_it_logs_a_warning(self, log): """ It should warn on document mismatches. It should warn if there's an existing DocumentMeta with a different Document. """ session = mock_db_session() document_one = mock_document() document_two = mock_document() existing_document_meta = mock_document_meta(document=document_one) session.query.return_value.filter.return_value.one_or_none\ .return_value = existing_document_meta document.create_or_update_document_meta( session=session, claimant='http://example.com/claimant', type='title', value='new value', document=document_two, created=yesterday(), updated=now(), ) assert log.warn.call_count == 1
def test_it_updates_an_existing_DocumentMeta_if_there_is_one(self, db_session): claimant = 'http://example.com/claimant' type_ = 'title' value = 'the title' document_ = document.Document() created = yesterday() updated = now() document_meta = document.DocumentMeta( claimant=claimant, type=type_, value=value, document=document_, created=created, updated=updated, ) db_session.add(document_meta) new_updated = now() document.create_or_update_document_meta( session=db_session, claimant=claimant, type=type_, value='new value', document=document.Document(), # This should be ignored. created=now(), # This should be ignored. updated=new_updated, ) assert document_meta.value == 'new value' assert document_meta.updated == new_updated assert document_meta.created == created, "It shouldn't update created" assert document_meta.document == document_, ( "It shouldn't update document") assert len(db_session.query(document.DocumentMeta).all()) == 1, ( "It shouldn't have added any new objects to the db")
def test_it_updates_an_existing_DocumentMeta_if_there_is_one(self, db_session): claimant = 'http://example.com/claimant' type_ = 'title' value = 'the title' document_ = document.Document() created = yesterday() updated = now() document_meta = document.DocumentMeta( claimant=claimant, type=type_, value=value, document=document_, created=created, updated=updated, ) db_session.add(document_meta) new_updated = now() document.create_or_update_document_meta( session=db_session, claimant=claimant, type=type_, value='new value', document=document.Document(), # This should be ignored. created=now(), # This should be ignored. updated=new_updated, ) assert document_meta.value == 'new value' assert document_meta.updated == new_updated assert document_meta.created == created, "It shouldn't update created" assert document_meta.document == document_, ( "It shouldn't update document") assert len(db_session.query(document.DocumentMeta).all()) == 1, ( "It shouldn't have added any new objects to the db")
def test_raises_retryable_error_when_flush_fails(self, db_session, monkeypatch): document_ = document.Document() def err(): raise sa.exc.IntegrityError(None, None, None) monkeypatch.setattr(db_session, 'flush', err) with pytest.raises(transaction.interfaces.TransientError): with db_session.no_autoflush: # prevent premature IntegrityError document.create_or_update_document_meta( session=db_session, claimant='http://example.com', type='title', value='My Title', document=document_, created=now(), updated=now(), )
def test_raises_retryable_error_when_flush_fails(self, db_session, monkeypatch): document_ = document.Document() def err(): raise sa.exc.IntegrityError(None, None, None) monkeypatch.setattr(db_session, 'flush', err) with pytest.raises(transaction.interfaces.TransientError): with db_session.no_autoflush: # prevent premature IntegrityError document.create_or_update_document_meta( session=db_session, claimant='http://example.com', type='title', value='My Title', document=document_, created=now(), updated=now(), )
def test_it_skips_denormalizing_title_to_document_when_already_set(self, db_session): claimant = 'http://example.com/claimant' type_ = 'title' value = ['the title'] document_ = document.Document(title='foobar') created = yesterday() updated = now() db_session.add(document_) document.create_or_update_document_meta( session=db_session, claimant=claimant, type=type_, value=value, document=document_, created=created, updated=updated, ) document_ = db_session.query(document.Document).get(document_.id) assert document_.title == 'foobar'
def test_it_skips_denormalizing_title_to_document_when_already_set(self, db_session): claimant = 'http://example.com/claimant' type_ = 'title' value = ['the title'] document_ = document.Document(title='foobar') created = yesterday() updated = now() db_session.add(document_) document.create_or_update_document_meta( session=db_session, claimant=claimant, type=type_, value=value, document=document_, created=created, updated=updated, ) document_ = db_session.query(document.Document).get(document_.id) assert document_.title == 'foobar'
def test_it_creates_a_new_DocumentMeta_if_there_is_no_existing_one( self, db_session): claimant = 'http://example.com/claimant' type_ = 'title' value = 'the title' document_ = document.Document() created = yesterday() updated = now() # Add one non-matching DocumentMeta to the database. # This should be ignored. db_session.add( document.DocumentMeta( claimant=claimant, # Different type means this should not match the query. type='different', value=value, document=document_, created=created, updated=updated, )) document.create_or_update_document_meta( session=db_session, claimant=claimant, type=type_, value=value, document=document_, created=created, updated=updated, ) document_meta = db_session.query(document.DocumentMeta).all()[-1] assert document_meta.claimant == claimant assert document_meta.type == type_ assert document_meta.value == value assert document_meta.document == document_ assert document_meta.created == created assert document_meta.updated == updated
def test_it_creates_a_new_DocumentMeta_if_there_is_no_existing_one(self, db_session): claimant = 'http://example.com/claimant' type_ = 'title' value = 'the title' document_ = document.Document() created = yesterday() updated = now() # Add one non-matching DocumentMeta to the database. # This should be ignored. db_session.add(document.DocumentMeta( claimant=claimant, # Different type means this should not match the query. type='different', value=value, document=document_, created=created, updated=updated, )) document.create_or_update_document_meta( session=db_session, claimant=claimant, type=type_, value=value, document=document_, created=created, updated=updated, ) document_meta = db_session.query(document.DocumentMeta).all()[-1] assert document_meta.claimant == claimant assert document_meta.type == type_ assert document_meta.value == value assert document_meta.document == document_ assert document_meta.created == created assert document_meta.updated == updated