Skip to content

Commit 64ecee0

Browse files
AlistairNormanforkata
authored andcommitted
Add tests to ensure compute amount doesn't persist
We want to make sure we don't persist anything when computing amounts becuase that will cause issues with the in-memory order updater.
1 parent 41f2a2e commit 64ecee0

File tree

5 files changed

+79
-3
lines changed

5 files changed

+79
-3
lines changed

legacy_promotions/spec/models/spree/promotion/actions/create_adjustment_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,42 @@
126126
is_expected.to eq(Spree::Config.promotions.calculators[described_class.to_s])
127127
}
128128
end
129+
130+
describe "#compute_amount" do
131+
subject { action.compute_amount(order) }
132+
133+
before do
134+
promotion.promotion_actions = [action]
135+
action.calculator = Spree::Calculator::FlatRate.new(preferred_amount:)
136+
end
137+
138+
let(:preferred_amount) { 50 }
139+
140+
context "when the adjustable is actionable" do
141+
it "calls compute on the calculator" do
142+
expect(action.calculator).to receive(:compute).with(order).and_call_original
143+
subject
144+
end
145+
146+
it "doesn't persist anything to the database" do
147+
allow(action.calculator).to receive(:compute).with(order).and_call_original
148+
149+
expect {
150+
subject
151+
}.not_to make_database_queries(manipulative: true)
152+
end
153+
154+
context "calculator returns amount greater than order total" do
155+
let(:preferred_amount) { 300 }
156+
157+
before do
158+
allow(order).to receive_messages(item_total: 50, ship_total: 50)
159+
end
160+
161+
it "does not exceed it" do
162+
expect(subject).to eql(-100)
163+
end
164+
end
165+
end
166+
end
129167
end

legacy_promotions/spec/models/spree/promotion/actions/create_item_adjustments_spec.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,23 @@ module Spree
9292
end
9393
end
9494

95-
context "#compute_amount" do
95+
describe "#compute_amount" do
96+
subject { action.compute_amount(line_item) }
97+
9698
before { promotion.promotion_actions = [action] }
9799

98100
context "when the adjustable is actionable" do
99101
it "calls compute on the calculator" do
100102
expect(action.calculator).to receive(:compute).with(line_item).and_call_original
101-
action.compute_amount(line_item)
103+
subject
104+
end
105+
106+
it "doesn't persist anything to the database" do
107+
allow(action.calculator).to receive(:compute).with(line_item).and_call_original
108+
109+
expect {
110+
subject
111+
}.not_to make_database_queries(manipulative: true)
102112
end
103113

104114
context "calculator returns amount greater than item total" do
@@ -108,7 +118,7 @@ module Spree
108118
end
109119

110120
it "does not exceed it" do
111-
expect(action.compute_amount(line_item)).to eql(-100)
121+
expect(subject).to eql(-100)
112122
end
113123
end
114124
end

legacy_promotions/spec/rails_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
require 'rspec/rails'
2828
require 'rspec-activemodel-mocks'
2929
require 'database_cleaner'
30+
require 'db-query-matchers'
3031

3132
Dir["./spec/support/**/*.rb"].sort.each { |f| require f }
3233

promotions/spec/models/solidus_promotions/benefit_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,25 @@ def compute_line_item(_line_item, _options) = 1
122122
end
123123
end
124124

125+
describe "#compute_amount" do
126+
subject { benefit.compute_amount(discountable) }
127+
128+
let(:variant) { create(:variant) }
129+
let(:order) { create(:order) }
130+
let(:discountable) { Spree::LineItem.new(order: order, variant: variant, price: 10, quantity: 1) }
131+
let(:promotion) { SolidusPromotions::Promotion.new(customer_label: "20 Perzent off") }
132+
let(:calculator) { SolidusPromotions::Calculators::Percent.new(preferred_percent: 20) }
133+
let(:benefit) { described_class.new(promotion: promotion, calculator: calculator) }
134+
135+
it "doesn't save anything to the database" do
136+
discountable
137+
138+
expect {
139+
subject
140+
}.not_to make_database_queries(manipulative: true)
141+
end
142+
end
143+
125144
describe ".original_promotion_action" do
126145
let(:spree_promotion) { create :promotion, :with_adjustable_action }
127146
let(:spree_promotion_action) { spree_promotion.actions.first }

promotions/spec/models/solidus_promotions/benefits/adjust_line_item_quantity_groups_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@
5050
context "and an item with a quantity of 3" do
5151
let(:quantity) { 3 }
5252
it { is_expected.to eq(-10) }
53+
54+
it "doesn't save anything to the database" do
55+
line_item
56+
57+
expect {
58+
subject
59+
}.not_to make_database_queries(manipulative: true)
60+
end
5361
end
5462

5563
context "and an item with a quantity of 4" do

0 commit comments

Comments
 (0)