/
shared_adapter_specs.rb
169 lines (134 loc) · 5.72 KB
/
shared_adapter_specs.rb
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
161
162
163
164
165
166
167
168
169
# Requires the following methods:
# * subject - The instance of the adapter
shared_examples_for 'a flipper adapter' do
let(:actor_class) { Struct.new(:flipper_id) }
let(:flipper) { Flipper.new(subject) }
let(:feature) { flipper[:stats] }
let(:boolean_gate) { feature.gate(:boolean) }
let(:group_gate) { feature.gate(:group) }
let(:actor_gate) { feature.gate(:actor) }
let(:actors_gate) { feature.gate(:percentage_of_actors) }
let(:random_gate) { feature.gate(:percentage_of_random) }
before do
Flipper.register(:admins) { |actor|
actor.respond_to?(:admin?) && actor.admin?
}
Flipper.register(:early_access) { |actor|
actor.respond_to?(:early_access?) && actor.early_access?
}
end
after do
Flipper.unregister_groups
end
it "has name that is a symbol" do
subject.name.should_not be_nil
subject.name.should be_instance_of(Symbol)
end
it "returns correct default values for the gates if none are enabled" do
subject.get(feature).should eq({
:boolean => nil,
:groups => Set.new,
:actors => Set.new,
:percentage_of_actors => nil,
:percentage_of_random => nil,
})
end
it "can enable, disable and get value for boolean gate" do
subject.enable(feature, boolean_gate, flipper.boolean).should be_true
result = subject.get(feature)
result[:boolean].should eq('true')
subject.disable(feature, boolean_gate, flipper.boolean(false)).should be_true
result = subject.get(feature)
result[:boolean].should be_nil
end
it "fully disables all enabled things when boolean gate disabled" do
actor_22 = actor_class.new('22')
subject.enable(feature, boolean_gate, flipper.boolean).should be_true
subject.enable(feature, group_gate, flipper.group(:admins)).should be_true
subject.enable(feature, actor_gate, flipper.actor(actor_22)).should be_true
subject.enable(feature, actors_gate, flipper.actors(25)).should be_true
subject.enable(feature, random_gate, flipper.random(45)).should be_true
subject.disable(feature, boolean_gate, flipper.boolean).should be_true
subject.get(feature).should eq({
:boolean => nil,
:groups => Set.new,
:actors => Set.new,
:percentage_of_actors => nil,
:percentage_of_random => nil,
})
end
it "can enable, disable and get value for group gate" do
subject.enable(feature, group_gate, flipper.group(:admins)).should be_true
subject.enable(feature, group_gate, flipper.group(:early_access)).should be_true
result = subject.get(feature)
result[:groups].should eq(Set['admins', 'early_access'])
subject.disable(feature, group_gate, flipper.group(:early_access)).should be_true
result = subject.get(feature)
result[:groups].should eq(Set['admins'])
subject.disable(feature, group_gate, flipper.group(:admins)).should be_true
result = subject.get(feature)
result[:groups].should eq(Set.new)
end
it "can enable, disable and get value for actor gate" do
actor_22 = actor_class.new('22')
actor_asdf = actor_class.new('asdf')
subject.enable(feature, actor_gate, flipper.actor(actor_22)).should be_true
subject.enable(feature, actor_gate, flipper.actor(actor_asdf)).should be_true
result = subject.get(feature)
result[:actors].should eq(Set['22', 'asdf'])
subject.disable(feature, actor_gate, flipper.actor(actor_22)).should be_true
result = subject.get(feature)
result[:actors].should eq(Set['asdf'])
subject.disable(feature, actor_gate, flipper.actor(actor_asdf)).should be_true
result = subject.get(feature)
result[:actors].should eq(Set.new)
end
it "can enable, disable and get value for percentage of actors gate" do
subject.enable(feature, actors_gate, flipper.actors(15)).should be_true
result = subject.get(feature)
result[:percentage_of_actors].should eq('15')
subject.disable(feature, actors_gate, flipper.actors(0)).should be_true
result = subject.get(feature)
result[:percentage_of_actors].should eq('0')
end
it "can enable, disable and get value for percentage of random gate" do
subject.enable(feature, random_gate, flipper.random(10)).should be_true
result = subject.get(feature)
result[:percentage_of_random].should eq('10')
subject.disable(feature, random_gate, flipper.random(0)).should be_true
result = subject.get(feature)
result[:percentage_of_random].should eq('0')
end
it "converts boolean value to a string" do
subject.enable(feature, boolean_gate, flipper.boolean).should be_true
result = subject.get(feature)
result[:boolean].should eq('true')
end
it "converts the actor value to a string" do
subject.enable(feature, actor_gate, flipper.actor(actor_class.new(22))).should be_true
result = subject.get(feature)
result[:actors].should eq(Set['22'])
end
it "converts group value to a string" do
subject.enable(feature, group_gate, flipper.group(:admins)).should be_true
result = subject.get(feature)
result[:groups].should eq(Set['admins'])
end
it "converts percentage of random integer value to a string" do
subject.enable(feature, random_gate, flipper.random(10)).should be_true
result = subject.get(feature)
result[:percentage_of_random].should eq('10')
end
it "converts percentage of actors integer value to a string" do
subject.enable(feature, actors_gate, flipper.actors(10)).should be_true
result = subject.get(feature)
result[:percentage_of_actors].should eq('10')
end
it "can add and list known features" do
subject.features.should eq(Set.new)
subject.add(flipper[:stats]).should be_true
subject.features.should eq(Set['stats'])
subject.add(flipper[:search]).should be_true
subject.features.should eq(Set['stats', 'search'])
end
end