Example #1
0
def test_postgresql():
    """
    Tests the PostgreSQLStore on a locally-hosted PostgreSQL database
    cluster, version 7.4 or later.  To run this test, you must have:

    - The 'psycopg' python module (version 1.1) installed

    - PostgreSQL running locally

    - An 'openid_test' user account in your database cluster, which
      you can create by running 'createuser -Ad openid_test' as the
      'postgres' user

    - Trust auth for the 'openid_test' account, which you can activate
      by adding the following line to your pg_hba.conf file:

      local all openid_test trust

    This test connects to the database cluster three times:

    - To the 'template1' database, to create the test database

    - To the test database, to run the store tests

    - To the 'template1' database once more, to drop the test database
    """
    from openid.store import sqlstore
    try:
        import psycopg
    except ImportError:
        pass
    else:
        db_name = getTmpDbName()
        db_user = '******'

        # Connect once to create the database; reconnect to access the
        # new database.
        conn_create = psycopg.connect(database = 'template1', user = db_user,
                                      host = db_host)
        conn_create.autocommit()

        # Create the test database.
        cursor = conn_create.cursor()
        cursor.execute('CREATE DATABASE %s;' % (db_name,))
        conn_create.close()

        # Connect to the test database.
        conn_test = psycopg.connect(database = db_name, user = db_user,
                                    host = db_host)

        # OK, we're in the right environment. Create the store
        # instance and create the tables.
        store = sqlstore.PostgreSQLStore(conn_test)
        store.createTables()

        # At last, we get to run the test.
        testStore(store)

        # Disconnect.
        conn_test.close()

        # It takes a little time for the close() call above to take
        # effect, so we'll wait for a second before trying to remove
        # the database.  (Maybe this is because we're using a UNIX
        # socket to connect to postgres rather than TCP?)
        import time
        time.sleep(1)

        # Remove the database now that the test is over.
        conn_remove = psycopg.connect(database = 'template1', user = db_user,
                                      host = db_host)
        conn_remove.autocommit()

        cursor = conn_remove.cursor()
        cursor.execute('DROP DATABASE %s;' % (db_name,))
        conn_remove.close()
Example #2
0
def test_postgresql():
    """
    Tests the PostgreSQLStore on a locally-hosted PostgreSQL database
    cluster, version 7.4 or later.  To run this test, you must have:

    - The 'psycopg2' python module (version 1.1) installed

    - PostgreSQL running locally

    - An 'openid_test' user account in your database cluster, which
      you can create by running 'createuser -Ad openid_test' as the
      'postgres' user

    - Trust auth for the 'openid_test' account, which you can activate
      by adding the following line to your pg_hba.conf file:

      local all openid_test trust

    This test connects to the database cluster three times:

    - To the 'template1' database, to create the test database

    - To the test database, to run the store tests

    - To the 'template1' database once more, to drop the test database
    """
    from openid.store import sqlstore

    try:
        import psycopg2
    except ImportError:
        raise unittest.SkipTest("Skipping PostgreSQL store tests. "
                                "Could not import psycopg2.")
    else:
        db_name = getTmpDbName()
        db_host = os.environ.get("TEST_POSTGRES_HOST", "localhost")
        db_user = os.environ.get("TEST_POSTGRES_USER", "openid_test")
        db_passwd = os.environ.get("TEST_POSTGRES_PASSWORD", "password")

        # Connect once to create the database; reconnect to access the
        # new database.
        try:
            conn_create = psycopg2.connect(database="template1",
                                           user=db_user,
                                           host=db_host,
                                           password=db_passwd)
        except psycopg2.OperationalError as why:
            raise unittest.SkipTest("Skipping PostgreSQL store test: %s" % why)

        conn_create.autocommit = True

        # Create the test database.
        cursor = conn_create.cursor()
        cursor.execute("CREATE DATABASE %s;" % (db_name, ))
        conn_create.close()

        # Connect to the test database.
        conn_test = psycopg2.connect(database=db_name,
                                     user=db_user,
                                     host=db_host)

        # OK, we're in the right environment. Create the store
        # instance and create the tables.
        store = sqlstore.PostgreSQLStore(conn_test)
        store.createTables()

        # At last, we get to run the test.
        testStore(store)

        # Disconnect.
        conn_test.close()

        # It takes a little time for the close() call above to take
        # effect, so we'll wait for a second before trying to remove
        # the database.  (Maybe this is because we're using a UNIX
        # socket to connect to postgres rather than TCP?)
        import time

        time.sleep(1)

        # Remove the database now that the test is over.
        conn_remove = psycopg2.connect(database="template1",
                                       user=db_user,
                                       host=db_host)
        conn_remove.autocommit = True

        cursor = conn_remove.cursor()
        cursor.execute("DROP DATABASE %s;" % (db_name, ))
        conn_remove.close()