Skip to content

Commit

Permalink
refactored for better testability
Browse files Browse the repository at this point in the history
  • Loading branch information
durandom committed Apr 7, 2017
1 parent 8b28480 commit 185f56e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
9 changes: 5 additions & 4 deletions app/models/dialog.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
class Dialog < ApplicationRecord
DIALOG_DIR = Rails.root.join("product/dialogs/service_dialogs")
DIALOG_DIR_CORE = 'product/dialogs/service_dialogs'.freeze
DIALOG_DIR_PLUGIN = 'content/service_dialogs'.freeze

# The following gets around a glob symbolic link issue
ALL_YAML_FILES = DIALOG_DIR.join("{,*/**/}*.{yaml,yml}")
YAML_FILES_PATTERN = "{,*/**/}*.{yaml,yml}".freeze

has_many :dialog_tabs, -> { order :position }, :dependent => :destroy
validate :validate_children
Expand All @@ -25,12 +26,12 @@ class Dialog < ApplicationRecord
def self.seed
dialog_import_service = DialogImportService.new

Dir.glob(ALL_YAML_FILES).each do |file|
Dir.glob(Rails.root.join(DIALOG_DIR_CORE, YAML_FILES_PATTERN)).each do |file|
dialog_import_service.import_all_service_dialogs_from_yaml_file(file)
end

Vmdb::Plugins.instance.registered_provider_plugins.each do |plugin|
Dir.glob(plugin.root.join('content', 'service_dialogs', '*.{yaml,yml}')).each do |file|
Dir.glob(plugin.root.join(DIALOG_DIR_PLUGIN, YAML_FILES_PATTERN)).each do |file|
dialog_import_service.import_all_service_dialogs_from_yaml_file(file)
end
end
Expand Down
45 changes: 31 additions & 14 deletions spec/models/dialog_spec.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,58 @@
describe Dialog do
describe ".seed" do
let(:dialog_import_service) { double("DialogImportService") }
let(:test_file_path) { Rails.root.join("spec/fixtures/files/dialogs") }
let(:all_yaml_files) { test_file_path.join("{,*/**/}*.{yaml,yml}") }
let(:test_file_path) { "spec/fixtures/files/dialogs" }

before do
allow(DialogImportService).to receive(:new).and_return(dialog_import_service)
allow(dialog_import_service).to receive(:import_all_service_dialogs_from_yaml_file)
end

it "delegates to the dialog import service with a file in the default directory" do
Dialog.with_constants(:DIALOG_DIR => test_file_path, :ALL_YAML_FILES => all_yaml_files) do
described_class.with_constants(:DIALOG_DIR_CORE => test_file_path) do
expect(dialog_import_service).to receive(:import_all_service_dialogs_from_yaml_file).with(
test_file_path.join("seed_test.yaml").to_path)
Rails.root.join(test_file_path, "seed_test.yaml").to_path
)
expect(dialog_import_service).to receive(:import_all_service_dialogs_from_yaml_file).with(
test_file_path.join("seed_test.yml").to_path)
Dialog.seed
Rails.root.join(test_file_path, "seed_test.yml").to_path
)
described_class.seed
end
end

it "delegates to the dialog import service with a file in a sub directory" do
Dialog.with_constants(:DIALOG_DIR => test_file_path, :ALL_YAML_FILES => all_yaml_files) do
described_class.with_constants(:DIALOG_DIR_CORE => test_file_path) do
expect(dialog_import_service).to receive(:import_all_service_dialogs_from_yaml_file).with(
test_file_path.join("service_dialogs/service_seed_test.yaml").to_path)
Rails.root.join(test_file_path, "service_dialogs/service_seed_test.yaml").to_path
)
expect(dialog_import_service).to receive(:import_all_service_dialogs_from_yaml_file).with(
test_file_path.join("service_dialogs/service_seed_test.yml").to_path)
Dialog.seed
Rails.root.join(test_file_path, "service_dialogs/service_seed_test.yml").to_path
)
described_class.seed
end
end

it "delegates to the dialog import service with a symlinked file" do
Dialog.with_constants(:DIALOG_DIR => test_file_path, :ALL_YAML_FILES => all_yaml_files) do
described_class.with_constants(:DIALOG_DIR_CORE => test_file_path) do
expect(dialog_import_service).to receive(:import_all_service_dialogs_from_yaml_file).with(
test_file_path.join("service_dialog_symlink/service_seed_test.yaml").to_path)
Rails.root.join(test_file_path, "service_dialog_symlink/service_seed_test.yaml").to_path
)
expect(dialog_import_service).to receive(:import_all_service_dialogs_from_yaml_file).with(
test_file_path.join("service_dialog_symlink/service_seed_test.yml").to_path)
Dialog.seed
Rails.root.join(test_file_path, "service_dialog_symlink/service_seed_test.yml").to_path
)
described_class.seed
end
end

it "seed files from plugins" do
mock_engine = double(:root => Rails.root)
expect(Vmdb::Plugins.instance).to receive(:registered_provider_plugins).and_return([mock_engine])

described_class.with_constants(:DIALOG_DIR_PLUGIN => test_file_path) do
expect(dialog_import_service).to receive(:import_all_service_dialogs_from_yaml_file).with(
Rails.root.join(test_file_path, "seed_test.yaml").to_path
)
described_class.seed
end
end
end
Expand Down

0 comments on commit 185f56e

Please sign in to comment.