Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options to PromotionCode::BatchBuilder and spec for unique promotion code contention #2579

Merged
merged 2 commits into from
Feb 16, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add spec for BatchBuilder contention
Previously we had an issue where BatchBuilder would create the wrong
number of promotion codes if it encountered a conflict. Out specs didn't
catch this.

This commit adds an additional spec which runs the BatchBuilder with
settings that make it extremely likely that there will a conflict: from
a space of 100 possible codes (00 to 99) we generate 50.

An online birthday problem calculator tells me this is 99.99997%
likely that the generator will encounter at least one conflict. That
seems plenty. I haven't verified this, but intuitively it seems very
likely.
  • Loading branch information
jhawthorn committed Feb 16, 2018
commit 17f0ef0ff2870d60ac1c210a69fe9cc7a9c5fd80
19 changes: 18 additions & 1 deletion core/spec/models/spree/promotion_code/batch_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
let(:promotion) { create(:promotion) }
let(:base_code) { "abc" }
let(:options) { {} }
let(:number_of_codes) { 10 }
let(:promotion_code_batch) do
Spree::PromotionCodeBatch.create!(
promotion_id: promotion.id,
base_code: base_code,
number_of_codes: 10,
number_of_codes: number_of_codes,
email: "[email protected]"
)
end
Expand Down Expand Up @@ -54,6 +55,22 @@
expect(promotion_code_batch.state).to eq("completed")
end
end

context "with likely code contention" do
let(:number_of_codes) { 50 }
let(:options) do
{
batch_size: 10,
sample_characters: (0..9).to_a.map(&:to_s),
random_code_length: 2
}
end

it "creates the correct number of codes" do
subject.build_promotion_codes
expect(promotion.codes.size).to eq(number_of_codes)
end
end
end

describe "#join_character" do
Expand Down