1+ # frozen_string_literal: true
2+
3+ require 'spec_helper'
4+
5+ describe SplitIoClient ::RuleBasedSegmentMatcher do
6+ let ( :config ) { SplitIoClient ::SplitConfig . new ( debug_enabled : true ) }
7+ let ( :segments_repository ) { SplitIoClient ::Cache ::Repositories ::SegmentsRepository . new ( config ) }
8+ let ( :flag_sets_repository ) { SplitIoClient ::Cache ::Repositories ::MemoryFlagSetsRepository . new ( [ ] ) }
9+ let ( :flag_set_filter ) { SplitIoClient ::Cache ::Filter ::FlagSetsFilter . new ( [ ] ) }
10+ let ( :splits_repository ) { SplitIoClient ::Cache ::Repositories ::SplitsRepository . new ( config , flag_sets_repository , flag_set_filter ) }
11+ let ( :evaluator ) { SplitIoClient ::Engine ::Parser ::Evaluator . new ( segments_repository , splits_repository , true ) }
12+
13+
14+ context '#string_type' do
15+ it 'is not string type matcher' do
16+ expect ( described_class . new ( nil , nil , nil , config , nil ) . string_type? ) . to be false
17+ end
18+ end
19+
20+ context 'test_matcher' do
21+ it 'return false if excluded key is passed' do
22+ rbs_repositoy = SplitIoClient ::Cache ::Repositories ::RuleBasedSegmentsRepository . new ( config )
23+ rbs_repositoy . update ( [ { name : 'foo' , trafficTypeName : 'tt_name_1' , conditions : [ ] , excluded : { keys : [ 'key1' ] , segments : [ ] } } ] , [ ] , -1 )
24+ matcher = described_class . new ( rbs_repositoy , segments_repository , 'foo' , config , nil )
25+ expect ( matcher . match? ( value : 'key1' ) ) . to be false
26+ end
27+
28+ it 'return false if excluded segment is passed' do
29+ rbs_repositoy = SplitIoClient ::Cache ::Repositories ::RuleBasedSegmentsRepository . new ( config )
30+ segments_repository . add_to_segment ( { :name => 'segment1' , :added => [ ] , :removed => [ ] } )
31+ rbs_repositoy . update ( [ { name : 'foo' , trafficTypeName : 'tt_name_1' , conditions : [ ] , excluded : { keys : [ 'key1' ] , segments : [ 'segment1' ] } } ] , [ ] , -1 )
32+ matcher = described_class . new ( rbs_repositoy , segments_repository , 'foo' , config , nil )
33+ expect ( matcher . match? ( value : 'key2' ) ) . to be false
34+ end
35+
36+ it 'return true if condition matches' do
37+ rule_based_segment = { :name => 'corge' , :trafficTypeName => 'tt_name_5' ,
38+ :excluded => { :keys => [ ] , :segments => [ ] } ,
39+ :conditions => [
40+ {
41+ :contitionType => 'WHITELIST' ,
42+ :label => 'some_label' ,
43+ :matcherGroup => {
44+ :matchers => [
45+ {
46+ :keySelector => nil ,
47+ :matcherType => 'WHITELIST' ,
48+ :whitelistMatcherData => {
49+ :whitelist => [ 'k1' , 'k2' , 'k3' ]
50+ } ,
51+ :negate => false ,
52+ }
53+ ] ,
54+ :combiner => 'AND'
55+ }
56+ } ]
57+ }
58+
59+ rbs_repositoy = SplitIoClient ::Cache ::Repositories ::RuleBasedSegmentsRepository . new ( config )
60+ rbs_repositoy . update ( [ rule_based_segment ] , [ ] , -1 )
61+ matcher = described_class . new ( rbs_repositoy , segments_repository , 'corge' , config , evaluator )
62+ expect ( matcher . match? ( { :matching_key => 'user' , :attributes => { } } ) ) . to be false
63+ expect ( matcher . match? ( { :matching_key => 'k1' , :attributes => { } } ) ) . to be true
64+ end
65+ end
66+ end
0 commit comments