Пример #1
0
 def test_connect_failure_raises_interface_error(self):
     datastore = PostgresDatastore(
         dbname="eventsourcing",
         host="127.0.0.1",
         port="9876543210",  # bad port
         user="******",
         password="******",
     )
     with self.assertRaises(InterfaceError):
         datastore.transaction(commit=True)
Пример #2
0
def drop_postgres_table(datastore: PostgresDatastore, table_name):
    statement = f"DROP TABLE {table_name};"
    try:
        with datastore.transaction(commit=True) as curs:
            curs.execute(statement)
    except PersistenceError:
        pass
Пример #3
0
    def test_connection_of_transaction_not_used_as_context_manager_also_goes_idle(
            self):
        datastore = PostgresDatastore(
            dbname="eventsourcing",
            host="127.0.0.1",
            port="5432",
            user="******",
            password="******",
        )

        # Get a transaction.
        transaction = datastore.transaction(commit=False)

        # Check connection is not idle.
        conn = transaction.c
        self.assertFalse(conn.is_idle.is_set())

        # Delete the transaction context manager before entering.
        print(
            "Testing transaction not used as context manager, expecting exception..."
        )
        del transaction

        # Check connection is idle after garbage collection.
        self.assertTrue(conn.is_idle.wait(timeout=0.1))
Пример #4
0
    def test_timer_closes_connection(self):
        datastore = PostgresDatastore(
            dbname="eventsourcing",
            host="127.0.0.1",
            port="5432",
            user="******",
            password="******",
            conn_max_age=0,
        )

        # Check connection is closed after using transaction.
        transaction = datastore.transaction(commit=False)
        with transaction as conn:
            with conn.cursor() as c:
                c.execute("SELECT 1")
                self.assertEqual(c.fetchall(), [[1]])

        self.assertTrue(transaction.c.is_closing.wait(timeout=0.5))
        for _ in range(1000):
            if transaction.c.is_closed:
                break
            else:
                sleep(0.0001)
        else:
            self.fail("Connection is not closed")

        with self.assertRaises(psycopg2.InterfaceError) as cm:
            transaction.c.cursor()
        self.assertEqual(cm.exception.args[0], "connection already closed")

        # Check closed connection can be recreated and also closed.
        transaction = datastore.transaction(commit=False)
        with transaction as conn:
            with conn.cursor() as c:
                c.execute("SELECT 1")
                self.assertEqual(c.fetchall(), [[1]])

        self.assertTrue(transaction.c.is_closing.wait(timeout=0.5))
        for _ in range(1000):
            if transaction.c.is_closed:
                break
            else:
                sleep(0.0001)
        else:
            self.fail("Connection is not closed")
Пример #5
0
def drop_postgres_table(datastore: PostgresDatastore, table_name):
    try:
        with datastore.transaction() as c:
            statement = f"DROP TABLE {table_name};"
            print("Executing statement:", statement)
            c.execute(statement)
            print("Executed statement:", statement)
    except psycopg2.errors.lookup(UNDEFINED_TABLE):
        print("Failed to execute statement:", statement)
        pass  # print(f"Table does not exist: {table_name}")
    except Exception:
        print("Strange error:", statement)
Пример #6
0
    def test_close_connection(self):
        datastore = PostgresDatastore(
            dbname="eventsourcing",
            host="127.0.0.1",
            port="5432",
            user="******",
            password="******",
        )
        # Try closing without first creating connection.
        datastore.close_connection()

        # Create a connection.
        with datastore.transaction(commit=False) as conn:
            with conn.cursor() as c:
                c.execute("SELECT 1")
                self.assertEqual(c.fetchall(), [[1]])

        # Try closing after creating connection.
        datastore.close_connection()
Пример #7
0
    def test_transaction(self):
        datastore = PostgresDatastore(
            dbname="eventsourcing",
            host="127.0.0.1",
            port="5432",
            user="******",
            password="******",
        )

        # Get a transaction.
        transaction = datastore.transaction(commit=False)

        # Check connection is not idle.
        self.assertFalse(transaction.c.is_idle.is_set())

        # Check transaction gives database cursor when used as context manager.
        with transaction as conn:
            with conn.cursor() as c:
                c.execute("SELECT 1")
                self.assertEqual(c.fetchall(), [[1]])

        # Check connection is idle after context manager has exited.
        self.assertTrue(transaction.c.is_idle.wait(timeout=0.1))
Пример #8
0
def drop_postgres_table(datastore: PostgresDatastore, table_name):
    try:
        with datastore.transaction() as c:
            c.execute(f"DROP TABLE {table_name};")
    except psycopg2.errors.lookup(UNDEFINED_TABLE):
        pass  # print(f"Table does not exist: {table_name}")
Пример #9
0
    def test_pre_ping(self):
        datastore = PostgresDatastore(
            dbname="eventsourcing",
            host="127.0.0.1",
            port="5432",
            user="******",
            password="******",
            pre_ping=True,
        )

        # Create a connection.
        transaction = datastore.transaction(commit=False)
        pg_conn = transaction.c.c
        self.assertEqual(pg_conn, transaction.c.c)

        # Check the connection works.
        with transaction as conn:
            with conn.cursor() as c:
                c.execute("SELECT 1")
                self.assertEqual(c.fetchall(), [[1]])

        # Close all connections via separate connection.
        pg_close_all_connections()

        # Check the connection doesn't think it's closed.
        self.assertFalse(transaction.c.is_closed)

        # Check we can get a new connection that works.
        transaction = datastore.transaction(commit=False)
        with transaction as conn:
            with conn.cursor() as c:
                c.execute("SELECT 1")
                self.assertEqual(c.fetchall(), [[1]])

        # Check it's actually a different connection.
        self.assertNotEqual(pg_conn, transaction.c.c)

        # Check this doesn't work if we don't use pre_ping.
        datastore = PostgresDatastore(
            dbname="eventsourcing",
            host="127.0.0.1",
            port="5432",
            user="******",
            password="******",
            pre_ping=False,
        )

        # Create a connection.
        transaction = datastore.transaction(commit=False)
        pg_conn = transaction.c.c
        self.assertEqual(pg_conn, transaction.c.c)

        # Check the connection works.
        with transaction as conn:
            with conn.cursor() as c:
                c.execute("SELECT 1")
                self.assertEqual(c.fetchall(), [[1]])

        # Close all connections via separate connection.
        pg_close_all_connections()

        # Check the connection doesn't think it's closed.
        self.assertFalse(transaction.c.is_closed)

        # Get a stale connection and check it doesn't work.
        transaction = datastore.transaction(commit=False)

        # Check it's the same connection.
        self.assertEqual(pg_conn, transaction.c.c)
        with self.assertRaises(InterfaceError):
            with transaction as conn:
                with conn.cursor() as c:
                    c.execute("SELECT 1")