Exemplo n.º 1
0
class TestPyEmbedPg(unittest.TestCase):
    def setUp(self):
        self.port = 15433
        self.embedpg = PyEmbedPg('9.4.0')
        self.postgres = self.embedpg.start(self.port)
        self.postgres.create_user('scott', 'tiger')
        self.postgres.create_database('testdb', 'scott')

    def test_simple_run(self):
        # Postgres is initialized, now run some queries
        with psycopg2.connect(database='postgres',
                              user='******',
                              password='******',
                              host='localhost',
                              port=self.port) as conn:
            with conn.cursor() as cursor:
                cursor.execute(
                    'CREATE TABLE employee(name VARCHAR(32), age INT)')
                cursor.execute("INSERT INTO employee VALUES ('John', 32)")
                cursor.execute("INSERT INTO employee VALUES ('Mary', 22)")
                cursor.execute('SELECT * FROM employee ORDER BY age')
                assert cursor.fetchall() == [('Mary', 22), ('John', 32)]

        # Test that the version is installed locally
        assert self.embedpg.get_latest_local_version() is not None

    def tearDown(self):
        self.postgres.shutdown()
Exemplo n.º 2
0
    def __init__(self, *args, **kw):
        if self.db_name is None:
            self.db_name = 'testdb%s' % "%012x" % random.getrandbits(48)

        postgres = PyEmbedPg(self.pg_version)
        runner = postgres.start(self.db_port_range)
        try:
            runner.create_user(self.db_user, self.db_pass)
        except psycopg2.errors.DuplicateObject:
            pass

        self.db_port = runner.running_port

        self.dsn = self.get_dsn(self.db_name)
        self.postgres = runner
    def test_simple_run(self):
        pg = PyEmbedPg('9.4.0')
        with pg.start(self.port) as postgres:
            postgres.create_user('scott', 'tiger')
            postgres.create_database('testdb', 'scott')

            # Postgres is initialized, now run some queries
            with psycopg2.connect(database='postgres', user='******', password='******', host='localhost', port=self.port) as conn:
                with conn.cursor() as cursor:
                    cursor.execute('CREATE TABLE employee(name VARCHAR(32), age INT)')
                    cursor.execute("INSERT INTO employee VALUES ('John', 32)")
                    cursor.execute("INSERT INTO employee VALUES ('Mary', 22)")
                    cursor.execute('SELECT * FROM employee ORDER BY age')
                    assert cursor.fetchall() == [('Mary', 22), ('John', 32)]

        # Test that the version is installed locally
        assert pg.get_latest_local_version() is not None