# Path: pyrseas/testutils.py
# class DatabaseToMapTestCase(PyrseasTestCase):
#     """Base class for "database to map" test cases"""
# 
#     superuser = False
# 
#     def to_map(self, stmts, config={}, schemas=[], tables=[], no_owner=True,
#                no_privs=True, superuser=False, multiple_files=False):
#         """Execute statements and return a database map.
# 
#         :param stmts: list of SQL statements to execute
#         :param config: dictionary of configuration information
#         :param schemas: list of schemas to map
#         :param tables: list of tables to map
#         :param no_owner: exclude object owner information
#         :param no_privs: exclude privilege information
#         :param superuser: must be superuser to run
#         :param multiple_files: emulate --multiple_files option
#         :return: possibly trimmed map of database
#         """
#         if (self.superuser or superuser) and not self.db.is_superuser():
#             self.skipTest("Must be a superuser to run this test")
#         for stmt in stmts:
#             self.db.execute(stmt)
#         self.db.conn.commit()
#         if multiple_files:
#             self.cfg.merge({'files': {'metadata_path': os.path.join(
#                             TEST_DIR, self.cfg['repository']['metadata'])}})
#         if 'datacopy' in config:
#             self.cfg.merge({'files': {'data_path': os.path.join(
#                             TEST_DIR, self.cfg['repository']['data'])}})
#         self.config_options(schemas=schemas, tables=tables, no_owner=no_owner,
#                             no_privs=no_privs, multiple_files=multiple_files)
#         self.cfg.merge(config)
#         return self.database().to_map()
# 
#     def yaml_load(self, filename, subdir=None):
#         """Read a file in the metadata_path and process it with YAML load
# 
#         :param filename: name of the file
#         :param subdir: name of a subdirectory where the file is located
#         :return: YAML dictionary
#         """
#         with open(os.path.join(self.cfg['files']['metadata_path'],
#                                subdir or '', filename), 'r') as f:
#             inmap = f.read()
#         return yaml.safe_load(inmap)
# 
#     def remove_tempfiles(self):
#         remove_temp_files(TEST_DIR)
# 
#     @staticmethod
#     def sort_privileges(data):
#         try:
#             sorted_privlist = []
#             for sortedItem in sorted([list(i.keys())[0]
#                                       for i in data['privileges']]):
#                 sorted_privlist.append(
#                     [item for item in data['privileges']
#                      if list(item.keys())[0] == sortedItem][0])
#             data['privileges'] = sorted_privlist
#         finally:
#             return data
# 
# Path: pyrseas/testutils.py
# class InputMapToSqlTestCase(PyrseasTestCase):
#     """Base class for "input map to SQL" test cases"""
# 
#     superuser = False
# 
#     def to_sql(self, inmap, stmts=None, config={}, superuser=False, schemas=[],
#                revert=False, quote_reserved=False):
#         """Execute statements and compare database to input map.
# 
#         :param inmap: dictionary defining target database
#         :param stmts: list of SQL database setup statements
#         :param config: dictionary of configuration information
#         :param superuser: indicates test requires superuser privilege
#         :param schemas: list of schemas to diff
#         :param revert: generate statements to back out changes
#         :param quote_reserved: fetch reserved words
#         :return: list of SQL statements
#         """
#         if (self.superuser or superuser) and not self.db.is_superuser():
#             self.skipTest("Must be a superuser to run this test")
#         if stmts:
#             for stmt in stmts:
#                 self.db.execute(stmt)
#             self.db.conn.commit()
# 
#         if 'datacopy' in config:
#             self.cfg.merge({'files': {'data_path': os.path.join(
#                             TEST_DIR, self.cfg['repository']['data'])}})
#         self.config_options(schemas=schemas, revert=revert),
#         self.cfg.merge(config)
#         return self.database().diff_map(inmap, quote_reserved=quote_reserved)
# 
#     def std_map(self, plpgsql_installed=False):
#         "Return a standard schema map for the default database"
#         base = {'schema sd': {
#                 'owner': self.db.user,
#                 'privileges': []}}
#         base.update({'extension plpgsql': {
#             'schema': 'pg_catalog', 'owner': PG_OWNER,
#             'description': "PL/pgSQL procedural language"}})
#         return base


from pyrseas.testutils import DatabaseToMapTestCase
from pyrseas.testutils import InputMapToSqlTestCase
"""Test object privileges

The majority of other tests exclude access privileges.  These
explicitly request it.  In addition, the roles 'user1' and 'user2'
are created if they don't exist.
"""


CREATE_TABLE = "CREATE TABLE t1 (c1 integer, c2 text)"
SOURCE1 = "SELECT 'dummy'::text"
CREATE_FUNC = "CREATE FUNCTION f1() RETURNS text LANGUAGE sql IMMUTABLE AS " \
    "$_$%s$_$" % SOURCE1
CREATE_FDW = "CREATE FOREIGN DATA WRAPPER fdw1"
CREATE_FS = "CREATE SERVER fs1 FOREIGN DATA WRAPPER fdw1"
GRANT_SELECT = "GRANT SELECT ON TABLE sd.t1 TO %s"
GRANT_INSUPD = "GRANT INSERT, UPDATE ON TABLE sd.t1 TO %s"


def check_extra_users(db):
    "Check existence of extra test users"
    for user in ['user1', 'user2']:
        row = db.fetchone("SELECT 1 FROM pg_roles WHERE rolname = %s", (user,))
        if row is None:
            db.execute_commit("CREATE ROLE %s" % user)


class PrivilegeToMapTestCase(DatabaseToMapTestCase):
    """Test mapping of object privilege information"""

    def setUp(self):

# Path: pyrseas/testutils.py
# class DatabaseToMapTestCase(PyrseasTestCase):
#     """Base class for "database to map" test cases"""
# 
#     superuser = False
# 
#     def to_map(self, stmts, config={}, schemas=[], tables=[], no_owner=True,
#                no_privs=True, superuser=False, multiple_files=False):
#         """Execute statements and return a database map.
# 
#         :param stmts: list of SQL statements to execute
#         :param config: dictionary of configuration information
#         :param schemas: list of schemas to map
#         :param tables: list of tables to map
#         :param no_owner: exclude object owner information
#         :param no_privs: exclude privilege information
#         :param superuser: must be superuser to run
#         :param multiple_files: emulate --multiple_files option
#         :return: possibly trimmed map of database
#         """
#         if (self.superuser or superuser) and not self.db.is_superuser():
#             self.skipTest("Must be a superuser to run this test")
#         for stmt in stmts:
#             self.db.execute(stmt)
#         self.db.conn.commit()
#         if multiple_files:
#             self.cfg.merge({'files': {'metadata_path': os.path.join(
#                             TEST_DIR, self.cfg['repository']['metadata'])}})
#         if 'datacopy' in config:
#             self.cfg.merge({'files': {'data_path': os.path.join(
#                             TEST_DIR, self.cfg['repository']['data'])}})
#         self.config_options(schemas=schemas, tables=tables, no_owner=no_owner,
#                             no_privs=no_privs, multiple_files=multiple_files)
#         self.cfg.merge(config)
#         return self.database().to_map()
# 
#     def yaml_load(self, filename, subdir=None):
#         """Read a file in the metadata_path and process it with YAML load
# 
#         :param filename: name of the file
#         :param subdir: name of a subdirectory where the file is located
#         :return: YAML dictionary
#         """
#         with open(os.path.join(self.cfg['files']['metadata_path'],
#                                subdir or '', filename), 'r') as f:
#             inmap = f.read()
#         return yaml.safe_load(inmap)
# 
#     def remove_tempfiles(self):
#         remove_temp_files(TEST_DIR)
# 
#     @staticmethod
#     def sort_privileges(data):
#         try:
#             sorted_privlist = []
#             for sortedItem in sorted([list(i.keys())[0]
#                                       for i in data['privileges']]):
#                 sorted_privlist.append(
#                     [item for item in data['privileges']
#                      if list(item.keys())[0] == sortedItem][0])
#             data['privileges'] = sorted_privlist
#         finally:
#             return data
# 
# Path: pyrseas/testutils.py
# class InputMapToSqlTestCase(PyrseasTestCase):
#     """Base class for "input map to SQL" test cases"""
# 
#     superuser = False
# 
#     def to_sql(self, inmap, stmts=None, config={}, superuser=False, schemas=[],
#                revert=False, quote_reserved=False):
#         """Execute statements and compare database to input map.
# 
#         :param inmap: dictionary defining target database
#         :param stmts: list of SQL database setup statements
#         :param config: dictionary of configuration information
#         :param superuser: indicates test requires superuser privilege
#         :param schemas: list of schemas to diff
#         :param revert: generate statements to back out changes
#         :param quote_reserved: fetch reserved words
#         :return: list of SQL statements
#         """
#         if (self.superuser or superuser) and not self.db.is_superuser():
#             self.skipTest("Must be a superuser to run this test")
#         if stmts:
#             for stmt in stmts:
#                 self.db.execute(stmt)
#             self.db.conn.commit()
# 
#         if 'datacopy' in config:
#             self.cfg.merge({'files': {'data_path': os.path.join(
#                             TEST_DIR, self.cfg['repository']['data'])}})
#         self.config_options(schemas=schemas, revert=revert),
#         self.cfg.merge(config)
#         return self.database().diff_map(inmap, quote_reserved=quote_reserved)
# 
#     def std_map(self, plpgsql_installed=False):
#         "Return a standard schema map for the default database"
#         base = {'schema sd': {
#                 'owner': self.db.user,
#                 'privileges': []}}
#         base.update({'extension plpgsql': {
#             'schema': 'pg_catalog', 'owner': PG_OWNER,
#             'description': "PL/pgSQL procedural language"}})
#         return base


# Path: tests/dbobject/test_privs.py
from pyrseas.testutils import DatabaseToMapTestCase
from pyrseas.testutils import InputMapToSqlTestCase

"""Test object privileges

The majority of other tests exclude access privileges.  These
explicitly request it.  In addition, the roles 'user1' and 'user2'
are created if they don't exist.
"""


CREATE_TABLE = "CREATE TABLE t1 (c1 integer, c2 text)"
SOURCE1 = "SELECT 'dummy'::text"
CREATE_FUNC = "CREATE FUNCTION f1() RETURNS text LANGUAGE sql IMMUTABLE AS " \
    "$_$%s$_$" % SOURCE1
CREATE_FDW = "CREATE FOREIGN DATA WRAPPER fdw1"
CREATE_FS = "CREATE SERVER fs1 FOREIGN DATA WRAPPER fdw1"
GRANT_SELECT = "GRANT SELECT ON TABLE sd.t1 TO %s"
GRANT_INSUPD = "GRANT INSERT, UPDATE ON TABLE sd.t1 TO %s"


def check_extra_users(db):
    "Check existence of extra test users"
    for user in ['user1', 'user2']:
        row = db.fetchone("SELECT 1 FROM pg_roles WHERE rolname = %s", (user,))
        if row is None:
            db.execute_commit("CREATE ROLE %s" % user)


class PrivilegeToMapTestCase(DatabaseToMapTestCase):
    """Test mapping of object privilege information"""

    def setUp(self):

        super(DatabaseToMapTestCase, self).setUp()