diff options
| author | teddywing | 2017-08-30 19:18:34 +0200 |
|---|---|---|
| committer | GitHub | 2017-08-30 19:18:34 +0200 |
| commit | 637defea87489a4f969766e638d190facd54009a (patch) | |
| tree | 0f80bacae657cb3999e9a3be27787b5f6c72550d | |
| parent | 6f9ab93c2af49d093a305d1dea904d5d53e74cb6 (diff) | |
| parent | 7669cd8ae27bcbb8dbf1b5a968d50a7977b78d60 (diff) | |
| download | chouette-core-637defea87489a4f969766e638d190facd54009a.tar.bz2 | |
Merge pull request #58 from af83/import-model--clean-up-code-and-add-specs
Import model clean up code and add specs
| -rw-r--r-- | app/models/import.rb | 4 | ||||
| -rw-r--r-- | spec/models/import_spec.rb | 164 |
2 files changed, 98 insertions, 70 deletions
diff --git a/app/models/import.rb b/app/models/import.rb index cdda3d0dc..eb2428b2b 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -29,11 +29,11 @@ class Import < ActiveRecord::Base end def notify_parent - parent.child_change(self) + parent.child_change update(notified_parent_at: DateTime.now) end - def child_change(child) + def child_change return if self.class.finished_statuses.include?(status) update_status diff --git a/spec/models/import_spec.rb b/spec/models/import_spec.rb index 69c77da91..941e5b386 100644 --- a/spec/models/import_spec.rb +++ b/spec/models/import_spec.rb @@ -1,4 +1,4 @@ -RSpec.describe Import, :type => :model do +RSpec.describe Import, type: :model do it { should belong_to(:referential) } it { should belong_to(:workbench) } @@ -48,12 +48,27 @@ RSpec.describe Import, :type => :model do end end - # TODO: Move most of these to #update_status - describe "#child_change", skip: "THE CODE CHANGED AND THESE SPECS NO LONGER WORK. FIX THEM ASAP!!!~!~!@~" do + describe "#child_change" do + it "calls #update_status" do + allow(workbench_import).to receive(:update) + + expect(workbench_import).to receive(:update_status) + workbench_import.child_change + end + + it "calls #update_referentials" do + allow(workbench_import).to receive(:update) + + expect(workbench_import).to receive(:update_referentials) + workbench_import.child_change + end + end + + describe "#update_status" do shared_examples( - "updates :status to failed when child status indicates failure" + "updates :status to failed when >=1 child has failing status" ) do |failure_status| - it "updates :status to failed when child status indicates failure" do + it "updates :status to failed when >=1 child has failing status" do workbench_import = create(:workbench_import) create( :netex_import, @@ -61,102 +76,115 @@ RSpec.describe Import, :type => :model do status: failure_status ) - expect(workbench_import).to receive(:update).with( - current_step: 1, - status: 'failed' - ) + workbench_import.update_status - workbench_import.child_change + expect(workbench_import.status).to eq('failed') end end include_examples( - "updates :status to failed when child status indicates failure", + "updates :status to failed when >=1 child has failing status", "failed" ) include_examples( - "updates :status to failed when child status indicates failure", + "updates :status to failed when >=1 child has failing status", "aborted" ) include_examples( - "updates :status to failed when child status indicates failure", + "updates :status to failed when >=1 child has failing status", "canceled" ) - # TODO: rewrite these for new #update_status - # it "updates :status to successful when #ready?" do - # expect(workbench_import).to receive(:update).with(status: 'successful') - # - # workbench_import.child_change - # end - # - # it "updates :status to failed when #ready? and child is failed" do - # build_stubbed( - # :netex_import, - # parent: workbench_import, - # status: :failed - # ) - # - # expect(workbench_import).to receive(:update).with(status: 'failed') - # - # workbench_import.child_change - # end + it "updates :status to successful when all children are successful" do + workbench_import = create(:workbench_import) + create_list( + :netex_import, + 2, + parent: workbench_import, + status: 'successful' + ) + + workbench_import.update_status + + expect(workbench_import.status).to eq('successful') + end + + it "Updates :status to failed when any child has failed" do + workbench_import = create(:workbench_import) + [ + 'failed', + 'successful' + ].each do |status| + create( + :netex_import, + parent: workbench_import, + status: status + ) + end + + workbench_import.update_status + + expect(workbench_import.status).to eq('failed') + end + + it "updates :ended_at to now when status is finished" do + workbench_import = create(:workbench_import) + create( + :netex_import, + parent: workbench_import, + status: 'failed' + ) + + Timecop.freeze(Time.now) do + workbench_import.update_status + + expect(workbench_import.ended_at).to eq(Time.now) + end + end + end + + describe "#update_referentials" do + it "doesn't update referentials if parent status isn't finished" do + workbench_import = create(:workbench_import, status: 'pending') + netex_import = create(:netex_import, parent: workbench_import) + netex_import.referential.update(ready: false) + + workbench_import.update_referentials + netex_import.referential.reload + + expect(netex_import.referential.ready).to be false + end shared_examples( - "doesn't update :status if parent import status is finished" + "makes child referentials `ready` when 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 - ) - double('Import') + it "makes child referentials `ready` when status is finished" do + workbench_import = create(:workbench_import, status: finished_status) + netex_import = create(:netex_import, parent: workbench_import) + netex_import.referential.update(ready: false) - expect(workbench_import).not_to receive(:update) + workbench_import.update_referentials + netex_import.referential.reload - workbench_import.child_change + expect(netex_import.referential.ready).to be true end end include_examples( - "doesn't update :status if parent import status is finished", + "makes child referentials `ready` when status is finished", "successful" ) include_examples( - "doesn't update :status if parent import status is finished", + "makes child referentials `ready` when status is finished", "failed" ) include_examples( - "doesn't update :status if parent import status is finished", + "makes child referentials `ready` when status is finished", "aborted" ) include_examples( - "doesn't update :status if parent import status is finished", + "makes child referentials `ready` when status is finished", "canceled" ) - - it "calls #update_status" do - allow(workbench_import).to receive(:update) - - expect(workbench_import).to receive(:update_status) - workbench_import.child_change - end - - it "calls #update_referential" do - allow(workbench_import).to receive(:update) - - expect(workbench_import).to receive(:update_referential) - workbench_import.child_change - end end - - describe "#update_status" do - it "updates :ended_at to now when status is finished" do - skip "Redo the `#update_status` code to make it easier to write this." - end - end - - # TODO: specs for #update_referential end |
