Skip to content

Commit

Permalink
BaseService specs
Browse files Browse the repository at this point in the history
  • Loading branch information
gangelo committed Feb 17, 2024
1 parent 991a0c8 commit 35a08c2
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/dsu/migration/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def backup_exist?
end

def backup_folder
@backup_folder ||= File.join(root_folder, "dsu-#{from_migration_version}-backup")
@backup_folder ||= backup_folder_for(migration_version: from_migration_version)
end

def update_migration_version!
Expand Down
4 changes: 0 additions & 4 deletions lib/dsu/migration/v20240210161248/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ def run_migration!
puts 'Migration completed successfully.'
end

def config_file_from
File.join(root_folder, '.dsu')
end

def dsu_folder_from
File.join(root_folder, 'dsu')
end
Expand Down
12 changes: 6 additions & 6 deletions lib/dsu/support/fileable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,6 @@ def gem_dir
Gem.loaded_specs['dsu'].gem_dir
end

# Back up folder

def backup_folder(version:)
File.join(dsu_folder, 'backup', version.to_s)
end

# Seed data files and folders

def seed_data_dsu_folder_for(migration_version:)
Expand Down Expand Up @@ -130,6 +124,12 @@ def project_file_for(project_name:)
end
alias project_file project_file_for

# Backup folders

def backup_folder_for(migration_version:)
File.join(root_folder, "dsu-#{migration_version}-backup")
end

extend self # rubocop:disable Style/ModuleFunction
end
end
Expand Down
144 changes: 144 additions & 0 deletions spec/dsu/migration/base_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# frozen_string_literal: true

# rubocop:disable RSpec/NestedGroups
RSpec.describe Dsu::Migration::BaseService, type: :migration do
subject(:base_service) { described_class.new(options: options) }

let(:options) { { pretend: false } }

describe '#initialize' do
it 'does not raise an error' do
expect { base_service }.to_not raise_error
end
end

context 'when the required methods are not implemented' do
describe '#migrate_if!' do
subject(:base_service_error) { base_service.migrate_if! }

let(:expected_error) { NotImplementedError }

it_behaves_like 'an error is raised'
end

describe '.migrates_to_latest_migration_version?' do
subject(:base_service_error) { described_class.migrates_to_latest_migration_version? }

let(:expected_error) { NotImplementedError }

it_behaves_like 'an error is raised'
end

describe '.from_migration_version' do
subject(:base_service_error) { described_class.from_migration_version }

let(:expected_error) { NotImplementedError }

it_behaves_like 'an error is raised'
end

describe '.to_migration_version' do
subject(:base_service_error) { described_class.to_migration_version }

let(:expected_error) { NotImplementedError }

it_behaves_like 'an error is raised'
end
end

context 'when the required methods are implemented' do
subject(:base_service) do
service = Class.new(described_class) do
class << self
attr_accessor :from_migration_version, :to_migration_version
end
end.new(options: options)
service.class.from_migration_version = from_migration_version
service.class.to_migration_version = to_migration_version
service
end

before do
create(:migration_version, version: to_migration_version, options: options)
end

let(:from_migration_version) { 1 }
let(:to_migration_version) { Dsu::Migration::VERSION }

describe 'class methods' do
describe '.from_migration_version' do
let(:from_migration_version) { 1 }
let(:to_migration_version) { 2 }

it 'returns the from migration version' do
expect(base_service.class.from_migration_version).to eq(from_migration_version)
end
end

describe '.to_migration_version' do
let(:from_migration_version) { 1 }
let(:to_migration_version) { 2 }

it 'returns the to migration version' do
expect(base_service.class.to_migration_version).to eq(to_migration_version)
end
end

describe '.migrates_to_latest_migration_version?' do
context 'when the to migration version is not the latest' do
let(:from_migration_version) { 1 }
let(:to_migration_version) { 1 + Dsu::Migration::VERSION }

it 'returns false' do
expect(base_service.class.migrates_to_latest_migration_version?).to be false
end
end

context 'when the to migration version is the latest' do
let(:from_migration_version) { 1 }
let(:to_migration_version) { Dsu::Migration::VERSION }

it 'returns true' do
expect(base_service.class.migrates_to_latest_migration_version?).to be true
end
end
end
end

describe 'instance methods' do
describe '#migrate_if!' do
before do
stub_const('Dsu::Migration::VERSION', to_migration_version)
mock_migration_version_for(version: from_migration_version)
create(:migration_version, version: migration_version, options: options)
base_service.migrate_if!
end

context 'when the current migration version is equal to the from migration version' do
let(:migration_version) { from_migration_version }
let(:from_migration_version) { 20230613121411 } # rubocop:disable Style/NumericLiterals
let(:to_migration_version) { 20240210161248 } # rubocop:disable Style/NumericLiterals

it 'creates a backup of the current dsu folder and updates the migration version' do
expected_backup_folder = Dsu::Support::Fileable.backup_folder_for(migration_version: migration_version)
expect(Dir.exist?(expected_backup_folder)).to be true
expect(Dsu::Models::MigrationVersion.new.version).to eq to_migration_version
end
end

context 'when the current migration version is not equal to the from migration version' do
let(:migration_version) { to_migration_version }
let(:from_migration_version) { 20230613121411 } # rubocop:disable Style/NumericLiterals
let(:to_migration_version) { 20240210161248 } # rubocop:disable Style/NumericLiterals

it 'does not create a backup of the current dsu folder and does not update the migration version' do
expected_backup_folder = Dsu::Support::Fileable.backup_folder_for(migration_version: from_migration_version)
expect(Dir.exist?(expected_backup_folder)).to be false
expect(Dsu::Models::MigrationVersion.new.version).to_not eq from_migration_version
end
end
end
end
end
end
# rubocop:enable RSpec/NestedGroups
6 changes: 0 additions & 6 deletions spec/dsu/migration/v20230613121411/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@
end

def known_deleted_files
# %w[
# entries/2023-12-28.json
# entries/2023-12-29.json
# entries/2024-01-01.json
# entries/2024-01-02.json
# ]
[]
end

Expand Down

0 comments on commit 35a08c2

Please sign in to comment.