Fix product import date when not all variants have it

This commit is contained in:
Kristina Lim
2018-10-06 03:02:13 +08:00
parent 58a99a7f89
commit da904c908d
2 changed files with 42 additions and 4 deletions

View File

@@ -177,10 +177,7 @@ Spree::Product.class_eval do
# Get the most recent import_date of a product's variants
def import_date
variants.map do |variant|
next if variant.import_date.blank?
variant.import_date
end.sort.last
variants.map(&:import_date).compact.max
end
# Build a product distribution for each distributor

View File

@@ -714,4 +714,45 @@ module Spree
end
end
end
describe "product import" do
describe "finding the most recent import date of the variants" do
let!(:product) { create(:product) }
let(:reference_time) { Time.zone.now.beginning_of_day }
before do
product.reload
end
context "when the variants do not have an import date" do
let!(:variant_a) { create(:variant, product: product, import_date: nil) }
let!(:variant_b) { create(:variant, product: product, import_date: nil) }
it "returns nil" do
expect(product.import_date).to be_nil
end
end
context "when some variants have import date and some do not" do
let!(:variant_a) { create(:variant, product: product, import_date: nil) }
let!(:variant_b) { create(:variant, product: product, import_date: reference_time - 1.hour) }
let!(:variant_c) { create(:variant, product: product, import_date: reference_time - 2.hour) }
it "returns the most recent import date" do
expect(product.import_date).to eq(variant_b.import_date)
end
end
context "when all variants have import date" do
let!(:variant_a) { create(:variant, product: product, import_date: reference_time - 2.hour) }
let!(:variant_b) { create(:variant, product: product, import_date: reference_time - 1.hour) }
let!(:variant_c) { create(:variant, product: product, import_date: reference_time - 3.hour) }
it "returns the most recent import date" do
expect(product.import_date).to eq(variant_b.import_date)
end
end
end
end
end