Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
stop making ddl changes for migrator tests
  • Loading branch information
tenderlove committed Jan 16, 2012
1 parent ccbd201 commit 867f504
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 67 deletions.
55 changes: 0 additions & 55 deletions activerecord/test/cases/migration_test.rb
Expand Up @@ -245,61 +245,6 @@ def test_only_loads_pending_migrations
assert names.include?('InnocentJointable')
end

def test_migrator_db_has_no_schema_migrations_table
# Oracle adapter raises error if semicolon is present as last character
if current_adapter?(:OracleAdapter)
ActiveRecord::Base.connection.execute("DROP TABLE schema_migrations")
else
ActiveRecord::Base.connection.execute("DROP TABLE schema_migrations;")
end
assert_nothing_raised do
ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid", 1)
end
end

def test_migrator_going_down_due_to_version_target
ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/valid", 1)
ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid", 0)

assert !Person.column_methods_hash.include?(:last_name)
assert !Reminder.table_exists?

ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid")

Person.reset_column_information
assert Person.column_methods_hash.include?(:last_name)
assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
assert_equal "hello world", Reminder.find(:first).content
end

def test_migrator_rollback
ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid")
assert_equal(3, ActiveRecord::Migrator.current_version)

ActiveRecord::Migrator.rollback(MIGRATIONS_ROOT + "/valid")
assert_equal(2, ActiveRecord::Migrator.current_version)

ActiveRecord::Migrator.rollback(MIGRATIONS_ROOT + "/valid")
assert_equal(1, ActiveRecord::Migrator.current_version)

ActiveRecord::Migrator.rollback(MIGRATIONS_ROOT + "/valid")
assert_equal(0, ActiveRecord::Migrator.current_version)

ActiveRecord::Migrator.rollback(MIGRATIONS_ROOT + "/valid")
assert_equal(0, ActiveRecord::Migrator.current_version)
end

def test_migrator_forward
ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid", 1)
assert_equal(1, ActiveRecord::Migrator.current_version)

ActiveRecord::Migrator.forward(MIGRATIONS_ROOT + "/valid", 2)
assert_equal(3, ActiveRecord::Migrator.current_version)

ActiveRecord::Migrator.forward(MIGRATIONS_ROOT + "/valid")
assert_equal(3, ActiveRecord::Migrator.current_version)
end

def test_get_all_versions
ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid")
assert_equal([1,2,3], ActiveRecord::Migrator.get_all_versions)
Expand Down
90 changes: 78 additions & 12 deletions activerecord/test/cases/migrator_test.rb
Expand Up @@ -160,42 +160,42 @@ def test_migrator_one_up
calls, migrations = sensors(3)

ActiveRecord::Migrator.new(:up, migrations, 1).migrate
assert_equal [[:up, 0], [:up, 1]], calls
assert_equal [[:up, 1], [:up, 2]], calls
calls.clear

ActiveRecord::Migrator.new(:up, migrations, 2).migrate
assert_equal [[:up, 2]], calls
assert_equal [[:up, 3]], calls
end

def test_migrator_one_down
calls, migrations = sensors(3)

ActiveRecord::Migrator.new(:up, migrations).migrate
assert_equal [[:up, 0], [:up, 1], [:up, 2]], calls
assert_equal [[:up, 1], [:up, 2], [:up, 3]], calls
calls.clear

ActiveRecord::Migrator.new(:down, migrations, 1).migrate

assert_equal [[:down, 2]], calls
assert_equal [[:down, 3]], calls
end

def test_migrator_one_up_one_down
calls, migrations = sensors(3)

ActiveRecord::Migrator.new(:up, migrations, 1).migrate
assert_equal [[:up, 0], [:up, 1]], calls
assert_equal [[:up, 1], [:up, 2]], calls
calls.clear

ActiveRecord::Migrator.new(:down, migrations, 0).migrate
assert_equal [[:down, 1]], calls
assert_equal [[:down, 2]], calls
end

def test_migrator_double_up
calls, migrations = sensors(3)
assert_equal(0, ActiveRecord::Migrator.current_version)

ActiveRecord::Migrator.new(:up, migrations, 1).migrate
assert_equal [[:up, 0], [:up, 1]], calls
assert_equal [[:up, 1], [:up, 2]], calls
calls.clear

ActiveRecord::Migrator.new(:up, migrations, 1).migrate
Expand All @@ -208,11 +208,11 @@ def test_migrator_double_down
assert_equal(0, ActiveRecord::Migrator.current_version)

ActiveRecord::Migrator.new(:up, migrations, 1).run
assert_equal [[:up, 1]], calls
assert_equal [[:up, 2]], calls
calls.clear

ActiveRecord::Migrator.new(:down, migrations, 1).run
assert_equal [[:down, 1]], calls
assert_equal [[:down, 2]], calls
calls.clear

ActiveRecord::Migrator.new(:down, migrations, 1).run
Expand Down Expand Up @@ -250,19 +250,74 @@ def test_target_version_zero_should_run_only_once

# migrate up to 1
ActiveRecord::Migrator.new(:up, migrations, 1).migrate
assert_equal [[:up, 0], [:up, 1]], calls
assert_equal [[:up, 1], [:up, 2]], calls
calls.clear

# migrate down to 0
ActiveRecord::Migrator.new(:down, migrations, 0).migrate
assert_equal [[:down, 1]], calls
assert_equal [[:down, 2]], calls
calls.clear

# migrate down to 0 again
ActiveRecord::Migrator.new(:down, migrations, 0).migrate
assert_equal [], calls
end

def test_migrator_going_down_due_to_version_target
calls, migrator = migrator_class(3)

migrator.up("valid", 1)
assert_equal [[:up, 1], [:up, 2]], calls
calls.clear

migrator.migrate("valid", 0)
assert_equal [[:down, 2]], calls
calls.clear

migrator.migrate("valid")
assert_equal [[:up, 2], [:up, 3]], calls
end

def test_migrator_rollback
_, migrator = migrator_class(4)

migrator.migrate("valid")
assert_equal(3, ActiveRecord::Migrator.current_version)

migrator.rollback("valid")
assert_equal(2, ActiveRecord::Migrator.current_version)

migrator.rollback("valid")
assert_equal(1, ActiveRecord::Migrator.current_version)

migrator.rollback("valid")
assert_equal(0, ActiveRecord::Migrator.current_version)

migrator.rollback("valid")
assert_equal(0, ActiveRecord::Migrator.current_version)
end

def test_migrator_db_has_no_schema_migrations_table
_, migrator = migrator_class(3)

ActiveRecord::Base.connection.execute("DROP TABLE schema_migrations")
refute ActiveRecord::Base.connection.table_exists?('schema_migrations')
migrator.migrate("valid", 1)
assert ActiveRecord::Base.connection.table_exists?('schema_migrations')
end

def test_migrator_forward
_, migrator = migrator_class(3)
migrator.migrate("/valid", 1)
assert_equal(1, ActiveRecord::Migrator.current_version)

migrator.forward("/valid", 2)
assert_equal(3, ActiveRecord::Migrator.current_version)

migrator.forward("/valid")
assert_equal(3, ActiveRecord::Migrator.current_version)
end

private
def m(name, version, &block)
x = Sensor.new name, version
Expand All @@ -276,10 +331,21 @@ def sensors(count)
calls = []
migrations = count.times.map { |i|
m(nil, i) { |c,migration|
calls << [c, migration.version]
calls << [c, migration.version + 1]
}
}
[calls, migrations]
end

def migrator_class(count)
calls, migrations = sensors(count)

migrator = Class.new(Migrator).extend(Module.new {
define_method(:migrations) { |paths|
migrations
}
})
[calls, migrator]
end
end
end

0 comments on commit 867f504

Please sign in to comment.