diff --git a/py_dbcn/connectors/core/core.py b/py_dbcn/connectors/core/core.py index 20970ad61e7761dba6753af4938e7f1882986f72..fb41cb3553ca8cfce6ab55111c5ff155c1073254 100644 --- a/py_dbcn/connectors/core/core.py +++ b/py_dbcn/connectors/core/core.py @@ -38,6 +38,8 @@ class AbstractDbConnector(ABC): self._connection = None self._debug = debug + # region Config Initialization + # Define class to hold config values. class Config: pass @@ -54,6 +56,25 @@ class AbstractDbConnector(ABC): self._config.db_type = None self._config._implemented_db_types = ['MySQL', 'PostgreSQL'] + # endregion Config Initialization + + # region Error Handler Setup + + # Define class to hold error handler references. + class Errors: + pass + + self.errors = Errors() + self.errors.handler = None + self.errors.database_does_not_exist = None + self.errors.database_already_exists = None + self.errors.table_does_not_exist = None + self.errors.table_already_exists = None + + # endregion Error Handler Setup + + # region Child Sub-Class Initialization + # Create references to related subclasses. self.database = self._get_related_database_class() self.display = self._get_related_display_class() @@ -63,6 +84,8 @@ class AbstractDbConnector(ABC): self.utils = self._get_related_utils_class() self.validate = self._get_related_validate_class() + # endregion Child Sub-Class Initialization + def __del__(self): """ Close database connection on exit. diff --git a/py_dbcn/connectors/mysql/core.py b/py_dbcn/connectors/mysql/core.py index 035cb030acc19d7ce44408288c2bfc4cd1554b94..9dd896c85b00dde38b9f00cafdcf8afffe43dd37 100644 --- a/py_dbcn/connectors/mysql/core.py +++ b/py_dbcn/connectors/mysql/core.py @@ -33,6 +33,13 @@ class MysqlDbConnector(AbstractDbConnector): # Call parent logic. super().__init__(*args, **kwargs) + # Initialize error handlers. + self.errors.handler = MySQLdb + self.errors.database_does_not_exist = self.errors.handler.OperationalError + self.errors.database_already_exists = self.errors.handler.ProgrammingError + self.errors.table_does_not_exist = self.errors.handler.OperationalError + self.errors.table_already_exists = self.errors.handler.OperationalError + # Initialize database connection. self._config.db_type = 'MySQL' self.create_connection() diff --git a/py_dbcn/connectors/postgresql/core.py b/py_dbcn/connectors/postgresql/core.py index e5c02efd3aa54a17e98c7abb04576657205c3d12..d7579fea6cc5a397446ebec476015cd4bb35ebfe 100644 --- a/py_dbcn/connectors/postgresql/core.py +++ b/py_dbcn/connectors/postgresql/core.py @@ -33,6 +33,13 @@ class PostgresqlDbConnector(AbstractDbConnector): # Call parent logic. super().__init__(*args, **kwargs) + # Initialize error handlers. + self.errors.handler = psycopg2.errors + self.errors.database_does_not_exist = self.errors.handler.InvalidCatalogName + self.errors.database_already_exists = self.errors.handler.DuplicateDatabase + self.errors.table_does_not_exist = self.errors.handler.UndefinedTable + self.errors.table_already_exists = self.errors.handler.DuplicateTable + # Initialize database connection. self._config.db_type = 'PostgreSQL' self.create_connection() diff --git a/tests/connectors/core/test_database.py b/tests/connectors/core/test_database.py index 11c2fed45f770b4b19687e7b24ae0408c0fb3538..f935d6c5e0945522ba89dc543c31fd9a457018e6 100644 --- a/tests/connectors/core/test_database.py +++ b/tests/connectors/core/test_database.py @@ -25,9 +25,6 @@ class CoreDatabaseTestMixin: """ cls.test_db_name_start = cls.test_db_name_start.format(cls.db_type) - cls.error_handler__database_does_not_exist = None - cls.error_handler__database_already_exists = None - def test_error_catch_types(self): """Tests to ensure database ERROR types are properly caught. @@ -35,9 +32,9 @@ class CoreDatabaseTestMixin: These tests make sure this error (and others) are properly caught, regardless of what database is being called. """ # Ensure error types are first defined. - if not self.error_handler__database_does_not_exist: + if not self.connector.errors.database_does_not_exist: raise ValueError('Please define error handler for "Database Does Not Exist" error type.') - if not self.error_handler__database_already_exists: + if not self.connector.errors.database_already_exists: raise ValueError('Please define error handler for "Database Already Exists" error type.') def test__select(self): @@ -59,7 +56,7 @@ class CoreDatabaseTestMixin: # Verify database exists. try: self.connector.database.create(db_name) - except self.error_handler__database_already_exists: + except self.connector.errors.database_already_exists: # Database already exists, as we want. pass @@ -78,7 +75,7 @@ class CoreDatabaseTestMixin: # Verify database exists. try: self.connector.database.create(db_name) - except self.error_handler__database_already_exists: + except self.connector.errors.database_already_exists: # Database already exists, as we want. pass @@ -101,7 +98,7 @@ class CoreDatabaseTestMixin: # Verify database exists. try: self.connector.database.create(db_name) - except self.error_handler__database_already_exists: + except self.connector.errors.database_already_exists: # Database already exists, as we want. pass @@ -119,7 +116,7 @@ class CoreDatabaseTestMixin: # Verify database does not exist. try: self.connector.database.drop(db_name) - except self.error_handler__database_does_not_exist: + except self.connector.errors.database_does_not_exist: # Database does not yet exist, as we want. pass @@ -163,7 +160,7 @@ class CoreDatabaseTestMixin: # Verify database does not yet exist. try: self.connector.database.create(db_name) - except self.error_handler__database_already_exists: + except self.connector.errors.database_already_exists: # Database already exists, as we want. pass @@ -177,7 +174,7 @@ class CoreDatabaseTestMixin: if self.connector._config.db_type == 'MySQL': error_type = ValueError elif self.connector._config.db_type == 'PostgreSQL': - error_type = self.db_error_handler.errors.DuplicateDatabase + error_type = self.connector.errors.database_already_exists with self.assertRaises(error_type): self.connector.database.create(db_name) @@ -190,7 +187,7 @@ class CoreDatabaseTestMixin: # Verify database exists. try: self.connector.database.create(db_name) - except self.error_handler__database_already_exists: + except self.connector.errors.database_already_exists: # Database already exists, as we want. pass @@ -239,7 +236,7 @@ class CoreDatabaseTestMixin: # Verify database does not yet exist. try: self.connector.database.create(db_name) - except self.error_handler__database_already_exists: + except self.connector.errors.database_already_exists: # Database already exists, as we want. pass diff --git a/tests/connectors/core/test_records.py b/tests/connectors/core/test_records.py index 180c82e7fcdd8489f5678240837e2efb630948da..f7dd47da3bf2adc4c1055d6c76205c61190920fd 100644 --- a/tests/connectors/core/test_records.py +++ b/tests/connectors/core/test_records.py @@ -30,8 +30,6 @@ class CoreRecordsTestMixin: cls._columns_clause__basic = None cls._columns_clause__datetime = None - cls.error_handler__table_already_exists = None - def test_error_catch_types(self): """Tests to ensure database ERROR types are properly caught. @@ -39,7 +37,7 @@ class CoreRecordsTestMixin: These tests make sure this error (and others) are properly caught, regardless of what database is being called. """ # Ensure error types are first defined. - if not self.error_handler__table_already_exists: + if not self.connector.errors.table_already_exists: raise ValueError('Please define error handler for "Table Already Exists" error type.') def test__select__success(self): @@ -51,7 +49,7 @@ class CoreRecordsTestMixin: # Verify table exists. try: self.connector.query.execute('CREATE TABLE {0}{1};'.format(table_name, self._columns_clause__basic)) - except self.error_handler__table_already_exists: + except self.connector.errors.table_already_exists: # Table already exists, as we want. pass @@ -129,7 +127,7 @@ class CoreRecordsTestMixin: # Verify table exists. try: self.connector.query.execute('CREATE TABLE {0}{1};'.format(table_name, self._columns_clause__basic)) - except self.error_handler__table_already_exists: + except self.connector.errors.table_already_exists: # Table already exists, as we want. pass @@ -217,7 +215,7 @@ class CoreRecordsTestMixin: # Verify table exists. try: self.connector.query.execute('CREATE TABLE {0}{1};'.format(table_name, self._columns_clause__basic)) - except self.error_handler__table_already_exists: + except self.connector.errors.table_already_exists: # Table already exists, as we want. pass @@ -254,7 +252,7 @@ class CoreRecordsTestMixin: # Verify table exists. try: self.connector.query.execute('CREATE TABLE {0} {1};'.format(table_name, self._columns_clause__datetime)) - except self.error_handler__table_already_exists: + except self.connector.errors.table_already_exists: # Table already exists, as we want. pass @@ -316,7 +314,7 @@ class CoreRecordsTestMixin: # Verify table exists. try: self.connector.query.execute('CREATE TABLE {0}{1};'.format(table_name, self._columns_clause__basic)) - except self.error_handler__table_already_exists: + except self.connector.errors.table_already_exists: # Table already exists, as we want. pass @@ -416,7 +414,7 @@ class CoreRecordsTestMixin: # Verify table exists. try: self.connector.query.execute('CREATE TABLE {0}{1};'.format(table_name, self._columns_clause__datetime)) - except self.error_handler__table_already_exists: + except self.connector.errors.table_already_exists: # Table already exists, as we want. pass @@ -567,7 +565,7 @@ class CoreRecordsTestMixin: # Verify table exists. try: self.connector.query.execute('CREATE TABLE {0}{1};'.format(table_name, self._columns_clause__basic)) - except self.error_handler__table_already_exists: + except self.connector.errors.table_already_exists: # Table already exists, as we want. pass diff --git a/tests/connectors/core/test_tables.py b/tests/connectors/core/test_tables.py index 40157c0d859cd2b44045610c12b6a620699dd48c..25292a4cd8c5e68b4df36132a148d007721867be 100644 --- a/tests/connectors/core/test_tables.py +++ b/tests/connectors/core/test_tables.py @@ -25,9 +25,6 @@ class CoreTablesTestMixin: """ cls.test_db_name_start = cls.test_db_name_start.format(cls.db_type) - cls.error_handler__table_does_not_exist = None - cls.error_handler__table_already_exists = None - def test_error_catch_types(self): """Tests to ensure database ERROR types are properly caught. @@ -35,9 +32,9 @@ class CoreTablesTestMixin: These tests make sure this error (and others) are properly caught, regardless of what database is being called. """ # Ensure error types are first defined. - if not self.error_handler__table_does_not_exist: + if not self.connector.errors.table_does_not_exist: raise ValueError('Please define error handler for "Table Does Not Exist" error type.') - if not self.error_handler__table_already_exists: + if not self.connector.errors.table_already_exists: raise ValueError('Please define error handler for "Table Already Exists" error type.') def test__create_table___col_str(self): @@ -108,7 +105,7 @@ class CoreTablesTestMixin: # Verify table exists. try: self.connector.query.execute('CREATE TABLE {0}{1};'.format(table_name, self._columns_clause__basic)) - except self.error_handler__table_already_exists: + except self.connector.errors.table_already_exists: # Table already exists, as we want. pass @@ -124,7 +121,7 @@ class CoreTablesTestMixin: # Verify table does not exist. try: self.connector.query.execute('DROP TABLE {0};'.format(table_name)) - except self.error_handler__table_does_not_exist: + except self.connector.errors.table_does_not_exist: # Table does not yet exist, as we want. pass @@ -168,7 +165,7 @@ class CoreTablesTestMixin: # Verify table exists. try: self.connector.query.execute('CREATE TABLE {0}{1};'.format(table_name, self._columns_clause__basic)) - except self.error_handler__table_already_exists: + except self.connector.errors.table_already_exists: # Table already exists, as we want. pass @@ -193,7 +190,7 @@ class CoreTablesTestMixin: # # Verify table exists. # try: # self.connector.query.execute('CREATE TABLE {0}{1};'.format(table_name, _columns_clause__basic)) - # except self.error_handler__table_already_exists: + # except self.connector.errors.table_already_exists: # # Table already exists, as we want. # pass # @@ -280,7 +277,7 @@ class CoreTablesTestMixin: # Verify table exists. try: self.connector.query.execute('CREATE TABLE {0}{1};'.format(table_name, self._columns_clause__basic)) - except self.error_handler__table_already_exists: + except self.connector.errors.table_already_exists: # Table already exists, as we want. pass diff --git a/tests/connectors/mysql/test_core.py b/tests/connectors/mysql/test_core.py index 68f4789b6a5cd7aae2bbfbef39d68cfd9d82d31d..3c0d415601cb2bf36c80712b69076fd8e6ab5604 100644 --- a/tests/connectors/mysql/test_core.py +++ b/tests/connectors/mysql/test_core.py @@ -44,4 +44,4 @@ class TestMysqlDatabaseParent(CoreTestParent): ) cls.db_type = cls.connector._config.db_type cls._implemented_db_types = cls.connector._config._implemented_db_types - cls.db_error_handler = MySQLdb + cls.db_error_handler = cls.connector.errors.handler diff --git a/tests/connectors/mysql/test_database.py b/tests/connectors/mysql/test_database.py index 7b1983e158b0a811a8e835cd7e4103f8e835e180..f2c353f24a59c24e77a80b9d452bcfe9a1d04b00 100644 --- a/tests/connectors/mysql/test_database.py +++ b/tests/connectors/mysql/test_database.py @@ -34,9 +34,6 @@ class TestMysqlDatabase(TestMysqlDatabaseParent, CoreDatabaseTestMixin): for result in results: cls.connector.tables.drop(result) - cls.error_handler__database_does_not_exist = cls.db_error_handler.OperationalError - cls.error_handler__database_already_exists = cls.db_error_handler.ProgrammingError - def test_error_catch_types(self): """Tests to ensure database ERROR types are properly caught. @@ -55,7 +52,7 @@ class TestMysqlDatabase(TestMysqlDatabaseParent, CoreDatabaseTestMixin): raise AssertionError('Database already present. Incorrect name provided.') # Check that we use the correct handler. - with self.assertRaises(self.error_handler__database_does_not_exist): + with self.assertRaises(self.connector.errors.database_does_not_exist): self.connector.query.execute('DROP DATABASE {0};'.format(db_name)) with self.subTest('Verify handling when database already exists'): @@ -66,5 +63,5 @@ class TestMysqlDatabase(TestMysqlDatabaseParent, CoreDatabaseTestMixin): raise AssertionError('Database not yet present. Incorrect name provided.') # Check that we use the correct handler. - with self.assertRaises(self.error_handler__database_already_exists): + with self.assertRaises(self.connector.errors.database_already_exists): self.connector.query.execute('CREATE DATABASE {0};'.format(db_name)) diff --git a/tests/connectors/mysql/test_records.py b/tests/connectors/mysql/test_records.py index df595f2e9c36b4bdb8488f3677d6ef5a5ab3d005..a06aa1cba666fb4e93822a55aef0764aead91f67 100644 --- a/tests/connectors/mysql/test_records.py +++ b/tests/connectors/mysql/test_records.py @@ -40,8 +40,6 @@ class TestMysqlRecords(TestMysqlDatabaseParent, CoreRecordsTestMixin): cls._columns_clause__basic = COLUMNS_CLAUSE__BASIC cls._columns_clause__datetime = COLUMNS_CLAUSE__DATETIME - cls.error_handler__table_already_exists = cls.db_error_handler.OperationalError - def test_error_catch_types(self): """Tests to ensure database ERROR types are properly caught. @@ -62,5 +60,5 @@ class TestMysqlRecords(TestMysqlDatabaseParent, CoreRecordsTestMixin): raise AssertionError('Table not yet present. Incorrect name provided.') # Check that we use the correct handler. - with self.assertRaises(self.error_handler__table_already_exists): + with self.assertRaises(self.connector.errors.table_already_exists): self.connector.query.execute('CREATE TABLE {0} {1};'.format(table_name, self._columns_clause__basic)) diff --git a/tests/connectors/mysql/test_tables.py b/tests/connectors/mysql/test_tables.py index b97da30e14307f610500f082adea4bdbf4895af9..7012fb0b2c6d718e1f3c0875d78aaa379fca6618 100644 --- a/tests/connectors/mysql/test_tables.py +++ b/tests/connectors/mysql/test_tables.py @@ -43,9 +43,6 @@ class TestMysqlTables(TestMysqlDatabaseParent, CoreTablesTestMixin): cls._columns_clause__minimal = COLUMNS_CLAUSE__MINIMAL cls._columns_clause__basic = COLUMNS_CLAUSE__BASIC - cls.error_handler__table_does_not_exist = cls.db_error_handler.OperationalError - cls.error_handler__table_already_exists = cls.db_error_handler.OperationalError - def test_error_catch_types(self): """Tests to ensure database ERROR types are properly caught. @@ -64,7 +61,7 @@ class TestMysqlTables(TestMysqlDatabaseParent, CoreTablesTestMixin): raise AssertionError('Table already present. Incorrect name provided.') # Check that we use the correct handler. - with self.assertRaises(self.error_handler__table_does_not_exist): + with self.assertRaises(self.connector.errors.table_does_not_exist): self.connector.query.execute('DROP TABLE {0};'.format(table_name)) with self.subTest('Verify handling when database already exists'): @@ -77,5 +74,5 @@ class TestMysqlTables(TestMysqlDatabaseParent, CoreTablesTestMixin): raise AssertionError('Table not yet present. Incorrect name provided.') # Check that we use the correct handler. - with self.assertRaises(self.error_handler__table_already_exists): + with self.assertRaises(self.connector.errors.table_already_exists): self.connector.query.execute('CREATE TABLE {0} {1};'.format(table_name, self._columns_clause__minimal)) diff --git a/tests/connectors/postgresql/test_core.py b/tests/connectors/postgresql/test_core.py index 8ff727b568269380c9a78e4f44029b3a3b3861a2..1814a2ed6a8352217defe0b3936420558670385a 100644 --- a/tests/connectors/postgresql/test_core.py +++ b/tests/connectors/postgresql/test_core.py @@ -42,4 +42,4 @@ class TestPostgresqlDatabaseParent(CoreTestParent): ) cls.db_type = cls.connector._config.db_type cls._implemented_db_types = cls.connector._config._implemented_db_types - cls.db_error_handler = psycopg2 + cls.db_error_handler = cls.connector.errors.handler diff --git a/tests/connectors/postgresql/test_database.py b/tests/connectors/postgresql/test_database.py index 67da2b70780e72518586d222b9550398d11db1d3..c59a5db6d6a6862fa9af0556f8f95e6b17cc18fc 100644 --- a/tests/connectors/postgresql/test_database.py +++ b/tests/connectors/postgresql/test_database.py @@ -34,9 +34,6 @@ class TestPostgresqlDatabase(TestPostgresqlDatabaseParent, CoreDatabaseTestMixin for result in results: cls.connector.tables.drop(result) - cls.error_handler__database_does_not_exist = cls.db_error_handler.errors.InvalidCatalogName - cls.error_handler__database_already_exists = cls.db_error_handler.errors.DuplicateDatabase - def test_error_catch_types(self): """Tests to ensure database ERROR types are properly caught. @@ -55,7 +52,7 @@ class TestPostgresqlDatabase(TestPostgresqlDatabaseParent, CoreDatabaseTestMixin raise AssertionError('Database already present. Incorrect name provided.') # Check that we use the correct handler. - with self.assertRaises(self.error_handler__database_does_not_exist): + with self.assertRaises(self.connector.errors.database_does_not_exist): self.connector.query.execute('DROP DATABASE {0};'.format(db_name)) with self.subTest('Verify handling when database already exists'): @@ -66,5 +63,5 @@ class TestPostgresqlDatabase(TestPostgresqlDatabaseParent, CoreDatabaseTestMixin raise AssertionError('Database not yet present. Incorrect name provided.') # Check that we use the correct handler. - with self.assertRaises(self.error_handler__database_already_exists): + with self.assertRaises(self.connector.errors.database_already_exists): self.connector.query.execute('CREATE DATABASE {0};'.format(db_name)) diff --git a/tests/connectors/postgresql/test_records.py b/tests/connectors/postgresql/test_records.py index 3b4e43c6fab62ca39451dd0b65514edfda0415dc..3f07b1b4c56b1d691bd94369c3091f5bfc77532a 100644 --- a/tests/connectors/postgresql/test_records.py +++ b/tests/connectors/postgresql/test_records.py @@ -40,8 +40,6 @@ class TestPostgresqlRecords(TestPostgresqlDatabaseParent, CoreRecordsTestMixin): cls._columns_clause__basic = COLUMNS_CLAUSE__BASIC cls._columns_clause__datetime = COLUMNS_CLAUSE__DATETIME - cls.error_handler__table_already_exists = cls.db_error_handler.errors.DuplicateTable - def test_error_catch_types(self): """Tests to ensure database ERROR types are properly caught. @@ -62,5 +60,5 @@ class TestPostgresqlRecords(TestPostgresqlDatabaseParent, CoreRecordsTestMixin): raise AssertionError('Table not yet present. Incorrect name provided.') # Check that we use the correct handler. - with self.assertRaises(self.error_handler__table_already_exists): + with self.assertRaises(self.connector.errors.table_already_exists): self.connector.query.execute('CREATE TABLE {0} {1};'.format(table_name, self._columns_clause__basic)) diff --git a/tests/connectors/postgresql/test_tables.py b/tests/connectors/postgresql/test_tables.py index 2ff6c36f407e235d8efa393435776abbe6ad4306..b199fdf1165f885ce9955c9cd34054c00bff92e2 100644 --- a/tests/connectors/postgresql/test_tables.py +++ b/tests/connectors/postgresql/test_tables.py @@ -39,9 +39,6 @@ class TestPostgresqlTables(TestPostgresqlDatabaseParent, CoreTablesTestMixin): cls._columns_clause__minimal = COLUMNS_CLAUSE__MINIMAL cls._columns_clause__basic = COLUMNS_CLAUSE__BASIC - cls.error_handler__table_does_not_exist = cls.db_error_handler.errors.UndefinedTable - cls.error_handler__table_already_exists = cls.db_error_handler.errors.DuplicateTable - def test_error_catch_types(self): """Tests to ensure database ERROR types are properly caught. @@ -60,7 +57,7 @@ class TestPostgresqlTables(TestPostgresqlDatabaseParent, CoreTablesTestMixin): raise AssertionError('Table already present. Incorrect name provided.') # Check that we use the correct handler. - with self.assertRaises(self.error_handler__table_does_not_exist): + with self.assertRaises(self.connector.errors.table_does_not_exist): self.connector.query.execute('DROP TABLE {0};'.format(table_name)) with self.subTest('Verify handling when database already exists'): @@ -73,5 +70,5 @@ class TestPostgresqlTables(TestPostgresqlDatabaseParent, CoreTablesTestMixin): raise AssertionError('Table not yet present. Incorrect name provided.') # Check that we use the correct handler. - with self.assertRaises(self.error_handler__table_already_exists): + with self.assertRaises(self.connector.errors.table_already_exists): self.connector.query.execute('CREATE TABLE {0} {1};'.format(table_name, self._columns_clause__minimal))