require 'test_helper' class PtOscMigrationIntegrationTest < ActiveRecord::TestCase class TestMigration < ActiveRecord::PtOscMigration; end context 'a migration' do setup do @migration = TestMigration.new @migration.stubs(:logger).returns(stub_everything) end context 'connected to a pt-osc database' do setup do ActiveRecord::Base.establish_connection(test_spec('test_execute')) @migration.instance_variable_set(:@connection, ActiveRecord::Base.connection) end context 'on an existing table with an existing column' do setup do @table_name = Faker::Lorem.word @index_name = Faker::Lorem.words.join('_') ActiveRecord::Base.connection.execute "DROP TABLE IF EXISTS `#{@table_name}`;" # can't use "id" because it's generated by Faker::Lorem.word :) ActiveRecord::Base.connection.execute <<-SQL CREATE TABLE `#{@table_name}` ( `primary` int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`primary`) ); SQL end teardown do ActiveRecord::Base.connection.execute "DROP TABLE IF EXISTS `#{@table_name}`;" end context 'new columns' do setup do @new_column_name = Faker::Lorem.word @arguments = [ nil, @migration, @table_name, @new_column_name, {} ] end add_column_fixtures.each do |test| defaults = [test[:default]].flatten(1).compact # remove nils for columns that can't have a default expected_defaults = test[:expected_default] ? [test[:expected_default]].flatten(1) : defaults should "add a nullable #{test[:type]} column with default null" do column_name = "#{@new_column_name}_#{test[:type]}" @arguments[0] = sprintf(test[:command], table: @table_name, column: column_name, default: nil, nullable: true) @arguments[-2] = column_name @arguments[-1] = { type: test[:type], null: true, default: nil } migrate_and_test_field *@arguments end defaults.each_with_index do |default, index| should "add a nullable #{test[:type]} column with default value #{default}" do column_name = "#{@new_column_name}_#{test[:type]}" @arguments[0] = sprintf(test[:command], table: @table_name, column: column_name, default: default, nullable: true) @arguments[-2] = column_name @arguments[-1] = { type: test[:type], null: true, default: expected_defaults[index] } migrate_and_test_field *@arguments end should "add a not-nullable #{test[:type]} column with default value #{default}" do column_name = "#{@new_column_name}_#{test[:type]}" @arguments[0] = sprintf(test[:command], table: @table_name, column: column_name, default: default, nullable: false) @arguments[-2] = column_name @arguments[-1] = { type: test[:type], null: false, default: expected_defaults[index] } migrate_and_test_field *@arguments end end end end end end end end