require File.expand_path("#{File.dirname(__FILE__)}/../test_helper") class PluginTest < Test::Unit::TestCase def setup @plugin = Plugin.new("#{RAILS_ROOT}/vendor/plugins/plugin_with_migrations") ActiveRecord::Migration.verbose = true initialize_schema_information end def test_should_have_migration_path assert_equal "#{RAILS_ROOT}/vendor/plugins/plugin_with_migrations/db/migrate", @plugin.migration_path end def test_current_version_should_be_zero_if_no_version_found assert_equal 0, @plugin.current_version end def test_current_version_should_be_latest_migrated_in_the_database PluginSchemaInfo.create(:plugin_name => 'plugin_with_migrations', :version => 2) assert_equal 2, @plugin.current_version end def test_latest_version_should_be_zero_if_no_migrations_found plugin = Plugin.new("#{RAILS_ROOT}/vendor/plugins/plugin_with_no_migrations") assert_equal 0, plugin.latest_version end def test_latest_version_should_be_latest_migration_available_if_migrations_exist assert_equal 2, @plugin.latest_version end def test_should_migrate_to_latest_version_by_default @plugin.migrate assert Company.table_exists? assert Employee.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_to_specific_version_if_version_specified @plugin.migrate(1) assert Company.table_exists? assert !Employee.table_exists? assert_equal 1, PluginSchemaInfo.count expected = [ PluginSchemaInfo.new(:plugin_name => 'plugin_with_migrations', :version => 1) ] assert_equal expected, PluginSchemaInfo.find(:all) @plugin.migrate(0) assert !Company.table_exists? assert !Employee.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_have_fixtures_path assert_equal "#{RAILS_ROOT}/vendor/plugins/plugin_with_migrations/test/fixtures", @plugin.fixtures_path end def test_fixtures_should_include_all_by_default expected = [ "#{@plugin.fixtures_path}/companies.yml", "#{@plugin.fixtures_path}/employees.yml" ] assert_equal expected, @plugin.fixtures end def test_fixtures_should_only_include_specific_fixtures_if_specified assert_equal ["#{@plugin.fixtures_path}/companies.yml"], @plugin.fixtures('companies') expected = [ "#{@plugin.fixtures_path}/companies.yml", "#{@plugin.fixtures_path}/employees.yml" ] assert_equal expected, @plugin.fixtures('companies,employees') end def test_load_should_create_all_fixtures_by_default @plugin.migrate @plugin.load_fixtures assert_equal 1, Company.count assert_equal 2, Employee.count end def test_load_should_only_create_specific_fixtures_if_specified @plugin.migrate @plugin.load_fixtures('companies') assert_equal 1, Company.count assert_equal 0, Employee.count end def teardown ActiveRecord::Base.connection.drop_table(ActiveRecord::Migrator.schema_info_table_name) [Company, Employee, PluginSchemaInfo].each do |model| ActiveRecord::Base.connection.drop_table(model.table_name) if model.table_exists? end end end