Example #1
0
File: test_db.py Project: ome/omego
    def test_upgrade(self, needupdate):
        args = self.Args({'dry_run': False})
        db = self.PartialMockDb(args, None)
        self.mox.StubOutWithMock(db, 'get_current_db_version')
        self.mox.StubOutWithMock(db, 'sql_version_matrix')
        self.mox.StubOutWithMock(db, 'sql_version_resolve')
        self.mox.StubOutWithMock(db, 'psql')

        versions = ['OMERO3.0__0', 'OMERO4.4__0', 'OMERO5.0__0']
        if needupdate:
            db.get_current_db_version().AndReturn(('OMERO3.0', '0'))
            db.sql_version_matrix().AndReturn(([], versions))
            db.sql_version_resolve([], versions, versions[0]).AndReturn(
                ['./sql/psql/OMERO4.4__0/OMERO3.0__0.sql',
                 './sql/psql/OMERO5.0__0/OMERO4.4__0.sql'])
            db.psql('-f', './sql/psql/OMERO4.4__0/OMERO3.0__0.sql')
            db.psql('-f', './sql/psql/OMERO5.0__0/OMERO4.4__0.sql')
        else:
            db.get_current_db_version().AndReturn(('OMERO5.0', '0'))
            db.sql_version_matrix().AndReturn(([], versions))

        self.mox.ReplayAll()

        db.upgrade()
        self.mox.VerifyAll()
Example #2
0
    def test_upgrade(self, needupdate):
        args = self.Args({'dry_run': False})
        db = self.PartialMockDb(args, None)
        self.mox.StubOutWithMock(db, 'get_current_db_version')
        self.mox.StubOutWithMock(db, 'sql_version_matrix')
        self.mox.StubOutWithMock(db, 'sql_version_resolve')
        self.mox.StubOutWithMock(db, 'psql')

        versions = ['OMERO3.0__0', 'OMERO4.4__0', 'OMERO5.0__0']
        if needupdate:
            db.get_current_db_version().AndReturn(('OMERO3.0', '0'))
            db.sql_version_matrix().AndReturn(([], versions))
            db.sql_version_resolve([], versions, versions[0]).AndReturn([
                './sql/psql/OMERO4.4__0/OMERO3.0__0.sql',
                './sql/psql/OMERO5.0__0/OMERO4.4__0.sql'
            ])
            db.psql('-f', './sql/psql/OMERO4.4__0/OMERO3.0__0.sql')
            db.psql('-f', './sql/psql/OMERO5.0__0/OMERO4.4__0.sql')
        else:
            db.get_current_db_version().AndReturn(('OMERO5.0', '0'))
            db.sql_version_matrix().AndReturn(([], versions))

        self.mox.ReplayAll()

        db.upgrade()
        self.mox.VerifyAll()
Example #3
0
File: test_db.py Project: ome/omego
    def test_get_current_db_version(self):
        db = self.PartialMockDb(None, None)
        self.mox.StubOutWithMock(db, 'psql')

        db.psql('-c', 'SELECT currentversion, currentpatch FROM dbpatch '
                'ORDER BY id DESC LIMIT 1').AndReturn('OMERO4.4|0')
        self.mox.ReplayAll()

        assert db.get_current_db_version() == ('OMERO4.4', '0')
        self.mox.VerifyAll()
Example #4
0
    def test_get_current_db_version(self):
        db = self.PartialMockDb(None, None)
        self.mox.StubOutWithMock(db, 'psql')

        db.psql(
            '-c', 'SELECT currentversion, currentpatch FROM dbpatch '
            'ORDER BY id DESC LIMIT 1').AndReturn('OMERO4.4|0')
        self.mox.ReplayAll()

        assert db.get_current_db_version() == ('OMERO4.4', '0')
        self.mox.VerifyAll()
Example #5
0
    def test_psql(self):
        db = self.PartialMockDb(None, None)
        self.mox.StubOutWithMock(db, 'get_db_args_env')
        self.mox.StubOutWithMock(External, 'run')

        psqlargs = ['-d', 'name', '-h', 'host', '-U', 'user',
                    '-w', '-A', '-t', 'arg1', 'arg2']
        db.get_db_args_env().AndReturn(self.create_db_test_params())
        External.run('psql', psqlargs, capturestd=True,
                     env={'PGPASSWORD': '******'}).AndReturn(('', ''))
        self.mox.ReplayAll()

        db.psql('arg1', 'arg2')
        self.mox.VerifyAll()
Example #6
0
    def test_init(self, sqlfile, dryrun):
        ext = self.mox.CreateMock(External)
        if sqlfile != 'notprovided':
            omerosql = 'omero.sql'
        else:
            omerosql = None
        args = self.Args({
            'omerosql': omerosql,
            'rootpass': '******',
            'dry_run': dryrun
        })
        db = self.PartialMockDb(args, ext)
        self.mox.StubOutWithMock(db, 'psql')
        self.mox.StubOutWithMock(omego.fileutils, 'timestamp_filename')
        self.mox.StubOutWithMock(os.path, 'exists')
        self.mox.StubOutWithMock(db, 'upgrade')

        if sqlfile == 'notprovided':
            omerosql = 'omero-00000000-000000-000000.sql'
            omego.fileutils.timestamp_filename('omero',
                                               'sql').AndReturn(omerosql)
        else:
            os.path.exists(omerosql).AndReturn(sqlfile == 'exists')

        if sqlfile == 'notprovided' and not dryrun:
            ext.omero_cli(
                ['db', 'script', '-f', omerosql, '', '', args.rootpass])

        if sqlfile == 'exists':
            db.upgrade()

        if sqlfile != 'missing' and not dryrun:
            db.psql('-f', omerosql)

        self.mox.ReplayAll()

        if sqlfile == 'missing':
            with pytest.raises(Stop) as excinfo:
                db.init()
            assert str(excinfo.value) == 'SQL file not found'
        else:
            db.init()
        self.mox.VerifyAll()
Example #7
0
    def test_check_connection(self, connected):
        db = self.PartialMockDb(None, None)
        self.mox.StubOutWithMock(db, 'psql')

        if connected:
            db.psql('-c', r'\conninfo')
        else:
            db.psql('-c',
                    r'\conninfo').AndRaise(RunException('', '', [], 1, '', ''))
        self.mox.ReplayAll()

        if connected:
            db.check_connection()
        else:
            with pytest.raises(Stop) as excinfo:
                db.check_connection()
            assert str(excinfo.value) == 'Database connection check failed'

        self.mox.VerifyAll()
Example #8
0
File: test_db.py Project: ome/omego
    def test_check_connection(self, connected):
        db = self.PartialMockDb(None, None)
        self.mox.StubOutWithMock(db, 'psql')

        if connected:
            db.psql('-c', r'\conninfo')
        else:
            db.psql('-c', r'\conninfo').AndRaise(
                RunException('', '', [], 1, '', ''))
        self.mox.ReplayAll()

        if connected:
            db.check_connection()
        else:
            with pytest.raises(Stop) as excinfo:
                db.check_connection()
            assert str(excinfo.value) == 'Database connection check failed'

        self.mox.VerifyAll()
Example #9
0
    def test_psql(self):
        db = self.PartialMockDb(None, None)
        self.mox.StubOutWithMock(db, 'get_db_args_env')
        self.mox.StubOutWithMock(External, 'run')

        psqlargs = [
            '-v', 'ON_ERROR_STOP=on', '-d', 'name', '-h', 'host', '-U', 'user',
            '-w', '-A', '-t', 'arg1', 'arg2'
        ]
        db.get_db_args_env().AndReturn(self.create_db_test_params())
        External.run('psql',
                     psqlargs,
                     capturestd=True,
                     env={
                         'PGPASSWORD': '******'
                     }).AndReturn(('', ''))
        self.mox.ReplayAll()

        db.psql('arg1', 'arg2')
        self.mox.VerifyAll()
Example #10
0
File: test_db.py Project: ome/omego
    def test_init(self, sqlfile, dryrun):
        ext = self.mox.CreateMock(External)
        if sqlfile != 'notprovided':
            omerosql = 'omero.sql'
        else:
            omerosql = None
        args = self.Args({'omerosql': omerosql, 'rootpass': '******',
                          'dry_run': dryrun})
        db = self.PartialMockDb(args, ext)
        self.mox.StubOutWithMock(db, 'psql')
        self.mox.StubOutWithMock(omego.fileutils, 'timestamp_filename')
        self.mox.StubOutWithMock(os.path, 'exists')
        self.mox.StubOutWithMock(db, 'upgrade')

        if sqlfile == 'notprovided':
            omerosql = 'omero-00000000-000000-000000.sql'
            omego.fileutils.timestamp_filename('omero', 'sql').AndReturn(
                omerosql)
        else:
            os.path.exists(omerosql).AndReturn(sqlfile == 'exists')

        if sqlfile == 'notprovided' and not dryrun:
            ext.omero_cli([
                'db', 'script', '-f', omerosql, '', '', args.rootpass])

        if sqlfile == 'exists':
            db.upgrade()

        if sqlfile != 'missing' and not dryrun:
            db.psql('-f', omerosql)

        self.mox.ReplayAll()

        if sqlfile == 'missing':
            with pytest.raises(Stop) as excinfo:
                db.init()
            assert str(excinfo.value) == 'SQL file not found'
        else:
            db.init()
        self.mox.VerifyAll()
Example #11
0
    def test_psql(self, dbname):
        args = self.Args({'dbhost': 'host', 'dbname': dbname,
                          'dbuser': '******', 'dbpass': '******'})

        self.mox.StubOutWithMock(os.environ, 'copy')
        self.mox.StubOutWithMock(External, 'run')

        if dbname:
            os.environ.copy().AndReturn({'PGPASSWORD': '******'})
            psqlargs = ['-d', dbname, '-h', 'host', '-U', 'user',
                        '-w', '-A', '-t', 'arg1', 'arg2']
            External.run('psql', psqlargs, capturestd=True,
                         env={'PGPASSWORD': '******'}).AndReturn(('', ''))
        self.mox.ReplayAll()

        db = self.PartialMockDb(args, None)
        if dbname:
            db.psql('arg1', 'arg2')
        else:
            with pytest.raises(Exception) as excinfo:
                db.psql('arg1', 'arg2')
            assert str(excinfo.value) == 'Database name required'

        self.mox.VerifyAll()