def test_it_should_use_label_version_when_updating_schema_version(self): mysql = MySQL(self.config_mock, self.db_driver_mock) mysql.change( "create table spam();", "20090212112104", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;", label_version="label") expected_query_calls = [ call('create database if not exists `migration_test`;') ] self.assertEqual(expected_query_calls, self.db_mock.query.mock_calls) self.db_mock.select_db.assert_called_with('migration_test') self.assertEqual(4, self.db_mock.commit.call_count) self.assertEqual(6, self.db_mock.close.call_count) expected_execute_calls = [ call( 'create table if not exists __db_version__ ( id int(11) NOT NULL AUTO_INCREMENT, version varchar(20) NOT NULL default "0", label varchar(255), name varchar(255), sql_up LONGTEXT, sql_down LONGTEXT, PRIMARY KEY (id))' ), call('select count(*) from __db_version__;'), call('insert into __db_version__ (version) values ("0")'), call('create table spam()'), call( 'insert into __db_version__ (version, label, name, sql_up, sql_down) values ("20090212112104", "label", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;");' ) ] self.assertEqual(expected_execute_calls, self.cursor_mock.execute.mock_calls) self.assertEqual(5, self.cursor_mock.close.call_count)
def test_it_should_stop_process_when_an_error_occur_during_log_schema_version(self): self.execute_returns['insert into __db_version__ (version, label, name, sql_up, sql_down) values ("20090212112104", "label", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;");'] = Exception("invalid sql") try: mysql = MySQL(self.config_mock, self.db_driver_mock) mysql.change("create table spam();", "20090212112104", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;", label_version="label") except Exception as e: self.assertEqual('error logging migration: invalid sql\n\n[ERROR DETAILS] SQL command was:\n20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration', str(e)) self.assertTrue(isinstance(e, simple_db_migrate.core.exceptions.MigrationException)) expected_query_calls = [ call('create database if not exists `migration_test`;') ] self.assertEqual(expected_query_calls, self.db_mock.query.mock_calls) self.db_mock.select_db.assert_called_with('migration_test') self.assertEqual(1, self.db_mock.rollback.call_count) self.assertEqual(3, self.db_mock.commit.call_count) self.assertEqual(6, self.db_mock.close.call_count) expected_execute_calls = [ call('create table if not exists __db_version__ ( id int(11) NOT NULL AUTO_INCREMENT, version varchar(20) NOT NULL default "0", label varchar(255), name varchar(255), sql_up LONGTEXT, sql_down LONGTEXT, PRIMARY KEY (id))'), call('select count(*) from __db_version__;'), call('insert into __db_version__ (version) values ("0")'), call('create table spam()'), call('insert into __db_version__ (version, label, name, sql_up, sql_down) values ("20090212112104", "label", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;");') ] self.assertEqual(expected_execute_calls, self.cursor_mock.execute.mock_calls) self.assertEqual(4, self.cursor_mock.close.call_count)
def test_it_should_log_execution_when_a_function_is_given_when_updating_schema_version(self): execution_log_mock = Mock() mysql = MySQL(self.config_mock, self.db_driver_mock) mysql.change("create table spam();", "20090212112104", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;", execution_log=execution_log_mock) expected_execution_log_calls = [ call('create table spam()\n-- 0 row(s) affected\n'), call('migration 20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration registered\n') ] self.assertEqual(expected_execution_log_calls, execution_log_mock.mock_calls)
def test_it_should_not_execute_migration_if_fake_execution_is_set(self): mysql = MySQL(self.config_mock, self.db_driver_mock) mysql.change("create table spam();", "20090212112104", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;", fake_execution=True) expected_execute_calls = [ call('create table if not exists __db_version__ ( id int(11) NOT NULL AUTO_INCREMENT, version varchar(20) NOT NULL default "0", label varchar(255), name varchar(255), sql_up LONGTEXT, sql_down LONGTEXT, PRIMARY KEY (id))'), call('select count(*) from __db_version__;'), call('insert into __db_version__ (version) values ("0")'), call('insert into __db_version__ (version, label, name, sql_up, sql_down) values ("20090212112104", NULL, "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;");') ] self.assertEqual(expected_execute_calls, self.cursor_mock.execute.mock_calls) self.assertEqual(4, self.cursor_mock.close.call_count)
def test_it_should_stop_process_when_an_error_occur_during_log_schema_version( self): self.execute_returns[ 'insert into __db_version__ (version, label, name, sql_up, sql_down) values ("20090212112104", "label", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;");'] = Exception( "invalid sql") try: mysql = MySQL(self.config_mock, self.db_driver_mock) mysql.change( "create table spam();", "20090212112104", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;", label_version="label") except Exception as e: self.assertEqual( 'error logging migration: invalid sql\n\n[ERROR DETAILS] SQL command was:\n20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration', str(e)) self.assertTrue( isinstance( e, simple_db_migrate.core.exceptions.MigrationException)) expected_query_calls = [ call('create database if not exists `migration_test`;') ] self.assertEqual(expected_query_calls, self.db_mock.query.mock_calls) self.db_mock.select_db.assert_called_with('migration_test') self.assertEqual(1, self.db_mock.rollback.call_count) self.assertEqual(3, self.db_mock.commit.call_count) self.assertEqual(6, self.db_mock.close.call_count) expected_execute_calls = [ call( 'create table if not exists __db_version__ ( id int(11) NOT NULL AUTO_INCREMENT, version varchar(20) NOT NULL default "0", label varchar(255), name varchar(255), sql_up LONGTEXT, sql_down LONGTEXT, PRIMARY KEY (id))' ), call('select count(*) from __db_version__;'), call('insert into __db_version__ (version) values ("0")'), call('create table spam()'), call( 'insert into __db_version__ (version, label, name, sql_up, sql_down) values ("20090212112104", "label", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;");' ) ] self.assertEqual(expected_execute_calls, self.cursor_mock.execute.mock_calls) self.assertEqual(4, self.cursor_mock.close.call_count)
def test_it_should_log_execution_when_a_function_is_given_when_updating_schema_version( self): execution_log_mock = Mock() mysql = MySQL(self.config_mock, self.db_driver_mock) mysql.change( "create table spam();", "20090212112104", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;", execution_log=execution_log_mock) expected_execution_log_calls = [ call('create table spam()\n-- 0 row(s) affected\n'), call( 'migration 20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration registered\n' ) ] self.assertEqual(expected_execution_log_calls, execution_log_mock.mock_calls)
def test_it_should_use_label_version_when_updating_schema_version(self): mysql = MySQL(self.config_mock, self.db_driver_mock) mysql.change("create table spam();", "20090212112104", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;", label_version="label") expected_query_calls = [ call('create database if not exists `migration_test`;') ] self.assertEqual(expected_query_calls, self.db_mock.query.mock_calls) self.db_mock.select_db.assert_called_with('migration_test') self.assertEqual(4, self.db_mock.commit.call_count) self.assertEqual(6, self.db_mock.close.call_count) expected_execute_calls = [ call('create table if not exists __db_version__ ( id int(11) NOT NULL AUTO_INCREMENT, version varchar(20) NOT NULL default "0", label varchar(255), name varchar(255), sql_up LONGTEXT, sql_down LONGTEXT, PRIMARY KEY (id))'), call('select count(*) from __db_version__;'), call('insert into __db_version__ (version) values ("0")'), call('create table spam()'), call('insert into __db_version__ (version, label, name, sql_up, sql_down) values ("20090212112104", "label", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;");') ] self.assertEqual(expected_execute_calls, self.cursor_mock.execute.mock_calls) self.assertEqual(5, self.cursor_mock.close.call_count)