def test_connect_to_db(self):
     conn = esg_postgres.connect_to_db("postgres", "postgres")
     conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
     cur = conn.cursor()
     try:
         cur.execute("CREATE DATABASE unittestdb;")
     except Exception, error:
         print "error:", error
    def test_list_users(self):
        user_list = esg_postgres.list_users(user_name="postgres",
                                            db_name="postgres")
        self.assertIsNotNone(user_list)

        conn = esg_postgres.connect_to_db("postgres", "postgres")
        user_list = esg_postgres.list_users(conn=conn)
        self.assertIsNotNone(user_list)
    def test_add_schema_from_file(self):
        user_list = esg_postgres.list_users(user_name="postgres",
                                            db_name="postgres")
        conn = esg_postgres.connect_to_db("postgres", "postgres")
        conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
        cur = conn.cursor()
        if "dbsuper" not in user_list:
            cur.execute(
                "CREATE USER dbsuper with CREATEROLE superuser PASSWORD 'password';"
            )
        if "esgcet" not in user_list:
            cur.execute("CREATE USER esgcet PASSWORD 'password';")
            cur.execute("CREATE DATABASE esgcet;")
            cur.execute("GRANT dbsuper TO esgcet;")
        conn.commit()
        conn.close()

        conn2 = esg_postgres.connect_to_db("esgcet",
                                           db_name="esgcet",
                                           password='******')
        cur2 = conn2.cursor()
        try:
            cur2.execute(
                "SELECT table_schema,table_name FROM information_schema.tables ORDER BY table_schema,table_name;"
            )
            tables = cur2.fetchall()
            before_tables_list = [
                table[1] for table in tables if table[0] == 'public'
            ]
            print "tables before:", before_tables_list
            print "tables before length:", len(before_tables_list)

            cur2.execute(open("sqldata/esgf_esgcet.sql", "r").read())

            cur2.execute(
                "SELECT table_schema,table_name FROM information_schema.tables ORDER BY table_schema,table_name;"
            )
            tables = cur2.fetchall()
            after_tables_list = [
                table[1] for table in tables if table[0] == 'public'
            ]
            print "tables after:", after_tables_list
            print "tables after length:", len(after_tables_list)
        except Exception, error:
            print 'error:', error
    def test_list_schemas(self):
        output = esg_postgres.postgres_list_db_schemas(user_name="postgres",
                                                       db_name="postgres")
        self.assertIsNotNone(output)

        conn = esg_postgres.connect_to_db("postgres", "postgres")
        schemas_list = esg_postgres.postgres_list_db_schemas(conn=conn)
        print "schemas_list:", schemas_list
        self.assertIsNotNone(schemas_list)
 def test_add_user_to_db(self):
     conn = esg_postgres.connect_to_db("postgres", "postgres")
     cur = conn.cursor()
     try:
         cur.execute(
             "CREATE USER testuser with CREATEROLE superuser PASSWORD 'password';"
         )
     except Exception, error:
         print "error:", error
 def test_tear_down(self):
     print "\n*******************************"
     print "Tearing down ESGF Postgres Test Fixture"
     print "******************************* \n"
     conn = esg_postgres.connect_to_db("postgres", "postgres")
     users_list = esg_postgres.list_users(conn=conn)
     conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
     cur = conn.cursor()
     cur.execute("DROP USER IF EXISTS testuser;")
     # if "testuser" in users_list:
     #     cur.execute("DROP USER testuser;")
     if "dbsuper" in users_list:
         cur.execute("DROP USER dbsuper;")
     if "esgcet" in users_list:
         cur.execute("DROP USER esgcet;")
     cur.execute("DROP DATABASE IF EXISTS unittestdb;")
     cur.execute("DROP DATABASE IF EXISTS esgcet;")
     conn.close()
     purge_postgres()
 def tearDownClass(cls):
     print "\n*******************************"
     print "Tearing down ESGF Postgres Test Fixture"
     print "******************************* \n"
     conn = esg_postgres.connect_to_db("postgres", "postgres")
     #Tests have already been cleaned up with self.test_setup()
     if not conn:
         return
     users_list = esg_postgres.list_users(conn=conn)
     conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
     cur = conn.cursor()
     cur.execute("DROP USER IF EXISTS testuser;")
     # if "testuser" in users_list:
     #     cur.execute("DROP USER testuser;")
     if "dbsuper" in users_list:
         cur.execute("DROP USER dbsuper;")
     if "esgcet" in users_list:
         cur.execute("DROP USER esgcet;")
     cur.execute("DROP DATABASE IF EXISTS unittestdb;")
     cur.execute("DROP DATABASE IF EXISTS esgcet;")
     conn.close()
     purge_postgres()
    def test_add_user_to_db(self):
        conn = esg_postgres.connect_to_db("postgres", "postgres")
        cur = conn.cursor()
        try:
            cur.execute(
                "CREATE USER testuser with CREATEROLE superuser PASSWORD 'password';"
            )
        except Exception, error:
            print "error:", error
        conn.commit()
        conn.close()
        cur.close()

        conn2 = esg_postgres.connect_to_db("testuser",
                                           db_name="postgres",
                                           password='******')
        cur2 = conn2.cursor()
        cur2.execute("""SELECT usename FROM pg_user;""")
        users = cur2.fetchall()
        print "\nUsers: \n"
        print users
        self.assertIsNotNone(users)
        conn2.close()
        cur2.close()
        # self.test_tear_down()

    def test_list_users(self):
        user_list = esg_postgres.list_users(user_name="postgres",
                                            db_name="postgres")
        self.assertIsNotNone(user_list)