Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Retire the state machines on Import and ImportRow
Damian Legawiec0ba1aaa0a6da
diff --git a/spree/api/app/controllers/spree/api/v3/admin/imports_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/imports_controller.rbindex 10188af415..0d62b1c27f 100644--- a/spree/api/app/controllers/spree/api/v3/admin/imports_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/imports_controller.rb@@ -29,7 +29,7 @@ module Spreeif @resource.savebegin- @resource.start_mapping!+ Spree.import_start_mapping_workflow.call(import: @resource)rescue ::CSV::MalformedCSVError, EncodingError => e@resource.update_columns(status: 'failed', processing_errors: e.message, updated_at: Time.current)return render_error(@@ -72,7 +72,7 @@ module Spreeapply_mappings!(@resource)if @resource.mapping_done?- @resource.complete_mapping!+ Spree.import_complete_mapping_workflow.call(import: @resource)render json: serialize_resource(@resource)elsemissing = @resource.required_fields - @resource.mappings.mapped.pluck(:schema_field)@@ -96,7 +96,9 @@ module Spree@resource = find_resourceauthorize_resource!(@resource, :update)- if @resource.retry_failed_rows+ result = Spree.import_retry_failed_rows_workflow.call(import: @resource)++ if result.success?render json: serialize_resource(@resource)elserender_error(diff --git a/spree/api/spec/controllers/spree/api/v3/admin/imports_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/imports_controller_spec.rbindex e56346fdef..af20db7505 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/imports_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/imports_controller_spec.rb@@ -204,7 +204,7 @@ RSpec.describe Spree::Api::V3::Admin::ImportsController, type: :controller doimp.attachment.attach(io: StringIO.new(csv_content), filename: 'import.csv', content_type: 'text/csv')- imp.start_mapping!+ Spree.import_start_mapping_workflow.call(import: imp)endenddiff --git a/spree/api/spec/integration/spree/api/v3/admin/imports_spec.rb b/spree/api/spec/integration/spree/api/v3/admin/imports_spec.rbindex df46337bba..84ce045d20 100644--- a/spree/api/spec/integration/spree/api/v3/admin/imports_spec.rb+++ b/spree/api/spec/integration/spree/api/v3/admin/imports_spec.rb@@ -252,7 +252,7 @@ RSpec.describe 'Admin Imports API', type: :request, swagger_doc: 'api-reference/path '/api/v3/admin/imports/{id}/complete_mapping' dolet(:import_in_mapping) do- create(:product_import, owner: store, user: admin_user).tap(&:start_mapping!)+ create(:product_import, owner: store, user: admin_user).tap { |imp| Spree.import_start_mapping_workflow.call(import: imp) }endlet(:id) { import_in_mapping.prefixed_id }diff --git a/spree/core/app/jobs/spree/imports/process_group_job.rb b/spree/core/app/jobs/spree/imports/process_group_job.rbindex 52e653ceb5..708210a449 100644--- a/spree/core/app/jobs/spree/imports/process_group_job.rb+++ b/spree/core/app/jobs/spree/imports/process_group_job.rb@@ -115,7 +115,7 @@ module Spreeimport.reloadif import.completed_groups_count >= import.processing_groups_count && import.rows.in_flight.none?- import.complete! if import.status == 'processing'+ Spree.import_complete_workflow.call(import: import) if import.status == 'processing'elsif large && (import.completed_groups_count % 10).zero?import.publish_event('import.progress')enddiff --git a/spree/core/app/jobs/spree/imports/process_job.rb b/spree/core/app/jobs/spree/imports/process_job.rbindex 581e0c7464..801954ddf6 100644--- a/spree/core/app/jobs/spree/imports/process_job.rb+++ b/spree/core/app/jobs/spree/imports/process_job.rb@@ -25,7 +25,7 @@ module Spreeunless skip_row_creationstep :begin_processing do- @import.start_processing! if @import.status != 'processing'+ Spree.import_start_processing_workflow.call(import: @import) if @import.status != 'processing'endstep :create_rows, start: 1# A permanent CSV failure marked the import failed inside the stepdiff --git a/spree/core/app/models/spree/import.rb b/spree/core/app/models/spree/import.rbindex 47b9c80fbe..da03100d41 100644--- a/spree/core/app/models/spree/import.rb+++ b/spree/core/app/models/spree/import.rb@@ -56,40 +56,14 @@ module Spreehas_one_attached :attachment, service: Spree.private_storage_service_name#- # State machine+ # Status#- state_machine initial: :pending, attribute: :status do- event :start_mapping do- transition to: :mapping- end- before_transition to: :mapping, do: :create_mappings-- event :complete_mapping do- transition from: :mapping, to: :completed_mapping- end- after_transition to: :completed_mapping, do: :create_rows_async-- event :start_processing do- transition from: :completed_mapping, to: :processing- end-- event :complete do- transition from: :processing, to: :completed- end- after_transition to: :completed, do: :touch_store- after_transition to: :completed, do: :publish_import_completed_event-- event :fail do- transition to: :failed- end-- # Re-processes rows that failed: the dispatcher targets pending_and_failed- # rows, so retry is just a transition + re-dispatch.- event :retry_failed_rows do- transition from: :completed, to: :processing, if: ->(import) { import.rows.failed.exists? }- end- after_transition on: :retry_failed_rows, do: :process_rows_async- end+ # No state machine — the pipeline's steps are the Spree::Imports workflows+ # (docs/plans/6.0-service-workflows.md), which is also what let the row+ # dispatch stop tiptoeing around a transition's transaction.+ include Spree::HasStatus+ has_status :pending, :mapping, :completed_mapping, :processing, :completed, :failed,+ default: :pending## Preferences@@ -162,6 +136,44 @@ module Spreenilend+ # @deprecated Call the Spree::Imports workflows — removed in 6.1.+ def start_mapping!+ Spree::Deprecation.warn('Spree::Import#start_mapping! is deprecated and will be removed in Spree 6.1. Call Spree.import_start_mapping_workflow instead.')+ run_pipeline_workflow(Spree.import_start_mapping_workflow)+ end++ # @deprecated Call Spree.import_complete_mapping_workflow — removed in 6.1.+ def complete_mapping!+ Spree::Deprecation.warn('Spree::Import#complete_mapping! is deprecated and will be removed in Spree 6.1. Call Spree.import_complete_mapping_workflow instead.')+ run_pipeline_workflow(Spree.import_complete_mapping_workflow)+ end++ # @deprecated Call Spree.import_start_processing_workflow — removed in 6.1.+ def start_processing!+ Spree::Deprecation.warn('Spree::Import#start_processing! is deprecated and will be removed in Spree 6.1. Call Spree.import_start_processing_workflow instead.')+ run_pipeline_workflow(Spree.import_start_processing_workflow)+ end++ # @deprecated Call Spree.import_complete_workflow — removed in 6.1.+ def complete!+ Spree::Deprecation.warn('Spree::Import#complete! is deprecated and will be removed in Spree 6.1. Call Spree.import_complete_workflow instead.')+ run_pipeline_workflow(Spree.import_complete_workflow)+ end++ # @deprecated Set the status — removed in 6.1. Failure was never a step of+ # its own; the controller records the parse error directly.+ def fail!+ Spree::Deprecation.warn('Spree::Import#fail! is deprecated and will be removed in Spree 6.1. Write the status instead.')+ update!(status: 'failed')+ end++ # @deprecated Call Spree.import_retry_failed_rows_workflow — removed in 6.1.+ # Returns false when there is nothing to retry, as the guarded event did.+ def retry_failed_rows+ Spree::Deprecation.warn('Spree::Import#retry_failed_rows is deprecated and will be removed in Spree 6.1. Call Spree.import_retry_failed_rows_workflow instead.')+ Spree.import_retry_failed_rows_workflow.call(import: self).success?+ end+# Returns true if the import is in mapping state# @return [Boolean]def mapping?@@ -305,14 +317,6 @@ module Spree"#{Spree.t(type.demodulize.pluralize.downcase)} #{number}"end- def touch_store- store.touch- end-- def publish_import_completed_event- publish_event('import.completed')- end-# Returns the headers of the csv file# @return [Array<String>]def csv_headers@@ -330,43 +334,6 @@ module Spree@attachment_file_content ||= attachment.attached? ? attachment.blob.download&.force_encoding('UTF-8') : nilend- # Creates mappings from the schema fields- # TODO: get mappings from the previous import if it exists, so user won't have to map the same columns again- def create_mappings- schema_fields.each do |schema_field|- mapping = mappings.find_or_create_by!(schema_field: schema_field[:name])- mapping.try_to_auto_assign_file_column(csv_headers)- mapping.save!- end- end-- # Creates rows asynchronously- # @return [void]- def create_rows_async- if preferred_inline- # The transition callback runs inside the state machine's- # transaction and Continuations forbid checkpoints there — the- # inline run starts at commit, still before the transition call- # returns to the caller.- ActiveRecord.after_all_transactions_commit { Spree::Imports::ProcessJob.perform_now(id) }- return- end-- # The 2s delay lets the attachment settle before the job reads it.- Spree::Imports::ProcessJob.set(wait: 2.seconds).perform_later(id)- end-- # Processes rows asynchronously- # @return [void]- def process_rows_async- if preferred_inline- ActiveRecord.after_all_transactions_commit { Spree::Imports::ProcessJob.perform_now(id, skip_row_creation: true) }- return- end-- Spree::Imports::ProcessJob.perform_later(id, skip_row_creation: true)- end-# Returns the store for the import# @return [Spree::Store]def store@@ -473,6 +440,18 @@ module Spreeprivate+ # Mirrors the machine's bang events: an illegal move raised.+ def run_pipeline_workflow(workflow)+ result = workflow.call(import: self)++ if result.failure?+ errors.add(:base, result.error.value.to_s)+ raise ActiveRecord::RecordInvalid, self+ end++ true+ end+def ensure_whitelisted_typereturn if type.blank?diff --git a/spree/core/app/models/spree/import_row.rb b/spree/core/app/models/spree/import_row.rbindex 9760a8c42d..61849ebfd1 100644--- a/spree/core/app/models/spree/import_row.rb+++ b/spree/core/app/models/spree/import_row.rb@@ -24,23 +24,13 @@ module Spreeself.whitelisted_ransackable_attributes = %w[status row_number]#- # State machine+ # Status#- state_machine initial: :pending, attribute: :status do- event :start_processing do- transition to: :processing- end-- event :fail do- transition to: :failed- end- after_transition to: :failed, do: :publish_import_row_failed_event-- event :complete do- transition to: :completed- end- after_transition to: :completed, do: :publish_import_row_completed_event- end+ # No state machine — a row's whole lifecycle already lives in +process!+,+ # which is the only thing that moves it. The events publish from the write+ # rather than a transition callback.+ include Spree::HasStatus+ has_status :pending, :processing, :completed, :failed, default: :pending# How long a row may sit in `processing` before we consider its owning worker dead# (OOM, SIGKILL, deploy without graceful drain — none of these trigger a Sidekiq@@ -90,15 +80,40 @@ module Spreedata_json[mapping.file_column]end+ # @deprecated Write the status — removed in 6.1. A row's lifecycle belongs+ # to +process!+, which is the only thing that should move it.+ def start_processing!+ Spree::Deprecation.warn('Spree::ImportRow#start_processing! is deprecated and will be removed in Spree 6.1. Use #process! instead.')+ update!(status: 'processing')+ end++ # @deprecated Write the status — removed in 6.1.+ def complete!+ Spree::Deprecation.warn('Spree::ImportRow#complete! is deprecated and will be removed in Spree 6.1. Use #process! instead.')+ update!(status: 'completed')+ publish_event('import_row.completed')+ true+ end++ # @deprecated Write the status — removed in 6.1.+ def fail!+ Spree::Deprecation.warn('Spree::ImportRow#fail! is deprecated and will be removed in Spree 6.1. Use #process! instead.')+ update!(status: 'failed')+ publish_event('import_row.failed')+ true+ end+def process!(mappings: nil, schema_fields: nil)- start_processing!+ update!(status: 'processing')self.item = import.row_processor_class.new(self, mappings: mappings, schema_fields: schema_fields).process!self.validation_errors = nil # clear a stale error when a retried row succeeds- complete!+ update!(status: 'completed')+ publish_event('import_row.completed')rescue StandardError => eRails.error.report(e, handled: true, context: { import_row_id: id }, source: 'spree.core')self.validation_errors = e.message- fail!+ update!(status: 'failed')+ publish_event('import_row.failed')end# Bulk processing mode for large imports.@@ -113,12 +128,5 @@ module Spreeupdate_columns(status: 'failed', validation_errors: e.message, updated_at: Time.current)end- def publish_import_row_completed_event- publish_event('import_row.completed')- end-- def publish_import_row_failed_event- publish_event('import_row.failed')- endendenddiff --git a/spree/core/app/services/spree/sample_data/import_runner.rb b/spree/core/app/services/spree/sample_data/import_runner.rbindex 5917bec897..2a14840d7d 100644--- a/spree/core/app/services/spree/sample_data/import_runner.rb+++ b/spree/core/app/services/spree/sample_data/import_runner.rb@@ -25,8 +25,8 @@ module Spreeinline: inline, skip_events: skip_events)- import.start_mapping- import.complete_mapping+ Spree.import_start_mapping_workflow.call(import: import)+ Spree.import_complete_mapping_workflow.call(import: import)success(import.reload)enddiff --git a/spree/core/lib/spree/testing_support/imports.rb b/spree/core/lib/spree/testing_support/imports.rbnew file mode 100644index 0000000000..c1540258a8--- /dev/null+++ b/spree/core/lib/spree/testing_support/imports.rb@@ -0,0 +1,16 @@+module Spree+ module TestingSupport+ module Imports+ # Builds an import's column mappings without moving it out of whatever+ # status the example put it in. Spree::Imports::StartMapping does both,+ # which is right for the pipeline but wrong for a spec that starts an+ # import mid-flight.+ def seed_import_mappings(import)+ status_before = import.status+ Spree.import_start_mapping_workflow.call(import: import)+ import.update_columns(status: status_before, updated_at: Time.current)+ import.reload+ end+ end+ end+enddiff --git a/spree/core/spec/jobs/spree/imports/process_group_job_spec.rb b/spree/core/spec/jobs/spree/imports/process_group_job_spec.rbindex edeb20b166..7782f1c375 100644--- a/spree/core/spec/jobs/spree/imports/process_group_job_spec.rb+++ b/spree/core/spec/jobs/spree/imports/process_group_job_spec.rb@@ -15,7 +15,7 @@ RSpec.describe Spree::Imports::ProcessGroupJob, type: :job dobefore doallow_any_instance_of(Spree::Import).to receive(:csv_headers).and_return(['slug', 'sku', 'name', 'price'])- import.create_mappings+ seed_import_mappings(import)endit 'sets Spree::Current.store from the import' do@@ -225,7 +225,7 @@ RSpec.describe Spree::Imports::ProcessGroupJob, type: :job dobefore doallow_any_instance_of(Spree::Import).to receive(:csv_headers).and_return(['slug', 'sku', 'name', 'price', 'option1_name', 'option1_value'])import.mappings.destroy_all- import.create_mappings+ seed_import_mappings(import)endit 'creates product and variant in row_number order' do@@ -270,7 +270,7 @@ RSpec.describe Spree::Imports::ProcessGroupJob, type: :job dobefore doallow_any_instance_of(Spree::Import).to receive(:csv_headers).and_return(['slug', 'sku', 'name', 'price', 'option1_name', 'option1_value'])import.mappings.destroy_all- import.create_mappings+ seed_import_mappings(import)endit 'creates product and variant via bulk_process!' dodiff --git a/spree/core/spec/jobs/spree/imports/process_job_spec.rb b/spree/core/spec/jobs/spree/imports/process_job_spec.rbindex 0f9f844907..649f410f40 100644--- a/spree/core/spec/jobs/spree/imports/process_job_spec.rb+++ b/spree/core/spec/jobs/spree/imports/process_job_spec.rb@@ -98,7 +98,7 @@ RSpec.describe Spree::Imports::ProcessJob, type: :job dobefore doallow_any_instance_of(Spree::Import).to receive(:csv_headers).and_return(['slug', 'sku', 'name', 'price', 'option1_name', 'option1_value'])- import.create_mappings+ seed_import_mappings(import)endlet!(:product_row) dodiff --git a/spree/core/spec/models/spree/import_row_spec.rb b/spree/core/spec/models/spree/import_row_spec.rbindex 0a324826c4..df94277cf4 100644--- a/spree/core/spec/models/spree/import_row_spec.rb+++ b/spree/core/spec/models/spree/import_row_spec.rb@@ -39,47 +39,20 @@ RSpec.describe Spree::ImportRow, :job, type: :model doendend- describe 'State machine' do+ describe 'status' dobefore { import_row.save! }- describe 'initial state' do- it 'starts in pending state' do- expect(import_row.status).to eq('pending')- end+ it 'has no state machine' do+ expect(described_class).not_to respond_to(:state_machines)end- describe 'start_processing event' do- it 'transitions from pending to processing' do- expect { import_row.start_processing! }.to change(import_row, :status).from('pending').to('processing')- end- end-- describe 'fail event' do- before { import_row.start_processing! }-- it 'transitions from processing to failed' do- expect { import_row.fail! }.to change(import_row, :status).from('processing').to('failed')- end-- it 'publishes import_row.fail event' do- expect(import_row).to receive(:publish_import_row_failed_event)- import_row.fail!- end-+ it 'starts in pending status' do+ expect(import_row.status).to eq('pending')end- describe 'complete event' do- before { import_row.start_processing! }-- it 'transitions from processing to completed' do- expect { import_row.complete! }.to change(import_row, :status).from('processing').to('completed')- end-- it 'publishes import_row.complete event' do- expect(import_row).to receive(:publish_import_row_completed_event)- import_row.complete!- end-+ it 'rejects an unknown status' do+ import_row.status = 'nonsense'+ expect(import_row).not_to be_validendend@@ -145,7 +118,7 @@ RSpec.describe Spree::ImportRow, :job, type: :model dodescribe '#to_schema_hash' dobefore do- import.create_mappings+ Spree.import_start_mapping_workflow.call(import: import)endit 'returns attributes mapped to schema fields' do@@ -157,7 +130,7 @@ RSpec.describe Spree::ImportRow, :job, type: :model dodescribe '#attribute_by_schema_field' dobefore do- import.create_mappings+ Spree.import_start_mapping_workflow.call(import: import)endit 'returns the mapped attribute value' do@@ -201,6 +174,13 @@ RSpec.describe Spree::ImportRow, :job, type: :model doexpect { import_row.process! }.to change(import_row, :status).from('pending').to('failed')end+ it 'publishes import_row.failed', events: true do+ allow(import_row).to receive(:publish_event).with(anything)+ expect(import_row).to receive(:publish_event).with('import_row.failed')++ import_row.process!+ end+it 'sets validation errors' doimport_row.process!expect(import_row.validation_errors).to eq(error_message)diff --git a/spree/core/spec/models/spree/import_spec.rb b/spree/core/spec/models/spree/import_spec.rbindex 99aa0d2892..61b7ee58e3 100644--- a/spree/core/spec/models/spree/import_spec.rb+++ b/spree/core/spec/models/spree/import_spec.rb@@ -41,71 +41,20 @@ RSpec.describe Spree::Import, :job, type: :model doendend- describe 'State machine' do+ describe 'status' dobefore { import.save! }- describe 'initial state' do- it 'starts in pending state' do- expect(import.status).to eq('pending')- end+ it 'has no state machine' do+ expect(described_class).not_to respond_to(:state_machines)end- describe 'start_mapping event' do- it 'transitions from pending to mapping' do- expect { import.start_mapping! }.to change(import, :status).from('pending').to('mapping')- end-- it 'creates mappings before transition' do- expect { import.start_mapping! }.to change { import.mappings.count }.from(0)- end- end-- describe 'complete_mapping event' do- before { import.start_mapping! }-- it 'transitions from mapping to completed_mapping' do- expect { import.complete_mapping! }.to change(import, :status).from('mapping').to('completed_mapping')- end-- it 'creates rows asynchronously after transition' do- expect(import).to receive(:create_rows_async)- import.complete_mapping!- end+ it 'starts in pending status' do+ expect(import.status).to eq('pending')end- describe 'start_processing event' do- before do- import.start_mapping!- import.complete_mapping!- end-- it 'transitions from completed_mapping to processing' do- expect { import.start_processing! }.to change(import, :status).from('completed_mapping').to('processing')- end- end-- describe 'complete event' do- before do- import.start_mapping!- import.complete_mapping!- import.start_processing!- end-- it 'transitions from processing to completed' do- expect { import.complete! }.to change(import, :status).from('processing').to('completed')- end-- it 'touches the store' do- expect { import.complete! }.to change { store.reload.updated_at }- end-- it 'publishes import.completed event' do- expect(import).to receive(:publish_import_completed_event)- import.complete!- end-- # Email and loader updates are now handled by ImportSubscriber- # See spec/subscribers/spree/import_subscriber_spec.rb+ it 'rejects an unknown status' do+ import.status = 'nonsense'+ expect(import).not_to be_validendend@@ -288,15 +237,15 @@ RSpec.describe Spree::Import, :job, type: :model doendend- describe '#create_mappings' do+ describe 'mapping creation' dobefore { import.save! }it 'creates mappings for schema fields' do- expect { import.create_mappings }.to change { import.mappings.count }.from(0)+ expect { Spree.import_start_mapping_workflow.call(import: import) }.to change { import.mappings.count }.from(0)endit 'auto-assigns file columns when possible' do- import.create_mappings+ Spree.import_start_mapping_workflow.call(import: import)slug_mapping = import.mappings.find_by(schema_field: 'slug')expect(slug_mapping.file_column).to eq('slug')end@@ -305,7 +254,7 @@ RSpec.describe Spree::Import, :job, type: :model dodescribe '#unmapped_file_columns' dobefore doimport.save!- import.create_mappings+ Spree.import_start_mapping_workflow.call(import: import)# Map only slug columnimport.mappings.find_by(schema_field: 'slug').update!(file_column: 'slug')end@@ -323,7 +272,7 @@ RSpec.describe Spree::Import, :job, type: :model docontext 'when all required fields are mapped' dobefore do- import.create_mappings+ Spree.import_start_mapping_workflow.call(import: import)endit 'returns true' do@@ -338,24 +287,6 @@ RSpec.describe Spree::Import, :job, type: :model doendend- describe '#create_rows_async' do- before { import.save! }-- it 'enqueues ProcessJob' do- expect { import.create_rows_async }- .to have_enqueued_job(Spree::Imports::ProcessJob).with(import.id).on_queue(Spree.queues.imports)- end- end-- describe '#process_rows_async' do- before { import.save! }-- it 'enqueues ProcessJob, skipping row creation' do- expect { import.process_rows_async }- .to have_enqueued_job(Spree::Imports::ProcessJob).with(import.id, skip_row_creation: true)- end- end-describe '#store' docontext 'when owner is a Store' doit 'returns the owner' do@@ -455,16 +386,16 @@ RSpec.describe Spree::Import, :job, type: :model dodescribe 'import.completed' dobefore doimport.save!- import.start_mapping!- import.complete_mapping!- import.start_processing!+ Spree.import_start_mapping_workflow.call(import: import)+ Spree.import_complete_mapping_workflow.call(import: import)+ Spree.import_start_processing_workflow.call(import: import)endit 'publishes import.completed event when completed' doexpect(import).to receive(:publish_event).with('import.completed')allow(import).to receive(:publish_event).with(anything)- import.complete!+ Spree.import_complete_workflow.call(import: import)endendenddiff --git a/spree/core/spec/services/spree/imports/row_processors/customer_spec.rb b/spree/core/spec/services/spree/imports/row_processors/customer_spec.rbindex 27a0abaa51..0af2218796 100644--- a/spree/core/spec/services/spree/imports/row_processors/customer_spec.rb+++ b/spree/core/spec/services/spree/imports/row_processors/customer_spec.rb@@ -9,7 +9,7 @@ RSpec.describe Spree::Imports::RowProcessors::Customer, type: :service dolet(:csv_row_headers) { Spree::ImportSchemas::Customers.new.headers }before do- import.create_mappings+ Spree.import_start_mapping_workflow.call(import: import)enddef csv_row_hash(attrs = {})diff --git a/spree/core/spec/services/spree/imports/row_processors/product_translation_spec.rb b/spree/core/spec/services/spree/imports/row_processors/product_translation_spec.rbindex 5e73f72dfd..8c135a9f88 100644--- a/spree/core/spec/services/spree/imports/row_processors/product_translation_spec.rb+++ b/spree/core/spec/services/spree/imports/row_processors/product_translation_spec.rb@@ -9,7 +9,7 @@ RSpec.describe Spree::Imports::RowProcessors::ProductTranslation, type: :servicelet(:csv_row_headers) { Spree::ImportSchemas::ProductTranslations.new.headers }before do- import.create_mappings+ Spree.import_start_mapping_workflow.call(import: import)enddef csv_row_hash(attrs = {})diff --git a/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rb b/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rbindex 73ee7374f5..d94ef4e469 100644--- a/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rb+++ b/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rb@@ -10,7 +10,7 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service dolet(:variant) { subject.process! }before do- import.create_mappings+ Spree.import_start_mapping_workflow.call(import: import)# Manually map fields since there's no CSV file attachedimport.mappings.find_by(schema_field: 'tax_category')&.update(file_column: 'tax_category')import.mappings.find_by(schema_field: 'product_type')&.update(file_column: 'product_type')@@ -641,7 +641,7 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service do# Override the top-level before to create mappings after custom_field definitions existbefore do- import.create_mappings+ Spree.import_start_mapping_workflow.call(import: import)endlet(:product) { subject.process! }diff --git a/spree/core/spec/spec_helper.rb b/spree/core/spec/spec_helper.rbindex d1c5b80e46..ce9f96c5ab 100644--- a/spree/core/spec/spec_helper.rb+++ b/spree/core/spec/spec_helper.rb@@ -63,6 +63,7 @@ require 'spree/testing_support/lifecycle_events'require 'spree/testing_support/preferences'require 'spree/testing_support/url_helpers'require 'spree/testing_support/kernel'+require 'spree/testing_support/imports'require 'spree/testing_support/rspec_retry_config'require 'spree/testing_support/next_instance_of'@@ -102,6 +103,7 @@ RSpec.configure do |config|config.include FactoryBot::Syntax::Methodsconfig.include Spree::TestingSupport::Preferencesconfig.include Spree::TestingSupport::Kernel+ config.include Spree::TestingSupport::Importsconfig.order = :randomKernel.srand config.seeddiff --git a/spree/core/spec/workflows/spree/imports/pipeline_spec.rb b/spree/core/spec/workflows/spree/imports/pipeline_spec.rbnew file mode 100644index 0000000000..bfd852b4df--- /dev/null+++ b/spree/core/spec/workflows/spree/imports/pipeline_spec.rb@@ -0,0 +1,109 @@+require 'spec_helper'++RSpec.describe 'Spree::Imports pipeline workflows' do+ let(:store) { Spree::Store.default }+ let(:import) { create(:product_import, owner: store) }++ describe Spree::Imports::StartMapping do+ it 'moves the import into mapping and builds a mapping per schema field' do+ expect { described_class.call(import: import) }.to change { import.reload.status }.from('pending').to('mapping')+ expect(import.mappings.count).to be > 0+ end+ end++ describe Spree::Imports::CompleteMapping do+ before { Spree.import_start_mapping_workflow.call(import: import) }++ it 'accepts the mapping' do+ expect { described_class.call(import: import) }.to change { import.reload.status }.from('mapping').to('completed_mapping')+ end++ it 'enqueues row creation' do+ expect { described_class.call(import: import) }+ .to have_enqueued_job(Spree::Imports::ProcessJob).with(import.id).on_queue(Spree.queues.imports)+ end++ it 'refuses an import that is not being mapped' do+ import.update!(status: 'pending')++ result = described_class.call(import: import)++ expect(result).not_to be_success+ expect(result.error.value).to eq(:import_not_mapping)+ end+ end++ # The machine ran its callbacks inside a transaction, so an inline run had+ # to defer itself to after_all_transactions_commit. Dispatching outside the+ # transaction removes that workaround — this proves the whole pipeline still+ # runs to completion with no worker attached, which is what seeds and rake+ # tasks depend on.+ describe 'an inline run', :job do+ it 'processes every row through to a completed import' do+ import.set_preference(:inline, true)+ import.save!++ Spree.import_start_mapping_workflow.call(import: import)+ Spree.import_complete_mapping_workflow.call(import: import)++ expect(import.reload.status).to eq('completed')+ expect(import.rows.count).to be > 0+ expect(import.rows.failed.count).to eq(0)+ expect(import.rows.completed.count).to eq(import.rows.count)+ end+ end++ describe Spree::Imports::StartProcessing do+ before { import.update!(status: 'completed_mapping') }++ it 'moves the import into processing' do+ expect { described_class.call(import: import) }.to change { import.reload.status }.to('processing')+ end+ end++ describe Spree::Imports::Complete do+ before { import.update!(status: 'processing') }++ it 'completes the import' do+ expect { described_class.call(import: import) }.to change { import.reload.status }.from('processing').to('completed')+ end++ it 'touches the store, so its cache keys move with the catalog' do+ expect { described_class.call(import: import) }.to change { store.reload.updated_at }+ end++ it 'publishes import.completed', events: true do+ allow(import).to receive(:publish_event).with(anything)+ expect(import).to receive(:publish_event).with('import.completed')++ described_class.call(import: import)+ end+ end++ describe Spree::Imports::RetryFailedRows do+ before { import.update!(status: 'completed') }++ context 'with failed rows' do+ before { create(:import_row, import: import, status: 'failed') }++ it 'puts the import back to processing' do+ expect { described_class.call(import: import) }.to change { import.reload.status }.from('completed').to('processing')+ end++ it 'enqueues processing that skips row creation' do+ expect { described_class.call(import: import) }+ .to have_enqueued_job(Spree::Imports::ProcessJob).with(import.id, skip_row_creation: true)+ end+ end++ context 'without failed rows' do+ it 'refuses' do+ result = described_class.call(import: import)++ expect(result).not_to be_success+ expect(result.error.value).to eq(:import_has_no_failed_rows)+ expect(import.reload.status).to eq('completed')+ end+ end+ end+end