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

Conversation

jhawthorn
Copy link
Contributor

@jhawthorn jhawthorn commented Feb 15, 2018

The issue fixed by #2578 wasn't caught by our specs. It was hard to write a spec to test that issue because the PromotionCode::BatchBuilder was configured by class_attributes.

This PR deprecates the existing class-attributes in favour of having a defaults option hash.

It uses this to add 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.

This would have caught the issue prior to #2578

Failures:

  1) Spree::PromotionCode::BatchBuilder#build_promotion_codes with likely code contention creates the correct number of codes
     Failure/Error: expect(promotion.codes.size).to eq(number_of_codes)

       expected: 50
            got: 61

       (compared using ==)
     # ./spec/models/spree/promotion_code/batch_builder_spec.rb:71:in `block (4 levels) in <top (required)>'

delegate attr, to: self
end

def initialize(promotion_code_batch, options={})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surrounding space missing in default value assignment.

@jhawthorn jhawthorn force-pushed the promotion_batch_options branch 2 times, most recently from fe2584f to 0d44f96 Compare February 15, 2018 22:15
@jhawthorn
Copy link
Contributor Author

Failure should be fixed by #2580

Copy link
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides a small nit. Nice 👍

define_singleton_method(:"#{attr}=") do |val|
Spree::Deprecation.warn "#{name}.#{attr}= is deprecated. Use #{name}::DEFAULT_OPTIONS[:#{attr}]= instead"
DEFAULT_OPTIONS[attr] = val
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍nice

@promotion_code_batch = promotion_code_batch
@options = DEFAULT_OPTIONS.merge(options)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary, but could we use a attr_reader for @options? Consistency

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with @tvdeyen , attr_reader for @options will make this class more consistent.

Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, not necessary, note as @tvdeyen. The rest is great, thanks!

@promotion_code_batch = promotion_code_batch
@options = DEFAULT_OPTIONS.merge(options)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with @tvdeyen , attr_reader for @options will make this class more consistent.

This deprecates the existing class-attributes in favour of having a
defaults option hash.

This allows creating instances of BatchBuilder with different rules than
the defaults. This makes testing different rules a lot easier and may
allow for per-batch settings in the future.
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.
@jhawthorn jhawthorn merged commit 637db0b into solidusio:master Feb 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants