def test_list_tables_ann(self, name, ann_space, parent):
        r1, r2, iid = self.setup_tables_for_list()
        r1noann = tuple(r1[:-1] + (None,))
        r2noann = tuple(r2[:-1] + (None,))

        if parent is None:
            parentim = None
        elif parent:
            parentim = "Image:%s" % iid
        else:
            # Non-existent
            parentim = "Image:%s" % (iid + 1)

        expected1 = (
            (not name or name == "name-1")
            and (not ann_space or ann_space == "ann_space-1")
            and (parent is None or parent)
        )
        expected2 = (not name or name == "name-2") and (not ann_space) and (parent is None)

        if name is None and ann_space is None and parent is None:
            with pytest.raises(OmeroTablesFeatureStore.OmeroTableException):
                tables = OmeroTablesFeatureStore.list_tables(self.sess, name=name, ann_space=ann_space, parent=parentim)
        else:
            tables = OmeroTablesFeatureStore.list_tables(self.sess, name=name, ann_space=ann_space, parent=parentim)
            assert len(tables) in (0, 1, 2)
            if ann_space is None and parent is None:
                assert (r1noann in tables) == expected1
                assert (r2noann in tables) == expected2
            else:
                assert (r1 in tables) == expected1
                assert (r2 in tables) == expected2
    def test_new_table(self, parent):
        tcols, meta, ftnames = TableStoreHelper.get_columns([2], "multi", False)

        if parent:
            iid = unwrap(TableStoreHelper.create_image(self.sess).getId())
            store = OmeroTablesFeatureStore.new_table(
                self.sess, self.name, self.ft_space, self.ann_space, meta, ftnames, "Image:%d" % iid
            )
        else:
            store = OmeroTablesFeatureStore.new_table(
                self.sess, self.name, self.ft_space, self.ann_space, meta, ftnames
            )

        assert store.table
        TableStoreHelper.assert_coltypes_equal(store.cols, tcols)

        if parent:
            tid = unwrap(store.get_table().getOriginalFile().getId())
            q = "SELECT link.child FROM ImageAnnotationLink link " "WHERE link.parent.id=:id"
            p = omero.sys.ParametersI()
            p.addId(iid)
            r = self.sess.getQueryService().findAllByQuery(q, p)
            assert len(r) == 1
            assert isinstance(r[0], omero.model.FileAnnotation)
            assert unwrap(r[0].getFile().getId()) == tid

        store.close()
    def test_list_tables_noann(self, name, ft_space, owner):
        uid = unwrap(self.user.getId())
        r1, r2, iid = self.setup_tables_for_list()
        # Ignore ann_space
        r1noann = tuple(r1[:-1] + (None,))
        r2noann = tuple(r2[:-1] + (None,))

        if owner == "uid":
            ownerid = uid
        else:
            ownerid = owner

        expected1 = (
            (not name or name == "name-1")
            and (not ft_space or ft_space == "ft_space-12")
            and (ownerid is None or ownerid == -1 or ownerid == uid)
        )
        expected2 = (
            (not name or name == "name-2")
            and (not ft_space or ft_space == "ft_space-12")
            and (ownerid is None or ownerid == -1 or ownerid == uid)
        )

        if name is None and ft_space is None and (owner in (None, -1)):
            with pytest.raises(OmeroTablesFeatureStore.OmeroTableException):
                tables = OmeroTablesFeatureStore.list_tables(self.sess, name=name, ft_space=ft_space, ownerid=ownerid)
        else:
            tables = OmeroTablesFeatureStore.list_tables(self.sess, name=name, ft_space=ft_space, ownerid=ownerid)
            assert len(tables) in (0, 1, 2)
            assert (r1noann in tables) == expected1
            assert (r2noann in tables) == expected2
    def test_create(self):
        ownerid = 123
        session = MockSession(None, None, ownerid)
        fs = MockFeatureTable(None)
        self.mox.StubOutWithMock(OmeroTablesFeatureStore, 'list_tables')
        self.mox.StubOutWithMock(OmeroTablesFeatureStore, 'new_table')
        fsname = 'fsname'
        meta = [('Float', 'f')]
        colnames = ['x1', 'x2']

        OmeroTablesFeatureStore.list_tables(
            session, fsname, 'x/features', ownerid=ownerid).AndReturn([])

        OmeroTablesFeatureStore.new_table(
            session, fsname, 'x/features', 'x/source', meta, colnames
            ).AndReturn(fs)

        self.mox.ReplayAll()

        fts = OmeroTablesFeatureStore.FeatureTableManager(
            session, namespace='x')
        assert fts.create(fsname, meta, colnames) == fs

        assert len(fts.fss) == 1
        assert fts.fss.get((fsname, ownerid)) == fs

        self.mox.VerifyAll()
    def test_list_tables_noann(self, name, ft_space, ownerid):
        session = MockSession(None, None, 1)
        perms = self.mox.CreateMock(MockPermissionsHandler)
        store = MockFeatureTable(None)
        store.perms = perms

        self.mox.StubOutWithMock(session.qs, 'projection')
        self.mox.StubOutWithMock(session.qs, 'findAllByQuery')

        params = omero.sys.ParametersI()

        # Need to reconstruct the dict to get the conditions in the
        # same order
        kvs = {}
        if name:
            kvs['name'] = name
            params.addString('name', name)
        if ft_space:
            kvs['path'] = ft_space
            params.addString('path', ft_space)
        if ownerid > -1:
            kvs['details.owner.id'] = ownerid
            params.addLong('details_owner_id', ownerid)

        q = ('FROM OriginalFile')
        gotwhere = False
        for k in kvs:
            if not gotwhere:
                q += ' WHERE '
                gotwhere = True
            else:
                q += ' AND '
            if k == 'details.owner.id':
                q += 'details.owner.id = :details_owner_id'
            else:
                q += '%s = :%s' % (k, k)

        if name or ft_space or ownerid > -1:
            mf = MockOriginalFile(1L, name, ft_space)
            session.qs.findAllByQuery(q, mox.Func(
                lambda o: TestFeatureTable.parameters_equal(
                    params, o))).AndReturn([mf])

        self.mox.ReplayAll()
        if name or ft_space or ownerid > -1:
            result = OmeroTablesFeatureStore.list_tables(
                session, name, ft_space, None, ownerid, None)
            assert len(result) == 1
            assert result[0] == (1L, name, ft_space, None)
        else:
            with pytest.raises(OmeroTablesFeatureStore.OmeroTableException):
                result = OmeroTablesFeatureStore.list_tables(
                    session, name, ft_space, None, ownerid, None)

        self.mox.VerifyAll()
    def setup_tables_for_list(self):
        tcols, meta, ftnames = TableStoreHelper.get_columns([2], "multi", False)

        iid = unwrap(TableStoreHelper.create_image(self.sess).getId())
        store1 = OmeroTablesFeatureStore.new_table(
            self.sess, "name-1", "ft_space-12", "ann_space-1", meta, ftnames, "Image:%s" % iid
        )
        store2 = OmeroTablesFeatureStore.new_table(self.sess, "name-2", "ft_space-12", "ann_space-2", meta, ftnames)

        r1 = (self.get_table_id(store1), "name-1", "ft_space-12", "ann_space-1")
        r2 = (self.get_table_id(store2), "name-2", "ft_space-12", "ann_space-2")

        store1.close()
        store2.close()

        return r1, r2, iid
 def test_open_table(self):
     tid, tcols, meta, ftnames = TableStoreHelper.create_table(
         self.sess, self.ft_space, self.name, [1], "multi", False
     )
     store = OmeroTablesFeatureStore.open_table(self.sess, tid, self.ann_space)
     assert unwrap(store.get_table().getOriginalFile().getId()) == tid
     assert store.ann_space == self.ann_space
     store.close()
    def test_list_tables_ann(self, name, ft_space, ann_space, ownerid, parent):
        if not parent and not ann_space:
            return

        session = MockSession(None, None, 1)
        self.mox.StubOutWithMock(session.qs, 'projection')
        self.mox.StubOutWithMock(session.qs, 'findAllByQuery')

        params = omero.sys.ParametersI()

        if parent:
            q = ('SELECT child.file.id, child.file.name, child.file.path, '
                 'child.ns '
                 'FROM DatasetAnnotationLink '
                 'WHERE child.class=FileAnnotation '
                 'AND parent.id=:id')
            if ann_space:
                q += ' AND child.ns=:ns'
            if name:
                q += ' AND child.file.name=:name'
            if ft_space:
                q += ' AND child.file.path=:ft_space'
            if ownerid > -1:
                q += ' AND child.file.details.owner.id=:ownerid'

        if ann_space and not parent:
            q = ('SELECT file.id, file.name, file.path, ns '
                 'FROM FileAnnotation WHERE ns=:ns')
            if name:
                q += ' AND file.name=:name'
            if ft_space:
                q += ' AND file.path=:ft_space'
            if ownerid > -1:
                q += ' AND file.details.owner.id=:ownerid'

        if parent:
            params.addId(2)
        if ann_space:
            params.addString('ns', ann_space)
        if name:
            params.addString('name', name)
        if ft_space:
            params.addString('ft_space', ft_space)
        if ownerid > -1:
            params.addLong('ownerid', ownerid)

        r = [1L, 'table-name', '/test/features/ft_space',
             '/test/features/ann_space']
        session.qs.projection(q, mox.Func(
            lambda o: TestFeatureTable.parameters_equal(
                params, o))).AndReturn([r])

        self.mox.ReplayAll()
        result = OmeroTablesFeatureStore.list_tables(
            session, name, ft_space, ann_space, ownerid, parent)
        assert len(result) == 1
        assert result[0] == tuple(r)
        self.mox.VerifyAll()
    def test_get(self, state):
        ownerid = 123
        session = MockSession(None, None, ownerid)
        fs = MockFeatureTable(session)
        fs.table = object()
        self.mox.StubOutWithMock(OmeroTablesFeatureStore, 'list_tables')
        self.mox.StubOutWithMock(OmeroTablesFeatureStore, 'open_table')
        fsname = 'fsname'
        fts = OmeroTablesFeatureStore.FeatureTableManager(
            session, namespace='x')

        self.mox.StubOutWithMock(fts.fss, 'get')
        self.mox.StubOutWithMock(fts.fss, 'insert')

        k = (fsname, ownerid)

        if state == 'opened':
            fts.fss.get(k).AndReturn(fs)
        else:
            if state == 'unopened':
                fts.fss.get(k).AndReturn(None)
            if state == 'closed':
                fsold = MockFeatureTable(None)
                fts.fss.get(k).AndReturn(fsold)

            r = (1234, None, None, None)
            OmeroTablesFeatureStore.list_tables(
                session, fsname, 'x/features', ownerid=ownerid).AndReturn([r])
            OmeroTablesFeatureStore.open_table(
                session, r[0], 'x/source').AndReturn(fs)
            fts.fss.insert(k, fs)

        self.mox.ReplayAll()

        assert fts.get(fsname, ownerid) == fs
        self.mox.VerifyAll()
    def test_list_tables_ann(self, name, ft_space, ann_space, ownerid, parent):
        if not parent and not ann_space:
            return

        session = MockSession(None, None, 1)
        self.mox.StubOutWithMock(session.qs, 'projection')
        self.mox.StubOutWithMock(session.qs, 'findAllByQuery')

        params = omero.sys.ParametersI()

        if parent:
            q = ('SELECT child.file.id, child.file.name, child.file.path, '
                 'child.ns '
                 'FROM DatasetAnnotationLink '
                 'WHERE child.class=FileAnnotation '
                 'AND parent.id=:id')
            if ann_space:
                q += ' AND child.ns=:ns'
            if name:
                q += ' AND child.file.name=:name'
            if ft_space:
                q += ' AND child.file.path=:ft_space'
            if ownerid > -1:
                q += ' AND child.file.details.owner.id=:ownerid'

        if ann_space and not parent:
            q = ('SELECT file.id, file.name, file.path, ns '
                 'FROM FileAnnotation WHERE ns=:ns')
            if name:
                q += ' AND file.name=:name'
            if ft_space:
                q += ' AND file.path=:ft_space'
            if ownerid > -1:
                q += ' AND file.details.owner.id=:ownerid'

        if parent:
            params.addId(2)
        if ann_space:
            params.addString('ns', ann_space)
        if name:
            params.addString('name', name)
        if ft_space:
            params.addString('ft_space', ft_space)
        if ownerid > -1:
            params.addLong('ownerid', ownerid)

        r = [
            1L, 'table-name', '/test/features/ft_space',
            '/test/features/ann_space'
        ]
        session.qs.projection(
            q, mox.Func(lambda o: TestFeatureTable.parameters_equal(params, o))
        ).AndReturn([r])

        self.mox.ReplayAll()
        result = OmeroTablesFeatureStore.list_tables(session, name, ft_space,
                                                     ann_space, ownerid,
                                                     parent)
        assert len(result) == 1
        assert result[0] == tuple(r)
        self.mox.VerifyAll()