示例#1
0
    def test_it_should_execute_migration_up_and_update_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;")

        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", 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(5, self.cursor_mock.close.call_count)
示例#2
0
    def test_it_should_drop_database_on_init_if_its_asked(self):
        self.config_dict["drop_db_first"] = True

        MySQL(self.config_mock, self.db_driver_mock)

        expected_query_calls = [
            call(
                'set foreign_key_checks=0; drop database if exists `migration_test`;'
            ),
            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(2, self.db_mock.commit.call_count)
        self.assertEqual(5, 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")')
        ]
        self.assertEqual(expected_execute_calls,
                         self.cursor_mock.execute.mock_calls)
        self.assertEqual(3, self.cursor_mock.close.call_count)
示例#3
0
    def test_it_should_get_none_for_a_non_existent_label_in_database(self):
        mysql = MySQL(self.config_mock, self.db_driver_mock)
        ret = mysql.get_version_number_from_label('xxx')
        self.assertEqual(None, ret)

        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(2, self.db_mock.commit.call_count)
        self.assertEqual(5, 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(
                "select version from __db_version__ where label = 'xxx' order by id desc"
            )
        ]
        self.assertEqual(expected_execute_calls,
                         self.cursor_mock.execute.mock_calls)
        self.assertEqual(4, self.cursor_mock.close.call_count)
示例#4
0
    def test_it_should_get_all_schema_versions(self):
        expected_versions = []
        expected_versions.append("0")
        expected_versions.append("20090211120001")
        expected_versions.append("20090211120002")
        expected_versions.append("20090211120003")

        self.cursor_mock.fetchall.return_value = tuple(zip(expected_versions))

        mysql = MySQL(self.config_mock, self.db_driver_mock)
        schema_versions = mysql.get_all_schema_versions()

        self.assertEqual(len(expected_versions), len(schema_versions))
        for version in schema_versions:
            self.assertTrue(version in expected_versions)

        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(2, self.db_mock.commit.call_count)
        self.assertEqual(5, 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('select version from __db_version__ order by id;')
        ]
        self.assertEqual(expected_execute_calls,
                         self.cursor_mock.execute.mock_calls)
        self.assertEqual(4, self.cursor_mock.close.call_count)
示例#5
0
    def test_it_should_get_current_schema_version(self):
        self.fetchone_returns = {
            'select count(*) from __db_version__;': [0],
            'select version from __db_version__ order by id desc limit 0,1;':
            ["0"]
        }

        mysql = MySQL(self.config_mock, self.db_driver_mock)
        self.assertEqual("0", mysql.get_current_schema_version())

        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(2, self.db_mock.commit.call_count)
        self.assertEqual(5, 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(
                'select version from __db_version__ order by id desc limit 0,1;'
            )
        ]
        self.assertEqual(expected_execute_calls,
                         self.cursor_mock.execute.mock_calls)
        self.assertEqual(4, self.cursor_mock.close.call_count)
示例#6
0
 def test_it_should_raise_whem_migration_sql_has_a_syntax_error(self):
     mysql = MySQL(self.config_mock, self.db_driver_mock)
     self.assertRaisesWithMessage(
         Exception,
         "error executing migration: invalid sql syntax 'create table foo(); create table spam());'",
         mysql.change,
         "create table foo(); create table spam());",
         "20090212112104",
         "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration",
         "create table foo(); create table spam());",
         "drop table spam;",
         label_version="label")
示例#7
0
    def test_it_should_get_all_schema_migrations(self):
        expected_versions = []
        expected_versions.append([1, "0", None, None, None, None])
        expected_versions.append([
            2, "20090211120001", "label", "20090211120001_name", "sql_up",
            "sql_down"
        ])

        self.cursor_mock.fetchall.return_value = tuple(expected_versions)

        mysql = MySQL(self.config_mock, self.db_driver_mock)
        schema_migrations = mysql.get_all_schema_migrations()

        self.assertEquals(len(expected_versions), len(schema_migrations))
        for index, migration in enumerate(schema_migrations):
            self.assertEqual(migration.id, expected_versions[index][0])
            self.assertEqual(migration.version, expected_versions[index][1])
            self.assertEqual(migration.label, expected_versions[index][2])
            self.assertEqual(migration.file_name, expected_versions[index][3])
            self.assertEqual(
                migration.sql_up,
                expected_versions[index][4] and expected_versions[index][4]
                or "")
            self.assertEqual(
                migration.sql_down,
                expected_versions[index][5] and expected_versions[index][5]
                or "")

        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(2, self.db_mock.commit.call_count)
        self.assertEqual(5, 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(
                'select id, version, label, name, sql_up, sql_down from __db_version__ order by id;'
            )
        ]
        self.assertEqual(expected_execute_calls,
                         self.cursor_mock.execute.mock_calls)
        self.assertEqual(4, self.cursor_mock.close.call_count)
示例#8
0
 def test_it_should_raise_whem_migration_sql_has_a_syntax_error_sql_with_codec_error(
         self):
     mysql = MySQL(self.config_mock, self.db_driver_mock)
     expected_raised_message = u"error executing migration: invalid sql syntax 'create table foo(); create table spam()); -- ônibus'".encode(
         "utf-8")
     self.assertRaisesWithMessage(
         Exception,
         expected_raised_message,
         mysql.change,
         u"create table foo(); create table spam()); -- ônibus",
         "20090212112104",
         "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration",
         "create table foo(); create table spam());",
         "drop table spam;",
         label_version="label")
示例#9
0
    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)
示例#10
0
    def test_it_should_stop_process_when_an_error_occur_during_connect_database(
            self):
        self.db_driver_mock.connect.side_effect = Exception(
            "error when connecting")

        try:
            MySQL(self.config_mock, self.db_driver_mock)
            self.fail("it should not get here")
        except Exception as e:
            self.assertEqual(
                "could not connect to database: error when connecting", str(e))

        self.assertEqual(0, self.db_mock.query.call_count)
        self.assertEqual(0, self.db_mock.commit.call_count)
        self.assertEqual(0, self.db_mock.close.call_count)

        self.assertEqual(0, self.cursor_mock.execute.call_count)
        self.assertEqual(0, self.cursor_mock.close.call_count)
示例#11
0
    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)
示例#12
0
    def test_it_should_stop_process_when_an_error_occur_during_drop_database(
            self):
        self.config_dict["drop_db_first"] = True
        self.db_mock.query.side_effect = Exception("error when dropping")

        try:
            MySQL(self.config_mock, self.db_driver_mock)
            self.fail("it should not get here")
        except Exception as e:
            self.assertEqual(
                "can't drop database 'migration_test'; \nerror when dropping",
                str(e))

        expected_query_calls = [
            call(
                'set foreign_key_checks=0; drop database if exists `migration_test`;'
            )
        ]
        self.assertEqual(expected_query_calls, self.db_mock.query.mock_calls)
        self.assertEqual(0, self.db_mock.commit.call_count)
        self.assertEqual(1, self.db_mock.close.call_count)

        self.assertEqual(0, self.cursor_mock.execute.call_count)
        self.assertEqual(0, self.cursor_mock.close.call_count)
示例#13
0
 def test_it_should_use_given_configuration(self):
     self.config_dict['database_port'] = 9876
     MySQL(self.config_mock)
     self.assertEqual(
         call(passwd='pass', host='somehost', user='******', port=9876),
         sys.modules['MySQLdb'].connect.call_args)
示例#14
0
 def test_it_should_use_default_port(self):
     MySQL(self.config_mock)
     self.assertEqual(
         call(passwd='pass', host='somehost', user='******', port=3306),
         sys.modules['MySQLdb'].connect.call_args)
示例#15
0
 def test_it_should_use_mysqldb_as_driver(self):
     MySQL(self.config_mock)
     self.assertNotEqual(0, sys.modules['MySQLdb'].connect.call_count)