Merge branch 'master' into 2-0-stable-jan-8th

This commit is contained in:
luisramos0
2019-01-08 14:29:50 +00:00
42 changed files with 977 additions and 144 deletions

View File

@@ -35,6 +35,82 @@ describe VariantOverride do
end
end
describe "validation" do
describe "ensuring that on_demand and count_on_hand are compatible" do
let(:variant_override) { build(:variant_override, hub: hub, variant: variant,
on_demand: on_demand, count_on_hand: count_on_hand) }
context "when using producer stock settings" do
let(:on_demand) { nil }
context "when count_on_hand is blank" do
let(:count_on_hand) { nil }
it "is valid" do
expect(variant_override.save).to be_truthy
end
end
context "when count_on_hand is set" do
let(:count_on_hand) { 1 }
it "is invalid" do
expect(variant_override.save).to be_falsey
error_message = I18n.t("using_producer_stock_settings_but_count_on_hand_set",
scope: [i18n_scope_for_error, "count_on_hand"])
expect(variant_override.errors[:count_on_hand]).to eq([error_message])
end
end
end
context "when on demand" do
let(:on_demand) { true }
context "when count_on_hand is blank" do
let(:count_on_hand) { nil }
it "is valid" do
expect(variant_override.save).to be_truthy
end
end
context "when count_on_hand is set" do
let(:count_on_hand) { 1 }
it "is invalid" do
expect(variant_override.save).to be_falsey
error_message = I18n.t("on_demand_but_count_on_hand_set",
scope: [i18n_scope_for_error, "count_on_hand"])
expect(variant_override.errors[:count_on_hand]).to eq([error_message])
end
end
end
context "when limited stock" do
let(:on_demand) { false }
context "when count_on_hand is blank" do
let(:count_on_hand) { nil }
it "is invalid" do
expect(variant_override.save).to be_falsey
error_message = I18n.t("limited_stock_but_no_count_on_hand",
scope: [i18n_scope_for_error, "count_on_hand"])
expect(variant_override.errors[:count_on_hand]).to eq([error_message])
end
end
context "when count_on_hand is set" do
let(:count_on_hand) { 1 }
it "is valid" do
expect(variant_override.save).to be_truthy
end
end
end
end
end
describe "callbacks" do
let!(:vo) { create(:variant_override, hub: hub, variant: variant) }
@@ -119,17 +195,42 @@ describe VariantOverride do
end
describe "resetting stock levels" do
it "resets the on hand level to the value in the default_stock field" do
vo = create(:variant_override, variant: variant, hub: hub, count_on_hand: 12, default_stock: 20, resettable: true)
vo.reset_stock!
expect(vo.reload.count_on_hand).to eq(20)
describe "forcing the on hand level to the value in the default_stock field" do
it "succeeds for variant override that forces limited stock" do
vo = create(:variant_override, variant: variant, hub: hub, count_on_hand: 12, default_stock: 20, resettable: true)
vo.reset_stock!
vo.reload
expect(vo.on_demand).to eq(false)
expect(vo.count_on_hand).to eq(20)
end
it "succeeds for variant override that forces unlimited stock" do
vo = create(:variant_override, :on_demand, variant: variant, hub: hub, default_stock: 20, resettable: true)
vo.reset_stock!
vo.reload
expect(vo.on_demand).to eq(false)
expect(vo.count_on_hand).to eq(20)
end
it "succeeds for variant override that uses producer stock settings" do
vo = create(:variant_override, :use_producer_stock_settings, variant: variant, hub: hub, default_stock: 20, resettable: true)
vo.reset_stock!
vo.reload
expect(vo.on_demand).to eq(false)
expect(vo.count_on_hand).to eq(20)
end
end
it "silently logs an error if the variant override doesn't have a default stock level" do
vo = create(:variant_override, variant: variant, hub: hub, count_on_hand: 12, default_stock: nil, resettable: true)
expect(Bugsnag).to receive(:notify)
vo.reset_stock!
expect(vo.reload.count_on_hand).to eq(12)
end
it "doesn't reset the level if the behaviour is disabled" do
vo = create(:variant_override, variant: variant, hub: hub, count_on_hand: 12, default_stock: 10, resettable: false)
vo.reset_stock!
@@ -140,4 +241,8 @@ describe VariantOverride do
context "extends LocalizedNumber" do
it_behaves_like "a model using the LocalizedNumber module", [:price]
end
def i18n_scope_for_error
"activerecord.errors.models.variant_override"
end
end