From 12b24c86bb09de2384e7de0e1ab573c087e130e1 Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Fri, 30 Sep 2022 10:39:51 -0400 Subject: [PATCH] Refactor error handlers to be more centrally defined and easily accessible --- py_dbcn/connectors/core/core.py | 23 ++++++++++++++++++++ py_dbcn/connectors/mysql/core.py | 7 ++++++ py_dbcn/connectors/postgresql/core.py | 7 ++++++ tests/connectors/core/test_database.py | 23 +++++++++----------- tests/connectors/core/test_records.py | 18 +++++++-------- tests/connectors/core/test_tables.py | 17 ++++++--------- tests/connectors/mysql/test_core.py | 2 +- tests/connectors/mysql/test_database.py | 7 ++---- tests/connectors/mysql/test_records.py | 4 +--- tests/connectors/mysql/test_tables.py | 7 ++---- tests/connectors/postgresql/test_core.py | 2 +- tests/connectors/postgresql/test_database.py | 7 ++---- tests/connectors/postgresql/test_records.py | 4 +--- tests/connectors/postgresql/test_tables.py | 7 ++---- 14 files changed, 74 insertions(+), 61 deletions(-) diff --git a/py_dbcn/connectors/core/core.py b/py_dbcn/connectors/core/core.py index 20970ad..fb41cb3 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 035cb03..9dd896c 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 e5c02ef..d7579fe 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 11c2fed..f935d6c 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 180c82e..f7dd47d 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 40157c0..25292a4 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 68f4789..3c0d415 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 7b1983e..f2c353f 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 df595f2..a06aa1c 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 b97da30..7012fb0 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 8ff727b..1814a2e 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 67da2b7..c59a5db 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 3b4e43c..3f07b1b 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 2ff6c36..b199fdf 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)) -- GitLab