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 01c6bb7 commit 6cb1d28
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/.vscode/
*.code-workspace

.dsu
#.dsu
scratch*.rb
snippets.rb
readme.txt
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
dsu (3.0.0.rc.1)
dsu (3.0.0.beta.1)
activemodel (>= 7.0.8, < 8.0)
activesupport (>= 7.0.8, < 8.0)
colorize (>= 1.1, < 2.0)
Expand Down
4 changes: 2 additions & 2 deletions lib/dsu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
# NOTE: Add a new migration service to the array whenever a new migration is created.
options = { pretend: false }
migration_services = [
V20230613121411::Service.new(options: options),
V20240210161248::Service.new(options: options)
Dsu::Migration::V20230613121411::Service.new(options: options),
Dsu::Migration::V20240210161248::Service.new(options: options)
]
Dsu::Migration::Migrator.migrate_if!(migration_services: migration_services)
end
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
2 changes: 1 addition & 1 deletion lib/dsu/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

module Dsu
VERSION_REGEX = /\A\d+\.\d+\.\d+(\.(alpha|beta|rc)\.\d+)?\z/
VERSION = '3.0.0.rc.1'
VERSION = '3.0.0.beta.3'
end
5 changes: 5 additions & 0 deletions lib/seed_data/0/.dsu
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
editor: nano
entries_display_order: desc
entries_file_name: "%Y-%m-%d.json"
entries_folder: "./dsu/entries"
8 changes: 8 additions & 0 deletions lib/seed_data/20230613121411/.dsu
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"version": 20230613121411,
"editor": "nano",
"entries_display_order": "desc",
"carry_over_entries_to_today": false,
"include_all": false,
"theme_name": "default"
}
9 changes: 9 additions & 0 deletions lib/seed_data/20240210161248/.dsu
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"version": 20240210161248,
"editor": "nano",
"entries_display_order": "desc",
"carry_over_entries_to_today": false,
"include_all": false,
"theme_name": "default",
"default_project": "default"
}
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 6cb1d28

Please sign in to comment.