|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | +require 'set' |
| 5 | + |
| 6 | +describe SplitIoClient::Cache::Repositories::RuleBasedSegmentsRepository do |
| 7 | + RSpec.shared_examples 'RuleBasedSegments Repository' do |cache_adapter| |
| 8 | + let(:config) { SplitIoClient::SplitConfig.new(cache_adapter: cache_adapter) } |
| 9 | + let(:repository) { described_class.new(config) } |
| 10 | + |
| 11 | + before :all do |
| 12 | + redis = Redis.new |
| 13 | + redis.flushall |
| 14 | + end |
| 15 | + |
| 16 | + before do |
| 17 | + # in memory setup |
| 18 | + repository.update([{name: 'foo', trafficTypeName: 'tt_name_1', conditions: []}, |
| 19 | + {name: 'bar', trafficTypeName: 'tt_name_2', conditions: []}, |
| 20 | + {name: 'baz', trafficTypeName: 'tt_name_1', conditions: []}], [], -1) |
| 21 | + end |
| 22 | + |
| 23 | + after do |
| 24 | + repository.update([], [{name: 'foo', trafficTypeName: 'tt_name_1', conditions: []}, |
| 25 | + {name: 'bar', trafficTypeName: 'tt_name_2', conditions: []}, |
| 26 | + {name: 'bar', trafficTypeName: 'tt_name_2', conditions: []}, |
| 27 | + {name: 'qux', trafficTypeName: 'tt_name_3', conditions: []}, |
| 28 | + {name: 'quux', trafficTypeName: 'tt_name_4', conditions: []}, |
| 29 | + {name: 'corge', trafficTypeName: 'tt_name_5', conditions: []}, |
| 30 | + {name: 'corge', trafficTypeName: 'tt_name_6', conditions: []}], -1) |
| 31 | + end |
| 32 | + |
| 33 | + it 'returns rule_based_segments names' do |
| 34 | + expect(Set.new(repository.rule_based_segment_names)).to eq(Set.new(%w[foo bar baz])) |
| 35 | + end |
| 36 | + |
| 37 | + it 'returns rule_based_segment data' do |
| 38 | + expect(repository.get_rule_based_segment('foo')).to eq( |
| 39 | + { conditions: [] , name: 'foo', trafficTypeName: 'tt_name_1' }, |
| 40 | + ) |
| 41 | + end |
| 42 | + |
| 43 | + it 'remove undefined matcher with template condition' do |
| 44 | + rule_based_segment = { name: 'corge', trafficTypeName: 'tt_name_5', conditions: [ |
| 45 | + { |
| 46 | + contitionType: 'WHITELIST', |
| 47 | + label: 'some_label', |
| 48 | + matcherGroup: { |
| 49 | + matchers: [ |
| 50 | + { |
| 51 | + matcherType: 'UNDEFINED', |
| 52 | + whitelistMatcherData: { |
| 53 | + whitelist: ['k1', 'k2', 'k3'] |
| 54 | + }, |
| 55 | + negate: false, |
| 56 | + } |
| 57 | + ], |
| 58 | + combiner: 'AND' |
| 59 | + } |
| 60 | + }] |
| 61 | + } |
| 62 | + |
| 63 | + repository.update([rule_based_segment], [], -1) |
| 64 | + expect(repository.get_rule_based_segment('corge')[:conditions]).to eq SplitIoClient::Cache::Repositories::RuleBasedSegmentsRepository::DEFAULT_CONDITIONS_TEMPLATE |
| 65 | + |
| 66 | + # test with multiple conditions |
| 67 | + rule_based_segment2 = { |
| 68 | + name: 'corge2', |
| 69 | + trafficTypeName: 'tt_name_5', |
| 70 | + conditions: [ |
| 71 | + { |
| 72 | + contitionType: 'WHITELIST', |
| 73 | + label: 'some_label', |
| 74 | + matcherGroup: { |
| 75 | + matchers: [ |
| 76 | + { |
| 77 | + matcherType: 'UNDEFINED', |
| 78 | + whitelistMatcherData: { |
| 79 | + whitelist: ['k1', 'k2', 'k3'] |
| 80 | + }, |
| 81 | + negate: false, |
| 82 | + } |
| 83 | + ], |
| 84 | + combiner: 'AND' |
| 85 | + } |
| 86 | + }, |
| 87 | + { |
| 88 | + contitionType: 'WHITELIST', |
| 89 | + label: 'some_other_label', |
| 90 | + matcherGroup: { |
| 91 | + matchers: [{matcherType: 'ALL_KEYS', negate: false}], |
| 92 | + combiner: 'AND' |
| 93 | + } |
| 94 | + }] |
| 95 | + } |
| 96 | + |
| 97 | + repository.update([rule_based_segment2], [], -1) |
| 98 | + expect(repository.get_rule_based_segment('corge2')[:conditions]).to eq SplitIoClient::Cache::Repositories::RuleBasedSegmentsRepository::DEFAULT_CONDITIONS_TEMPLATE |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + describe 'with Memory Adapter' do |
| 103 | + it_behaves_like 'RuleBasedSegments Repository', :memory |
| 104 | + end |
| 105 | + |
| 106 | + describe 'with Redis Adapter' do |
| 107 | + it_behaves_like 'RuleBasedSegments Repository', :redis |
| 108 | + end |
| 109 | +end |
0 commit comments