leaderboard 2.0.5 → 2.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,484 +0,0 @@
1
- require 'test_helper'
2
-
3
- class TestRevLeaderboard < LeaderboardTest
4
- def setup
5
- @redis_connection = Redis.new(:host => "127.0.0.1")
6
- @leaderboard = Leaderboard.new('name', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:reverse => true}), :host => "127.0.0.1")
7
- end
8
-
9
- def teardown
10
- @redis_connection.flushdb
11
- @leaderboard.disconnect
12
- @redis_connection.client.disconnect
13
- end
14
-
15
- def test_initialize_with_defaults
16
- assert_equal 'name', @leaderboard.leaderboard_name
17
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, @leaderboard.page_size
18
- end
19
-
20
- def test_disconnect
21
- assert_equal nil, @leaderboard.disconnect
22
- end
23
-
24
- def test_will_automatically_reconnect_after_a_disconnect
25
- assert_equal 0, @leaderboard.total_members
26
- rank_members_in_leaderboard(5)
27
- assert_equal 5, @leaderboard.total_members
28
- assert_equal nil, @leaderboard.disconnect
29
- assert_equal 5, @leaderboard.total_members
30
- end
31
-
32
- def test_page_size_is_default_page_size_if_set_to_invalid_value
33
- some_leaderboard = Leaderboard.new('name', {:page_size => 0})
34
-
35
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, some_leaderboard.page_size
36
- some_leaderboard.disconnect
37
- end
38
-
39
- def test_delete_leaderboard
40
- rank_members_in_leaderboard
41
-
42
- assert_equal true, @redis_connection.exists('name')
43
- @leaderboard.delete_leaderboard
44
- assert_equal false, @redis_connection.exists('name')
45
- end
46
-
47
- def test_can_pass_existing_redis_connection_to_initializer
48
- @leaderboard = Leaderboard.new('name', Leaderboard::DEFAULT_OPTIONS, {:redis_connection => @redis_connection})
49
- rank_members_in_leaderboard
50
-
51
- assert_equal 1, @redis_connection.info["connected_clients"].to_i
52
- end
53
-
54
- def test_rank_member_and_total_members
55
- @leaderboard.rank_member('member', 1)
56
-
57
- assert_equal 1, @leaderboard.total_members
58
- end
59
-
60
- def test_total_members_in_score_range
61
- rank_members_in_leaderboard(5)
62
-
63
- assert_equal 3, @leaderboard.total_members_in_score_range(2, 4)
64
- end
65
-
66
- def test_rank_for
67
- rank_members_in_leaderboard(5)
68
-
69
- assert_equal 4, @leaderboard.rank_for('member_4')
70
- assert_equal 3, @leaderboard.rank_for('member_4', true)
71
- end
72
-
73
- def test_score_for
74
- rank_members_in_leaderboard(5)
75
-
76
- assert_equal 4, @leaderboard.score_for('member_4')
77
- end
78
-
79
- def test_total_pages
80
- rank_members_in_leaderboard(10)
81
-
82
- assert_equal 1, @leaderboard.total_pages
83
-
84
- @redis_connection.flushdb
85
-
86
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE + 1)
87
-
88
- assert_equal 2, @leaderboard.total_pages
89
- end
90
-
91
- def test_leaders
92
- rank_members_in_leaderboard(25)
93
-
94
- assert_equal 25, @leaderboard.total_members
95
-
96
- leaders = @leaderboard.leaders(1)
97
-
98
- assert_equal 25, leaders.size
99
- assert_equal 'member_1', leaders[0][:member]
100
- assert_equal 'member_24', leaders[-2][:member]
101
- assert_equal 'member_25', leaders[-1][:member]
102
- assert_equal 25, leaders[-1][:score].to_i
103
- end
104
-
105
- def test_leaders_with_multiple_pages
106
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)
107
-
108
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1, @leaderboard.total_members
109
-
110
- leaders = @leaderboard.leaders(1)
111
- assert_equal @leaderboard.page_size, leaders.size
112
-
113
- leaders = @leaderboard.leaders(2)
114
- assert_equal @leaderboard.page_size, leaders.size
115
-
116
- leaders = @leaderboard.leaders(3)
117
- assert_equal @leaderboard.page_size, leaders.size
118
-
119
- leaders = @leaderboard.leaders(4)
120
- assert_equal 1, leaders.size
121
-
122
- leaders = @leaderboard.leaders(-5)
123
- assert_equal @leaderboard.page_size, leaders.size
124
-
125
- leaders = @leaderboard.leaders(10)
126
- assert_equal 1, leaders.size
127
- end
128
-
129
- def test_leaders_without_retrieving_scores_and_ranks
130
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
131
-
132
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, @leaderboard.total_members
133
- leaders = @leaderboard.leaders(1, {:with_scores => false, :with_rank => false})
134
-
135
- member_1 = {:member => 'member_1'}
136
- assert_equal member_1, leaders[0]
137
-
138
- member_25 = {:member => 'member_25'}
139
- assert_equal member_25, leaders[24]
140
- end
141
-
142
- def test_leaders_with_only_various_options_should_respect_other_defaults
143
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE + 1)
144
-
145
- leaders = @leaderboard.leaders(1, :page_size => 1)
146
- assert_equal 1, leaders.size
147
-
148
- leaders = @leaderboard.leaders(1, :with_rank => false)
149
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, leaders.size
150
- member_1 = {:member => 'member_1', :score => 1}
151
- member_2 = {:member => 'member_2', :score => 2}
152
- member_3 = {:member => 'member_3', :score => 3}
153
- assert_equal member_1, leaders[0]
154
- assert_equal member_2, leaders[1]
155
- assert_equal member_3, leaders[2]
156
-
157
- leaders = @leaderboard.leaders(1, :with_scores => false)
158
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, leaders.size
159
- member_1 = {:member => 'member_1', :rank => 1}
160
- member_2 = {:member => 'member_2', :rank => 2}
161
- assert_equal member_1, leaders[0]
162
- assert_equal member_2, leaders[1]
163
-
164
- leaders = @leaderboard.leaders(1, :with_scores => false, :with_rank => false)
165
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, leaders.size
166
- member_1 = {:member => 'member_1'}
167
- member_2 = {:member => 'member_2'}
168
- assert_equal member_1, leaders[0]
169
- assert_equal member_2, leaders[1]
170
-
171
- leaders = @leaderboard.leaders(1, :with_rank => false, :page_size => 1)
172
- assert_equal 1, leaders.size
173
- member_1 = {:member => 'member_1', :score => 1}
174
- assert_equal member_1, leaders[0]
175
- end
176
-
177
- def test_around_me
178
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)
179
-
180
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1, @leaderboard.total_members
181
-
182
- leaders_around_me = @leaderboard.around_me('member_30')
183
- assert_equal @leaderboard.page_size / 2, leaders_around_me.size / 2
184
-
185
- leaders_around_me = @leaderboard.around_me('member_76')
186
- assert_equal @leaderboard.page_size / 2 + 1, leaders_around_me.size
187
-
188
- leaders_around_me = @leaderboard.around_me('member_1')
189
- assert_equal @leaderboard.page_size / 2, leaders_around_me.size / 2
190
- end
191
-
192
- def test_ranked_in_list
193
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
194
-
195
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, @leaderboard.total_members
196
-
197
- members = ['member_1', 'member_5', 'member_10']
198
- ranked_members = @leaderboard.ranked_in_list(members, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS)
199
-
200
- assert_equal 3, ranked_members.size
201
-
202
- assert_equal 1, ranked_members[0][:rank]
203
- assert_equal 1, ranked_members[0][:score]
204
-
205
- assert_equal 5, ranked_members[1][:rank]
206
- assert_equal 5, ranked_members[1][:score]
207
-
208
- assert_equal 10, ranked_members[2][:rank]
209
- assert_equal 10, ranked_members[2][:score]
210
- end
211
-
212
- def test_ranked_in_list_without_scores
213
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
214
-
215
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, @leaderboard.total_members
216
-
217
- members = ['member_1', 'member_5', 'member_10']
218
- ranked_members = @leaderboard.ranked_in_list(members, {:with_scores => false, :with_rank => true, :use_zero_index_for_rank => false})
219
-
220
- assert_equal 3, ranked_members.size
221
-
222
- assert_equal 1, ranked_members[0][:rank]
223
-
224
- assert_equal 5, ranked_members[1][:rank]
225
-
226
- assert_equal 10, ranked_members[2][:rank]
227
- end
228
-
229
- def test_remove_member
230
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
231
-
232
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, @leaderboard.total_members
233
-
234
- @leaderboard.remove_member('member_1')
235
-
236
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE - 1, @leaderboard.total_members
237
- assert_nil @leaderboard.rank_for('member_1')
238
- end
239
-
240
- def test_change_score_for
241
- @leaderboard.rank_member('member_1', 5)
242
- assert_equal 5, @leaderboard.score_for('member_1')
243
-
244
- @leaderboard.change_score_for('member_1', 5)
245
- assert_equal 10, @leaderboard.score_for('member_1')
246
-
247
- @leaderboard.change_score_for('member_1', -5)
248
- assert_equal 5, @leaderboard.score_for('member_1')
249
- end
250
-
251
- def test_check_member
252
- @leaderboard.rank_member('member_1', 10)
253
-
254
- assert_equal true, @leaderboard.check_member?('member_1')
255
- assert_equal false, @leaderboard.check_member?('member_2')
256
- end
257
-
258
- def test_can_change_page_size_and_have_it_reflected_in_size_of_result_set
259
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
260
-
261
- @leaderboard.page_size = 5
262
-
263
- assert_equal 5, @leaderboard.total_pages
264
- assert_equal 5, @leaderboard.leaders(1).size
265
- end
266
-
267
- def test_cannot_set_page_size_to_invalid_page_size
268
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
269
-
270
- @leaderboard.page_size = 0
271
- assert_equal 1, @leaderboard.total_pages
272
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, @leaderboard.leaders(1).size
273
- end
274
-
275
- def test_score_and_rank_for
276
- rank_members_in_leaderboard
277
-
278
- data = @leaderboard.score_and_rank_for('member_1')
279
- assert_equal 'member_1', data[:member]
280
- assert_equal 1, data[:score]
281
- assert_equal 1, data[:rank]
282
- end
283
-
284
- def test_remove_members_in_score_range
285
- rank_members_in_leaderboard
286
-
287
- assert_equal 5, @leaderboard.total_members
288
-
289
- @leaderboard.rank_member('cheater_1', 100)
290
- @leaderboard.rank_member('cheater_2', 101)
291
- @leaderboard.rank_member('cheater_3', 102)
292
-
293
- assert_equal 8, @leaderboard.total_members
294
-
295
- @leaderboard.remove_members_in_score_range(100, 102)
296
-
297
- assert_equal 5, @leaderboard.total_members
298
-
299
- leaders = @leaderboard.leaders(1)
300
- leaders.each do |leader|
301
- assert leader[:score] < 100
302
- end
303
- end
304
-
305
- def test_merge_leaderboards
306
- foo = Leaderboard.new('foo')
307
- bar = Leaderboard.new('bar')
308
-
309
- foo.rank_member('foo_1', 1)
310
- foo.rank_member('foo_2', 2)
311
- bar.rank_member('bar_1', 3)
312
- bar.rank_member('bar_2', 4)
313
- bar.rank_member('bar_3', 5)
314
-
315
- foobar_keys = foo.merge_leaderboards('foobar', ['bar'])
316
- assert_equal 5, foobar_keys
317
-
318
- foobar = Leaderboard.new('foobar')
319
- assert_equal 5, foobar.total_members
320
-
321
- first_leader_in_foobar = foobar.leaders(1).first
322
- assert_equal 1, first_leader_in_foobar[:rank]
323
- assert_equal 'bar_3', first_leader_in_foobar[:member]
324
- assert_equal 5, first_leader_in_foobar[:score]
325
-
326
- foo.disconnect
327
- bar.disconnect
328
- foobar.disconnect
329
- end
330
-
331
- def test_intersect_leaderboards
332
- foo = Leaderboard.new('foo')
333
- bar = Leaderboard.new('bar')
334
-
335
- foo.rank_member('foo_1', 1)
336
- foo.rank_member('foo_2', 2)
337
- foo.rank_member('bar_3', 6)
338
- bar.rank_member('bar_1', 3)
339
- bar.rank_member('foo_1', 4)
340
- bar.rank_member('bar_3', 5)
341
-
342
- foobar_keys = foo.intersect_leaderboards('foobar', ['bar'], {:aggregate => :max})
343
- assert_equal 2, foobar_keys
344
-
345
- foobar = Leaderboard.new('foobar')
346
- assert_equal 2, foobar.total_members
347
-
348
- first_leader_in_foobar = foobar.leaders(1).first
349
- assert_equal 1, first_leader_in_foobar[:rank]
350
- assert_equal 'bar_3', first_leader_in_foobar[:member]
351
- assert_equal 6, first_leader_in_foobar[:score]
352
-
353
- foo.disconnect
354
- bar.disconnect
355
- foobar.disconnect
356
- end
357
-
358
- def test_massage_leader_data_respects_with_scores
359
- rank_members_in_leaderboard(25)
360
-
361
- assert_equal 25, @leaderboard.total_members
362
-
363
- leaders = @leaderboard.leaders(1, {:with_scores => false, :with_rank => false})
364
- assert_not_nil leaders[0][:member]
365
- assert_nil leaders[0][:score]
366
- assert_nil leaders[0][:rank]
367
-
368
- @leaderboard.page_size = 25
369
- leaders = @leaderboard.leaders(1, {:with_scores => false, :with_rank => false})
370
- assert_equal 25, leaders.size
371
-
372
- @leaderboard.page_size = Leaderboard::DEFAULT_PAGE_SIZE
373
- leaders = @leaderboard.leaders(1, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS)
374
- assert_not_nil leaders[0][:member]
375
- assert_not_nil leaders[0][:score]
376
- assert_not_nil leaders[0][:rank]
377
-
378
- @leaderboard.page_size = 25
379
- leaders = @leaderboard.leaders(1, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS)
380
- assert_equal 25, leaders.size
381
- end
382
-
383
- def test_total_pages_in_with_new_page_size
384
- rank_members_in_leaderboard(25)
385
-
386
- assert_equal 1, @leaderboard.total_pages_in(@leaderboard.leaderboard_name)
387
- assert_equal 5, @leaderboard.total_pages_in(@leaderboard.leaderboard_name, 5)
388
- end
389
-
390
- def test_leaders_call_with_new_page_size
391
- rank_members_in_leaderboard(25)
392
-
393
- assert_equal 5, @leaderboard.leaders(1, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 5})).size
394
- assert_equal 10, @leaderboard.leaders(1, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 10})).size
395
- assert_equal 10, @leaderboard.leaders(2, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 10})).size
396
- assert_equal 5, @leaderboard.leaders(3, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 10})).size
397
- end
398
-
399
- def test_around_me_call_with_new_page_size
400
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)
401
-
402
- leaders_around_me = @leaderboard.around_me('member_30', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 3}))
403
- assert_equal 3, leaders_around_me.size
404
- assert_equal 'member_31', leaders_around_me[2][:member]
405
- assert_equal 'member_29', leaders_around_me[0][:member]
406
- end
407
-
408
- def test_percentile_for
409
- rank_members_in_leaderboard(12)
410
-
411
- assert_equal 100, @leaderboard.percentile_for('member_1')
412
- assert_equal 91, @leaderboard.percentile_for('member_2')
413
- assert_equal 83, @leaderboard.percentile_for('member_3')
414
- assert_equal 75, @leaderboard.percentile_for('member_4')
415
- assert_equal 8, @leaderboard.percentile_for('member_12')
416
- end
417
-
418
- def test_around_me_for_invalid_member
419
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)
420
-
421
- leaders_around_me = @leaderboard.around_me('jones', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 3}))
422
- assert_equal 0, leaders_around_me.size
423
- end
424
-
425
- def test_score_and_rank_for_non_existent_member
426
- score_and_rank_for_member = @leaderboard.score_and_rank_for('jones')
427
-
428
- assert_equal 'jones', score_and_rank_for_member[:member]
429
- assert_equal 0.0, score_and_rank_for_member[:score]
430
- assert_nil score_and_rank_for_member[:rank]
431
- end
432
-
433
- def test_ranked_in_list_for_non_existent_member
434
- rank_members_in_leaderboard
435
-
436
- members = ['member_1', 'member_5', 'jones']
437
- ranked_members = @leaderboard.ranked_in_list(members)
438
-
439
- assert_equal 3, ranked_members.size
440
- assert_nil ranked_members[2][:rank]
441
- end
442
-
443
- def test_percentile_for_non_existent_member
444
- percentile = @leaderboard.percentile_for('jones')
445
-
446
- assert_nil percentile
447
- end
448
-
449
- def test_change_score_for_non_existent_member
450
- assert_equal 0.0, @leaderboard.score_for('jones')
451
- @leaderboard.change_score_for('jones', 5)
452
- assert_equal 5.0, @leaderboard.score_for('jones')
453
- end
454
-
455
- def test_page_for
456
- assert_equal 0, @leaderboard.page_for('jones')
457
-
458
- rank_members_in_leaderboard(20)
459
-
460
- assert_equal 1, @leaderboard.page_for('member_17')
461
- assert_equal 1, @leaderboard.page_for('member_11')
462
- assert_equal 1, @leaderboard.page_for('member_10')
463
- assert_equal 1, @leaderboard.page_for('member_1')
464
-
465
- assert_equal 1, @leaderboard.page_for('member_10', 10)
466
- assert_equal 1, @leaderboard.page_for('member_1', 10)
467
- assert_equal 2, @leaderboard.page_for('member_17', 10)
468
- assert_equal 2, @leaderboard.page_for('member_11', 10)
469
- end
470
-
471
- def test_rank_members
472
- assert_equal 0, @leaderboard.total_members
473
- @leaderboard.rank_members('member_1', 1, 'member_10', 10)
474
- assert_equal 2, @leaderboard.total_members
475
- assert_equal 'member_1', @leaderboard.leaders(1).first[:member]
476
- end
477
-
478
- def test_rank_members_with_array
479
- assert_equal 0, @leaderboard.total_members
480
- @leaderboard.rank_members(['member_1', 1, 'member_10', 10])
481
- assert_equal 2, @leaderboard.total_members
482
- assert_equal 'member_1', @leaderboard.leaders(1).first[:member]
483
- end
484
- end