summaryrefslogtreecommitdiffstats
path: root/vendor/plugins/engines/test/unit/migration_test.rb
blob: eae7fe7d4b9dbf058bc6d2bdd1e1b42a59311b0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require File.dirname(__FILE__) + '/../test_helper'
require 'rails_generator'
require 'rails_generator/scripts/generate'

class MigrationsTest < Test::Unit::TestCase
  
  @@migration_dir = "#{RAILS_ROOT}/db/migrate"

  def setup
    ActiveRecord::Migration.verbose = false
    Engines.plugins[:test_migration].migrate(0)
  end
  
  def teardown
    FileUtils.rm_r(@@migration_dir) if File.exist?(@@migration_dir)
  end
  
  def test_engine_migrations_can_run_down
    assert !table_exists?('tests'), ActiveRecord::Base.connection.tables.inspect
    assert !table_exists?('others'), ActiveRecord::Base.connection.tables.inspect
    assert !table_exists?('extras'), ActiveRecord::Base.connection.tables.inspect
  end
    
  def test_engine_migrations_can_run_up
    Engines.plugins[:test_migration].migrate(3)
    assert table_exists?('tests')
    assert table_exists?('others')
    assert table_exists?('extras')
  end
  
  def test_engine_migrations_can_upgrade_incrementally
    Engines.plugins[:test_migration].migrate(1)
    assert table_exists?('tests')
    assert !table_exists?('others')
    assert !table_exists?('extras')
    assert_equal 1, Engines::Plugin::Migrator.current_version(Engines.plugins[:test_migration])
    
    
    Engines.plugins[:test_migration].migrate(2)
    assert table_exists?('others')
    assert_equal 2, Engines::Plugin::Migrator.current_version(Engines.plugins[:test_migration])
    
    
    Engines.plugins[:test_migration].migrate(3)
    assert table_exists?('extras')
    assert_equal 3, Engines::Plugin::Migrator.current_version(Engines.plugins[:test_migration])
  end
    
  def test_generator_creates_plugin_migration_file
    Rails::Generator::Scripts::Generate.new.run(['plugin_migration', 'test_migration'], :quiet => true)
    assert migration_file, "migration file is missing"
  end
  
  private
  
  def table_exists?(table)
    ActiveRecord::Base.connection.tables.include?(table)
  end
  
  def migration_file
    Dir["#{@@migration_dir}/*test_migration_to_version_3.rb"][0]
  end
end