def test_you_cannot_add_duplicate_document_uris(self, db_session): # You can't add DocumentURI's with the same claimant, uri, type and # content_type, even if they have different documents. attrs = { "claimant": "http://www.example.com", "uri": "http://www.example.com", "type": "foo", "content_type": "bar", } db_session.add(DocumentURI(**attrs, document=Document())) db_session.add(DocumentURI(**attrs, document=Document())) with pytest.raises(sa.exc.IntegrityError): db_session.commit()
def test_repr(self): uri = DocumentURI(id=1234) repr_string = repr(uri) assert "DocumentURI" in repr_string assert "1234" in repr_string
def test_it_creates_a_new_DocumentURI_if_there_is_no_existing_one( self, db_session, doc_uri_attrs): original_attrs = doc_uri_attrs updated_attrs = dict(original_attrs, created=datetime.now(), updated=datetime.now()) # Add one non-matching DocumentURI to the database. db_session.add( DocumentURI(**dict(original_attrs, content_type="different"))) create_or_update_document_uri(session=db_session, **updated_attrs) document_uri = (db_session.query(DocumentURI).order_by( DocumentURI.created.desc()).first()) assert document_uri == Any.object.with_attrs(updated_attrs)
def test_it_updates_the_existing_DocumentURI_if_there_is_one( self, db_session, doc_uri_attrs): original_attrs = doc_uri_attrs updated_attrs = dict(original_attrs, created=datetime.now(), updated=datetime.now()) document_uri = DocumentURI(**original_attrs) db_session.add(document_uri) create_or_update_document_uri(session=db_session, **updated_attrs) assert document_uri.created == original_attrs["created"] assert document_uri.updated == updated_attrs["updated"] assert (len(db_session.query(DocumentURI).all()) == 1 ), "It shouldn't have added any new objects to the db"
def find_or_create_by_uris( # pylint: disable=too-many-arguments cls, session, claimant_uri, uris, created=None, updated=None): """ Find or create documents from a claimant uri and a list of uris. It tries to find a document based on the claimant and the set of uris. If none can be found it will return a new document with the claimant uri as its only document uri as a self-claim. It is the callers responsibility to create any other document uris. """ finduris = [claimant_uri] + uris documents = cls.find_by_uris(session, finduris) if not documents.count(): doc = Document(created=created, updated=updated) DocumentURI( document=doc, claimant=claimant_uri, uri=claimant_uri, type="self-claim", created=created, updated=updated, ) session.add(doc) try: session.flush() except sa.exc.IntegrityError as err: raise ConcurrentUpdateError( "concurrent document creation") from err return documents
def test_it_normalizes_the_uri(self): document_uri = DocumentURI(uri="http://example.com/") assert document_uri.uri_normalized == "httpx://example.com"