# Path: jubakit/loader/postgresql.py
# class PostgreSQLoader(BaseLoader):
#   """
#   Loader to process columns of PostgreSQL.
# 
#   This loader that load data from PostgreSQL's table as below.
#   We access the "test" table of the "test" database in the below example.
# 
#   Example:
#     from jubakit.loader.postgresql import PostgreSQLoader
#     from jubakit.loader.postgresql import PostgreSQLAuthHandler
#     
#     auth = PostgreSQLAuthHandler(dbname='test', user='postgres', password='postgres', host='localhost', port='5432')
#     
#     loader = PostgreSQLoader(auth, table='test')
#     for row in loader:
#       print(row)
#     
#     # {'id': 1, 'num': 100, 'data': 'abcdef'}
#     # {'id': 2, 'num': 200, 'data': 'ghijkl'}
#     # {'id': 3, 'num': 300, 'data': 'mnopqr'}
#   """
# 
#   def __init__(self, auth, table, **kwargs):
#     self.auth = auth
#     self.table = table
#     self.kwargs = kwargs
# 
#   def rows(self):
#     with connect(self.auth.get()) as connection:
#       with connection.cursor(cursor_factory=DictCursor) as cursor:
#         cursor.execute(
#           sql.SQL("SELECT * FROM {}")
#           .format(sql.Identifier(self.table)))
#         column_names = [column.name for column in cursor.description]
# 
#         for row in cursor:
#           yield dict(zip(column_names, row))
# 
# Path: jubakit/loader/postgresql.py
# class PostgreSQLAuthHandler(object):
#   """
#   Handles authentication required to access PostgreSQL.
#   """
# 
#   def __init__(self, **kwargs):
#     """
#     Authentication information must be specified as follows:
# 
#     >>> PostgreSQLAuth(
#     ...   user='XXXXXXXXXXXXXXXXXXXX',
#     ...   password='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
#     ...   host='XXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
#     ...   port='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
#     ... )
# 
#     Any other connection parameter supported by this loader can be passed as a keyword.
#     The complete list of the supported parameters are contained the PostgreSQL documentation.
#     (https://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-PARAMKEYWORDS)
#     """
#     auth = ''
#     auth_informations = (
#       'host', 'hostaddr',
#       'port',
#       'dbname',
#       'user',
#       'password', 'passfile',
#       'connect_timeout',
#       'client_encoding',
#       'options',
#       'application_name',
#       'fallback_application_name',
#       'keepalives', 'keepalives_idle', 'keepalives_interval', 'keepalives_count',
#       'tty',
#       'sslmode', 'requiressl', 'sslcompression', 'sslcert', 'sslkey', 'sslrootcert', 'sslcrl',
#       'requirepeer',
#       'krbsrvname',
#       'gsslib',
#       'service',
#       'target_session_attrs')
#     for auth_key in auth_informations:
#       if auth_key in kwargs:
#         auth = auth + '%s=%s ' % (auth_key, kwargs[auth_key])
#     self.auth = auth
# 
#   def get(self):
#     return self.auth


from unittest import TestCase
from jubakit.loader.postgresql import PostgreSQLoader
from jubakit.loader.postgresql import PostgreSQLAuthHandler
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
import psycopg2
  def setUp(self):
    print("setUp")
    connection = psycopg2.connect("host=localhost port=5432 user=postgres password=postgres")
    connection.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
    cursor = connection.cursor()
    cursor.execute("DROP DATABASE IF EXISTS test;")
    cursor.execute("CREATE DATABASE test;")

    cursor.execute("DROP TABLE IF EXISTS test;")
    cursor.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")
    cursor.execute("INSERT INTO test (num, data) VALUES (%s, %s)", (100, "abcdef"))
    cursor.execute("INSERT INTO test (num, data) VALUES (%s, %s)", (200, "ghijkl"))
    cursor.execute("INSERT INTO test (num, data) VALUES (%s, %s)", (300, "mnopqr"))

    connection.commit()
    connection.close()

    connection = psycopg2.connect("dbname=test host=localhost port=5432 user=postgres password=postgres")
    cursor = connection.cursor()

    cursor.execute("DROP TABLE IF EXISTS test2;")
    cursor.execute("CREATE TABLE test2 (id serial PRIMARY KEY, num integer, data varchar);")
    cursor.execute("INSERT INTO test2 (num, data) VALUES (%s, %s)", (1000, "st"))
    cursor.execute("INSERT INTO test2 (num, data) VALUES (%s, %s)", (2000, "uv"))
    cursor.execute("INSERT INTO test2 (num, data) VALUES (%s, %s)", (3000, "wx"))

    connection.commit()
    connection.close()

  def test_simple(self):

# Path: jubakit/loader/postgresql.py
# class PostgreSQLoader(BaseLoader):
#   """
#   Loader to process columns of PostgreSQL.
# 
#   This loader that load data from PostgreSQL's table as below.
#   We access the "test" table of the "test" database in the below example.
# 
#   Example:
#     from jubakit.loader.postgresql import PostgreSQLoader
#     from jubakit.loader.postgresql import PostgreSQLAuthHandler
#     
#     auth = PostgreSQLAuthHandler(dbname='test', user='postgres', password='postgres', host='localhost', port='5432')
#     
#     loader = PostgreSQLoader(auth, table='test')
#     for row in loader:
#       print(row)
#     
#     # {'id': 1, 'num': 100, 'data': 'abcdef'}
#     # {'id': 2, 'num': 200, 'data': 'ghijkl'}
#     # {'id': 3, 'num': 300, 'data': 'mnopqr'}
#   """
# 
#   def __init__(self, auth, table, **kwargs):
#     self.auth = auth
#     self.table = table
#     self.kwargs = kwargs
# 
#   def rows(self):
#     with connect(self.auth.get()) as connection:
#       with connection.cursor(cursor_factory=DictCursor) as cursor:
#         cursor.execute(
#           sql.SQL("SELECT * FROM {}")
#           .format(sql.Identifier(self.table)))
#         column_names = [column.name for column in cursor.description]
# 
#         for row in cursor:
#           yield dict(zip(column_names, row))
# 
# Path: jubakit/loader/postgresql.py
# class PostgreSQLAuthHandler(object):
#   """
#   Handles authentication required to access PostgreSQL.
#   """
# 
#   def __init__(self, **kwargs):
#     """
#     Authentication information must be specified as follows:
# 
#     >>> PostgreSQLAuth(
#     ...   user='XXXXXXXXXXXXXXXXXXXX',
#     ...   password='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
#     ...   host='XXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
#     ...   port='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
#     ... )
# 
#     Any other connection parameter supported by this loader can be passed as a keyword.
#     The complete list of the supported parameters are contained the PostgreSQL documentation.
#     (https://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-PARAMKEYWORDS)
#     """
#     auth = ''
#     auth_informations = (
#       'host', 'hostaddr',
#       'port',
#       'dbname',
#       'user',
#       'password', 'passfile',
#       'connect_timeout',
#       'client_encoding',
#       'options',
#       'application_name',
#       'fallback_application_name',
#       'keepalives', 'keepalives_idle', 'keepalives_interval', 'keepalives_count',
#       'tty',
#       'sslmode', 'requiressl', 'sslcompression', 'sslcert', 'sslkey', 'sslrootcert', 'sslcrl',
#       'requirepeer',
#       'krbsrvname',
#       'gsslib',
#       'service',
#       'target_session_attrs')
#     for auth_key in auth_informations:
#       if auth_key in kwargs:
#         auth = auth + '%s=%s ' % (auth_key, kwargs[auth_key])
#     self.auth = auth
# 
#   def get(self):
#     return self.auth


# Path: jubakit/test/loader/test_postgresql.py
from unittest import TestCase
from jubakit.loader.postgresql import PostgreSQLoader
from jubakit.loader.postgresql import PostgreSQLAuthHandler
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
import psycopg2

  def setUp(self):
    print("setUp")
    connection = psycopg2.connect("host=localhost port=5432 user=postgres password=postgres")
    connection.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
    cursor = connection.cursor()
    cursor.execute("DROP DATABASE IF EXISTS test;")
    cursor.execute("CREATE DATABASE test;")

    cursor.execute("DROP TABLE IF EXISTS test;")
    cursor.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")
    cursor.execute("INSERT INTO test (num, data) VALUES (%s, %s)", (100, "abcdef"))
    cursor.execute("INSERT INTO test (num, data) VALUES (%s, %s)", (200, "ghijkl"))
    cursor.execute("INSERT INTO test (num, data) VALUES (%s, %s)", (300, "mnopqr"))

    connection.commit()
    connection.close()

    connection = psycopg2.connect("dbname=test host=localhost port=5432 user=postgres password=postgres")
    cursor = connection.cursor()

    cursor.execute("DROP TABLE IF EXISTS test2;")
    cursor.execute("CREATE TABLE test2 (id serial PRIMARY KEY, num integer, data varchar);")
    cursor.execute("INSERT INTO test2 (num, data) VALUES (%s, %s)", (1000, "st"))
    cursor.execute("INSERT INTO test2 (num, data) VALUES (%s, %s)", (2000, "uv"))
    cursor.execute("INSERT INTO test2 (num, data) VALUES (%s, %s)", (3000, "wx"))

    connection.commit()
    connection.close()

  def test_simple(self):

    loader = PostgreSQLoader(self.auth, table='test')