Example #1
0
    def test_disconnected(self):
        db_name = 'testing'

        conn = self.create_conn()
        conn.set_isolation_level(0)
        cursor = conn.cursor()
        cursor.execute(
            "SELECT pg_terminate_backend(pid) " + "FROM pg_stat_activity " +
            "WHERE usename=%(user)s" + "AND pid <> pg_backend_pid()",
            {'user': self.user})

        try:
            cursor.execute('DROP DATABASE %s' % db_name)
            cursor.execute('DROP ROLE %s' % db_name)
            conn.commit()
        except:
            pass

        cursor.close()
        conn.close()

        with self.app.app_context():
            cdb_name = canonicalize_db_name(db_name)
            manager = plans.get_manager_by_plan('shared')

            try:
                manager.create_instance(cdb_name)
                assert False
            except psycopg2.OperationalError:
                assert True

            instance = manager.create_instance(cdb_name)
            manager.delete_instance(instance)
Example #2
0
    def test_disconnected(self):
        db_name = 'testing'

        conn = self.create_conn()
        conn.set_isolation_level(0)
        cursor = conn.cursor()
        cursor.execute(
            "SELECT pg_terminate_backend(pid) " +
            "FROM pg_stat_activity " +
            "WHERE usename=%(user)s" +
            "AND pid <> pg_backend_pid()",
            {'user': self.user})

        try:
            cursor.execute('DROP DATABASE %s' % db_name)
            cursor.execute('DROP ROLE %s' % db_name)
            conn.commit()
        except:
            pass

        cursor.close()
        conn.close()

        with self.app.app_context():
            cdb_name = canonicalize_db_name(db_name)
            manager = plans.get_manager_by_plan('shared')

            try:
                manager.create_instance(cdb_name)
                assert False
            except psycopg2.OperationalError:
                assert True

            instance = manager.create_instance(cdb_name)
            manager.delete_instance(instance)
Example #3
0
    def test_instance(self):
        with self.app.app_context():
            db_name = canonicalize_db_name('testing')
            manager = plans.get_manager_by_plan('shared')

            con = self.create_conn()
            con.set_isolation_level(0)
            cursor = con.cursor()
            try:
                cursor.execute('DROP DATABASE testing')
                cursor.execute('DROP ROLE testing')
                con.commit()
            except:
                pass
            finally:
                cursor.close()
                con.close()

            instance = manager.create_instance(db_name)
            self.database = db_name

            try:
                self.user, self.password = instance.create_user('first')

                first_con = self.create_conn()
                cursor = first_con.cursor()

                try:
                    cursor.execute('CREATE TABLE test(name text)')
                    first_con.commit()
                    cursor.execute('INSERT INTO test VALUES (\'test\')')
                    first_con.commit()

                    self.user, self.password = instance.create_user('second')
                    second_con = self.create_conn()
                    second_cursor = second_con.cursor()

                    try:
                        second_cursor.execute('SELECT name FROM test')
                        second_cursor.fetchall()
                    except:
                        second_con.rollback()
                        raise
                    finally:
                        second_cursor.close()
                        second_con.close()
                except:
                    first_con.rollback()
                    raise
                finally:
                    cursor.execute('DROP TABLE test')
                    first_con.commit()
                    cursor.close()
                    first_con.close()
            except:
                raise
            finally:
                instance.drop_user('first')
                instance.drop_user('second')
Example #4
0
    def test_instance(self):
        with self.app.app_context():
            db_name = canonicalize_db_name('testing')
            manager = plans.get_manager_by_plan('shared')

            con = self.create_conn()
            con.set_isolation_level(0)
            cursor = con.cursor()
            try:
                cursor.execute('DROP DATABASE testing')
                cursor.execute('DROP ROLE testing')
                con.commit()
            except:
                pass
            finally:
                cursor.close()
                con.close()

            instance = manager.create_instance(db_name)
            self.database = db_name

            try:
                self.user, self.password = instance.create_user('first')

                first_con = self.create_conn()
                cursor = first_con.cursor()

                try:
                    cursor.execute('CREATE TABLE test(name text)')
                    first_con.commit()
                    cursor.execute('INSERT INTO test VALUES (\'test\')')
                    first_con.commit()

                    self.user, self.password = instance.create_user('second')
                    second_con = self.create_conn()
                    second_cursor = second_con.cursor()

                    try:
                        second_cursor.execute('SELECT name FROM test')
                        second_cursor.fetchall()
                    except:
                        second_con.rollback()
                        raise
                    finally:
                        second_cursor.close()
                        second_con.close()
                except:
                    first_con.rollback()
                    raise
                finally:
                    cursor.execute('DROP TABLE test')
                    first_con.commit()
                    cursor.close()
                    first_con.close()
            except:
                raise
            finally:
                instance.drop_user('first')
                instance.drop_user('second')