-
Notifications
You must be signed in to change notification settings - Fork 6
feat: match array properties element-wise for non-set operators #92
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
Merged
+183
−32
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module AmplitudeExperiment | ||
| describe Evaluation::Engine do | ||
| let(:engine) { Evaluation::Engine.new } | ||
|
|
||
| def flag_with_condition(op, values) | ||
| Evaluation::Flag.from_hash( | ||
| 'key' => 'test-flag', | ||
| 'variants' => { | ||
| 'on' => { 'key' => 'on', 'value' => 'on' } | ||
| }, | ||
| 'segments' => [ | ||
| { | ||
| 'conditions' => [[ | ||
| { | ||
| 'selector' => %w[context user user_properties test_prop], | ||
| 'op' => op, | ||
| 'values' => values | ||
| } | ||
| ]], | ||
| 'variant' => 'on' | ||
| } | ||
| ] | ||
| ) | ||
| end | ||
|
|
||
| def context_with_prop(value) | ||
| { | ||
| 'user' => { | ||
| 'user_properties' => { 'test_prop' => value } | ||
| } | ||
| } | ||
| end | ||
|
|
||
| def evaluate(prop_value, op, values) | ||
| flag = flag_with_condition(op, values) | ||
| context = context_with_prop(prop_value) | ||
| engine.evaluate(context, [flag])['test-flag'] | ||
| end | ||
|
|
||
| def assert_match(prop_value, op, values) | ||
| result = evaluate(prop_value, op, values) | ||
| expect(result).not_to be_nil | ||
| expect(result.key).to eq('on') | ||
| end | ||
|
|
||
| def assert_no_match(prop_value, op, values) | ||
| result = evaluate(prop_value, op, values) | ||
| expect(result).to be_nil | ||
| end | ||
|
|
||
| describe 'non-set operator array matching' do | ||
| it 'matches scalar string IS' do | ||
| assert_match('hello', Evaluation::Operator::IS, ['hello']) | ||
| end | ||
|
|
||
| it 'matches scalar string CONTAINS' do | ||
| assert_match('hello', Evaluation::Operator::CONTAINS, ['ell']) | ||
| end | ||
|
|
||
| it 'matches scalar string GREATER_THAN' do | ||
| assert_match('2', Evaluation::Operator::GREATER_THAN, ['1']) | ||
| end | ||
|
|
||
| it 'does not match scalar string IS when value differs' do | ||
| assert_no_match('world', Evaluation::Operator::IS, ['hello']) | ||
| end | ||
|
|
||
| it 'matches non-string scalar GREATER_THAN' do | ||
| assert_match(42, Evaluation::Operator::GREATER_THAN, ['1']) | ||
| end | ||
|
|
||
| it 'matches non-string scalar IS (boolean)' do | ||
| assert_match(true, Evaluation::Operator::IS, ['true']) | ||
| end | ||
|
|
||
| it 'matches JSON array string with set operator' do | ||
| assert_match('["a","b"]', Evaluation::Operator::SET_CONTAINS, ['a']) | ||
| end | ||
|
|
||
| it 'matches JSON array string with non-set operator' do | ||
| assert_match('["a","b"]', Evaluation::Operator::IS, ['a']) | ||
| end | ||
|
|
||
| it 'matches collection with set operator' do | ||
| assert_match(%w[a b], Evaluation::Operator::SET_CONTAINS, ['a']) | ||
| end | ||
|
|
||
| it 'matches collection with non-set operator' do | ||
| assert_match(%w[a b], Evaluation::Operator::IS, ['a']) | ||
| end | ||
|
|
||
| it 'falls through for malformed JSON array and matches as scalar' do | ||
| assert_match('[broken', Evaluation::Operator::IS, ['[broken']) | ||
| end | ||
|
|
||
| it 'does not match empty JSON array for set operator' do | ||
| assert_no_match('[]', Evaluation::Operator::SET_CONTAINS, ['a']) | ||
| end | ||
|
|
||
| it 'treats leading-whitespace string as scalar for non-set operator' do | ||
| assert_match(' ["a"]', Evaluation::Operator::IS, [' ["a"]']) | ||
| end | ||
|
|
||
| it 'treats leading-whitespace string as scalar for set operator (no match)' do | ||
| assert_no_match(' ["a"]', Evaluation::Operator::SET_CONTAINS, ['a']) | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.