1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
require 'redis'
require 'rollout'
require 'flipper/adapters/rollout'
RSpec.describe Flipper::Adapters::Rollout do
let(:redis) { Redis.new }
let(:rollout) { Rollout.new(redis) }
let(:source_adapter) { described_class.new(rollout) }
let(:source_flipper) { Flipper.new(source_adapter) }
let(:destination_adapter) { Flipper::Adapters::Memory.new }
let(:destination_flipper) { Flipper.new(destination_adapter) }
before do
begin
redis.flushdb
rescue Redis::CannotConnectError
ENV['CI'] ? raise : skip('Redis not available')
end
end
describe '#name' do
it 'has name that is a symbol' do
expect(source_adapter.name).not_to be_nil
expect(source_adapter.name).to be_instance_of(Symbol)
end
end
describe '#get' do
it 'returns hash of gate data' do
rollout.activate_user(:chat, Struct.new(:id).new(1))
rollout.activate_percentage(:chat, 20)
rollout.activate_group(:chat, :admins)
feature = source_flipper[:chat]
expected = {
boolean: nil,
groups: Set.new([:admins]),
actors: Set.new(["1"]),
percentage_of_actors: 20.0,
percentage_of_time: nil,
}
expect(source_adapter.get(feature)).to eq(expected)
end
it 'returns fully flipper enabled for fully rollout activated' do
rollout.activate(:chat)
feature = source_flipper[:chat]
expected = {
boolean: true,
groups: Set.new,
actors: Set.new,
percentage_of_actors: nil,
percentage_of_time: nil,
}
expect(source_adapter.get(feature)).to eq(expected)
end
it 'returns fully flipper enabled for fully rollout activated with user/group' do
rollout.activate_user(:chat, Struct.new(:id).new(1))
rollout.activate_group(:chat, :admins)
rollout.activate(:chat)
feature = source_flipper[:chat]
expected = {
boolean: true,
groups: Set.new,
actors: Set.new,
percentage_of_actors: nil,
percentage_of_time: nil,
}
expect(source_adapter.get(feature)).to eq(expected)
end
it 'returns default hash of gate data for feature not existing in rollout' do
feature = source_flipper[:chat]
expect(source_adapter.get(feature)).to eq(source_adapter.default_config)
end
end
describe '#features' do
it 'returns all feature keys' do
rollout.activate(:chat)
rollout.activate(:messaging)
rollout.activate(:push_notifications)
expect(source_adapter.features).to match_array([:chat, :messaging, :push_notifications])
end
end
it 'can have one feature imported' do
rollout.activate(:search)
destination_flipper.import(source_flipper)
expect(destination_flipper.features.map(&:key)).to eq(["search"])
end
it 'can have multiple features imported' do
rollout.activate(:yep)
rollout.activate_group(:preview_features, :developers)
rollout.activate_group(:preview_features, :marketers)
rollout.activate_group(:preview_features, :company)
rollout.activate_group(:preview_features, :early_access)
rollout.activate_user(:preview_features, Struct.new(:id).new(1))
rollout.activate_user(:preview_features, Struct.new(:id).new(2))
rollout.activate_user(:preview_features, Struct.new(:id).new(3))
rollout.activate_percentage(:issues_next, 25)
destination_flipper.import(source_flipper)
feature = destination_flipper[:yep]
expect(feature.boolean_value).to eq(true)
feature = destination_flipper[:preview_features]
expect(feature.boolean_value).to be(false)
expect(feature.actors_value).to eq(Set['1', '2', '3'])
expected_groups = Set['developers', 'marketers', 'company', 'early_access']
expect(feature.groups_value).to eq(expected_groups)
expect(feature.percentage_of_actors_value).to be(0)
feature = destination_flipper[:issues_next]
expect(feature.boolean_value).to eq(false)
expect(feature.actors_value).to eq(Set.new)
expect(feature.groups_value).to eq(Set.new)
expect(feature.percentage_of_actors_value).to be(25)
feature = destination_flipper[:verbose_logging]
expect(feature.boolean_value).to eq(false)
expect(feature.actors_value).to eq(Set.new)
expect(feature.groups_value).to eq(Set.new)
expect(feature.percentage_of_actors_value).to be(0)
end
describe 'unsupported methods' do
it 'raises on add' do
expect { source_adapter.add(:feature) }
.to raise_error(Flipper::Adapters::Rollout::AdapterMethodNotSupportedError)
end
it 'raises on remove' do
expect { source_adapter.remove(:feature) }
.to raise_error(Flipper::Adapters::Rollout::AdapterMethodNotSupportedError)
end
it 'raises on clear' do
expect { source_adapter.clear(:feature) }
.to raise_error(Flipper::Adapters::Rollout::AdapterMethodNotSupportedError)
end
it 'raises on enable' do
expect { source_adapter.enable(:feature, :gate, :thing) }
.to raise_error(Flipper::Adapters::Rollout::AdapterMethodNotSupportedError)
end
it 'raises on disable' do
expect { source_adapter.disable(:feature, :gate, :thing) }
.to raise_error(Flipper::Adapters::Rollout::AdapterMethodNotSupportedError)
end
it 'raises on import' do
expect { source_adapter.import(:source_adapter) }
.to raise_error(Flipper::Adapters::Rollout::AdapterMethodNotSupportedError)
end
end
end
|