require File.expand_path("#{File.dirname(__FILE__)}/../test_helper") class PluginMigrationsTest < Test::Unit::TestCase def setup ActiveRecord::Migration.verbose = true initialize_schema_information end def test_should_migrate_all_plugins_up_to_latest_version_by_default PluginAWeek::PluginMigrations.migrate assert Company.table_exists? assert Employee.table_exists? assert Author.table_exists? assert Article.table_exists? assert Comment.table_exists? assert_equal 2, PluginSchemaInfo.count expected = [ PluginSchemaInfo.new(:plugin_name => 'plugin_with_migrations', :version => 2), PluginSchemaInfo.new(:plugin_name => 'second_plugin_with_migrations', :version => 3) ] assert_equal expected, PluginSchemaInfo.find(:all) end def test_should_migrate_all_plugins_up_to_specific_version_if_specified PluginAWeek::PluginMigrations.migrate(nil, 3) assert Company.table_exists? assert Employee.table_exists? assert Author.table_exists? assert Article.table_exists? assert Comment.table_exists? assert_equal 2, PluginSchemaInfo.count expected = [ PluginSchemaInfo.new(:plugin_name => 'plugin_with_migrations', :version => 2), PluginSchemaInfo.new(:plugin_name => 'second_plugin_with_migrations', :version => 3) ] assert_equal expected, PluginSchemaInfo.find(:all) end def test_should_migrate_all_plugins_down_to_specific_version_if_specified PluginAWeek::PluginMigrations.migrate(nil, 3) PluginAWeek::PluginMigrations.migrate(nil, 1) assert Company.table_exists? assert !Employee.table_exists? assert Author.table_exists? assert !Article.table_exists? assert !Comment.table_exists? assert_equal 2, PluginSchemaInfo.count expected = [ PluginSchemaInfo.new(:plugin_name => 'plugin_with_migrations', :version => 1), PluginSchemaInfo.new(:plugin_name => 'second_plugin_with_migrations', :version => 1) ] assert_equal expected, PluginSchemaInfo.find(:all) end def test_should_migrate_specific_plugins_to_latest_version_if_specified PluginAWeek::PluginMigrations.migrate('plugin_with_migrations') assert Company.table_exists? assert Employee.table_exists? assert !Author.table_exists? assert !Article.table_exists? assert !Comment.table_exists? assert_equal 1, PluginSchemaInfo.count expected = [ PluginSchemaInfo.new(:plugin_name => 'plugin_with_migrations', :version => 2) ] assert_equal expected, PluginSchemaInfo.find(:all) end def test_should_migrate_specific_plugins_up_to_specific_version_if_both_specified PluginAWeek::PluginMigrations.migrate('plugin_with_migrations', 1) assert Company.table_exists? assert !Employee.table_exists? assert !Author.table_exists? assert !Article.table_exists? assert !Comment.table_exists? assert_equal 1, PluginSchemaInfo.count expected = [ PluginSchemaInfo.new(:plugin_name => 'plugin_with_migrations', :version => 2) ] assert_equal expected, PluginSchemaInfo.find(:all) end def test_should_migrate_specific_plugins_down_to_specific_version_if_both_specified PluginAWeek::PluginMigrations.migrate('plugin_with_migrations', 1) PluginAWeek::PluginMigrations.migrate('plugin_with_migrations', 0) assert !Company.table_exists? assert !Employee.table_exists? assert !Author.table_exists? assert !Article.table_exists? assert !Comment.table_exists? assert_equal 1, PluginSchemaInfo.count expected = [ PluginSchemaInfo.new(:plugin_name => 'plugin_with_migrations', :version => 0) ] assert_equal expected, PluginSchemaInfo.find(:all) end def test_should_load_fixtures_for_all_plugins_by_default PluginAWeek::PluginMigrations.migrate PluginAWeek::PluginMigrations.load_fixtures assert_equal 1, Company.count assert_equal 2, Employee.count assert_equal 2, Author.count assert_equal 1, Article.count end def test_should_load_specific_fixtures_for_all_plugins_if_specified PluginAWeek::PluginMigrations.migrate PluginAWeek::PluginMigrations.load_fixtures(nil, 'companies,authors') assert_equal 1, Company.count assert_equal 0, Employee.count assert_equal 2, Author.count assert_equal 0, Article.count end def test_should_load_all_fixtures_for_specific_plugins_if_specified PluginAWeek::PluginMigrations.migrate PluginAWeek::PluginMigrations.load_fixtures('plugin_with_migrations') assert_equal 1, Company.count assert_equal 2, Employee.count assert_equal 0, Author.count assert_equal 0, Article.count end def test_should_load_specific_fixtures_specific_plugins_if_both_specified PluginAWeek::PluginMigrations.migrate PluginAWeek::PluginMigrations.load_fixtures('plugin_with_migrations', 'companies') assert_equal 1, Company.count assert_equal 0, Employee.count assert_equal 0, Author.count assert_equal 0, Article.count end def teardown ActiveRecord::Base.table_name_prefix = '' ActiveRecord::Base.table_name_suffix = '' ActiveRecord::Base.connection.drop_table(ActiveRecord::Migrator.schema_info_table_name) [Article, Author, Comment, Company, Employee, PluginSchemaInfo].each do |model| ActiveRecord::Base.connection.drop_table(model.table_name) if model.table_exists? end end end