abongo 0.0.1

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.
@@ -0,0 +1,472 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'mongo'
4
+
5
+ class TestAbongo < Test::Unit::TestCase
6
+
7
+ def setup
8
+ conn = Mongo::Connection.new
9
+ db = conn['abongo_test']
10
+ Abongo.db = db
11
+ Abongo.options = {}
12
+ Abongo.identity = nil
13
+ Abongo.salt = 'Not really necessary.'
14
+ Abongo.participants.drop
15
+ Abongo.alternatives.drop
16
+ Abongo.conversions.drop
17
+ Abongo.experiments.drop
18
+ end
19
+
20
+ def teardown
21
+ end
22
+
23
+ def test_experiment_creation
24
+ experiment = Abongo.start_experiment!('test_test', ['alt1', 'alt2'])
25
+ assert_equal('test_test', experiment['name'])
26
+ assert_equal(['alt1', 'alt2'], experiment['alternatives'])
27
+ assert(Abongo.tests_listening_to_conversion('test_test').include?(experiment['_id']))
28
+ end
29
+
30
+ def test_experiment_creation_occurs_once
31
+ experiment1 = Abongo.start_experiment!('test_test', ['alt1', 'alt2'])
32
+ experiment2 = Abongo.start_experiment!('test_test', ['alt1', 'alt2'])
33
+ assert_equal(experiment1, experiment2)
34
+ end
35
+
36
+ def test_experiment_creation_with_conversion
37
+ Abongo.start_experiment!('test_test', ['alt1', 'alt2'], 'convert')
38
+ experiment = Abongo.get_test('test_test')
39
+ assert_equal('test_test', experiment['name'])
40
+ assert_equal(['alt1', 'alt2'], experiment['alternatives'])
41
+ assert(Abongo.tests_listening_to_conversion('convert').include?(experiment['_id']))
42
+ end
43
+
44
+ def test_default_identity
45
+ assert 0 <= Abongo.identity.to_i
46
+ assert 10**10 >= Abongo.identity.to_i
47
+ end
48
+
49
+ def test_add_participation
50
+ Abongo.add_participation('ident', 'test1')
51
+ assert_equal(['test1'], Abongo.find_participant('ident')['tests'])
52
+ Abongo.add_participation('ident', 'test2')
53
+ assert_equal(['test1', 'test2'], Abongo.find_participant('ident')['tests'])
54
+ end
55
+
56
+ def test_add_conversions
57
+ Abongo.add_conversion('ident', 'test1')
58
+ assert_equal(['test1'], Abongo.find_participant('ident')['conversions'])
59
+ Abongo.add_conversion('ident', 'test2')
60
+ assert_equal(['test1', 'test2'], Abongo.find_participant('ident')['conversions'])
61
+ end
62
+
63
+ def test_find_alternative_for_user
64
+ Abongo.identity = 'ident'
65
+ experiment = Abongo.start_experiment!('test_test', ['alt1', 'alt2'])
66
+ assert_equal('alt1', Abongo.find_alternative_for_user('ident', experiment))
67
+ end
68
+
69
+ def test_test
70
+ Abongo.identity = 'ident'
71
+ assert_equal('alt1', Abongo.test('test_test', ['alt1', 'alt2']))
72
+ experiment = Abongo.get_test('test_test')
73
+ assert_equal('test_test', experiment['name'])
74
+ assert_equal(['alt1', 'alt2'], experiment['alternatives'])
75
+ assert_equal([experiment['_id']], Abongo.find_participant('ident')['tests'])
76
+ end
77
+
78
+ def test_test_with_block
79
+ Abongo.identity = 'ident'
80
+ Abongo.test('test_test', ['alt1', 'alt2']){|alt|
81
+ assert_equal('alt1', alt)
82
+ }
83
+ end
84
+
85
+ def test_flip
86
+ Abongo.identity = 'ident'
87
+ assert_equal(true, Abongo.flip('test_test'))
88
+ experiment = Abongo.get_test('test_test')
89
+ assert_equal('test_test', experiment['name'])
90
+ assert_equal([true, false], experiment['alternatives'])
91
+ assert_equal([experiment['_id']], Abongo.find_participant('ident')['tests'])
92
+ end
93
+
94
+ def test_flip_with_block
95
+ Abongo.identity = 'ident'
96
+ Abongo.flip('test_test'){|alt|
97
+ assert_equal(true, alt)
98
+ }
99
+ experiment = Abongo.get_test('test_test')
100
+ assert_equal([true, false], experiment['alternatives'])
101
+ end
102
+
103
+ def test_flip_with_options
104
+ Abongo.identity = 'ident'
105
+ Abongo.flip('test_test', :conversion => 'test_conversions')
106
+ experiment = Abongo.get_test('test_test')
107
+ assert_equal([experiment['_id']], Abongo.tests_listening_to_conversion('test_conversions'))
108
+ end
109
+
110
+ def test_flip_with_options
111
+ Abongo.identity = 'ident'
112
+ Abongo.flip('test_test', :conversion => 'test_conversions') do |alt|
113
+ # do nothing
114
+ end
115
+ experiment = Abongo.get_test('test_test')
116
+ assert_equal([experiment['_id']], Abongo.tests_listening_to_conversion('test_conversions'))
117
+ end
118
+
119
+ def test_test_short_circuit
120
+ Abongo.identity = 'ident'
121
+ assert_equal('alt1', Abongo.test('test_test', ['alt1', 'alt2']))
122
+ Abongo.end_experiment!('test_test', 'alt2')
123
+ assert_equal('alt2', Abongo.test('test_test', ['alt1', 'alt2']))
124
+ Abongo.end_experiment!('test_test', 'alt3')
125
+ assert_equal('alt3', Abongo.test('test_test', ['alt1', 'alt2']))
126
+ end
127
+
128
+ def test_ensure_one_participation_per_participant
129
+ Abongo.identity = 'ident'
130
+ 10.times do
131
+ Abongo.test('test_test', ['alt1', 'alt2'])
132
+ participant = Abongo.find_participant('ident')
133
+ assert_equal(1, participant['tests'].size)
134
+ alternative = Abongo.alternatives.find_one(:test => participant['tests'].first, :content => 'alt1')
135
+ assert_equal(1, alternative['participants'])
136
+ end
137
+ end
138
+
139
+ def test_ensure_multiple_participation
140
+ Abongo.identity = 'ident'
141
+ 10.times do |num|
142
+ Abongo.test('test_test', ['alt1', 'alt2'], {:multiple_participation => true})
143
+ participant = Abongo.find_participant('ident')
144
+ assert_equal(1, participant['tests'].size)
145
+ alternative = Abongo.alternatives.find_one(:test => participant['tests'].first, :content => 'alt1')
146
+ assert_equal(num+1, alternative['participants'])
147
+ end
148
+ end
149
+
150
+ def test_score_conversion
151
+ Abongo.identity = 'ident'
152
+ Abongo.test('test_test', ['alt1', 'alt2'])
153
+ participant = Abongo.find_participant('ident')
154
+ alternative = Abongo.alternatives.find_one(:test => participant['tests'].first, :content => 'alt1')
155
+ assert_equal(1, alternative['participants'])
156
+ assert_equal(0, alternative['conversions'])
157
+ experiment = Abongo.get_test('test_test')
158
+ Abongo.score_conversion!(experiment['_id'])
159
+ alternative = Abongo.alternatives.find_one(:test => participant['tests'].first, :content => 'alt1')
160
+ assert_equal(1, alternative['conversions'])
161
+ end
162
+
163
+ def test_score_conversion_with_name
164
+ Abongo.identity = 'ident'
165
+ Abongo.test('test_test', ['alt1', 'alt2'])
166
+ participant = Abongo.find_participant('ident')
167
+ alternative = Abongo.alternatives.find_one(:test => participant['tests'].first, :content => 'alt1')
168
+ assert_equal(1, alternative['participants'])
169
+ assert_equal(0, alternative['conversions'])
170
+ experiment = Abongo.get_test('test_test')
171
+ Abongo.score_conversion!('test_test')
172
+ alternative = Abongo.alternatives.find_one(:test => participant['tests'].first, :content => 'alt1')
173
+ assert_equal(1, alternative['conversions'])
174
+ end
175
+
176
+ def test_score_conversion_only_once_per_participant
177
+ Abongo.identity = 'ident'
178
+ Abongo.test('test_test', ['alt1', 'alt2'])
179
+ participant = Abongo.find_participant('ident')
180
+ alternative = Abongo.alternatives.find_one(:test => participant['tests'].first, :content => 'alt1')
181
+ assert_equal(1, alternative['participants'])
182
+ assert_equal(0, alternative['conversions'])
183
+ experiment = Abongo.get_test('test_test')
184
+ 10.times do
185
+ Abongo.score_conversion!(experiment['_id'])
186
+ alternative = Abongo.alternatives.find_one(:test => participant['tests'].first, :content => 'alt1')
187
+ assert_equal(1, alternative['conversions'])
188
+ end
189
+ end
190
+
191
+ def test_score_conversion_with_multiple_conversions
192
+ Abongo.identity = 'ident'
193
+ Abongo.options[:multiple_conversions] = true
194
+ Abongo.test('test_test', ['alt1', 'alt2'])
195
+ experiment = Abongo.get_test('test_test')
196
+ alternative = Abongo.alternatives.find_one(:test => experiment["_id"], :content => 'alt1')
197
+ assert_equal(1, alternative['participants'])
198
+ assert_equal(0, alternative['conversions'])
199
+ 10.times do |num|
200
+ Abongo.score_conversion!(experiment['_id'])
201
+ alternative = Abongo.alternatives.find_one(:test => experiment["_id"], :content => 'alt1')
202
+ assert_equal(num+1, alternative['conversions'])
203
+ end
204
+ end
205
+
206
+ def test_salt
207
+ Abongo.identity = 'ident'
208
+ assert_equal('alt1', Abongo.test('test_test', ['alt1', 'alt2']))
209
+ Abongo.salt = "This will change the result"
210
+ assert_equal('alt2', Abongo.test('test_test', ['alt1', 'alt2']))
211
+ end
212
+
213
+ def test_count_humans_only
214
+ Abongo.identity = 'ident'
215
+ Abongo.options[:count_humans_only] = true
216
+ assert_equal("alt1", Abongo.test('test_test', ['alt1', 'alt2']))
217
+
218
+ experiment = Abongo.get_test('test_test')
219
+ alternative = Abongo.alternatives.find_one(:test => experiment['_id'], :content => 'alt1')
220
+ assert_equal(0, alternative['participants'])
221
+ assert_equal(0, alternative['conversions'])
222
+
223
+ Abongo.score_conversion!('test_test')
224
+ alternative = Abongo.alternatives.find_one(:test => experiment['_id'], :content => 'alt1')
225
+ assert_equal(0, alternative['participants'])
226
+ assert_equal(0, alternative['conversions'])
227
+
228
+ Abongo.human!
229
+ alternative = Abongo.alternatives.find_one(:test => experiment['_id'], :content => 'alt1')
230
+ assert_equal(1, alternative['participants'])
231
+ assert_equal(1, alternative['conversions'])
232
+
233
+
234
+ assert_equal("alt1", Abongo.test('test2', ['alt1', 'alt2']))
235
+
236
+ experiment = Abongo.get_test('test2')
237
+ alternative = Abongo.alternatives.find_one(:test => experiment['_id'], :content => 'alt1')
238
+ assert_equal(1, alternative['participants'])
239
+ assert_equal(0, alternative['conversions'])
240
+
241
+ Abongo.score_conversion!('test2')
242
+ alternative = Abongo.alternatives.find_one(:test => experiment['_id'], :content => 'alt1')
243
+ assert_equal(1, alternative['participants'])
244
+ assert_equal(1, alternative['conversions'])
245
+
246
+ Abongo.human!
247
+ alternative = Abongo.alternatives.find_one(:test => experiment['_id'], :content => 'alt1')
248
+ assert_equal(1, alternative['participants'])
249
+ assert_equal(1, alternative['conversions'])
250
+ end
251
+
252
+ def test_count_humans_only_without_conversion_before_marked_human
253
+ Abongo.identity = 'ident'
254
+ Abongo.options[:count_humans_only] = true
255
+ assert_equal("alt1", Abongo.test('test_test', ['alt1', 'alt2']))
256
+
257
+ experiment = Abongo.get_test('test_test')
258
+ alternative = Abongo.alternatives.find_one(:test => experiment['_id'], :content => 'alt1')
259
+ assert_equal(0, alternative['participants'])
260
+ assert_equal(0, alternative['conversions'])
261
+
262
+ Abongo.human!
263
+ alternative = Abongo.alternatives.find_one(:test => experiment['_id'], :content => 'alt1')
264
+ assert_equal(1, alternative['participants'])
265
+ assert_equal(0, alternative['conversions'])
266
+
267
+ Abongo.score_conversion!('test_test')
268
+ alternative = Abongo.alternatives.find_one(:test => experiment['_id'], :content => 'alt1')
269
+ assert_equal(1, alternative['participants'])
270
+ assert_equal(1, alternative['conversions'])
271
+ end
272
+
273
+ def test_parse_alternatives_array
274
+ assert_equal([1, 5, 2, 4, true], Abongo.parse_alternatives([1, 5, 2, 4, true]))
275
+ end
276
+
277
+ def test_parse_alternatives_integer
278
+ assert_equal([1, 2, 3, 4, 5], Abongo.parse_alternatives(5))
279
+ end
280
+
281
+ def test_parse_alternatives_range
282
+ assert_equal([2, 3, 4, 5], Abongo.parse_alternatives(2..5))
283
+ end
284
+
285
+ def test_parse_alternatives_hash
286
+ assert_equal(["three", "three", "three", "one"], Abongo.parse_alternatives({"three" => 3, "one" => 1}))
287
+ end
288
+
289
+ def test_parse_alternatives_hash_invalid_value
290
+ assert_raise RuntimeError do
291
+ Abongo.parse_alternatives({"three" => "bob"})
292
+ end
293
+ end
294
+
295
+ def test_parse_alternatives_invalid_type
296
+ assert_raise RuntimeError do
297
+ Abongo.parse_alternatives(Abongo.new)
298
+ end
299
+ end
300
+
301
+ def test_bongo_array
302
+ Abongo.identity = 'ident'
303
+ test1 = Abongo.start_experiment!('test1', ['alt1', 'alt2'])
304
+ test2 = Abongo.start_experiment!('test2', ['alt1', 'alt2'])
305
+ test3 = Abongo.start_experiment!('test3', ['alt1', 'alt2'])
306
+ test4 = Abongo.start_experiment!('test3', ['alt1', 'alt2'])
307
+ Abongo.test('test1', ['alt1', 'alt2'])
308
+ Abongo.test('test2', ['alt1', 'alt2'])
309
+ Abongo.test('test3', ['alt1', 'alt2'])
310
+ assert_equal(0, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test1), :test => test1['_id']})['conversions'])
311
+ assert_equal(0, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test2), :test => test2['_id']})['conversions'])
312
+ assert_equal(0, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test3), :test => test3['_id']})['conversions'])
313
+ assert_equal(0, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test4), :test => test4['_id']})['conversions'])
314
+
315
+ Abongo.bongo!(['test1', 'test2'])
316
+ assert_equal(1, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test1), :test => test1['_id']})['conversions'])
317
+ assert_equal(1, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test2), :test => test2['_id']})['conversions'])
318
+ assert_equal(0, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test3), :test => test3['_id']})['conversions'])
319
+ assert_equal(0, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test4), :test => test4['_id']})['conversions'])
320
+ end
321
+
322
+ def test_bongo_nil
323
+ Abongo.identity = 'ident'
324
+ test1 = Abongo.start_experiment!('test1', ['alt1', 'alt2'])
325
+ test2 = Abongo.start_experiment!('test2', ['alt1', 'alt2'])
326
+ test3 = Abongo.start_experiment!('test3', ['alt1', 'alt2'])
327
+ Abongo.test('test1', ['alt1', 'alt2'])
328
+ Abongo.test('test2', ['alt1', 'alt2'])
329
+ assert_equal(0, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test1), :test => test1['_id']})['conversions'])
330
+ assert_equal(0, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test2), :test => test2['_id']})['conversions'])
331
+ assert_equal(0, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test3), :test => test3['_id']})['conversions'])
332
+
333
+ Abongo.bongo!
334
+ assert_equal(1, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test1), :test => test1['_id']})['conversions'])
335
+ assert_equal(1, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test2), :test => test2['_id']})['conversions'])
336
+ assert_equal(0, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test3), :test => test3['_id']})['conversions'])
337
+ end
338
+
339
+ def test_bongo_with_alternative_conversion
340
+ Abongo.identity = 'ident'
341
+ test1 = Abongo.start_experiment!('test1', ['alt1', 'alt2'], "convert!")
342
+ test2 = Abongo.start_experiment!('test2', ['alt1', 'alt2'], "convert!")
343
+ test3 = Abongo.start_experiment!('test3', ['alt1', 'alt2'], "dontconvert!")
344
+ Abongo.test('test1', ['alt1', 'alt2'])
345
+ Abongo.test('test2', ['alt1', 'alt2'])
346
+ assert_equal(0, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test1), :test => test1['_id']})['conversions'])
347
+ assert_equal(0, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test2), :test => test2['_id']})['conversions'])
348
+ assert_equal(0, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test3), :test => test3['_id']})['conversions'])
349
+
350
+ Abongo.bongo!('convert!')
351
+ assert_equal(1, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test1), :test => test1['_id']})['conversions'])
352
+ assert_equal(1, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test2), :test => test2['_id']})['conversions'])
353
+ assert_equal(0, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test3), :test => test3['_id']})['conversions'])
354
+ end
355
+
356
+ def test_bongo_with_test_name
357
+ Abongo.identity = 'ident'
358
+ test1 = Abongo.start_experiment!('test1', ['alt1', 'alt2'])
359
+ test2 = Abongo.start_experiment!('test2', ['alt1', 'alt2'])
360
+ test3 = Abongo.start_experiment!('test3', ['alt1', 'alt2'])
361
+ Abongo.test('test1', ['alt1', 'alt2'])
362
+ Abongo.test('test2', ['alt1', 'alt2'])
363
+ assert_equal(0, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test1), :test => test1['_id']})['conversions'])
364
+ assert_equal(0, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test2), :test => test2['_id']})['conversions'])
365
+ Abongo.bongo!('test1')
366
+ assert_equal(1, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test1), :test => test1['_id']})['conversions'])
367
+ assert_equal(0, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test2), :test => test2['_id']})['conversions'])
368
+ end
369
+
370
+ def test_bongo_doesnt_assume_participation
371
+ Abongo.identity = 'ident'
372
+ test1 = Abongo.start_experiment!('test1', ['alt1', 'alt2'])
373
+ Abongo.bongo!('test1')
374
+ assert_equal(0, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test1), :test => test1['_id']})['conversions'])
375
+ Abongo.test('test1', ['alt1', 'alt2'])
376
+ assert_equal(0, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test1), :test => test1['_id']})['conversions'])
377
+ Abongo.bongo!('test1')
378
+ assert_equal(1, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test1), :test => test1['_id']})['conversions'])
379
+ end
380
+
381
+ def test_bongo_with_assume_participation
382
+ Abongo.identity = 'ident'
383
+ Abongo.options[:assume_participation] = true
384
+ test1 = Abongo.start_experiment!('test1', ['alt1', 'alt2'])
385
+ assert_equal(0, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test1), :test => test1['_id']})['conversions'])
386
+ Abongo.bongo!('test1')
387
+ assert_equal(1, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test1), :test => test1['_id']})['conversions'])
388
+ Abongo.test('test1', ['alt1', 'alt2'])
389
+ assert_equal(1, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test1), :test => test1['_id']})['conversions'])
390
+ Abongo.bongo!('test1')
391
+ assert_equal(1, Abongo.alternatives.find_one({:content => Abongo.find_alternative_for_user(Abongo.identity, test1), :test => test1['_id']})['conversions'])
392
+ end
393
+
394
+ def test_participating_tests
395
+ Abongo.identity = 'ident'
396
+ test1 = Abongo.start_experiment!('test1', ['alt1', 'alt2'])
397
+ test2 = Abongo.start_experiment!('test2', ['alt1', 'alt2'])
398
+ test3 = Abongo.start_experiment!('test3', ['alt1', 'alt2'])
399
+ assert_equal({}, Abongo.participating_tests)
400
+ Abongo.test('test1', ['alt1', 'alt2'])
401
+ assert_equal({'test1' => 'alt1'}, Abongo.participating_tests)
402
+ Abongo.test('test2', ['alt1', 'alt2'])
403
+ assert_equal({'test1' => 'alt1', 'test2' => 'alt1'}, Abongo.participating_tests)
404
+ Abongo.end_experiment!('test1', 'alt1')
405
+ assert_equal({'test2' => 'alt1'}, Abongo.participating_tests)
406
+ Abongo.end_experiment!('test2', 'alt1')
407
+ assert_equal({}, Abongo.participating_tests)
408
+ end
409
+
410
+ def test_participating_tests_with_noncurrent
411
+ Abongo.identity = 'ident'
412
+ test1 = Abongo.start_experiment!('test1', ['alt1', 'alt2'])
413
+ test2 = Abongo.start_experiment!('test2', ['alt1', 'alt2'])
414
+ test3 = Abongo.start_experiment!('test3', ['alt1', 'alt2'])
415
+ assert_equal({}, Abongo.participating_tests)
416
+ Abongo.test('test1', ['alt1', 'alt2'])
417
+ assert_equal({'test1' => 'alt1'}, Abongo.participating_tests(false))
418
+ Abongo.test('test2', ['alt1', 'alt2'])
419
+ assert_equal({'test1' => 'alt1', 'test2' => 'alt1'}, Abongo.participating_tests(false))
420
+ Abongo.end_experiment!('test1', 'alt1')
421
+ assert_equal({'test1' => 'alt1', 'test2' => 'alt1'}, Abongo.participating_tests(false))
422
+ Abongo.end_experiment!('test2', 'alt1')
423
+ assert_equal({'test1' => 'alt1', 'test2' => 'alt1'}, Abongo.participating_tests(false))
424
+ end
425
+
426
+ def test_expires_in
427
+ Abongo.identity = 'ident'
428
+ Abongo.options[:expires_in] = 1
429
+ experiment = Abongo.start_experiment!('test1', ['alt1', 'alt2'])
430
+
431
+ alternative = Abongo.alternatives.find_one(:test => experiment['_id'], :content => 'alt1')
432
+ assert_equal(0, alternative['participants'])
433
+ assert_equal(0, alternative['conversions'])
434
+
435
+ Abongo.test('test1', ['alt1', 'alt2'])
436
+ Abongo.bongo!
437
+ alternative = Abongo.alternatives.find_one(:test => experiment['_id'], :content => 'alt1')
438
+ assert_equal(1, alternative['participants'])
439
+ assert_equal(1, alternative['conversions'])
440
+
441
+ sleep(1)
442
+
443
+ Abongo.test('test1', ['alt1', 'alt2'])
444
+ Abongo.bongo!
445
+ alternative = Abongo.alternatives.find_one(:test => experiment['_id'], :content => 'alt1')
446
+ assert_equal(2, alternative['participants'])
447
+ assert_equal(2, alternative['conversions'])
448
+ end
449
+
450
+ def test_expires_in_for_bots
451
+ Abongo.identity = 'ident'
452
+ Abongo.options[:count_humans_only] = true
453
+ Abongo.options[:expires_in] = 1000
454
+ Abongo.options[:expires_in_for_bots] = 1
455
+ experiment = Abongo.start_experiment!('test1', ['alt1', 'alt2'])
456
+
457
+ Abongo.test('test1', ['alt1', 'alt2'])
458
+ participant = Abongo.find_participant(Abongo.identity)
459
+ assert(participant['expires'] < Time.now+1)
460
+
461
+ Abongo.human!
462
+ participant = Abongo.find_participant(Abongo.identity)
463
+ assert(participant['expires'] > Time.now+1)
464
+
465
+ Abongo.identity = 'ident2'
466
+ Abongo.human!
467
+ Abongo.test('test1', ['alt1', 'alt2'])
468
+ participant = Abongo.find_participant(Abongo.identity)
469
+ assert(participant['expires'] > Time.now+1)
470
+ end
471
+
472
+ end