def test_it_should_validate_if_filename_has_only_alphanumeric_chars_and_migration_extension( self): self.assertTrue( Migration.is_file_name_valid( '20090214120600_valid_migration_file_name.migration')) self.assertFalse( Migration.is_file_name_valid( '200902141206000_valid_migration_file_name.migration')) self.assertFalse( Migration.is_file_name_valid( '20090214120600_invalid_migration_file_name.migration~')) self.assertFalse( Migration.is_file_name_valid('simple-db-migrate.conf')) self.assertFalse(Migration.is_file_name_valid('abra.cadabra')) self.assertFalse( Migration.is_file_name_valid('randomrandomrandom.migration')) self.assertFalse( Migration.is_file_name_valid( '21420101000000-wrong-separators.migration')) self.assertFalse( Migration.is_file_name_valid( '2009021401_old_file_name_style.migration')) self.assertFalse( Migration.is_file_name_valid( '20090214120600_good_name_bad_extension.foo')) self.assertFalse(Migration.is_file_name_valid('spamspamspamspamspaam'))
def test_it_should_create_migration_file(self, strftime_mock): self.assertFalse( os.path.exists( '20120303194030_create_a_file_test_migration.migration')) Migration.create('create_a_file_test_migration', '.') self.assertTrue( os.path.exists( '20120303194030_create_a_file_test_migration.migration'))
def test_it_should_raise_exception_if_an_error_hapens_when_writing_the_file( self, open_mock, strftime_mock): try: Migration.create('test_migration') self.fail('it should not pass here') except Exception as e: self.assertEqual( "could not create file ('./20120303194030_test_migration.migration')", str(e))
def test_it_should_validate_if_filename_has_only_alphanumeric_chars_and_migration_extension(self): self.assertTrue(Migration.is_file_name_valid('20090214120600_valid_migration_file_name.migration')) self.assertFalse(Migration.is_file_name_valid('200902141206000_valid_migration_file_name.migration')) self.assertFalse(Migration.is_file_name_valid('20090214120600_invalid_migration_file_name.migration~')) self.assertFalse(Migration.is_file_name_valid('simple-db-migrate.conf')) self.assertFalse(Migration.is_file_name_valid('abra.cadabra')) self.assertFalse(Migration.is_file_name_valid('randomrandomrandom.migration')) self.assertFalse(Migration.is_file_name_valid('21420101000000-wrong-separators.migration')) self.assertFalse(Migration.is_file_name_valid('2009021401_old_file_name_style.migration')) self.assertFalse(Migration.is_file_name_valid('20090214120600_good_name_bad_extension.foo')) self.assertFalse(Migration.is_file_name_valid('spamspamspamspamspaam'))
def test_it_should_sort_a_migrations_list_in_rerverse_order(self): migrations = [] migrations.append(Migration('20090727141400_test_migration.migration')) migrations.append(Migration('20090214120600_example_file_name_test_migration.migration')) migrations.append(Migration('20090727141503_test_migration.migration')) migrations.append(Migration('20090727104700_test_migration.migration')) sorted_migrations = Migration.sort_migrations_list(migrations, reverse=True) self.assertEqual('20090727141503', sorted_migrations[0].version) self.assertEqual('20090727141400', sorted_migrations[1].version) self.assertEqual('20090727104700', sorted_migrations[2].version) self.assertEqual('20090214120600', sorted_migrations[3].version)
def test_it_should_raise_exception_when_file_does_not_exist(self): try: Migration('20090727104700_this_file_does_not_exist.migration') except Exception as e: self.assertEqual( 'migration file does not exist (20090727104700_this_file_does_not_exist.migration)', str(e))
def test_it_should_get_basic_properties_when_path_is_relative2(self): migration = Migration(file='./20090727104700_test_migration.migration') self.assertEqual('20090727104700', migration.version) self.assertEqual('20090727104700_test_migration.migration', migration.file_name) self.assertEqual( os.path.abspath('./20090727104700_test_migration.migration'), migration.abspath)
def test_it_should_get_sql_command_containing_unicode_characters(self): file_name = '20090508155742_test_migration.migration' create_file( file_name, content='SQL_UP=u"some sql command"\nSQL_DOWN=u"other sql command"' ) migration = Migration(file_name) self.assertEqual(u"some sql command", migration.sql_up) self.assertEqual(u"other sql command", migration.sql_down)
def test_it_should_return_an_empty_string_if_sql_or_script_encoding_are_invalid(self): self.assertEqual('', Migration.ensure_sql_unicode('', '')) self.assertEqual('', Migration.ensure_sql_unicode('', "iso8859-1")) self.assertEqual('', Migration.ensure_sql_unicode('', None)) self.assertEqual('', Migration.ensure_sql_unicode('', False)) self.assertEqual('', Migration.ensure_sql_unicode('sql', '')) self.assertEqual('', Migration.ensure_sql_unicode(None, "iso8859-1")) self.assertEqual('', Migration.ensure_sql_unicode(False, "iso8859-1"))
def test_it_should_get_sql_command_containing_non_ascii_characters(self): file_name = '20090508155742_test_migration.migration' create_file( file_name, content= 'SQL_UP=u"some sql command ç"\nSQL_DOWN=u"other sql command ã"', encoding='utf-8') migration = Migration(file_name) self.assertEqual(u"some sql command ç", migration.sql_up) self.assertEqual(u"other sql command ã", migration.sql_down)
def test_it_should_get_basic_properties_when_path_is_relative3(self): here = os.path.dirname(os.path.relpath(__file__)) migration = Migration( file='%s/../20090727104700_test_migration.migration' % here) self.assertEqual('20090727104700', migration.version) self.assertEqual('20090727104700_test_migration.migration', migration.file_name) self.assertEqual( os.path.abspath('./20090727104700_test_migration.migration'), migration.abspath)
def test_it_should_get_sql_command_containing_non_ascii_characters_with_non_utf8_encoding( self): file_name = '20090508155742_test_migration.migration' create_file( file_name, content= 'SQL_UP=u"some sql command ç"\nSQL_DOWN=u"other sql command ã"'. decode('iso8859-1'), encoding='iso8859-1') migration = Migration(file_name, script_encoding='iso8859-1') self.assertEqual(u"some sql command \xc3\xa7", migration.sql_up) self.assertEqual(u"other sql command \xc3\xa3", migration.sql_down)
def setUp(self): create_migration_file('20090214120600_example_file_name_test_migration.migration', sql_up='xxx', sql_down='yyy') create_migration_file('20090727104700_test_migration.migration', sql_up='xxx', sql_down='yyy') create_migration_file('20090727141400_test_migration.migration', sql_up='xxx', sql_down='yyy') create_migration_file('20090727141503_test_migration.migration', sql_up='xxx', sql_down='yyy') create_migration_file('20090727141505_01_test_migration.migration', sql_up='xxx', sql_down='yyy') create_migration_file('20090727141505_02_test_migration.migration', sql_up='xxx', sql_down='yyy') create_migration_file('20090727113900_empty_sql_up_test_migration.migration', sql_up='', sql_down='zzz') create_migration_file('20090727113900_empty_sql_down_test_migration.migration', sql_up='zzz', sql_down='') create_file('20090727114700_empty_file_test_migration.migration') create_file('20090727114700_without_sql_down_test_migration.migration', 'SQL_UP=""') create_file('20090727114700_without_sql_up_test_migration.migration', 'SQL_DOWN=""') self.migration = Migration(label="generate_test_migration", version="20120101010100", sql_up="some_sql", sql_down="some_sql_down")
def test_it_should_get_sql_command_containing_unicode_characters_and_python_code( self): file_name = '20090508155742_test_migration.migration' create_file( file_name, content= 'import os\nSQL_UP=u"some sql command %s" % os.path.abspath(\'.\')\nSQL_DOWN=u"other sql command %s" % os.path.abspath(\'.\')' ) migration = Migration(file_name) self.assertEqual(u"some sql command %s" % os.path.abspath('.'), migration.sql_up) self.assertEqual(u"other sql command %s" % os.path.abspath('.'), migration.sql_down)
def test_it_should_get_sql_command_containing_non_ascii_characters_and_python_code_without_scope( self): file_name = '20090508155742_test_migration.migration' create_file( file_name, content= 'SQL_UP=u"some sql command ç %s" % os.path.abspath(\'.\')\nSQL_DOWN=u"other sql command ã %s" % os.path.abspath(\'.\')', encoding='utf-8') migration = Migration(file_name) self.assertEqual(u"some sql command ç %s" % os.path.abspath('.'), migration.sql_up) self.assertEqual(u"other sql command ã %s" % os.path.abspath('.'), migration.sql_down)
def test_it_should_get_sql_command_containing_non_ascii_characters_and_python_code_with_non_utf8_encoding( self): file_name = '20090508155742_test_migration.migration' create_file( file_name, content= 'import os\nSQL_UP=u"some sql command ç %s" % os.path.abspath(\'.\')\nSQL_DOWN=u"other sql command ã %s" % os.path.abspath(\'.\')' .decode('iso8859-1'), encoding='iso8859-1') migration = Migration(file_name, script_encoding='iso8859-1') self.assertEqual( u"some sql command \xc3\xa7 %s" % os.path.abspath('.'), migration.sql_up) self.assertEqual( u"other sql command \xc3\xa3 %s" % os.path.abspath('.'), migration.sql_down)
def test_it_should_use_gmt_time_when_asked_to_use_utc(self, gmtime_mock): Migration.create('test_migration', utc_timestamp=True) gmtime_mock.assert_called_once()
def test_it_should_raise_exception_if_an_error_hapens_when_writing_the_file(self, open_mock, strftime_mock): try: Migration.create('test_migration') self.fail('it should not pass here') except Exception as e: self.assertEqual("could not create file ('./20120303194030_test_migration.migration')", str(e))
def test_it_should_create_migration_file(self, strftime_mock): self.assertFalse(os.path.exists('20120303194030_create_a_file_test_migration.migration')) Migration.create('create_a_file_test_migration', '.') self.assertTrue(os.path.exists('20120303194030_create_a_file_test_migration.migration'))
def test_it_should_compare_to_migration_versions_and_tell_which_is_newer(self): m1 = Migration('20090727104700_test_migration.migration') m2 = Migration('20090727141400_test_migration.migration') m3 = Migration('20090727141503_test_migration.migration') m4 = Migration('20090727141505_01_test_migration.migration') m5 = Migration('20090727141505_02_test_migration.migration') self.assertEqual(-1, m1.compare_to(m2)) self.assertEqual(-1, m2.compare_to(m3)) self.assertEqual(-1, m1.compare_to(m3)) self.assertEqual(-1, m4.compare_to(m5)) self.assertEqual(1, m2.compare_to(m1)) self.assertEqual(1, m3.compare_to(m2)) self.assertEqual(1, m3.compare_to(m1)) self.assertEqual(1, m5.compare_to(m4)) self.assertEqual(0, m1.compare_to(m1)) self.assertEqual(0, m2.compare_to(m2)) self.assertEqual(0, m3.compare_to(m3)) self.assertEqual(0, m4.compare_to(m4)) self.assertEqual(0, m5.compare_to(m5))
def test_it_should_convert_sql_to_unicode_from_script_encoding(self): self.assertEqual(u'sql in iso8859-1', Migration.ensure_sql_unicode('sql in iso8859-1'.encode("iso8859-1"), "iso8859-1"))
def test_it_should_get_migration_version_from_file(self): migration = Migration( '20090214120600_example_file_name_test_migration.migration') self.assertEqual('20090214120600', migration.version)
def test_it_should_accept_a_dash_on_migration_file(self): migration = Migration( '20090214121600_example_file_name_with_dash-test_migration.migration' ) self.assertEqual('20090214121600', migration.version)
def test_it_should_use_local_time_when_asked_to_not_use_utc(self, localtime_mock): Migration.create('test_migration', utc_timestamp=False) localtime_mock.assert_called_once()
class MigrationTest(BaseTest): def setUp(self): create_migration_file('20090214120600_example_file_name_test_migration.migration', sql_up='xxx', sql_down='yyy') create_migration_file('20090727104700_test_migration.migration', sql_up='xxx', sql_down='yyy') create_migration_file('20090727141400_test_migration.migration', sql_up='xxx', sql_down='yyy') create_migration_file('20090727141503_test_migration.migration', sql_up='xxx', sql_down='yyy') create_migration_file('20090727141505_01_test_migration.migration', sql_up='xxx', sql_down='yyy') create_migration_file('20090727141505_02_test_migration.migration', sql_up='xxx', sql_down='yyy') create_migration_file('20090727113900_empty_sql_up_test_migration.migration', sql_up='', sql_down='zzz') create_migration_file('20090727113900_empty_sql_down_test_migration.migration', sql_up='zzz', sql_down='') create_file('20090727114700_empty_file_test_migration.migration') create_file('20090727114700_without_sql_down_test_migration.migration', 'SQL_UP=""') create_file('20090727114700_without_sql_up_test_migration.migration', 'SQL_DOWN=""') self.migration = Migration(label="generate_test_migration", version="20120101010100", sql_up="some_sql", sql_down="some_sql_down") def tearDown(self): delete_files('*test_migration.migration') def test_it_should_have_generate_file_method(self): assert self.migration.dumps_to_file, "Should have generate file method" def test_it_should_generate_migration_file(self): self.migration.dumps_to_file() assert os.path.exists("20120101010100_generate_test_migration.migration"), "File should exists" def test_it_should_generate_migration_with_rigth_content(self): self.migration.dumps_to_file() migration_expected = Migration("20120101010100_generate_test_migration.migration") assert self.migration.sql_up in migration_expected.sql_up assert self.migration.sql_down in migration_expected.sql_down def test_it_should_get_migration_version_from_file(self): migration = Migration('20090214120600_example_file_name_test_migration.migration') self.assertEqual('20090214120600', migration.version) def test_it_should_get_basic_properties_when_path_is_relative1(self): migration = Migration(file='20090727104700_test_migration.migration') self.assertEqual('20090727104700', migration.version) self.assertEqual('20090727104700_test_migration.migration', migration.file_name) self.assertEqual(os.path.abspath('./20090727104700_test_migration.migration'), migration.abspath) def test_it_should_get_basic_properties_when_path_is_relative2(self): migration = Migration(file='./20090727104700_test_migration.migration') self.assertEqual('20090727104700', migration.version) self.assertEqual('20090727104700_test_migration.migration', migration.file_name) self.assertEqual(os.path.abspath('./20090727104700_test_migration.migration'), migration.abspath) def test_it_should_get_basic_properties_when_path_is_relative3(self): here = os.path.dirname(os.path.relpath(__file__)) migration = Migration(file='%s/../20090727104700_test_migration.migration' % here) self.assertEqual('20090727104700', migration.version) self.assertEqual('20090727104700_test_migration.migration', migration.file_name) self.assertEqual(os.path.abspath('./20090727104700_test_migration.migration'), migration.abspath) def test_it_should_get_basic_properties_when_path_is_absolute(self): migration = Migration(file=os.path.abspath('./20090727104700_test_migration.migration')) self.assertEqual('20090727104700', migration.version) self.assertEqual('20090727104700_test_migration.migration', migration.file_name) self.assertEqual(os.path.abspath('./20090727104700_test_migration.migration'), migration.abspath) def test_it_should_get_sql_up_and_down(self): migration = Migration(file='20090727104700_test_migration.migration') self.assertEqual(migration.sql_up, 'xxx') self.assertEqual(migration.sql_down, 'yyy') def test_it_should_get_sql_command_containing_unicode_characters(self): file_name = '20090508155742_test_migration.migration' create_file(file_name, content='SQL_UP=u"some sql command"\nSQL_DOWN=u"other sql command"') migration = Migration(file_name) self.assertEqual(u"some sql command", migration.sql_up) self.assertEqual(u"other sql command", migration.sql_down) def test_it_should_get_sql_command_containing_unicode_characters_and_python_code(self): file_name = '20090508155742_test_migration.migration' create_file(file_name, content='import os\nSQL_UP=u"some sql command %s" % os.path.abspath(\'.\')\nSQL_DOWN=u"other sql command %s" % os.path.abspath(\'.\')') migration = Migration(file_name) self.assertEqual(u"some sql command %s" % os.path.abspath('.'), migration.sql_up) self.assertEqual(u"other sql command %s" % os.path.abspath('.'), migration.sql_down) def test_it_should_get_sql_command_containing_unicode_characters_and_python_code_without_scope(self): file_name = '20090508155742_test_migration.migration' create_file(file_name, content='SQL_UP=u"some sql command %s" % os.path.abspath(\'.\')\nSQL_DOWN=u"other sql command %s" % os.path.abspath(\'.\')') migration = Migration(file_name) self.assertEqual(u"some sql command %s" % os.path.abspath('.'), migration.sql_up) self.assertEqual(u"other sql command %s" % os.path.abspath('.'), migration.sql_down) def test_it_should_get_sql_command_containing_non_ascii_characters(self): file_name = '20090508155742_test_migration.migration' create_file(file_name, content='SQL_UP=u"some sql command ç"\nSQL_DOWN=u"other sql command ã"'.decode('utf-8')) migration = Migration(file_name) self.assertEqual(u"some sql command ç", migration.sql_up) self.assertEqual(u"other sql command ã", migration.sql_down) def test_it_should_get_sql_command_containing_non_ascii_characters_and_python_code(self): file_name = '20090508155742_test_migration.migration' create_file(file_name, content='import os\nSQL_UP=u"some sql command ç %s" % os.path.abspath(\'.\')\nSQL_DOWN=u"other sql command ã %s" % os.path.abspath(\'.\')'.decode('utf-8') ) migration = Migration(file_name) self.assertEqual(u"some sql command ç %s" % os.path.abspath('.'), migration.sql_up) self.assertEqual(u"other sql command ã %s" % os.path.abspath('.'), migration.sql_down) def test_it_should_get_sql_command_containing_non_ascii_characters_and_python_code_without_scope(self): file_name = '20090508155742_test_migration.migration' create_file(file_name, content='SQL_UP=u"some sql command ç %s" % os.path.abspath(\'.\')\nSQL_DOWN=u"other sql command ã %s" % os.path.abspath(\'.\')'.decode('utf-8')) migration = Migration(file_name) self.assertEqual(u"some sql command ç %s" % os.path.abspath('.'), migration.sql_up) self.assertEqual(u"other sql command ã %s" % os.path.abspath('.'), migration.sql_down) def test_it_should_get_sql_command_containing_non_ascii_characters_with_non_utf8_encoding(self): file_name = '20090508155742_test_migration.migration' create_file(file_name, content='SQL_UP=u"some sql command ç"\nSQL_DOWN=u"other sql command ã"'.decode('iso8859-1'), encoding='iso8859-1') migration = Migration(file_name, script_encoding='iso8859-1') self.assertEqual(u"some sql command \xc3\xa7", migration.sql_up) self.assertEqual(u"other sql command \xc3\xa3", migration.sql_down) def test_it_should_get_sql_command_containing_non_ascii_characters_and_python_code_with_non_utf8_encoding(self): file_name = '20090508155742_test_migration.migration' create_file(file_name, content='import os\nSQL_UP=u"some sql command ç %s" % os.path.abspath(\'.\')\nSQL_DOWN=u"other sql command ã %s" % os.path.abspath(\'.\')'.decode('iso8859-1'), encoding='iso8859-1') migration = Migration(file_name, script_encoding='iso8859-1') self.assertEqual(u"some sql command \xc3\xa7 %s" % os.path.abspath('.'), migration.sql_up) self.assertEqual(u"other sql command \xc3\xa3 %s" % os.path.abspath('.'), migration.sql_down) def test_it_should_get_sql_command_containing_non_ascii_characters_and_python_code_without_scope_with_non_utf8_encoding(self): file_name = '20090508155742_test_migration.migration' create_file(file_name, content='SQL_UP=u"some sql command ç %s" % os.path.abspath(\'.\')\nSQL_DOWN=u"other sql command ã %s" % os.path.abspath(\'.\')'.decode('iso8859-1'), encoding='iso8859-1') migration = Migration(file_name, script_encoding='iso8859-1') self.assertEqual(u"some sql command \xc3\xa7 %s" % os.path.abspath('.'), migration.sql_up) self.assertEqual(u"other sql command \xc3\xa3 %s" % os.path.abspath('.'), migration.sql_down) def test_it_should_raise_exception_when_migration_commands_are_empty(self): self.assertRaisesWithMessage(Exception, "migration command 'SQL_UP' is empty (%s)" % os.path.abspath('20090727113900_empty_sql_up_test_migration.migration'), Migration, '20090727113900_empty_sql_up_test_migration.migration') self.assertRaisesWithMessage(Exception, "migration command 'SQL_DOWN' is empty (%s)" % os.path.abspath('20090727113900_empty_sql_down_test_migration.migration'), Migration, '20090727113900_empty_sql_down_test_migration.migration') def test_it_should_raise_exception_when_migration_file_is_empty(self): self.assertRaisesWithMessage(Exception, "migration file is incorrect; it does not define 'SQL_UP' or 'SQL_DOWN' (%s)" % os.path.abspath('20090727114700_empty_file_test_migration.migration'), Migration, '20090727114700_empty_file_test_migration.migration') def test_it_should_raise_exception_when_migration_file_do_not_have_sql_up_constant(self): self.assertRaisesWithMessage(Exception, "migration file is incorrect; it does not define 'SQL_UP' or 'SQL_DOWN' (%s)" % os.path.abspath('20090727114700_without_sql_up_test_migration.migration'), Migration, '20090727114700_without_sql_up_test_migration.migration') def test_it_should_raise_exception_when_migration_file_do_not_have_sql_down_constant(self): self.assertRaisesWithMessage(Exception, "migration file is incorrect; it does not define 'SQL_UP' or 'SQL_DOWN' (%s)" % os.path.abspath('20090727114700_without_sql_down_test_migration.migration'), Migration, '20090727114700_without_sql_down_test_migration.migration') def test_it_should_compare_to_migration_versions_and_tell_which_is_newer(self): m1 = Migration('20090727104700_test_migration.migration') m2 = Migration('20090727141400_test_migration.migration') m3 = Migration('20090727141503_test_migration.migration') m4 = Migration('20090727141505_01_test_migration.migration') m5 = Migration('20090727141505_02_test_migration.migration') self.assertEqual(-1, m1.compare_to(m2)) self.assertEqual(-1, m2.compare_to(m3)) self.assertEqual(-1, m1.compare_to(m3)) self.assertEqual(-1, m4.compare_to(m5)) self.assertEqual(1, m2.compare_to(m1)) self.assertEqual(1, m3.compare_to(m2)) self.assertEqual(1, m3.compare_to(m1)) self.assertEqual(1, m5.compare_to(m4)) self.assertEqual(0, m1.compare_to(m1)) self.assertEqual(0, m2.compare_to(m2)) self.assertEqual(0, m3.compare_to(m3)) self.assertEqual(0, m4.compare_to(m4)) self.assertEqual(0, m5.compare_to(m5)) def test_it_should_raise_exception_when_file_does_not_exist(self): try: Migration('20090727104700_this_file_does_not_exist.migration') except Exception, e: self.assertEqual('migration file does not exist (20090727104700_this_file_does_not_exist.migration)', str(e))
def test_it_should_use_local_time_when_asked_to_not_use_utc( self, localtime_mock): Migration.create('test_migration', utc_timestamp=False) localtime_mock.assert_called_once()
def test_it_should_compare_to_migration_versions_and_tell_which_is_newer( self): m1 = Migration('20090727104700_test_migration.migration') m2 = Migration('20090727141400_test_migration.migration') m3 = Migration('20090727141503_test_migration.migration') m4 = Migration('20090727141505_01_test_migration.migration') m5 = Migration('20090727141505_02_test_migration.migration') self.assertEqual(-1, m1.compare_to(m2)) self.assertEqual(-1, m2.compare_to(m3)) self.assertEqual(-1, m1.compare_to(m3)) self.assertEqual(-1, m4.compare_to(m5)) self.assertEqual(1, m2.compare_to(m1)) self.assertEqual(1, m3.compare_to(m2)) self.assertEqual(1, m3.compare_to(m1)) self.assertEqual(1, m5.compare_to(m4)) self.assertEqual(0, m1.compare_to(m1)) self.assertEqual(0, m2.compare_to(m2)) self.assertEqual(0, m3.compare_to(m3)) self.assertEqual(0, m4.compare_to(m4)) self.assertEqual(0, m5.compare_to(m5))
def test_it_should_convert_sql_to_unicode_from_script_encoding(self): self.assertEqual( u'sql in iso8859-1', Migration.ensure_sql_unicode( 'sql in iso8859-1'.encode("iso8859-1"), "iso8859-1"))
def test_it_should_get_sql_up_and_down(self): migration = Migration(file='20090727104700_test_migration.migration') self.assertEqual(migration.sql_up, 'xxx') self.assertEqual(migration.sql_down, 'yyy')