leaderboard 2.0.5 → 2.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,292 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Leaderboard (reverse option)' do
4
+ before(:each) do
5
+ @redis_connection = Redis.new(:host => "127.0.0.1", :db => 15)
6
+ @leaderboard = Leaderboard.new('name', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:reverse => true}), :host => "127.0.0.1", :db => 15)
7
+ end
8
+
9
+ after(:each) do
10
+ @redis_connection.flushdb
11
+ @leaderboard.disconnect
12
+ @redis_connection.client.disconnect
13
+ end
14
+
15
+ it 'should return the correct rank when calling rank_for' do
16
+ rank_members_in_leaderboard(5)
17
+
18
+ @leaderboard.rank_for('member_4').should be(4)
19
+ @leaderboard.rank_for('member_4', true).should be(3)
20
+ end
21
+
22
+ it 'should return the correct list when calling leaders' do
23
+ rank_members_in_leaderboard(25)
24
+
25
+ @leaderboard.total_members.should be(25)
26
+
27
+ leaders = @leaderboard.leaders(1)
28
+
29
+ leaders.size.should be(25)
30
+ leaders[0][:member].should == 'member_1'
31
+ leaders[-2][:member].should == 'member_24'
32
+ leaders[-1][:member].should == 'member_25'
33
+ leaders[-1][:score].to_i.should be(25)
34
+ end
35
+
36
+ it 'should allow you to retrieve leaders without scores and ranks' do
37
+ rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
38
+
39
+ @leaderboard.total_members.should be(Leaderboard::DEFAULT_PAGE_SIZE)
40
+ leaders = @leaderboard.leaders(1, {:with_scores => false, :with_rank => false})
41
+
42
+ member_1 = {:member => 'member_1'}
43
+ leaders[0].should == member_1
44
+
45
+ member_25 = {:member => 'member_25'}
46
+ leaders[24].should == member_25
47
+ end
48
+
49
+ it 'should allow you to call leaders with various options that respect the defaults for the options not passed in' do
50
+ rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE + 1)
51
+
52
+ leaders = @leaderboard.leaders(1, :page_size => 1)
53
+ leaders.size.should be(1)
54
+
55
+ leaders = @leaderboard.leaders(1, :with_rank => false)
56
+ leaders.size.should be(Leaderboard::DEFAULT_PAGE_SIZE)
57
+ member_1 = {:member => 'member_1', :score => 1}
58
+ member_2 = {:member => 'member_2', :score => 2}
59
+ member_3 = {:member => 'member_3', :score => 3}
60
+ leaders[0].should == member_1
61
+ leaders[1].should == member_2
62
+ leaders[2].should == member_3
63
+
64
+ leaders = @leaderboard.leaders(1, :with_scores => false)
65
+ leaders.size.should be(Leaderboard::DEFAULT_PAGE_SIZE)
66
+ member_1 = {:member => 'member_1', :rank => 1}
67
+ member_2 = {:member => 'member_2', :rank => 2}
68
+ leaders[0].should == member_1
69
+ leaders[1].should == member_2
70
+
71
+ leaders = @leaderboard.leaders(1, :with_scores => false, :with_rank => false)
72
+ leaders.size.should be(Leaderboard::DEFAULT_PAGE_SIZE)
73
+ member_1 = {:member => 'member_1'}
74
+ member_2 = {:member => 'member_2'}
75
+ leaders[0].should == member_1
76
+ leaders[1].should == member_2
77
+
78
+ leaders = @leaderboard.leaders(1, :with_rank => false, :page_size => 1)
79
+ leaders.size.should be(1)
80
+ member_1 = {:member => 'member_1', :score => 1}
81
+ leaders[0].should == member_1
82
+ end
83
+
84
+ it 'should return the correct information when calling around_me' do
85
+ rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)
86
+
87
+ @leaderboard.total_members.should be(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)
88
+
89
+ leaders_around_me = @leaderboard.around_me('member_30')
90
+ (leaders_around_me.size / 2).should be(@leaderboard.page_size / 2)
91
+
92
+ leaders_around_me = @leaderboard.around_me('member_76')
93
+ leaders_around_me.size.should be(@leaderboard.page_size / 2 + 1)
94
+
95
+ leaders_around_me = @leaderboard.around_me('member_1')
96
+ (leaders_around_me.size / 2).should be(@leaderboard.page_size / 2)
97
+ end
98
+
99
+ it 'should return the correct information when calling ranked_in_list' do
100
+ rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
101
+
102
+ @leaderboard.total_members.should be(Leaderboard::DEFAULT_PAGE_SIZE)
103
+
104
+ members = ['member_1', 'member_5', 'member_10']
105
+ ranked_members = @leaderboard.ranked_in_list(members, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS)
106
+
107
+ ranked_members.size.should be(3)
108
+
109
+ ranked_members[0][:rank].should == 1
110
+ ranked_members[0][:score].should == 1
111
+
112
+ ranked_members[1][:rank].should == 5
113
+ ranked_members[1][:score].should == 5
114
+
115
+ ranked_members[2][:rank].should == 10
116
+ ranked_members[2][:score].should == 10
117
+ end
118
+
119
+ it 'should return the correct information when calling ranked_in_list without scores' do
120
+ rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
121
+
122
+ @leaderboard.total_members.should be(Leaderboard::DEFAULT_PAGE_SIZE)
123
+
124
+ members = ['member_1', 'member_5', 'member_10']
125
+ ranked_members = @leaderboard.ranked_in_list(members, {:with_scores => false, :with_rank => true, :use_zero_index_for_rank => false})
126
+
127
+ ranked_members.size.should be(3)
128
+
129
+ ranked_members[0][:rank].should be(1)
130
+
131
+ ranked_members[1][:rank].should be(5)
132
+
133
+ ranked_members[2][:rank].should be(10)
134
+ end
135
+
136
+ it 'should return the correct information when calling score_and_rank_for' do
137
+ rank_members_in_leaderboard
138
+
139
+ data = @leaderboard.score_and_rank_for('member_1')
140
+ data[:member].should == 'member_1'
141
+ data[:score].should == 1
142
+ data[:rank].should be(1)
143
+ end
144
+
145
+ it 'should allow you to remove members in a given score range' do
146
+ rank_members_in_leaderboard
147
+
148
+ @leaderboard.total_members.should be(5)
149
+
150
+ @leaderboard.rank_member('cheater_1', 100)
151
+ @leaderboard.rank_member('cheater_2', 101)
152
+ @leaderboard.rank_member('cheater_3', 102)
153
+
154
+ @leaderboard.total_members.should be(8)
155
+
156
+ @leaderboard.remove_members_in_score_range(100, 102)
157
+
158
+ @leaderboard.total_members.should be(5)
159
+
160
+ leaders = @leaderboard.leaders(1)
161
+ leaders.each do |leader|
162
+ leader[:score].should be < 100
163
+ end
164
+ end
165
+
166
+ it 'should allow you to merge leaderboards' do
167
+ foo = Leaderboard.new('foo', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
168
+ bar = Leaderboard.new('bar', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
169
+
170
+ foo.rank_member('foo_1', 1)
171
+ foo.rank_member('foo_2', 2)
172
+ bar.rank_member('bar_1', 3)
173
+ bar.rank_member('bar_2', 4)
174
+ bar.rank_member('bar_3', 5)
175
+
176
+ foobar_keys = foo.merge_leaderboards('foobar', ['bar'])
177
+ foobar_keys.should be(5)
178
+
179
+ foobar = Leaderboard.new('foobar', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
180
+ foobar.total_members.should be(5)
181
+
182
+ first_leader_in_foobar = foobar.leaders(1).first
183
+ first_leader_in_foobar[:rank].should be(1)
184
+ first_leader_in_foobar[:member].should == 'bar_3'
185
+ first_leader_in_foobar[:score].should == 5
186
+
187
+ foo.disconnect
188
+ bar.disconnect
189
+ foobar.disconnect
190
+ end
191
+
192
+ it 'should allow you to intersect leaderboards' do
193
+ foo = Leaderboard.new('foo', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
194
+ bar = Leaderboard.new('bar', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
195
+
196
+ foo.rank_member('foo_1', 1)
197
+ foo.rank_member('foo_2', 2)
198
+ foo.rank_member('bar_3', 6)
199
+ bar.rank_member('bar_1', 3)
200
+ bar.rank_member('foo_1', 4)
201
+ bar.rank_member('bar_3', 5)
202
+
203
+ foobar_keys = foo.intersect_leaderboards('foobar', ['bar'], {:aggregate => :max})
204
+ foobar_keys.should be(2)
205
+
206
+ foobar = Leaderboard.new('foobar', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
207
+ foobar.total_members.should be(2)
208
+
209
+ first_leader_in_foobar = foobar.leaders(1).first
210
+ first_leader_in_foobar[:rank].should be(1)
211
+ first_leader_in_foobar[:member].should == 'bar_3'
212
+ first_leader_in_foobar[:score].should == 6
213
+
214
+ foo.disconnect
215
+ bar.disconnect
216
+ foobar.disconnect
217
+ end
218
+
219
+ it 'should respect the with_scores option in the massage_leader_data method' do
220
+ rank_members_in_leaderboard(25)
221
+
222
+ @leaderboard.total_members.should be(25)
223
+
224
+ leaders = @leaderboard.leaders(1, {:with_scores => false, :with_rank => false})
225
+ leaders[0][:member].should_not be_nil
226
+ leaders[0][:score].should be_nil
227
+ leaders[0][:rank].should be_nil
228
+
229
+ @leaderboard.page_size = 25
230
+ leaders = @leaderboard.leaders(1, {:with_scores => false, :with_rank => false})
231
+ leaders.size.should be(25)
232
+
233
+ @leaderboard.page_size = Leaderboard::DEFAULT_PAGE_SIZE
234
+ leaders = @leaderboard.leaders(1, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS)
235
+ leaders[0][:member].should_not be_nil
236
+ leaders[0][:score].should_not be_nil
237
+ leaders[0][:rank].should_not be_nil
238
+
239
+ @leaderboard.page_size = 25
240
+ leaders = @leaderboard.leaders(1, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS)
241
+ leaders.size.should be(25)
242
+ end
243
+
244
+ it 'should return the correct number of members when calling around_me with a page_size options' do
245
+ rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)
246
+
247
+ leaders_around_me = @leaderboard.around_me('member_30', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 3}))
248
+ leaders_around_me.size.should be(3)
249
+ leaders_around_me[2][:member].should == 'member_31'
250
+ leaders_around_me[0][:member].should == 'member_29'
251
+ end
252
+
253
+ it 'should return the correct information when calling percentile_for' do
254
+ rank_members_in_leaderboard(12)
255
+
256
+ @leaderboard.percentile_for('member_1').should == 100
257
+ @leaderboard.percentile_for('member_2').should == 91
258
+ @leaderboard.percentile_for('member_3').should == 83
259
+ @leaderboard.percentile_for('member_4').should == 75
260
+ @leaderboard.percentile_for('member_12').should == 8
261
+ end
262
+
263
+ it 'should return the correct page when calling page_for a given member in the leaderboard' do
264
+ @leaderboard.page_for('jones').should be(0)
265
+
266
+ rank_members_in_leaderboard(20)
267
+
268
+ @leaderboard.page_for('member_17').should be(1)
269
+ @leaderboard.page_for('member_11').should be(1)
270
+ @leaderboard.page_for('member_10').should be(1)
271
+ @leaderboard.page_for('member_1').should be(1)
272
+
273
+ @leaderboard.page_for('member_10', 10).should be(1)
274
+ @leaderboard.page_for('member_1', 10).should be(1)
275
+ @leaderboard.page_for('member_17', 10).should be(2)
276
+ @leaderboard.page_for('member_11', 10).should be(2)
277
+ end
278
+
279
+ it 'should allow you to rank multiple members with a variable number of arguments' do
280
+ @leaderboard.total_members.should be(0)
281
+ @leaderboard.rank_members('member_1', 1, 'member_10', 10)
282
+ @leaderboard.total_members.should be(2)
283
+ @leaderboard.leaders(1).first[:member].should == 'member_1'
284
+ end
285
+
286
+ it 'should allow you to rank multiple members with an array' do
287
+ @leaderboard.total_members.should be(0)
288
+ @leaderboard.rank_members(['member_1', 1, 'member_10', 10])
289
+ @leaderboard.total_members.should be(2)
290
+ @leaderboard.leaders(1).first[:member].should == 'member_1'
291
+ end
292
+ end
@@ -0,0 +1,15 @@
1
+ require 'rspec'
2
+ require 'leaderboard'
3
+
4
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
5
+
6
+ RSpec.configure do |config|
7
+ # Helper method to rank members in a leaderboard
8
+ #
9
+ # @param members_to_add [int] Number of members to add to the leaderboard.
10
+ def rank_members_in_leaderboard(members_to_add = 5)
11
+ 1.upto(members_to_add) do |index|
12
+ @leaderboard.rank_member("member_#{index}", index)
13
+ end
14
+ end
15
+ end
@@ -1,4 +1,4 @@
1
- dir ./test/db
1
+ dir ./spec/db
2
2
  pidfile ./redis.pid
3
3
  port 6379
4
4
  timeout 300
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Leaderboard::VERSION' do
4
+ it 'should be the correct version' do
5
+ Leaderboard::VERSION.should == '2.0.6'
6
+ end
7
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: leaderboard
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.5
4
+ version: 2.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-15 00:00:00.000000000 Z
12
+ date: 2012-04-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis
16
- requirement: &2161382920 !ruby/object:Gem::Requirement
16
+ requirement: &70173076945220 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2161382920
24
+ version_requirements: *70173076945220
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &2161382500 !ruby/object:Gem::Requirement
27
+ requirement: &70173076944740 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,18 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2161382500
35
+ version_requirements: *70173076944740
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &70173076944140 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70173076944140
36
47
  description: Leaderboards backed by Redis in Ruby
37
48
  email:
38
49
  - dczarnecki@agoragames.com
@@ -41,6 +52,7 @@ extensions: []
41
52
  extra_rdoc_files: []
42
53
  files:
43
54
  - .gitignore
55
+ - .rspec
44
56
  - .rvmrc
45
57
  - CHANGELOG.markdown
46
58
  - Gemfile
@@ -50,11 +62,12 @@ files:
50
62
  - leaderboard.gemspec
51
63
  - lib/leaderboard.rb
52
64
  - lib/leaderboard/version.rb
53
- - test/db/.gitkeep
54
- - test/test.conf
55
- - test/test_helper.rb
56
- - test/test_leaderboard.rb
57
- - test/test_rev_leaderboard.rb
65
+ - spec/db/.gitkeep
66
+ - spec/leaderboard_spec.rb
67
+ - spec/reverse_leaderboard_spec.rb
68
+ - spec/spec_helper.rb
69
+ - spec/test.conf
70
+ - spec/version_spec.rb
58
71
  homepage: https://github.com/agoragames/leaderboard
59
72
  licenses: []
60
73
  post_install_message:
@@ -69,7 +82,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
69
82
  version: '0'
70
83
  segments:
71
84
  - 0
72
- hash: 1692368560274540289
85
+ hash: -4420304645092000909
73
86
  required_rubygems_version: !ruby/object:Gem::Requirement
74
87
  none: false
75
88
  requirements:
@@ -78,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
91
  version: '0'
79
92
  segments:
80
93
  - 0
81
- hash: 1692368560274540289
94
+ hash: -4420304645092000909
82
95
  requirements: []
83
96
  rubyforge_project: leaderboard
84
97
  rubygems_version: 1.8.10
@@ -86,8 +99,9 @@ signing_key:
86
99
  specification_version: 3
87
100
  summary: Leaderboards backed by Redis in Ruby
88
101
  test_files:
89
- - test/db/.gitkeep
90
- - test/test.conf
91
- - test/test_helper.rb
92
- - test/test_leaderboard.rb
93
- - test/test_rev_leaderboard.rb
102
+ - spec/db/.gitkeep
103
+ - spec/leaderboard_spec.rb
104
+ - spec/reverse_leaderboard_spec.rb
105
+ - spec/spec_helper.rb
106
+ - spec/test.conf
107
+ - spec/version_spec.rb
data/test/test_helper.rb DELETED
@@ -1,19 +0,0 @@
1
- require 'rubygems'
2
- require 'test/unit'
3
-
4
- require 'leaderboard'
5
-
6
- class LeaderboardTest < Test::Unit::TestCase
7
-
8
- def test_version
9
- assert_equal '2.0.5', Leaderboard::VERSION
10
- end
11
-
12
- private
13
-
14
- def rank_members_in_leaderboard(members_to_add = 5)
15
- 1.upto(members_to_add) do |index|
16
- @leaderboard.rank_member("member_#{index}", index)
17
- end
18
- end
19
- end