Part I test coverage increase for sequence migration code

pull/538/head
Rick Carlino 2017-11-27 10:51:01 -06:00
parent f896dbe075
commit a3a1ca9222
3 changed files with 32 additions and 14 deletions

View File

@ -14,6 +14,8 @@
# automagically run if the API determines a sequence is out of date.
module SequenceMigration
class Base
UP_IS_REQUIRED = "You forgot to implement an `up()`" +
" method on your migration"
# When we last ran a compaction (22 NOV 17), the highest version number was
# this:
HIGHEST_VERSION_AT_TIME_OF_LAST_COMPACTION = 4
@ -33,11 +35,6 @@ module SequenceMigration
self
.descendants
.map { |k| k::VERSION }
.each { |v|
if v < HIGHEST_VERSION_AT_TIME_OF_LAST_COMPACTION
raise "VERSION TOO LOW"
end
}
.max
end
@ -62,7 +59,7 @@ module SequenceMigration
end
def up
throw "You forgot to implement an `up()` method on your migration"
raise UP_IS_REQUIRED
end
def run

View File

@ -57,14 +57,6 @@ class Sequence < ApplicationRecord
Sequences::Migrate.run!(sequence: self, device: self.device)
end
# Helper used for QAing stuff on staging. Grabs a random sequence from the
# database, runs a migration (does not save) and prints to screen.
def self.spot_check
s = random
puts "Sequence ##{s.id} ========="
puts s.maybe_migrate.body.to_yaml
end
def self.random
Sequence.order("RANDOM()").first
end

View File

@ -44,6 +44,14 @@ FIXTURE = JSON.parse('[
}
]')
class MockMigration < SequenceMigration::Base
VERSION = -9
end
class MockMigrationTwo < SequenceMigration::Base
VERSION = 1
end
describe SequenceMigration do
it 'has a latest version' do
expect(SequenceMigration::Base.latest_version).to eq(5)
@ -63,4 +71,25 @@ describe SequenceMigration do
expect(s.body[1]["args"]["speed"]).to eq(100)
expect(s.body[2]["args"]["speed"]).to eq(100)
end
it 'warns developers that `up()` is required' do
base = SequenceMigration::Base.new(FactoryBot.create(:sequence))
expect { base.up }.to raise_error(SequenceMigration::Base::UP_IS_REQUIRED)
end
it 'checks for appropriate version number when running `before()`' do
base = MockMigration.new(FactoryBot.build(:sequence))
expect { base.before }
.to raise_error("Version must be -10 to run MockMigration. Got: 4")
end
it 'sorts migrations by version number' do
allow(SequenceMigration::Base)
.to receive(:descendants) { [MockMigration, MockMigrationTwo] }
sequence = FactoryBot.build(:sequence)
sequence.args["version"] = -10
result = SequenceMigration::Base.generate_list(sequence)
expect(result.first).to be_kind_of(MockMigration)
expect(result.last).to be_kind_of(MockMigrationTwo)
end
end