def __init__(self, *args, **kwargs): super(SchemaDifferMySQLTestCase, self).__init__(*args, **kwargs) self.__differ = SchemaDiffer( ConfigManager().value('default unit test connection'), 'schema_differ_source', ConfigManager().value('default unit test connection'), 'schema_differ_target' ) \ .dont_write_upgrade_scripts() \ .execute()
def test_use_of_autonomous_transactions(self): dsh_1 = \ provider.autonomous_tx_start( ConfigManager().value('default unit test connection'), 'tinyAPI' ) dsh_2 = \ provider.autonomous_tx_start( ConfigManager().value('default unit test connection'), 'tinyAPI' ) self.assertIsInstance(dsh_1.connection_id(), int) self.assertIsInstance(dsh_2.connection_id(), int) self.assertNotEqual(dsh_1.connection_id(), dsh_2.connection_id()) dsh_1.query('''insert into unit_test_table( id, value) values( 1000, 123)''') dsh_2.query('''insert into unit_test_table( id, value) values( 2000, 456)''') self.assertEqual( 1, dsh_1.count('''select count(*) from unit_test_table where id = 1000''')) self.assertEqual( 0, dsh_1.count('''select count(*) from unit_test_table where id = 2000''')) self.assertEqual( 1, dsh_2.count('''select count(*) from unit_test_table where id = 2000''')) self.assertEqual( 0, dsh_2.count('''select count(*) from unit_test_table where id = 1000''')) provider.autonomous_tx_stop_commit(dsh_1) provider.autonomous_tx_stop_rollback(dsh_2)
def setUp(self): self.__execute_tests = False if ConfigManager().value('data store') == 'mysql': self.__execute_tests = True tinyAPI.dsh.select_db( ConfigManager().value('default unit test connection'), 'tinyAPI') tinyAPI.dsh().query('''create table if not exists unit_test_table ( id integer not null auto_increment primary key, value integer not null, ti time null, message blob null )''')
def test_two_active_data_store_handles(self): dsh_1 = provider.DataStoreMySQL() dsh_2 = provider.DataStoreMySQL() dsh_1.select_db(ConfigManager().value('default unit test connection'), 'tinyAPI') dsh_2.select_db(ConfigManager().value('default unit test connection'), 'tinyAPI') self.assertIsInstance(dsh_1.connection_id(), int) self.assertIsInstance(dsh_2.connection_id(), int) self.assertNotEqual(dsh_1.connection_id(), dsh_2.connection_id()) self.assertEqual(123, dsh_1.count('select 123 from dual')) self.assertEqual(456, dsh_2.count('select 456 from dual')) dsh_1.close() dsh_2.close()
def test_getting_a_value_exceptions(self): try: ConfigManager().value('no-such-option') self.fail('Was able to get a configuration value for a key that ' + 'is invalid.') except ConfigurationException as e: self.assertEqual( '"no-such-option" is not configured in tinyAPI_config', e.get_message())
def test_auto_reconnect(self): connection_id = tinyAPI.dsh().connection_id() self.assertIsNotNone(connection_id) dsh_1 = \ provider.autonomous_tx_start( ConfigManager().value('default unit test connection'), 'tinyAPI' ) dsh_1.query('kill {}'.format(connection_id)) provider.autonomous_tx_stop_commit(dsh_1) records = tinyAPI.dsh().query('select 1 from dual') self.assertEqual(1, len(records))
def connect(self): '''Perform the tasks required for connecting to the database.''' if self.persistent is True: self.requests += 1 if self.__mysql: if self.persistent is True: if time.time() - self._inactive_since >= \ self._ping_interval - 3: self.__mysql.ping(True) self._inactive_since = time.time() else: self.hits += 1 return if not self._connection_name: raise DataStoreException( 'cannot connect to MySQL because a connection name has not ' + 'been provided') connection_data = ConfigManager().value('mysql connection data') if self._connection_name not in connection_data: raise DataStoreException( 'the MySQL connection name you provided is invalid') if not self._db_name: raise DataStoreException( 'cannot connection to MySQL because no database name was ' + 'selected') config = { 'user': connection_data[self._connection_name][1], 'passwd': connection_data[self._connection_name][2], 'host': connection_data[self._connection_name][0], 'database': self._db_name, 'charset': self._charset, 'local_infile': True, 'autocommit': False } self.__mysql = \ pymysql.connect(**config) self.__mysql.decoders[pymysql.FIELD_TYPE.TIME] = \ pymysql.converters.convert_time self._inactive_since = time.time()
def test_view_flipper(self): mysql = DataStoreMySQL() mysql.select_db(ConfigManager().value('default unit test connection'), 'tinyAPI') mysql.query("create table if not exists " + "schema_differ_source.unit_test_view_flipper_1(id int)") mysql.query("create table if not exists " + "schema_differ_source.unit_test_view_flipper_2(id int)") mysql.query("""create or replace view schema_differ_source.unit_test_view_flipper as select * from schema_differ_source.unit_test_view_flipper_2""") vf = ViewFlipper('unit_test_view_flipper') self.assertEqual('unit_test_view_flipper_2', vf.get_active_table_name()) self.assertEqual('unit_test_view_flipper_1', vf.get_inactive_table_name()) vf.execute() vf = ViewFlipper('unit_test_view_flipper') self.assertEqual('unit_test_view_flipper_1', vf.get_active_table_name()) self.assertEqual('unit_test_view_flipper_2', vf.get_inactive_table_name()) vf.execute() vf = ViewFlipper('unit_test_view_flipper') self.assertEqual('unit_test_view_flipper_2', vf.get_active_table_name()) self.assertEqual('unit_test_view_flipper_1', vf.get_inactive_table_name()) mysql.query("drop view schema_differ_source.unit_test_view_flipper") mysql.query("drop table schema_differ_source.unit_test_view_flipper_1") mysql.query("drop table schema_differ_source.unit_test_view_flipper_2")
def setUp(self): self.__execute_tests = False if len(ConfigManager().value('rdbms builder schemas')) > 0 and \ ConfigManager().value('reference definition file') is not None: self.__execute_tests = True
def _handle_cli_exception_logging(e): log_file = ConfigManager().value('cli log file') if log_file: logging.basicConfig(filename=log_file) logging.critical(traceback.format_exc()) logging.shutdown()
def setUp(self): if ConfigManager().value('data store') == 'mysql': tinyAPI.dsh.select_db('local', 'tinyAPI')