예제 #1
0
def testFileAnnotation(author_testimg_generated, gatewaywrapper):
    """ Creates a file annotation from a local file """
    tempFileName = "tempFile"
    f = open(tempFileName, "w")
    fileText = "Test text for writing to file for upload"
    f.write(fileText)
    f.close()
    fileSize = os.path.getsize(tempFileName)
    ns = TESTANN_NS
    image = author_testimg_generated

    # use the same file to create various file annotations with different namespaces
    fileAnn = gatewaywrapper.gateway.createFileAnnfromLocalFile(tempFileName, mimetype="text/plain", ns=ns)
    image.linkAnnotation(fileAnn)
    compAnn = gatewaywrapper.gateway.createFileAnnfromLocalFile(
        tempFileName, mimetype="text/plain", ns=omero.constants.namespaces.NSCOMPANIONFILE
    )
    image.linkAnnotation(compAnn)
    os.remove(tempFileName)

    # get user-id of another user to use below.
    gatewaywrapper.loginAsAdmin()
    adminId = gatewaywrapper.gateway.getUser().getId()
    gatewaywrapper.loginAsAuthor()

    # test listing of File Annotations. Should exclude companion files by default and all files should be loaded
    gateway = gatewaywrapper.gateway
    eid = gateway.getUser().getId()
    fas = list(gateway.listFileAnnotations(eid=eid, toInclude=[ns]))
    faIds = [fa.id for fa in fas]
    assert fileAnn.getId() in faIds
    assert compAnn.getId() not in faIds
    for fa in fas:
        assert fa.getNs() == ns, "All files should be filtered by this namespace"
        assert fa._obj.file.loaded, "All file annotations should have files loaded"

    # filtering by namespace
    fas = list(gateway.listFileAnnotations(toInclude=["nothing.with.this.namespace"], eid=eid))
    assert len(fas) == 0, "No file annotations should exist with bogus namespace"

    # filtering files by a different user should not return the annotations above.
    fas = list(gateway.listFileAnnotations(eid=adminId))
    faIds = [fa.id for fa in fas]
    assert fileAnn.getId() not in faIds
    assert compAnn.getId() not in faIds

    image._conn = gatewaywrapper.gateway  # needs a fresh connection, original was closed already
    ann = image.getAnnotation(ns)
    annId = ann.getId()
    assert ann.OMERO_TYPE == omero.model.FileAnnotationI
    for t in ann.getFileInChunks():
        assert str(t) == fileText  # we get whole text in one chunk

    # delete what we created
    assert gateway.getObject("Annotation", annId) is not None
    link = ann.link
    gateway.deleteObjectDirect(link._obj)  # delete link
    gateway.deleteObjectDirect(ann._obj)  # then the annotation
    gateway.deleteObjectDirect(ann._obj.file)  # then the file
    assert gateway.getObject("Annotation", annId) is None
예제 #2
0
def testFileAnnotation(author_testimg_generated, gatewaywrapper):
    """ Creates a file annotation from a local file """
    tempFileName = "tempFile"
    f = open(tempFileName, 'w')
    fileText = "Test text for writing to file for upload"
    f.write(fileText)
    f.close()
    ns = TESTANN_NS
    image = author_testimg_generated

    # use the same file to create various file annotations with different
    # namespaces
    fileAnn = gatewaywrapper.gateway.createFileAnnfromLocalFile(
        tempFileName, mimetype='text/plain', ns=ns)
    image.linkAnnotation(fileAnn)
    compAnn = gatewaywrapper.gateway.createFileAnnfromLocalFile(
        tempFileName,
        mimetype='text/plain',
        ns=omero.constants.namespaces.NSCOMPANIONFILE)
    image.linkAnnotation(compAnn)
    os.remove(tempFileName)

    # get user-id of another user to use below.
    gatewaywrapper.loginAsAdmin()
    adminId = gatewaywrapper.gateway.getUser().getId()
    gatewaywrapper.loginAsAuthor()

    # test listing of File Annotations. Should exclude companion files by
    # default and all files should be loaded
    gateway = gatewaywrapper.gateway
    eid = gateway.getUser().getId()
    fas = list(gateway.listFileAnnotations(eid=eid, toInclude=[ns]))
    faIds = [fa.id for fa in fas]
    assert fileAnn.getId() in faIds
    assert compAnn.getId() not in faIds
    for fa in fas:
        assert fa.getNs() == ns, \
            "All files should be filtered by this namespace"
        assert fa._obj.file.loaded, \
            "All file annotations should have files loaded"

    # filtering by namespace
    fas = list(
        gateway.listFileAnnotations(toInclude=["nothing.with.this.namespace"],
                                    eid=eid))
    assert len(fas) == 0, \
        "No file annotations should exist with bogus namespace"

    # filtering files by a different user should not return the annotations
    # above.
    fas = list(gateway.listFileAnnotations(eid=adminId))
    faIds = [fa.id for fa in fas]
    assert fileAnn.getId() not in faIds
    assert compAnn.getId() not in faIds

    # needs a fresh connection, original was closed already
    image._conn = gatewaywrapper.gateway
    ann = image.getAnnotation(ns)
    annId = ann.getId()
    assert ann.OMERO_TYPE == omero.model.FileAnnotationI
    for t in ann.getFileInChunks():
        assert str(t) == fileText  # we get whole text in one chunk

    # delete what we created
    assert gateway.getObject("Annotation", annId) is not None
    link = ann.link
    gateway.deleteObjectDirect(link._obj)  # delete link
    gateway.deleteObjectDirect(ann._obj)  # then the annotation
    gateway.deleteObjectDirect(ann._obj.file)  # then the file
    assert gateway.getObject("Annotation", annId) is None