Skip to content

Commit

Permalink
Spec coverage for migration 20240210161248
Browse files Browse the repository at this point in the history
  • Loading branch information
gangelo committed Feb 12, 2024
1 parent 3889703 commit 87d82e8
Show file tree
Hide file tree
Showing 41 changed files with 1,353 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ todo.txt
migration_version.yml

.DS_Store
current_project.bak
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.alpha.4)
dsu (3.0.0.alpha.12)
activemodel (>= 7.0.8, < 8.0)
activesupport (>= 7.0.8, < 8.0)
colorize (>= 1.1, < 2.0)
Expand Down
4 changes: 3 additions & 1 deletion lib/dsu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
end

unless Dsu.env.test? || Dsu.env.development?
# TODO: This is upgrading from migrations version 0 to 20230613121411
# Move this to to its own class and call from Migration::Factory.
if Dsu::Migration::Service.run_migrations?
begin
Dsu::Migration::Service.new.call
Expand All @@ -34,5 +36,5 @@
end
end

Dsu::Migration::Factory.migrate_if!
Dsu::Migration::Factory.migrate_if!(options: { pretend: false })
end
5 changes: 5 additions & 0 deletions lib/dsu/crud/json_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'active_model'
require 'json'
require_relative '../migration/version'

module Dsu
module Crud
Expand All @@ -14,6 +15,10 @@ def initialize(file_path)
@file_path = file_path
end

def update_version!
@version = Migration::VERSION
end

def delete
self.class.delete(file_path: file_path)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/dsu/migration/factory.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require_relative '../models/migration_version'
require_relative 'service_20230613121411'
require_relative 'service_20240210161248'
require_relative 'version'

module Dsu
Expand All @@ -11,14 +11,14 @@ class << self
def migrate_if!(options: {})
version = options.fetch(:version, migration_version)
if version == 20230613121411 # rubocop:disable Style/NumericLiterals
Service20230613121411.new(options: options).migrate!
Service20240210161248.new(options: options).migrate!
end
end

private

def migration_version
@migration_version ||= Models::MigrationVersion.new.version
Models::MigrationVersion.new.version
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@
require_relative '../support/fileable'
require_relative 'version'

# TODO: Read raw configuration .json file
# If default_project is not set...
# - Add default_project to configuration .json file and write it out.
# - Reload the configuration file.
# - Create a Models::Project object for the default project and initialize/save it.
# - Move the old entries folder into the default project folder.
# TODO: Add default_project to configuration .json file
module Dsu
module Migration
class Service20230613121411
class Service20240210161248
include Support::Fileable

def initialize(options: {})
Expand All @@ -31,13 +24,13 @@ def migrate!
puts

add_new_color_themes
backup
create_backup
create_default_project
update_configuration
update_entry_groups
update_color_themes
update_migration_version
delete_old_entry_folder
delete_old_theme_folder

puts 'Migration completed successfully.'
end
Expand All @@ -56,15 +49,16 @@ def add_new_color_themes

%w[light.json christmas.json].each do |theme_file|
destination_theme_file_path = File.join(Dsu::Support::Fileable.themes_folder, theme_file)
next if File.exist?(destination_theme_file_path)
# next if File.exist?(destination_theme_file_path)

source_theme_file_path = File.join(Dsu::Support::Fileable.seed_data_folder, 'themes', theme_file)
FileUtils.cp(source_theme_file_path, destination_theme_file_path) unless pretend?
puts I18n.t('migrations.information.theme_copied', from: source_theme_file_path, to: destination_theme_file_path)
puts I18n.t('migrations.information.theme_copied',
from: source_theme_file_path, to: destination_theme_file_path)
end
end

def backup
def create_backup
return if Dir.exist?(backup_folder)

puts 'Creating backup...'
Expand All @@ -87,14 +81,19 @@ def update_configuration
puts 'Updating configuration...'
puts

Models::Configuration.new.write! unless pretend?
return if pretend?

Models::Configuration.new.tap do |configuration|
configuration.version = Dsu::Migration::VERSION
configuration.write!
end
end

def update_entry_groups
puts 'Updating entry groups...'
puts

return if Dir.exist?(entries_folder) || pretend?
return if pretend? || Dir.exist?(entries_folder)

puts 'Copying entries to default project...'
puts
Expand All @@ -116,40 +115,42 @@ def update_color_themes
puts 'Updating color themes...'
puts

return if Dir.exist?(themes_folder) || pretend?

puts 'Copying color themes...'
puts

FileUtils.mkdir_p(themes_folder)
FileUtils.cp_r(File.join(backup_folder, 'themes', '.'), themes_folder)
unless pretend?
FileUtils.mkdir_p(themes_folder)
FileUtils.cp_r(File.join(backup_folder, 'themes', '.'), themes_folder)
end

puts 'Updating color theme version...'
puts

Models::ColorTheme.all.each do |color_theme|
puts "Updating color theme version: #{color_theme.theme_name}..."
color_theme.version = Dsu::Migration::VERSION
color_theme.update_version!
color_theme.save! unless pretend?
end
end

def delete_old_entry_folder
puts 'Cleaning up old entries...'
def update_migration_version
puts 'Updating migration version...'
puts

FileUtils.rm_rf(File.join(dsu_folder, 'entries')) unless pretend?
return if pretend? || migration_version == Migration::VERSION

Models::MigrationVersion.new(version: Migration::VERSION).save!
end

def delete_old_theme_folder
puts 'Cleaning up old themes...'
def delete_old_entry_folder
puts 'Cleaning up old entries...'
puts

FileUtils.rm_rf(File.join(dsu_folder, 'themes')) unless pretend?
FileUtils.rm_rf(File.join(dsu_folder, 'entries')) unless pretend?
end

def backup_folder
@backup_folder ||= File.join(dsu_folder, target_migration_version.to_s)
@backup_folder ||= File.join(root_folder, "dsu-#{target_migration_version}-backup")
end

def target_migration_version
Expand Down
6 changes: 6 additions & 0 deletions lib/dsu/models/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ def initialize(options: {})
write! unless exist?
end

class << self
def exist?
File.exist?(Support::Fileable.config_path)
end
end

# Temporarily sets the configuration to the given config_hash.
# To reset the configuration to its original state, call #reload
def replace!(config_hash: {})
Expand Down
2 changes: 2 additions & 0 deletions lib/dsu/support/project_file_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def current_project_name
end

def default_project_name
return Models::Configuration::DEFAULT_CONFIGURATION[:default_project] unless Models::Configuration.exist?

Models::Configuration.new.default_project
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|rc)\.\d+)?\z/
VERSION = '3.0.0.alpha.5'
VERSION = '3.0.0.alpha.12'
end
4 changes: 2 additions & 2 deletions lib/seed_data/themes/light.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"background": "default"
},
"footer": {
"color": "light_default",
"color": "default",
"mode": "default",
"background": "default"
},
Expand All @@ -42,7 +42,7 @@
"background": "default"
},
"index": {
"color": "light_default",
"color": "default",
"mode": "italic",
"background": "default"
},
Expand Down
38 changes: 38 additions & 0 deletions spec/dsu/migration/factory_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

RSpec.describe Dsu::Migration::Factory do
let(:options) { {} }

describe '.migrate_if!' do
before do
create(:migration_version, version: version)
end

context 'when the migration version is not 20230613121411' do
before do
allow(Dsu::Migration::Service20240210161248).to receive(:new) # Stub the :new method
described_class.migrate_if!(options: options)
end

let(:version) { 0 }

it 'does not call the migration service' do
expect(Dsu::Migration::Service20240210161248).to_not have_received(:new)
end
end

context 'when the migration version is 20230613121411' do
before do
create(:migration_version, version: 20230613121411) # rubocop:disable Style/NumericLiterals
allow(Dsu::Migration::Service20240210161248).to receive(:new).and_call_original # Stub :new but allow it to call the original method
described_class.migrate_if!(options: options)
end

let(:version) { 20230613121411 } # rubocop:disable Style/NumericLiterals

it 'calls the migration service' do
expect(Dsu::Migration::Service20240210161248).to have_received(:new)
end
end
end
end
97 changes: 97 additions & 0 deletions spec/dsu/migration/service20240210161248_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# frozen_string_literal: true

RSpec.describe Dsu::Migration::Service20240210161248, type: :migration do
subject(:service) { described_class.new(options: options) }

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

let(:options) { {} }
let(:migration_version) { 20230613121411 } # rubocop:disable Style/NumericLiterals

describe '#initialize' do
let(:migration_version) { 0 }

it_behaves_like 'no error is raised'
end

describe '#migrate!' do
subject(:service_migrate) { service.migrate! }

context 'when the migration version is not 20230613121411' do
let(:migration_version) { 0 }
let(:expected_error) do
/Actual migration version .+ is not the expected migration version 20230613121411/
end

it_behaves_like 'an error is raised'
end

context 'when the migration version is 20230613121411' do
before do
mock_migration_version_for(version: migration_version)
service_migrate
end

shared_examples 'the migration was run in pretend mode' do
let(:expected) { File.join('spec', 'fixtures', 'folders', migration_version.to_s) }
let(:actual) { Dsu::Support::Fileable.dsu_folder }

it 'does not make any changes to the dsu folder structure or configuration file' do
expect(dsu_folders_match(expected: expected, actual: actual)).to be(true)
end
end

context 'when the pretend option is implicitly set to true (not provided)' do
it_behaves_like 'the migration was run in pretend mode'
end

context 'when the pretend option is explicitly set to true' do
let(:options) { { pretend: true } }

it_behaves_like 'the migration was run in pretend mode'
end

context 'when the pretend option is set to false' do
let(:options) { { pretend: false } }
let(:expected) { File.join('spec', 'fixtures', 'folders', 20240210161248.to_s) } # rubocop:disable Style/NumericLiterals
let(:actual) { Dsu::Support::Fileable.dsu_folder }

it 'migrates the dsu folder structure and configuration file' do
expect(dsu_folders_match(expected: expected, actual: actual)).to be(true)
end

it 'sets all the entry group versions to the correct migration version' do
all_entry_groups = Dsu::Models::EntryGroup.all
expect(all_entry_groups.all? do |entry_group|
entry_group.version == 20240210161248 # rubocop:disable Style/NumericLiterals
end).to be(true)
end

it 'sets all the color theme versions to the correct migration version' do
all_color_themes = Dsu::Models::ColorTheme.all
expect(all_color_themes.all? do |color_theme|
color_theme.version == 20240210161248 # rubocop:disable Style/NumericLiterals
end).to be(true)
end

it 'sets the configuration file version to the correct migration version' do
expect(Dsu::Models::Configuration.new.version).to eq(20240210161248) # rubocop:disable Style/NumericLiterals
end

it 'sets the migration version file version to the correct migration version' do
expect(Dsu::Models::MigrationVersion.new.version).to eq(20240210161248) # rubocop:disable Style/NumericLiterals
end

it 'copies the christmas color theme' do
expect(Dsu::Models::ColorTheme.new(theme_name: 'christmas').exist?).to be(true)
end

it 'copies the light color theme' do
expect(Dsu::Models::ColorTheme.new(theme_name: 'light').exist?).to be(true)
end
end
end
end
end
Loading

0 comments on commit 87d82e8

Please sign in to comment.