diff options
| author | Teddy Wing | 2017-08-01 20:47:15 +0200 |
|---|---|---|
| committer | Teddy Wing | 2017-08-01 20:47:15 +0200 |
| commit | 985f22e3d4c7c503c4f830f00692a1219ade9612 (patch) | |
| tree | 5e8eb9d99d1ab9950c5eeceeff80947c51e9e9a0 | |
| parent | f189c4331d4f6f1ed10b7545d90a92dc2b7ca9b1 (diff) | |
| download | chouette-core-985f22e3d4c7c503c4f830f00692a1219ade9612.tar.bz2 | |
Import: Make #child_change work
The `#child_change` method should update the current import (the parent)
with a 'failed' or 'successful' status depending on the status of the
child passed in.
If the child has a failure status, the parent import should be updated
to failed. If the import is `#ready?`, that tells us that all children
have called `#child_change` without any problems, and we can update the
parent's status to be successful. THAT PARAGRAPH IS WRONG. We could have
`#ready?` with failed children. NEED TO UPDATE TO SUPPORT THAT.
Add a method that describes the possible "failing" statuses. These are
the ones that will cause 'failed' to be set on the parent.
Refs #3509, #3511
| -rw-r--r-- | app/models/import.rb | 9 | ||||
| -rw-r--r-- | spec/models/import_spec.rb | 60 |
2 files changed, 69 insertions, 0 deletions
diff --git a/app/models/import.rb b/app/models/import.rb index 46de0d727..c7f6d99df 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -18,6 +18,11 @@ class Import < ActiveRecord::Base end def child_change(child) + if failing_statuses.include?(child.status) + return update(status: 'failed') + end + + return update(status: 'successful') if ready? end def ready? @@ -30,4 +35,8 @@ class Import < ActiveRecord::Base self.token_download = SecureRandom.urlsafe_base64 self.status = Import.status.new end + + def failing_statuses + %i(failed aborted canceled).flat_map { |status| [status, status.to_s] } + end end diff --git a/spec/models/import_spec.rb b/spec/models/import_spec.rb index 1b8fe72c2..c97cfcaa2 100644 --- a/spec/models/import_spec.rb +++ b/spec/models/import_spec.rb @@ -24,6 +24,66 @@ RSpec.describe Import, :type => :model do end end + describe "#child_change" do + def updates_status_to_failed_when_child_status_indicates_failure( + failure_status + ) + workbench_import = build_stubbed(:workbench_import) + 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 + + it "updates :status to failed when child status indicates failure" do + updates_status_to_failed_when_child_status_indicates_failure('failed') + updates_status_to_failed_when_child_status_indicates_failure('aborted') + updates_status_to_failed_when_child_status_indicates_failure('canceled') + end + + it "updates :status to successful when #ready?" do + workbench_import = build_stubbed( + :workbench_import, + total_steps: 2, + current_step: 2 + ) + netex_import = build_stubbed( + :netex_import, + parent: workbench_import + ) + + 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 + workbench_import = build_stubbed( + :workbench_import, + total_steps: 2, + current_step: 2 + ) + 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 + # when status is failed, update status to failed + # else when ready? update status to successful + end + describe "#ready?" do it "returns true if #current_step == #total_steps" do import = build_stubbed( |
