rollout 2.1.0 → 2.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/spec/rollout_spec.rb CHANGED
@@ -1,6 +1,6 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require "spec_helper"
2
2
 
3
- describe "Rollout" do
3
+ RSpec.describe "Rollout" do
4
4
  before do
5
5
  @redis = Redis.new
6
6
  @rollout = Rollout.new(@redis)
@@ -13,16 +13,16 @@ describe "Rollout" do
13
13
  end
14
14
 
15
15
  it "the feature is active for users for which the block evaluates to true" do
16
- @rollout.should be_active(:chat, stub(:id => 5))
16
+ expect(@rollout).to be_active(:chat, double(id: 5))
17
17
  end
18
18
 
19
19
  it "is not active for users for which the block evaluates to false" do
20
- @rollout.should_not be_active(:chat, stub(:id => 1))
20
+ expect(@rollout).not_to be_active(:chat, double(id: 1))
21
21
  end
22
22
 
23
23
  it "is not active if a group is found in Redis but not defined in Rollout" do
24
24
  @rollout.activate_group(:chat, :fake)
25
- @rollout.should_not be_active(:chat, stub(:id => 1))
25
+ expect(@rollout).not_to be_active(:chat, double(id: 1))
26
26
  end
27
27
  end
28
28
 
@@ -32,7 +32,7 @@ describe "Rollout" do
32
32
  end
33
33
 
34
34
  it "evaluates to true no matter what" do
35
- @rollout.should be_active(:chat, stub(:id => 0))
35
+ expect(@rollout).to be_active(:chat, double(id: 0))
36
36
  end
37
37
  end
38
38
 
@@ -47,11 +47,17 @@ describe "Rollout" do
47
47
  end
48
48
 
49
49
  it "deactivates the rules for that group" do
50
- @rollout.should_not be_active(:chat, stub(:id => 10))
50
+ expect(@rollout).not_to be_active(:chat, double(id: 10))
51
51
  end
52
52
 
53
53
  it "leaves the other groups active" do
54
- @rollout.get(:chat).groups.should == [:fivesonly]
54
+ expect(@rollout.get(:chat).groups).to eq [:fivesonly]
55
+ end
56
+
57
+ it "leaves the other groups active using sets" do
58
+ @options = @rollout.instance_variable_get("@options")
59
+ @options[:use_sets] = true
60
+ expect(@rollout.get(:chat).groups).to eq [:fivesonly].to_set
55
61
  end
56
62
  end
57
63
 
@@ -60,40 +66,40 @@ describe "Rollout" do
60
66
  @rollout.define_group(:fivesonly) { |user| user.id == 5 }
61
67
  @rollout.activate_group(:chat, :all)
62
68
  @rollout.activate_group(:chat, :fivesonly)
63
- @rollout.activate_user(:chat, stub(:id => 51))
69
+ @rollout.activate_user(:chat, double(id: 51))
64
70
  @rollout.activate_percentage(:chat, 100)
65
71
  @rollout.activate(:chat)
66
72
  @rollout.deactivate(:chat)
67
73
  end
68
74
 
69
75
  it "removes all of the groups" do
70
- @rollout.should_not be_active(:chat, stub(:id => 0))
76
+ expect(@rollout).not_to be_active(:chat, double(id: 0))
71
77
  end
72
78
 
73
79
  it "removes all of the users" do
74
- @rollout.should_not be_active(:chat, stub(:id => 51))
80
+ expect(@rollout).not_to be_active(:chat, double(id: 51))
75
81
  end
76
82
 
77
83
  it "removes the percentage" do
78
- @rollout.should_not be_active(:chat, stub(:id => 24))
84
+ expect(@rollout).not_to be_active(:chat, double(id: 24))
79
85
  end
80
86
 
81
87
  it "removes globally" do
82
- @rollout.should_not be_active(:chat)
88
+ expect(@rollout).not_to be_active(:chat)
83
89
  end
84
90
  end
85
91
 
86
92
  describe "activating a specific user" do
87
93
  before do
88
- @rollout.activate_user(:chat, stub(:id => 42))
94
+ @rollout.activate_user(:chat, double(id: 42))
89
95
  end
90
96
 
91
97
  it "is active for that user" do
92
- @rollout.should be_active(:chat, stub(:id => 42))
98
+ expect(@rollout).to be_active(:chat, double(id: 42))
93
99
  end
94
100
 
95
101
  it "remains inactive for other users" do
96
- @rollout.should_not be_active(:chat, stub(:id => 24))
102
+ expect(@rollout).not_to be_active(:chat, double(id: 24))
97
103
  end
98
104
  end
99
105
 
@@ -103,43 +109,130 @@ describe "Rollout" do
103
109
  end
104
110
 
105
111
  it "is active for that user" do
106
- @rollout.should be_active(:chat, stub(:id => 42))
112
+ expect(@rollout).to be_active(:chat, double(id: 42))
107
113
  end
108
114
 
109
115
  it "remains inactive for other users" do
110
- @rollout.should_not be_active(:chat, stub(:id => 24))
116
+ expect(@rollout).not_to be_active(:chat, double(id: 24))
111
117
  end
112
118
  end
113
119
 
114
120
  describe "activating a specific user with a string id" do
115
121
  before do
116
- @rollout.activate_user(:chat, stub(:id => 'user-72'))
122
+ @rollout.activate_user(:chat, double(id: "user-72"))
117
123
  end
118
124
 
119
125
  it "is active for that user" do
120
- @rollout.should be_active(:chat, stub(:id => 'user-72'))
126
+ expect(@rollout).to be_active(:chat, double(id: "user-72"))
121
127
  end
122
128
 
123
129
  it "remains inactive for other users" do
124
- @rollout.should_not be_active(:chat, stub(:id => 'user-12'))
130
+ expect(@rollout).not_to be_active(:chat, double(id: "user-12"))
131
+ end
132
+ end
133
+
134
+ describe "activating a group of users" do
135
+ context "specified by user objects" do
136
+ let(:users) { [double(id: 1), double(id: 2), double(id: 3)] }
137
+
138
+ before { @rollout.activate_users(:chat, users) }
139
+
140
+ it "is active for the given users" do
141
+ users.each { |user| expect(@rollout).to be_active(:chat, user) }
142
+ end
143
+
144
+ it "remains inactive for other users" do
145
+ expect(@rollout).not_to be_active(:chat, double(id: 4))
146
+ end
147
+ end
148
+
149
+ context "specified by user ids" do
150
+ let(:users) { [1, 2, 3] }
151
+
152
+ before { @rollout.activate_users(:chat, users) }
153
+
154
+ it "is active for the given users" do
155
+ users.each { |user| expect(@rollout).to be_active(:chat, user) }
156
+ end
157
+
158
+ it "remains inactive for other users" do
159
+ expect(@rollout).not_to be_active(:chat, 4)
160
+ end
125
161
  end
126
162
  end
127
163
 
128
164
  describe "deactivating a specific user" do
129
165
  before do
130
- @rollout.activate_user(:chat, stub(:id => 42))
131
- @rollout.activate_user(:chat, stub(:id => 4242))
132
- @rollout.activate_user(:chat, stub(:id => 24))
133
- @rollout.deactivate_user(:chat, stub(:id => 42))
134
- @rollout.deactivate_user(:chat, stub(:id => "4242"))
166
+ @rollout.activate_user(:chat, double(id: 42))
167
+ @rollout.activate_user(:chat, double(id: 4242))
168
+ @rollout.activate_user(:chat, double(id: 24))
169
+ @rollout.deactivate_user(:chat, double(id: 42))
170
+ @rollout.deactivate_user(:chat, double(id: "4242"))
135
171
  end
136
172
 
137
173
  it "that user should no longer be active" do
138
- @rollout.should_not be_active(:chat, stub(:id => 42))
174
+ expect(@rollout).not_to be_active(:chat, double(id: 42))
139
175
  end
140
176
 
141
177
  it "remains active for other active users" do
142
- @rollout.get(:chat).users.should == %w(24)
178
+ @options = @rollout.instance_variable_get("@options")
179
+ @options[:use_sets] = false
180
+ expect(@rollout.get(:chat).users).to eq %w(24)
181
+ end
182
+
183
+ it "remains active for other active users using sets" do
184
+ @options = @rollout.instance_variable_get("@options")
185
+ @options[:use_sets] = true
186
+
187
+ expect(@rollout.get(:chat).users).to eq %w(24).to_set
188
+ end
189
+ end
190
+
191
+ describe "deactivating a group of users" do
192
+ context "specified by user objects" do
193
+ let(:active_users) { [double(id: 1), double(id: 2)] }
194
+ let(:inactive_users) { [double(id: 3), double(id: 4)] }
195
+
196
+ before do
197
+ @rollout.activate_users(:chat, active_users + inactive_users)
198
+ @rollout.deactivate_users(:chat, inactive_users)
199
+ end
200
+
201
+ it "is active for the active users" do
202
+ active_users.each { |user| expect(@rollout).to be_active(:chat, user) }
203
+ end
204
+
205
+ it "is not active for inactive users" do
206
+ inactive_users.each { |user| expect(@rollout).not_to be_active(:chat, user) }
207
+ end
208
+ end
209
+
210
+ context "specified by user ids" do
211
+ let(:active_users) { [1, 2] }
212
+ let(:inactive_users) { [3, 4] }
213
+
214
+ before do
215
+ @rollout.activate_users(:chat, active_users + inactive_users)
216
+ @rollout.deactivate_users(:chat, inactive_users)
217
+ end
218
+
219
+ it "is active for the active users" do
220
+ active_users.each { |user| expect(@rollout).to be_active(:chat, user) }
221
+ end
222
+
223
+ it "is not active for inactive users" do
224
+ inactive_users.each { |user| expect(@rollout).not_to be_active(:chat, user) }
225
+ end
226
+ end
227
+ end
228
+
229
+
230
+ describe 'set a group of users' do
231
+ it 'should replace the users with the given array' do
232
+ users = %w(1 2 3 4)
233
+ @rollout.activate_users(:chat, %w(10 20 30))
234
+ @rollout.set_users(:chat, users)
235
+ expect(@rollout.get(:chat).users).to eq(users)
143
236
  end
144
237
  end
145
238
 
@@ -149,7 +242,11 @@ describe "Rollout" do
149
242
  end
150
243
 
151
244
  it "activates the feature" do
152
- @rollout.should be_active(:chat)
245
+ expect(@rollout).to be_active(:chat)
246
+ end
247
+
248
+ it "sets @data to empty hash" do
249
+ expect(@rollout.get(:chat).data).to eq({})
153
250
  end
154
251
  end
155
252
 
@@ -159,7 +256,7 @@ describe "Rollout" do
159
256
  end
160
257
 
161
258
  it "activates the feature for that percentage of the users" do
162
- (1..120).select { |id| @rollout.active?(:chat, stub(:id => id)) }.length.should be_within(1).of(20)
259
+ expect((1..100).select { |id| @rollout.active?(:chat, double(id: id)) }.length).to be_within(2).of(20)
163
260
  end
164
261
  end
165
262
 
@@ -169,7 +266,7 @@ describe "Rollout" do
169
266
  end
170
267
 
171
268
  it "activates the feature for that percentage of the users" do
172
- (1..200).select { |id| @rollout.active?(:chat, stub(:id => id)) }.length.should be_within(5).of(40)
269
+ expect((1..200).select { |id| @rollout.active?(:chat, double(id: id)) }.length).to be_within(4).of(40)
173
270
  end
174
271
  end
175
272
 
@@ -179,22 +276,53 @@ describe "Rollout" do
179
276
  end
180
277
 
181
278
  it "activates the feature for that percentage of the users" do
182
- (1..100).select { |id| @rollout.active?(:chat, stub(:id => id)) }.length.should be_within(2).of(5)
279
+ expect((1..100).select { |id| @rollout.active?(:chat, double(id: id)) }.length).to be_within(2).of(5)
280
+ end
281
+ end
282
+
283
+ describe "activating a feature for a percentage of users" do
284
+ before do
285
+ @rollout.activate_percentage(:chat, 0.1)
286
+ end
287
+
288
+ it "activates the feature for that percentage of the users" do
289
+ expect((1..10_000).to_set.select { |id| @rollout.active?(:chat, double(id: id)) }.length).to be_within(2).of(10)
290
+ end
291
+ end
292
+
293
+ describe "activating a feature for a percentage of users" do
294
+ before do
295
+ @rollout.activate_percentage(:chat, 20)
296
+ @rollout.activate_percentage(:beta, 20)
297
+ @options = @rollout.instance_variable_get("@options")
298
+ end
299
+
300
+ it "activates the feature for a random set of users when opt is set" do
301
+ @options[:randomize_percentage] = true
302
+ chat_users = (1..100).select { |id| @rollout.active?(:chat, double(id: id)) }
303
+ beta_users = (1..100).select { |id| @rollout.active?(:beta, double(id: id)) }
304
+ expect(chat_users).not_to eq beta_users
305
+ end
306
+ it "activates the feature for the same set of users when opt is not set" do
307
+ @options[:randomize_percentage] = false
308
+ chat_users = (1..100).select { |id| @rollout.active?(:chat, double(id: id)) }
309
+ beta_users = (1..100).select { |id| @rollout.active?(:beta, double(id: id)) }
310
+ expect(chat_users).to eq beta_users
183
311
  end
184
312
  end
185
313
 
186
314
  describe "activating a feature for a group as a string" do
187
315
  before do
188
316
  @rollout.define_group(:admins) { |user| user.id == 5 }
189
- @rollout.activate_group(:chat, 'admins')
317
+ @rollout.activate_group(:chat, "admins")
190
318
  end
191
319
 
192
320
  it "the feature is active for users for which the block evaluates to true" do
193
- @rollout.should be_active(:chat, stub(:id => 5))
321
+ expect(@rollout).to be_active(:chat, double(id: 5))
194
322
  end
195
323
 
196
324
  it "is not active for users for which the block evaluates to false" do
197
- @rollout.should_not be_active(:chat, stub(:id => 1))
325
+ expect(@rollout).not_to be_active(:chat, double(id: 1))
198
326
  end
199
327
  end
200
328
 
@@ -205,7 +333,7 @@ describe "Rollout" do
205
333
  end
206
334
 
207
335
  it "becomes inactivate for all users" do
208
- @rollout.should_not be_active(:chat, stub(:id => 24))
336
+ expect(@rollout).not_to be_active(:chat, double(id: 24))
209
337
  end
210
338
  end
211
339
 
@@ -216,7 +344,7 @@ describe "Rollout" do
216
344
  end
217
345
 
218
346
  it "becomes inactivate" do
219
- @rollout.should_not be_active(:chat)
347
+ expect(@rollout).not_to be_active(:chat)
220
348
  end
221
349
  end
222
350
 
@@ -226,7 +354,7 @@ describe "Rollout" do
226
354
  end
227
355
 
228
356
  it "becomes activated" do
229
- @rollout.should be_active(:chat)
357
+ expect(@rollout).to be_active(:chat)
230
358
  end
231
359
  end
232
360
 
@@ -235,27 +363,53 @@ describe "Rollout" do
235
363
  @rollout.set(:chat, false)
236
364
  end
237
365
 
238
- it "becomes activated" do
239
- @rollout.should_not be_active(:chat)
366
+ it "becomes inactivated" do
367
+ expect(@rollout).not_to be_active(:chat)
368
+ end
369
+ end
370
+
371
+ describe "deleting a feature" do
372
+ before do
373
+ @rollout.set(:chat, true)
374
+ end
375
+
376
+ context "when feature was passed as string" do
377
+ it "should be removed from features list" do
378
+ expect(@rollout.features.size).to eq 1
379
+ @rollout.delete('chat')
380
+ expect(@rollout.features.size).to eq 0
381
+ end
382
+ end
383
+
384
+ it "should be removed from features list" do
385
+ expect(@rollout.features.size).to eq 1
386
+ @rollout.delete(:chat)
387
+ expect(@rollout.features.size).to eq 0
388
+ end
389
+
390
+ it "should have metadata cleared" do
391
+ expect(@rollout.get(:chat).percentage).to eq 100
392
+ @rollout.delete(:chat)
393
+ expect(@rollout.get(:chat).percentage).to eq 0
240
394
  end
241
395
  end
242
396
 
243
397
  describe "keeps a list of features" do
244
398
  it "saves the feature" do
245
399
  @rollout.activate(:chat)
246
- @rollout.features.should be_include(:chat)
400
+ expect(@rollout.features).to be_include(:chat)
247
401
  end
248
402
 
249
403
  it "does not contain doubles" do
250
404
  @rollout.activate(:chat)
251
405
  @rollout.activate(:chat)
252
- @rollout.features.size.should == 1
406
+ expect(@rollout.features.size).to eq(1)
253
407
  end
254
408
 
255
409
  it "does not contain doubles when using string" do
256
410
  @rollout.activate(:chat)
257
411
  @rollout.activate("chat")
258
- @rollout.features.size.should == 1
412
+ expect(@rollout.features.size).to eq(1)
259
413
  end
260
414
  end
261
415
 
@@ -265,24 +419,44 @@ describe "Rollout" do
265
419
  @rollout.activate_group(:chat, :caretakers)
266
420
  @rollout.activate_group(:chat, :greeters)
267
421
  @rollout.activate(:signup)
268
- @rollout.activate_user(:chat, stub(:id => 42))
422
+ @rollout.activate_user(:chat, double(id: 42))
269
423
  end
270
424
 
271
425
  it "returns the feature object" do
272
426
  feature = @rollout.get(:chat)
273
- feature.groups.should == [:caretakers, :greeters]
274
- feature.percentage.should == 10
275
- feature.users.should == %w(42)
276
- feature.to_hash.should == {
277
- :groups => [:caretakers, :greeters],
278
- :percentage => 10,
279
- :users => %w(42)
280
- }
427
+ expect(feature.groups).to eq [:caretakers, :greeters]
428
+ expect(feature.percentage).to eq 10
429
+ expect(feature.users).to eq %w(42)
430
+ expect(feature.to_hash).to eq(
431
+ groups: [:caretakers, :greeters],
432
+ percentage: 10,
433
+ users: %w(42)
434
+ )
281
435
 
282
436
  feature = @rollout.get(:signup)
283
- feature.groups.should be_empty
284
- feature.users.should be_empty
285
- feature.percentage.should == 100
437
+ expect(feature.groups).to be_empty
438
+ expect(feature.users).to be_empty
439
+ expect(feature.percentage).to eq(100)
440
+ end
441
+
442
+ it "returns the feature objects using sets" do
443
+ @options = @rollout.instance_variable_get("@options")
444
+ @options[:use_sets] = true
445
+
446
+ feature = @rollout.get(:chat)
447
+ expect(feature.groups).to eq [:caretakers, :greeters].to_set
448
+ expect(feature.percentage).to eq 10
449
+ expect(feature.users).to eq %w(42).to_set
450
+ expect(feature.to_hash).to eq(
451
+ groups: [:caretakers, :greeters].to_set,
452
+ percentage: 10,
453
+ users: %w(42).to_set
454
+ )
455
+
456
+ feature = @rollout.get(:signup)
457
+ expect(feature.groups).to be_empty
458
+ expect(feature.users).to be_empty
459
+ expect(feature.percentage).to eq(100)
286
460
  end
287
461
  end
288
462
 
@@ -297,61 +471,257 @@ describe "Rollout" do
297
471
 
298
472
  it "each feature is cleared" do
299
473
  features.each do |feature|
300
- @rollout.get(feature).to_hash.should == {
301
- :percentage => 0,
302
- :users => [],
303
- :groups => []
304
- }
474
+ expect(@rollout.get(feature).to_hash).to eq(
475
+ percentage: 0,
476
+ users: [],
477
+ groups: []
478
+ )
479
+ end
480
+ end
481
+
482
+ it "each feature is cleared with sets" do
483
+ @options = @rollout.instance_variable_get("@options")
484
+ @options[:use_sets] = true
485
+ features.each do |feature|
486
+ expect(@rollout.get(feature).to_hash).to eq(
487
+ percentage: 0,
488
+ users: Set.new,
489
+ groups: Set.new
490
+ )
305
491
  end
306
492
  end
307
493
 
308
494
  it "removes all features" do
309
- @rollout.features.should be_empty
495
+ expect(@rollout.features).to be_empty
310
496
  end
311
497
  end
312
498
 
313
- describe "migration mode" do
499
+ describe "#feature_states" do
500
+ let(:user_double) { double(id: 7) }
501
+
314
502
  before do
315
- @legacy = Rollout::Legacy.new(@redis)
316
- @legacy.activate_percentage(:chat, 12)
317
- @legacy.activate_user(:chat, stub(:id => 42))
318
- @legacy.activate_user(:chat, stub(:id => 24))
319
- @legacy.activate_group(:chat, :dope_people)
320
- @rollout = Rollout.new(@redis, :migrate => true)
321
- end
322
-
323
- it "imports the settings from the legacy rollout once" do
324
- @rollout.get(:chat).to_hash.should == {
325
- :percentage => 12,
326
- :users => %w(24 42),
327
- :groups => [:dope_people]
328
- }
329
- @legacy.deactivate_all(:chat)
330
- @rollout.get(:chat).to_hash.should == {
331
- :percentage => 12,
332
- :users => %w(24 42),
333
- :groups => [:dope_people]
334
- }
335
- @redis.get("feature:chat").should_not be_nil
336
- end
337
-
338
- it "imports settings that were globally activated" do
339
- @legacy.activate_globally(:video_chat)
340
- @rollout.get(:video_chat).to_hash[:percentage].should == 100
503
+ @rollout.activate(:chat)
504
+ @rollout.activate_user(:video, user_double)
505
+ @rollout.deactivate(:vr)
506
+ end
507
+
508
+ it "returns a hash" do
509
+ expect(@rollout.feature_states).to be_a(Hash)
510
+ end
511
+
512
+ context "with user argument" do
513
+ it "maps active feature as true" do
514
+ state = @rollout.feature_states(user_double)[:video]
515
+ expect(state).to eq(true)
516
+ end
517
+
518
+ it "maps inactive feature as false" do
519
+ state = @rollout.feature_states[:vr]
520
+ expect(state).to eq(false)
521
+ end
522
+ end
523
+
524
+ context "with no argument" do
525
+ it "maps active feature as true" do
526
+ state = @rollout.feature_states[:chat]
527
+ expect(state).to eq(true)
528
+ end
529
+
530
+ it "maps inactive feature as false" do
531
+ state = @rollout.feature_states[:video]
532
+ expect(state).to eq(false)
533
+ end
341
534
  end
342
535
  end
343
- end
344
536
 
345
- describe "Rollout::Feature" do
346
- before do
347
- @user = stub("User", :email => "test@test.com")
348
- @feature = Rollout::Feature.new(:chat, nil, :id_user_by => :email)
537
+ describe "#active_features" do
538
+ let(:user_double) { double(id: 19) }
539
+
540
+ before do
541
+ @rollout.activate(:chat)
542
+ @rollout.activate_user(:video, user_double)
543
+ @rollout.deactivate(:vr)
544
+ end
545
+
546
+ it "returns an array" do
547
+ expect(@rollout.active_features).to be_a(Array)
548
+ end
549
+
550
+ context "with user argument" do
551
+ it "includes active feature" do
552
+ features = @rollout.active_features(user_double)
553
+ expect(features).to include(:video)
554
+ expect(features).to include(:chat)
555
+ end
556
+
557
+ it "excludes inactive feature" do
558
+ features = @rollout.active_features(user_double)
559
+ expect(features).to_not include(:vr)
560
+ end
561
+ end
562
+
563
+ context "with no argument" do
564
+ it "includes active feature" do
565
+ features = @rollout.active_features
566
+ expect(features).to include(:chat)
567
+ end
568
+
569
+ it "excludes inactive feature" do
570
+ features = @rollout.active_features
571
+ expect(features).to_not include(:video)
572
+ end
573
+ end
349
574
  end
350
575
 
576
+ describe "#user_in_active_users?" do
577
+ it "returns true if activated for user" do
578
+ @rollout.activate_user(:chat, double(id: 5))
579
+ expect(@rollout.user_in_active_users?(:chat, "5")).to eq(true)
580
+ end
581
+
582
+ it "returns false if activated for group" do
583
+ @rollout.activate_group(:chat, :all)
584
+ expect(@rollout.user_in_active_users?(:chat, "5")).to eq(false)
585
+ end
586
+ end
587
+
588
+ describe "#multi_get" do
589
+ before do
590
+ @rollout.activate_percentage(:chat, 10)
591
+ @rollout.activate_group(:chat, :caretakers)
592
+ @rollout.activate_group(:videos, :greeters)
593
+ @rollout.activate(:signup)
594
+ @rollout.activate_user(:photos, double(id: 42))
595
+ end
596
+
597
+ it "returns an array of features" do
598
+ features = @rollout.multi_get(:chat, :videos, :signup)
599
+ expect(features[0].name).to eq :chat
600
+ expect(features[0].groups).to eq [:caretakers]
601
+ expect(features[0].percentage).to eq 10
602
+ expect(features[1].name).to eq :videos
603
+ expect(features[1].groups).to eq [:greeters]
604
+ expect(features[2].name).to eq :signup
605
+ expect(features[2].percentage).to eq 100
606
+ expect(features.size).to eq 3
607
+ end
608
+
609
+ describe 'when given feature keys is empty' do
610
+ it 'returns empty array' do
611
+ expect(@rollout.multi_get(*[])).to match_array([])
612
+ end
613
+ end
614
+ end
615
+
616
+ describe "#set_feature_data" do
617
+ before do
618
+ @rollout.set_feature_data(:chat, description: 'foo', release_date: 'bar')
619
+ end
620
+
621
+ it 'sets the data attribute on feature' do
622
+ expect(@rollout.get(:chat).data).to include('description' => 'foo', 'release_date' => 'bar')
623
+ end
624
+
625
+ it 'updates a data attribute' do
626
+ @rollout.set_feature_data(:chat, description: 'baz')
627
+ expect(@rollout.get(:chat).data).to include('description' => 'baz', 'release_date' => 'bar')
628
+ end
629
+
630
+ it 'only sets data on specified feature' do
631
+ @rollout.set_feature_data(:talk, image_url: 'kittens.png')
632
+ expect(@rollout.get(:chat).data).not_to include('image_url' => 'kittens.png')
633
+ expect(@rollout.get(:chat).data).to include('description' => 'foo', 'release_date' => 'bar')
634
+ end
635
+
636
+ it 'does not modify @data if param is nil' do
637
+ expect(@rollout.get(:chat).data).to include('description' => 'foo', 'release_date' => 'bar')
638
+ @rollout.set_feature_data(:chat, nil)
639
+ expect(@rollout.get(:chat).data).to include('description' => 'foo', 'release_date' => 'bar')
640
+ end
641
+
642
+ it 'does not modify @data if param is empty string' do
643
+ expect(@rollout.get(:chat).data).to include('description' => 'foo', 'release_date' => 'bar')
644
+ @rollout.set_feature_data(:chat, " ")
645
+ expect(@rollout.get(:chat).data).to include('description' => 'foo', 'release_date' => 'bar')
646
+ end
647
+
648
+ it 'properly parses data when it contains a |' do
649
+ user = double("User", id: 8)
650
+ @rollout.activate_user(:chat, user)
651
+ @rollout.set_feature_data(:chat, "|call||text|" => "a|bunch|of|stuff")
652
+ expect(@rollout.get(:chat).data).to include("|call||text|" => "a|bunch|of|stuff")
653
+ expect(@rollout.active?(:chat, user)).to be true
654
+ end
655
+ end
656
+
657
+ describe "#clear_feature_data" do
658
+ it 'resets data to empty string' do
659
+ @rollout.set_feature_data(:chat, description: 'foo')
660
+ expect(@rollout.get(:chat).data).to include('description' => 'foo')
661
+ @rollout.clear_feature_data(:chat)
662
+ expect(@rollout.get(:chat).data).to eq({})
663
+ end
664
+ end
665
+
666
+ describe 'Check if feature exists' do
667
+ it 'it should return true if the feature is exist' do
668
+ @rollout.activate_percentage(:chat, 1)
669
+ expect(@rollout.exists?(:chat)).to be true
670
+ end
671
+
672
+ it 'it should return false if the feature is not exist' do
673
+ expect(@rollout.exists?(:chat)).to be false
674
+ end
675
+ end
676
+ end
677
+
678
+ describe "Rollout::Feature" do
351
679
  describe "#add_user" do
352
680
  it "ids a user using id_user_by" do
353
- @feature.add_user(@user)
354
- @user.should have_received :email
681
+ user = double("User", email: "test@test.com")
682
+ feature = Rollout::Feature.new(:chat, nil, id_user_by: :email)
683
+ feature.add_user(user)
684
+ expect(user).to have_received :email
685
+ end
686
+ end
687
+
688
+ describe "#initialize" do
689
+ describe "when string does not exist" do
690
+ it 'clears feature attributes when string is not given' do
691
+ feature = Rollout::Feature.new(:chat)
692
+ expect(feature.groups).to be_empty
693
+ expect(feature.users).to be_empty
694
+ expect(feature.percentage).to eq 0
695
+ expect(feature.data).to eq({})
696
+ end
697
+
698
+ it 'clears feature attributes when string is nil' do
699
+ feature = Rollout::Feature.new(:chat, nil)
700
+ expect(feature.groups).to be_empty
701
+ expect(feature.users).to be_empty
702
+ expect(feature.percentage).to eq 0
703
+ expect(feature.data).to eq({})
704
+ end
705
+
706
+ it 'clears feature attributes when string is empty string' do
707
+ feature = Rollout::Feature.new(:chat, "")
708
+ expect(feature.groups).to be_empty
709
+ expect(feature.users).to be_empty
710
+ expect(feature.percentage).to eq 0
711
+ expect(feature.data).to eq({})
712
+ end
713
+
714
+ describe "when there is no data" do
715
+ it 'sets @data to empty hash' do
716
+ feature = Rollout::Feature.new(:chat, "0||")
717
+ expect(feature.data).to eq({})
718
+ end
719
+
720
+ it 'sets @data to empty hash' do
721
+ feature = Rollout::Feature.new(:chat, "||| ")
722
+ expect(feature.data).to eq({})
723
+ end
724
+ end
355
725
  end
356
726
  end
357
727
  end