diff options
| author | Luc Donnet | 2017-12-07 23:53:38 +0100 | 
|---|---|---|
| committer | Luc Donnet | 2017-12-07 23:53:38 +0100 | 
| commit | d1e342aa6630585341a4c3f5e99f8b7637c664b3 (patch) | |
| tree | 20b4d07af3320cfb0fbeb4d9749eea3b94e9d42a | |
| parent | 0bb71d8146126f4f2c53ca0c7145f0e7d5eaeda5 (diff) | |
| download | chouette-core-d1e342aa6630585341a4c3f5e99f8b7637c664b3.tar.bz2 | |
Fix worbench_import status update when a netex_import has a warning status and others successful Refs #5202
| -rw-r--r-- | app/models/import.rb | 47 | ||||
| -rw-r--r-- | spec/models/import_spec.rb | 22 | 
2 files changed, 39 insertions, 30 deletions
| diff --git a/app/models/import.rb b/app/models/import.rb index 20e7f2d8a..19e835986 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -14,7 +14,7 @@ class Import < ActiveRecord::Base     end    extend Enumerize -  enumerize :status, in: %i(new pending successful warning failed running aborted canceled), scope: true, default: :new +  enumerize :status, in: %w(new pending successful warning failed running aborted canceled), scope: true, default: :new    validates :name, presence: true    validates :file, presence: true @@ -28,15 +28,19 @@ class Import < ActiveRecord::Base    end    def children_succeedeed -    children.with_status(:successful).count +    children.with_status(:successful, :warning).count    end -  def self.failing_statuses -    symbols_with_indifferent_access(%i(failed aborted canceled)) +  def self.launched_statuses +    %w(new pending) +  end + +  def self.failed_statuses +    %w(failed aborted canceled)    end    def self.finished_statuses -    symbols_with_indifferent_access(%i(successful failed warning aborted canceled)) +    %w(successful failed warning aborted canceled)    end    def notify_parent @@ -52,35 +56,25 @@ class Import < ActiveRecord::Base    end    def update_status -    status_count = children.group(:status).count -    children_finished_count = children_failed_count = children_count = 0 - -    status_count.each do |status, count| -      if self.class.failing_statuses.include?(status) -        children_failed_count += count -      end -      if self.class.finished_statuses.include?(status) -        children_finished_count += count -      end -      children_count += count -    end - -    attributes = { -      current_step: children_finished_count -    } -      status = -      if children_failed_count > 0 +      if children.where(status: self.class.failed_statuses).count > 0          'failed' -      elsif status_count['successful'] == children_count +      elsif children.where(status: "warning").count > 0 +        'warning' +      elsif children.where(status: "successful").count == children.count          'successful'        end +    attributes = { +      current_step: children.count, +      status: status +    } +      if self.class.finished_statuses.include?(status)        attributes[:ended_at] = Time.now      end -    update attributes.merge(status: status) +    update attributes    end    def update_referentials @@ -97,7 +91,4 @@ class Import < ActiveRecord::Base      self.token_download = SecureRandom.urlsafe_base64    end -  def self.symbols_with_indifferent_access(array) -    array.flat_map { |symbol| [symbol, symbol.to_s] } -  end  end diff --git a/spec/models/import_spec.rb b/spec/models/import_spec.rb index 4e8aac3f4..3e4128865 100644 --- a/spec/models/import_spec.rb +++ b/spec/models/import_spec.rb @@ -128,7 +128,7 @@ RSpec.describe Import, type: :model do      it "updates :status to successful when all children are successful" do        workbench_import = create(:workbench_import) -      create_list( +      imports = create_list(          :netex_import,          2,          parent: workbench_import, @@ -140,7 +140,7 @@ RSpec.describe Import, type: :model do        expect(workbench_import.status).to eq('successful')      end -    it "Updates :status to failed when any child has failed" do +    it "updates :status to failed when any child has failed" do        workbench_import = create(:workbench_import)        [          'failed', @@ -158,6 +158,24 @@ RSpec.describe Import, type: :model do        expect(workbench_import.status).to eq('failed')      end +    it "updates :status to warning when any child has warning or successful" do +      workbench_import = create(:workbench_import) +      [ +        'warning', +        'successful' +      ].each do |status| +        create( +          :netex_import, +          parent: workbench_import, +          status: status +        ) +      end + +      workbench_import.update_status + +      expect(workbench_import.status).to eq('warning') +    end +      it "updates :ended_at to now when status is finished" do        workbench_import = create(:workbench_import)        create( | 
