Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Fix/spree sample load task (#11374)

Damian Legawiec279caaada972
diff --git a/bin/build-ci.rb b/bin/build-ci.rb
index 5a24cea0a1..c036ffaffa 100755
--- a/bin/build-ci.rb
+++ b/bin/build-ci.rb
@@ -21,7 +21,7 @@ class Project
@name = name
end
- ALL = %w[api core emails].map(&method(:new)).freeze
+ ALL = %w[api core emails sample].map(&method(:new)).freeze
CORE_GEMS = %w[api core].freeze
# Install subproject
diff --git a/bin/build.sh b/bin/build.sh
index 3d39b79c44..75c624c135 100755
--- a/bin/build.sh
+++ b/bin/build.sh
@@ -39,3 +39,9 @@ echo "* Setup Spree Emails and running RSpec..."
echo "*******************************************"
cd ../emails; prepare_app; bundle exec rspec spec
+# Spree Sample
+echo "*******************************************"
+echo "* Setup Spree Sample and running RSpec..."
+echo "*******************************************"
+cd ../sample; prepare_app; bundle exec rspec spec
+
diff --git a/core/app/services/spree/stock_locations/stock_items/create.rb b/core/app/services/spree/stock_locations/stock_items/create.rb
index a3c9dc6945..dc2c148271 100644
--- a/core/app/services/spree/stock_locations/stock_items/create.rb
+++ b/core/app/services/spree/stock_locations/stock_items/create.rb
@@ -14,7 +14,7 @@ module Spree
'created_at', Time.current,
'updated_at', Time.current
]
- end.uniq { |stock_item| stock_item.values_at('stock_location_id', 'variant_id', 'backorderable') }
+ end
if prepared_stock_items.any?
stock_location.stock_items.insert_all(prepared_stock_items)
variants_scope.touch_all
diff --git a/core/db/migrate/20210929090344_create_stock_item_stock_location_id_variant_id_coalesce_deleted_at_unique_index.rb b/core/db/migrate/20210929090344_create_stock_item_stock_location_id_variant_id_coalesce_deleted_at_unique_index.rb
new file mode 100644
index 0000000000..02e26e71a6
--- /dev/null
+++ b/core/db/migrate/20210929090344_create_stock_item_stock_location_id_variant_id_coalesce_deleted_at_unique_index.rb
@@ -0,0 +1,32 @@
+class CreateStockItemStockLocationIdVariantIdCoalesceDeletedAtUniqueIndex < ActiveRecord::Migration[5.2]
+ def change
+ remove_index :spree_stock_items, name: :stock_item_by_loc_var_id_deleted_at
+ reversible do |dir|
+ dir.up do
+ execute <<-SQL
+ CREATE UNIQUE INDEX stock_item_by_loc_var_id_deleted_at
+ ON spree_stock_items(
+ stock_location_id,
+ variant_id,
+ (COALESCE(deleted_at, CAST('1970-01-01 00:00:00' AS #{deleted_at_data_type})))
+ );
+ SQL
+ end
+
+ dir.down do
+ remove_index :spree_stock_items, name: :stock_item_by_loc_var_id_deleted_at
+ end
+ end
+ end
+
+ private
+
+ def deleted_at_data_type
+ case ActiveRecord::Base.connection.adapter_name
+ when 'Mysql2'
+ 'DATETIME'
+ when 'PostgreSQL'
+ 'TIMESTAMP'
+ end
+ end
+end
diff --git a/core/lib/spree/testing_support/common_rake.rb b/core/lib/spree/testing_support/common_rake.rb
index f2cd41acef..0cb219b7a2 100644
--- a/core/lib/spree/testing_support/common_rake.rb
+++ b/core/lib/spree/testing_support/common_rake.rb
@@ -29,9 +29,9 @@ namespace :common do
]
puts 'Setting up dummy database...'
- system("bundle exec rake db:drop db:create > #{File::NULL}")
+ system('bundle exec rake db:drop db:create')
Spree::DummyModelGenerator.start
- system("bundle exec rake db:migrate > #{File::NULL}")
+ system('bundle exec rake db:migrate')
begin
require "generators/#{ENV['LIB_NAME']}/install/install_generator"
@@ -43,12 +43,12 @@ namespace :common do
unless ['spree/api', 'spree/core', 'spree/sample'].include?(ENV['LIB_NAME'])
puts 'Precompiling assets...'
- system("bundle exec rake assets:precompile > #{File::NULL}")
+ system('bundle exec rake assets:precompile')
end
end
task :seed do |_t|
puts 'Seeding ...'
- system("bundle exec rake db:seed RAILS_ENV=test > #{File::NULL}")
+ system('bundle exec rake db:seed RAILS_ENV=test')
end
end
diff --git a/core/spec/services/spree/stock_locations/stock_items/create_spec.rb b/core/spec/services/spree/stock_locations/stock_items/create_spec.rb
index dc61c220ea..1e7a019d58 100644
--- a/core/spec/services/spree/stock_locations/stock_items/create_spec.rb
+++ b/core/spec/services/spree/stock_locations/stock_items/create_spec.rb
@@ -30,23 +30,26 @@ module Spree
end
context 'Rails >= 6', if: Rails::VERSION::MAJOR >= 6 do
- before do
- # Delete all stock_location.stock_items to start counting from 0.
- stock_location.stock_items.unscope(:where).delete_all
- end
-
let(:time_current) { Time.local(1990) }
context 'with prepared stock items' do
- context 'with duplicate stock items by stock_location_id and variant' do
- it 'inserts the stock location stock items without duplicates' do
- # Make a call of Spree::Variant to obtain the ids, return duplicated records.
- expect(Spree::Variant).to receive(:ids).and_return([1, 1])
- expect { result }.to change { stock_location.stock_items.count }.from(0).to(1)
+ context 'with stock items in the db' do
+ before { Spree::StockItem.find(Spree::StockItem.ids.sample).delete }
+
+ it 'inserts stock items without duplicates' do
+ total_stock_items = Spree::StockItem.count
+ expect { result }.to change {
+ stock_location.stock_items.count
+ }.from(total_stock_items).to(total_stock_items + 1)
end
end
- context 'without duplicate stock items to insert' do
+ context 'without stock items in the db' do
+ before do
+ # Delete all stock_location.stock_items to start counting from 0.
+ stock_location.stock_items.unscope(:where).delete_all
+ end
+
let(:created_stock_item) { stock_location.stock_items.order(:id).last }
let(:created_stock_item_attrs) do
created_stock_item.attributes.values_at(
diff --git a/sample/db/samples/payments.rb b/sample/db/samples/payments.rb
index b72aa4510a..955ffef218 100644
--- a/sample/db/samples/payments.rb
+++ b/sample/db/samples/payments.rb
@@ -1,3 +1,5 @@
+Spree::Sample.load_sample('payment_methods')
+
# create payments based on the totals since they can't be known in YAML (quantities are random)
method = Spree::PaymentMethod.where(name: 'Credit Card', active: true).first
diff --git a/sample/db/samples/stock.rb b/sample/db/samples/stock.rb
index 82073e074e..3fc0a39ec7 100644
--- a/sample/db/samples/stock.rb
+++ b/sample/db/samples/stock.rb
@@ -1,7 +1,7 @@
Spree::Sample.load_sample('variants')
country = Spree::Country.find_by(iso: 'US')
-location = Spree::StockLocation.find_or_create_by!(name: 'default')
+location = Spree::StockLocation.find_or_create_by!(name: 'default', propagate_all_variants: false)
location.update(
address1: 'Example Street',
city: 'City',
@@ -11,6 +11,8 @@ location.update(
active: true
)
+Spree::StockLocations::StockItems::Create.call(stock_location: location)
+
product_1 = Spree::Product.find_by!(name: 'Denim Shirt')
product_2 = Spree::Product.find_by!(name: 'Checked Shirt')
diff --git a/sample/db/samples/taxons.rb b/sample/db/samples/taxons.rb
index e1d9341585..bf66522e04 100644
--- a/sample/db/samples/taxons.rb
+++ b/sample/db/samples/taxons.rb
@@ -28,7 +28,7 @@ CHILDREN_TAXON_NAMES.each do |(parent_name, taxon_name)|
taxon.save!
end
-taxon = categories_taxon.children.where(name: 'New', permalink: 'newest').first_or_create!
+taxon = categories_taxon.children.where(name: 'New').first_or_create!
taxon.taxonomy = categories
taxon.save!
diff --git a/sample/spec/spec_helper.rb b/sample/spec/spec_helper.rb
index 797f98eb09..9249ccea92 100644
--- a/sample/spec/spec_helper.rb
+++ b/sample/spec/spec_helper.rb
@@ -24,4 +24,8 @@ RSpec.configure do |config|
config.order = :random
Kernel.srand config.seed
+
+ config.before do
+ Spree::Seeds::All.call
+ end
end
diff --git a/sample/spec/support/database_cleaner.rb b/sample/spec/support/database_cleaner.rb
new file mode 100644
index 0000000000..2c2d42763e
--- /dev/null
+++ b/sample/spec/support/database_cleaner.rb
@@ -0,0 +1,14 @@
+RSpec.configure do |config|
+ config.before(:suite) do
+ DatabaseCleaner.strategy = :transaction
+ DatabaseCleaner.clean_with(:truncation)
+ end
+
+ config.before do
+ DatabaseCleaner.start
+ end
+
+ config.append_after do
+ DatabaseCleaner.clean
+ end
+end