aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorRobert Dober2017-08-03 11:19:47 +0200
committerGitHub2017-08-03 11:19:47 +0200
commit494fdfab76e6557018de77dac6861513ffa0502b (patch)
tree7488393cfe925334893e78a7a3983ded78167267 /spec
parent8aba37303c2c2bd95df8c5117173579b3399c2c7 (diff)
parent517495a48e2a05c301159fc48573a9994fbff47b (diff)
downloadchouette-core-494fdfab76e6557018de77dac6861513ffa0502b.tar.bz2
Merge pull request #50 from af83/3511-cron-job-to-notify-parent-WorkbenchImport-of-sub-import-status--rb201708011853
3511 cron job to notify parent workbench import of sub import status rb201708011853
Diffstat (limited to 'spec')
-rw-r--r--spec/factories/imports.rb7
-rw-r--r--spec/factories/netex_imports.rb5
-rw-r--r--spec/factories/workbench_imports.rb5
-rw-r--r--spec/models/import_spec.rb148
-rw-r--r--spec/services/parent_import_notifier_spec.rb90
5 files changed, 249 insertions, 6 deletions
diff --git a/spec/factories/imports.rb b/spec/factories/imports.rb
index aa9288fe9..afced3b57 100644
--- a/spec/factories/imports.rb
+++ b/spec/factories/imports.rb
@@ -10,11 +10,8 @@ FactoryGirl.define do
started_at nil
ended_at nil
- factory :netex_import, class: NetexImport do
- file {File.open(Rails.root.join('spec', 'fixtures', 'terminated_job.json'))}
- end
- factory :workbench_import, class: WorkbenchImport do
- file {File.open(Rails.root.join('spec', 'fixtures', 'terminated_job.json'))}
+ after(:build) do |import|
+ import.class.skip_callback(:create, :before, :initialize_fields)
end
end
end
diff --git a/spec/factories/netex_imports.rb b/spec/factories/netex_imports.rb
new file mode 100644
index 000000000..057e47730
--- /dev/null
+++ b/spec/factories/netex_imports.rb
@@ -0,0 +1,5 @@
+FactoryGirl.define do
+ factory :netex_import, class: NetexImport, parent: :import do
+ file { File.open(Rails.root.join('spec', 'fixtures', 'terminated_job.json')) }
+ end
+end
diff --git a/spec/factories/workbench_imports.rb b/spec/factories/workbench_imports.rb
new file mode 100644
index 000000000..5cdcfd15f
--- /dev/null
+++ b/spec/factories/workbench_imports.rb
@@ -0,0 +1,5 @@
+FactoryGirl.define do
+ factory :workbench_import, class: WorkbenchImport, parent: :import do
+ file { File.open(Rails.root.join('spec', 'fixtures', 'terminated_job.json')) }
+ end
+end
diff --git a/spec/models/import_spec.rb b/spec/models/import_spec.rb
index c56858b44..164769fd5 100644
--- a/spec/models/import_spec.rb
+++ b/spec/models/import_spec.rb
@@ -2,11 +2,157 @@ RSpec.describe Import, :type => :model do
it { should belong_to(:referential) }
it { should belong_to(:workbench) }
- it { should belong_to(:parent).class_name(described_class.to_s) }
+ it { should belong_to(:parent) }
it { should enumerize(:status).in("aborted", "canceled", "failed", "new", "pending", "running", "successful") }
it { should validate_presence_of(:file) }
it { should validate_presence_of(:referential) }
it { should validate_presence_of(:workbench) }
+
+ let(:workbench_import) { build_stubbed(:workbench_import) }
+ let(:workbench_import_with_completed_steps) do
+ workbench_import = build_stubbed(
+ :workbench_import,
+ total_steps: 2,
+ current_step: 2
+ )
+ end
+
+ let(:netex_import) do
+ netex_import = build_stubbed(
+ :netex_import,
+ parent: workbench_import
+ )
+ end
+
+ describe "#notify_parent" do
+ it "must call #child_change on its parent" do
+ allow(netex_import).to receive(:update)
+
+ expect(workbench_import).to receive(:child_change).with(netex_import)
+
+ netex_import.notify_parent
+ end
+
+ it "must update the :notified_parent_at field of the child import" do
+ allow(workbench_import).to receive(:child_change)
+
+ Timecop.freeze(DateTime.now) do
+ expect(netex_import).to receive(:update).with(
+ notified_parent_at: DateTime.now
+ )
+
+ netex_import.notify_parent
+ end
+ end
+ end
+
+ describe "#child_change" do
+ shared_examples(
+ "updates :status to failed when child status indicates failure"
+ ) do |failure_status|
+ it "updates :status to failed when child status indicates failure" do
+ allow(workbench_import).to receive(:update)
+
+ netex_import = build_stubbed(
+ :netex_import,
+ parent: workbench_import,
+ status: failure_status
+ )
+
+ expect(workbench_import).to receive(:update).with(status: 'failed')
+
+ workbench_import.child_change(netex_import)
+ end
+ end
+
+ include_examples(
+ "updates :status to failed when child status indicates failure",
+ "failed"
+ )
+ include_examples(
+ "updates :status to failed when child status indicates failure",
+ "aborted"
+ )
+ include_examples(
+ "updates :status to failed when child status indicates failure",
+ "canceled"
+ )
+
+ it "updates :status to successful when #ready?" do
+ expect(workbench_import).to receive(:update).with(status: 'successful')
+
+ workbench_import.child_change(netex_import)
+ end
+
+ it "updates :status to failed when #ready? and child is failed" do
+ netex_import = build_stubbed(
+ :netex_import,
+ parent: workbench_import,
+ status: :failed
+ )
+
+ expect(workbench_import).to receive(:update).with(status: 'failed')
+
+ workbench_import.child_change(netex_import)
+ end
+
+ shared_examples(
+ "doesn't update :status if parent import status is finished"
+ ) do |finished_status|
+ it "doesn't update :status if parent import status is finished" do
+ workbench_import = build_stubbed(
+ :workbench_import,
+ total_steps: 2,
+ current_step: 2,
+ status: finished_status
+ )
+ child = double('Import')
+
+ expect(workbench_import).not_to receive(:update)
+
+ workbench_import.child_change(child)
+ end
+ end
+
+ include_examples(
+ "doesn't update :status if parent import status is finished",
+ "successful"
+ )
+ include_examples(
+ "doesn't update :status if parent import status is finished",
+ "failed"
+ )
+ include_examples(
+ "doesn't update :status if parent import status is finished",
+ "aborted"
+ )
+ include_examples(
+ "doesn't update :status if parent import status is finished",
+ "canceled"
+ )
+ end
+
+ describe "#ready?" do
+ it "returns true if #current_step == #total_steps" do
+ import = build_stubbed(
+ :import,
+ total_steps: 4,
+ current_step: 4
+ )
+
+ expect(import.ready?).to be true
+ end
+
+ it "returns false if #current_step != #total_steps" do
+ import = build_stubbed(
+ :import,
+ total_steps: 6,
+ current_step: 3
+ )
+
+ expect(import.ready?).to be false
+ end
+ end
end
diff --git a/spec/services/parent_import_notifier_spec.rb b/spec/services/parent_import_notifier_spec.rb
new file mode 100644
index 000000000..3ab505f88
--- /dev/null
+++ b/spec/services/parent_import_notifier_spec.rb
@@ -0,0 +1,90 @@
+RSpec.describe ParentImportNotifier do
+ let(:workbench_import) { create(:workbench_import) }
+
+ describe ".notify_when_finished" do
+ it "calls #notify_parent on finished imports" do
+ workbench_import = build_stubbed(:workbench_import)
+
+ finished_statuses = [:failed, :successful, :aborted, :canceled]
+ netex_imports = []
+
+ finished_statuses.each do |status|
+ netex_imports << build_stubbed(
+ :netex_import,
+ parent: workbench_import,
+ status: status
+ )
+ end
+
+ netex_imports.each do |netex_import|
+ expect(netex_import).to receive(:notify_parent)
+ end
+
+ ParentImportNotifier.notify_when_finished(netex_imports)
+ end
+
+ it "doesn't call #notify_parent if its `notified_parent_at` is set" do
+ netex_import = create(
+ :netex_import,
+ parent: workbench_import,
+ status: :failed,
+ notified_parent_at: DateTime.now
+ )
+
+ expect(netex_import).not_to receive(:notify_parent)
+
+ ParentImportNotifier.notify_when_finished
+ end
+ end
+
+ describe ".imports_pending_notification" do
+ it "includes imports with a parent and `notified_parent_at` unset" do
+ netex_import = create(
+ :netex_import,
+ parent: workbench_import,
+ status: :successful,
+ notified_parent_at: nil
+ )
+
+ expect(
+ ParentImportNotifier.imports_pending_notification
+ ).to eq([netex_import])
+ end
+
+ it "doesn't include imports without a parent" do
+ create(:import, parent: nil)
+
+ expect(
+ ParentImportNotifier.imports_pending_notification
+ ).to be_empty
+ end
+
+ it "doesn't include imports that aren't finished" do
+ [:new, :pending, :running].each do |status|
+ create(
+ :netex_import,
+ parent: workbench_import,
+ status: status,
+ notified_parent_at: nil
+ )
+ end
+
+ expect(
+ ParentImportNotifier.imports_pending_notification
+ ).to be_empty
+ end
+
+ it "doesn't include imports that have already notified their parent" do
+ create(
+ :netex_import,
+ parent: workbench_import,
+ status: :successful,
+ notified_parent_at: DateTime.now
+ )
+
+ expect(
+ ParentImportNotifier.imports_pending_notification
+ ).to be_empty
+ end
+ end
+end