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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
require "spec_helper"
describe DataMigrate::DataMigrator do
let(:subject) { DataMigrate::DataMigrator }
let(:db_config) do
{
adapter: "sqlite3",
database: "spec/db/test.db"
}
end
before do
allow(DataMigrate::DataMigrator).to receive(:db_config) { db_config }
ActiveRecord::Base.establish_connection(db_config)
end
describe :load_migrated do
before do
allow(subject).to receive(:db_config) { db_config }.at_least(:once)
ActiveRecord::Base.establish_connection(db_config)
::ActiveRecord::SchemaMigration.create_table
DataMigrate::DataSchemaMigration.create_table
end
after do
ActiveRecord::Migration.drop_table("data_migrations")
ActiveRecord::Migration.drop_table("schema_migrations")
end
it do
subject.assure_data_schema_table
DataMigrate::DataSchemaMigration.create(version: 20090000000000)
::ActiveRecord::SchemaMigration.create(version: 20100000000000)
DataMigrate::DataSchemaMigration.create(version: 20110000000000)
::ActiveRecord::SchemaMigration.create(version: 20120000000000)
migrated = subject.new(:up, []).load_migrated
expect(migrated.count).to eq 2
expect(migrated).to include 20090000000000
expect(migrated).to include 20110000000000
end
end
describe :assure_data_schema_table do
before do
allow(subject).to receive(:db_config) { db_config }.at_least(:once)
ActiveRecord::Base.establish_connection(db_config)
end
after do
ActiveRecord::Migration.drop_table("data_migrations")
end
it do
ActiveRecord::Migration.drop_table("data_migrations") rescue nil
subject.assure_data_schema_table
expect(
ActiveRecord::Base.connection.table_exists?("data_migrations")
).to eq true
end
end
describe "#migrations_status" do
before do
allow(subject).to receive(:db_config) { db_config }.at_least(:once)
ActiveRecord::Base.establish_connection(db_config)
::ActiveRecord::SchemaMigration.create_table
DataMigrate::DataSchemaMigration.create_table
end
it "returns all migrations statuses" do
status = subject.migrations_status
expect(status.length).to eq 2
expect(status.first).to eq ["down", "20091231235959", "Some name"]
expect(status.second).to eq ["down", "20171231235959", "Super update"]
end
end
describe :match do
context "when the file does not match" do
it "returns nil" do
expect(subject.match("not_a_data_migration_file")).to be_nil
end
end
context "when the file doesn't end in .rb" do
it "returns nil" do
expect(subject.match("20091231235959_some_name.rb.un~")).to be_nil
end
end
context "when the file matches" do
it "returns a valid MatchData object" do
match_data = subject.match("20091231235959_some_name.rb")
expect(match_data[0]).to eq "20091231235959_some_name.rb"
expect(match_data[1]).to eq "20091231235959"
expect(match_data[2]).to eq "some_name"
end
end
end
end
|