def get_table(self, table_name: str, schema: str) -> Table: """ This method queries the database for the table Args: table_name: The name of the table schema: Name of the schema Returns: sqlalchemy.Table """ # create the engine uri = self.__generate_conn_path(schema) engine = create_engine(uri) # get metadata metadata = MetaData(engine) metadata.reflect(bind=engine) if not metadata: raise (ProgrammingError(f"{table_name} does not exist in {schema}", table_name, schema)) # get all tables in the metadata tables = metadata.tables # dispose off all the connections by engine engine.dispose() # check and return if the table exist otherwise raise ProgrammingError if table_name in tables: return tables[table_name] raise (ProgrammingError(f"{table_name} does not exist in {schema}", table_name, schema))
def setUp(self): self.app = app.test_client() self.error = ProgrammingError('stuff failed', 'Program', 'Error') self.dataset_activity = [{ 'private': True, 'name': 'nps', 'licence_agreed': True }]
def test_retry_if_data_catalog_exception(self, engine, connection): dialect = engine.dialect exc = OperationalError( '', None, 'com.facebook.presto.hive.DataCatalogException: ' + 'Database also_does_not_exist not found. Please check your query.') self.assertTrue( dialect._retry_if_data_catalog_exception(exc, 'does_not_exist', 'this_does_not_exist')) self.assertFalse( dialect._retry_if_data_catalog_exception(exc, 'also_does_not_exist', 'this_does_not_exist')) exc = OperationalError( '', None, 'com.facebook.presto.hive.DataCatalogException: ' + 'Namespace also_does_not_exist not found. Please check your query.' ) self.assertTrue( dialect._retry_if_data_catalog_exception(exc, 'does_not_exist', 'this_does_not_exist')) self.assertFalse( dialect._retry_if_data_catalog_exception(exc, 'also_does_not_exist', 'this_does_not_exist')) exc = OperationalError( '', None, 'com.facebook.presto.hive.DataCatalogException: ' + 'Table this_does_not_exist not found. Please check your query.') self.assertTrue( dialect._retry_if_data_catalog_exception(exc, 'does_not_exist', 'does_not_exist')) self.assertFalse( dialect._retry_if_data_catalog_exception(exc, 'also_does_not_exist', 'this_does_not_exist')) exc = OperationalError( '', None, 'com.facebook.presto.hive.FooBarException: ' + 'Table this_does_not_exist not found. Please check your query.') self.assertTrue( dialect._retry_if_data_catalog_exception(exc, 'also_does_not_exist', 'this_does_not_exist')) exc = OperationalError( '', None, 'com.facebook.presto.hive.DataCatalogException: ' + 'foobar.') self.assertTrue( dialect._retry_if_data_catalog_exception(exc, 'also_does_not_exist', 'this_does_not_exist')) exc = ProgrammingError( '', None, 'com.facebook.presto.hive.DataCatalogException: ' + 'Database also_does_not_exist not found. Please check your query.') self.assertFalse( dialect._retry_if_data_catalog_exception(exc, 'does_not_exist', 'this_does_not_exist'))
def show_tables(self, schema: str) -> None: """ prints out the names of tables in a given schema """ if not self.__connect: raise ProgrammingError('No connection established! You can establish the connection by calling' + \ ' self.connect()', None, None) tables = self.inspector.get_table_names(schema=schema) self.pp.pprint(tables)
def show_schemas(self) -> None: """ This method prints all the schemas defined in the system as a list Needs the connection to be initialized before """ if not self.__connect: raise ProgrammingError('No connection established! You can establish the connection by calling' + \ ' self.connect()', None, None) db_list = self.inspector.get_schema_names() self.pp.pprint(db_list)
def test_materialized_view_refreshed(self, db_mock, exists_mock): """Test JOB leaderboard materialized view is refreshed.""" result = MagicMock() result.exists = True results = [result] exists_mock.return_value = True db_mock.slave_session.execute.side_effect = results db_mock.session.execute.side_effect = [ ProgrammingError('foo', 'bar', 'bar'), True ] res = leaderboard() assert db_mock.session.execute.called assert res == 'Materialized view refreshed'
def test_retry_if_data_catalog_exception(self, engine, connection): dialect = engine.dialect exc = OperationalError('', None, 'Database does_not_exist not found. Please check your query.') self.assertFalse(dialect._retry_if_data_catalog_exception( exc, 'does_not_exist', 'does_not_exist')) self.assertFalse(dialect._retry_if_data_catalog_exception( exc, 'does_not_exist', 'this_does_not_exist')) self.assertTrue(dialect._retry_if_data_catalog_exception( exc, 'this_does_not_exist', 'does_not_exist')) self.assertTrue(dialect._retry_if_data_catalog_exception( exc, 'this_does_not_exist', 'this_does_not_exist')) exc = OperationalError('', None, 'Namespace does_not_exist not found. Please check your query.') self.assertFalse(dialect._retry_if_data_catalog_exception( exc, 'does_not_exist', 'does_not_exist')) self.assertFalse(dialect._retry_if_data_catalog_exception( exc, 'does_not_exist', 'this_does_not_exist')) self.assertTrue(dialect._retry_if_data_catalog_exception( exc, 'this_does_not_exist', 'does_not_exist')) self.assertTrue(dialect._retry_if_data_catalog_exception( exc, 'this_does_not_exist', 'this_does_not_exist')) exc = OperationalError('', None, 'Table does_not_exist not found. Please check your query.') self.assertFalse(dialect._retry_if_data_catalog_exception( exc, 'does_not_exist', 'does_not_exist')) self.assertTrue(dialect._retry_if_data_catalog_exception( exc, 'does_not_exist', 'this_does_not_exist')) self.assertFalse(dialect._retry_if_data_catalog_exception( exc, 'this_does_not_exist', 'does_not_exist')) self.assertTrue(dialect._retry_if_data_catalog_exception( exc, 'this_does_not_exist', 'this_does_not_exist')) exc = OperationalError('', None, 'foobar.') self.assertTrue(dialect._retry_if_data_catalog_exception( exc, 'foobar', 'foobar')) exc = ProgrammingError('', None, 'Database does_not_exist not found. Please check your query.') self.assertFalse(dialect._retry_if_data_catalog_exception( exc, 'does_not_exist', 'does_not_exist')) self.assertFalse(dialect._retry_if_data_catalog_exception( exc, 'does_not_exist', 'this_does_not_exist')) self.assertFalse(dialect._retry_if_data_catalog_exception( exc, 'this_does_not_exist', 'does_not_exist')) self.assertFalse(dialect._retry_if_data_catalog_exception( exc, 'this_does_not_exist', 'this_does_not_exist'))
def programming_error(): return ProgrammingError("statement", "params", orig=Mock())
def test_retry_if_data_catalog_exception(self, engine, conn): dialect = engine.dialect exc = OperationalError( "", None, "Database does_not_exist not found. Please check your query.") self.assertFalse( dialect._retry_if_data_catalog_exception(exc, "does_not_exist", "does_not_exist")) self.assertFalse( dialect._retry_if_data_catalog_exception(exc, "does_not_exist", "this_does_not_exist")) self.assertTrue( dialect._retry_if_data_catalog_exception(exc, "this_does_not_exist", "does_not_exist")) self.assertTrue( dialect._retry_if_data_catalog_exception(exc, "this_does_not_exist", "this_does_not_exist")) exc = OperationalError( "", None, "Namespace does_not_exist not found. Please check your query.") self.assertFalse( dialect._retry_if_data_catalog_exception(exc, "does_not_exist", "does_not_exist")) self.assertFalse( dialect._retry_if_data_catalog_exception(exc, "does_not_exist", "this_does_not_exist")) self.assertTrue( dialect._retry_if_data_catalog_exception(exc, "this_does_not_exist", "does_not_exist")) self.assertTrue( dialect._retry_if_data_catalog_exception(exc, "this_does_not_exist", "this_does_not_exist")) exc = OperationalError( "", None, "Table does_not_exist not found. Please check your query.") self.assertFalse( dialect._retry_if_data_catalog_exception(exc, "does_not_exist", "does_not_exist")) self.assertTrue( dialect._retry_if_data_catalog_exception(exc, "does_not_exist", "this_does_not_exist")) self.assertFalse( dialect._retry_if_data_catalog_exception(exc, "this_does_not_exist", "does_not_exist")) self.assertTrue( dialect._retry_if_data_catalog_exception(exc, "this_does_not_exist", "this_does_not_exist")) exc = OperationalError("", None, "foobar.") self.assertTrue( dialect._retry_if_data_catalog_exception(exc, "foobar", "foobar")) exc = ProgrammingError( "", None, "Database does_not_exist not found. Please check your query.") self.assertFalse( dialect._retry_if_data_catalog_exception(exc, "does_not_exist", "does_not_exist")) self.assertFalse( dialect._retry_if_data_catalog_exception(exc, "does_not_exist", "this_does_not_exist")) self.assertFalse( dialect._retry_if_data_catalog_exception(exc, "this_does_not_exist", "does_not_exist")) self.assertFalse( dialect._retry_if_data_catalog_exception(exc, "this_does_not_exist", "this_does_not_exist"))
def _prog_error(*args, **kwargs): raise ProgrammingError('', [], DatabaseError)
def __raise(_) -> None: raise ProgrammingError("statement", [], "none", False)
def __raise(query, engine) -> None: raise ProgrammingError("statement", [], "none", False)
from app.views.product import (logical_delete, logical_restore, find_by_id, create, update, find) from random import randrange from sqlalchemy.exc import (NotSupportedError, OperationalError, ProgrammingError, IntegrityError, InternalError, DataError) from .test_user import user import unittest product = Product('id', company_id='company_id', value=10.00) integrity_error = IntegrityError('Mock', 'mock', Exception('mock', 'mock')) aleatory_errors = [ DataError('Mock', 'mock', Exception('mock', 'mock')), OperationalError('Mock', 'mock', Exception('mock', 'mock')), InternalError('Mock', 'mock', Exception('mock', 'mock')), ProgrammingError('Mock', 'mock', Exception('mock', 'mock')), NotSupportedError('Mock', 'mock', Exception('mock', 'mock')) ] product_validate = Schema({ 'id': Use(str), 'company_id': Use(str), 'value': Use(float), 'created': Or(str, None), 'updated': Or(str, None), 'removed': Or(str, None) }) companies_products_validate = Schema([{ 'id': Use(str),
def _raise_programming_error(*args, **kwargs): raise ProgrammingError('', {}, '')
def setUp(self): self.app = app.test_client() self.error = ProgrammingError('stuff failed', 'Program', 'Error')