Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don't worry, you can still create the pull request.
  • 4 commits
  • 11 files changed
  • 0 commit comments
  • 1 contributor
View
2  .rspec
@@ -0,0 +1,2 @@
+--color
+--format nested
View
24 Rakefile
@@ -1,15 +1,15 @@
-require 'bundler/gem_tasks'
-require 'rake'
-require 'rake/testtask'
-
-Rake::TestTask.new(:test) do |test|
- test.libs << 'lib' << 'test'
- test.pattern = 'test/**/test_*.rb'
- test.verbose = true
- # test.warning = true
+require 'bundler'
+Bundler::GemHelper.install_tasks
+
+require 'rspec/core/rake_task'
+
+RSpec::Core::RakeTask.new(:spec) do |spec|
+ spec.pattern = 'spec/**/*_spec.rb'
+ spec.rspec_opts = ['--backtrace']
+ # spec.ruby_opts = ['-w']
end
-REDIS_DIR = File.expand_path(File.join("..", "test"), __FILE__)
+REDIS_DIR = File.expand_path(File.join("..", "spec"), __FILE__)
REDIS_CNF = File.join(REDIS_DIR, "test.conf")
REDIS_PID = File.join(REDIS_DIR, "db", "redis.pid")
REDIS_LOCATION = ENV['REDIS_LOCATION']
@@ -17,7 +17,7 @@ REDIS_LOCATION = ENV['REDIS_LOCATION']
task :default => :run
desc "Run tests and manage server start/stop"
-task :run => [:start, :test, :stop]
+task :run => [:start, :spec, :stop]
desc "Start the Redis server"
task :start do
@@ -46,6 +46,6 @@ end
task :test_rubies do
Rake::Task['start'].execute
- system "rvm 1.8.7@leaderboard_gem,1.9.2@leaderboard_gem,1.9.3@leaderboard_gem do rake test"
+ system "rvm 1.8.7@leaderboard_gem,1.9.2@leaderboard_gem,1.9.3@leaderboard_gem do rake spec"
Rake::Task['stop'].execute
end
View
1  leaderboard.gemspec
@@ -24,4 +24,5 @@ Gem::Specification.new do |s|
if '1.8.7'.eql?(RUBY_VERSION)
s.add_development_dependency('SystemTimer')
end
+ s.add_development_dependency('rspec')
end
View
0  test/db/.gitkeep → spec/db/.gitkeep
File renamed without changes
View
481 spec/leaderboard_spec.rb
@@ -0,0 +1,481 @@
+require 'spec_helper'
+
+describe 'Leaderboard' do
+ before(:each) do
+ @redis_connection = Redis.new(:host => "127.0.0.1")
+ @leaderboard = Leaderboard.new('name', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1")
+ end
+
+ after(:each) do
+ @redis_connection.flushdb
+ @leaderboard.disconnect
+ @redis_connection.client.disconnect
+ end
+
+ it 'should be initialized with defaults' do
+ @leaderboard.leaderboard_name.should == 'name'
+ @leaderboard.page_size.should == Leaderboard::DEFAULT_PAGE_SIZE
+ end
+
+ it 'should be able to disconnect its connection to Redis' do
+ @leaderboard.disconnect.should be_nil
+ end
+
+ it 'should automatically reconnect to Redis after a disconnect' do
+ @leaderboard.total_members.should be(0)
+ rank_members_in_leaderboard(5)
+ @leaderboard.total_members.should be(5)
+ @leaderboard.disconnect.should be_nil
+ @leaderboard.total_members.should be(5)
+ end
+
+ it 'should set the page size to the default page size if passed an invalid value' do
+ some_leaderboard = Leaderboard.new('name', {:page_size => 0})
+
+ some_leaderboard.page_size.should be(Leaderboard::DEFAULT_PAGE_SIZE)
+ some_leaderboard.disconnect
+ end
+
+ it 'should allow you to delete a leaderboard' do
+ rank_members_in_leaderboard
+
+ @redis_connection.exists('name').should be_true
+ @leaderboard.delete_leaderboard
+ @redis_connection.exists('name').should be_false
+ end
+
+ it 'should allow you to pass in an existing redis connection in the initializer' do
+ @leaderboard = Leaderboard.new('name', Leaderboard::DEFAULT_OPTIONS, {:redis_connection => @redis_connection})
+ rank_members_in_leaderboard
+
+ @redis_connection.info["connected_clients"].to_i.should be(1)
+ end
+
+ it 'should allow you to rank a member and see that reflected in total members' do
+ @leaderboard.rank_member('member', 1)
+
+ @leaderboard.total_members.should be(1)
+ end
+
+ it 'should return the correct number of members in a given score range' do
+ rank_members_in_leaderboard(5)
+
+ @leaderboard.total_members_in_score_range(2, 4).should be(3)
+ end
+
+ it 'should return the correct rank when calling rank_for' do
+ rank_members_in_leaderboard(5)
+
+ @leaderboard.rank_for('member_4').should be(2)
+ @leaderboard.rank_for('member_4', true).should be(1)
+ end
+
+ it 'should return the correct score when calling score_for' do
+ rank_members_in_leaderboard(5)
+
+ @leaderboard.score_for('member_4').should == 4
+ end
+
+ it 'should return the correct total pages' do
+ rank_members_in_leaderboard(10)
+
+ @leaderboard.total_pages.should be(1)
+
+ @redis_connection.flushdb
+
+ rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE + 1)
+
+ @leaderboard.total_pages.should be(2)
+ end
+
+ it 'should return the correct list when calling leaders' do
+ rank_members_in_leaderboard(25)
+
+ @leaderboard.total_members.should be(25)
+
+ leaders = @leaderboard.leaders(1)
+
+ leaders.size.should be(25)
+ leaders[0][:member].should == 'member_25'
+ leaders[-2][:member].should == 'member_2'
+ leaders[-1][:member].should == 'member_1'
+ leaders[-1][:score].to_i.should be(1)
+ end
+
+ it 'should return the correct number of members when calling leaders with multiple pages' do
+ rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)
+
+ @leaderboard.total_members.should be(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)
+
+ leaders = @leaderboard.leaders(1)
+ leaders.size.should be(@leaderboard.page_size)
+
+ leaders = @leaderboard.leaders(2)
+ leaders.size.should be(@leaderboard.page_size)
+
+ leaders = @leaderboard.leaders(3)
+ leaders.size.should be(@leaderboard.page_size)
+
+ leaders = @leaderboard.leaders(4)
+ leaders.size.should be(1)
+
+ leaders = @leaderboard.leaders(-5)
+ leaders.size.should be(@leaderboard.page_size)
+
+ leaders = @leaderboard.leaders(10)
+ leaders.size.should be(1)
+ end
+
+ it 'should allow you to retrieve leaders without scores and ranks' do
+ rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
+
+ @leaderboard.total_members.should be(Leaderboard::DEFAULT_PAGE_SIZE)
+ leaders = @leaderboard.leaders(1, {:with_scores => false, :with_rank => false})
+
+ member_25 = {:member => 'member_25'}
+ leaders[0].should == member_25
+
+ member_1 = {:member => 'member_1'}
+ leaders[24].should == member_1
+ end
+
+ it 'should allow you to call leaders with various options that respect the defaults for the options not passed in' do
+ rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE + 1)
+
+ leaders = @leaderboard.leaders(1, :page_size => 1)
+ leaders.size.should be(1)
+
+ leaders = @leaderboard.leaders(1, :with_rank => false)
+ leaders.size.should be(Leaderboard::DEFAULT_PAGE_SIZE)
+ member_26 = {:member => 'member_26', :score => 26}
+ member_25 = {:member => 'member_25', :score => 25}
+ member_24 = {:member => 'member_24', :score => 24}
+ leaders[0].should == member_26
+ leaders[1].should == member_25
+ leaders[2].should == member_24
+
+ leaders = @leaderboard.leaders(1, :with_scores => false)
+ leaders.size.should be(Leaderboard::DEFAULT_PAGE_SIZE)
+ member_26 = {:member => 'member_26', :rank => 1}
+ member_25 = {:member => 'member_25', :rank => 2}
+ leaders[0].should == member_26
+ leaders[1].should == member_25
+
+ leaders = @leaderboard.leaders(1, :with_scores => false, :with_rank => false)
+ leaders.size.should be(Leaderboard::DEFAULT_PAGE_SIZE)
+ member_26 = {:member => 'member_26'}
+ member_25 = {:member => 'member_25'}
+ leaders[0].should == member_26
+ leaders[1].should == member_25
+
+ leaders = @leaderboard.leaders(1, :with_rank => false, :page_size => 1)
+ leaders.size.should be(1)
+ member_26 = {:member => 'member_26', :score => 26}
+ leaders[0].should == member_26
+ end
+
+ it 'should return the correct information when calling around_me' do
+ rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)
+
+ @leaderboard.total_members.should be(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)
+
+ leaders_around_me = @leaderboard.around_me('member_30')
+ (leaders_around_me.size / 2).should be(@leaderboard.page_size / 2)
+
+ leaders_around_me = @leaderboard.around_me('member_1')
+ leaders_around_me.size.should be(@leaderboard.page_size / 2 + 1)
+
+ leaders_around_me = @leaderboard.around_me('member_76')
+ (leaders_around_me.size / 2).should be(@leaderboard.page_size / 2)
+ end
+
+ it 'should return the correct information when calling ranked_in_list' do
+ rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
+
+ @leaderboard.total_members.should be(Leaderboard::DEFAULT_PAGE_SIZE)
+
+ members = ['member_1', 'member_5', 'member_10']
+ ranked_members = @leaderboard.ranked_in_list(members, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS)
+
+ ranked_members.size.should be(3)
+
+ ranked_members[0][:rank].should be(25)
+ ranked_members[0][:score].should == 1
+
+ ranked_members[1][:rank].should be(21)
+ ranked_members[1][:score].should == 5
+
+ ranked_members[2][:rank].should be(16)
+ ranked_members[2][:score].should == 10
+ end
+
+ it 'should return the correct information when calling ranked_in_list without scores' do
+ rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
+
+ @leaderboard.total_members.should be(Leaderboard::DEFAULT_PAGE_SIZE)
+
+ members = ['member_1', 'member_5', 'member_10']
+ ranked_members = @leaderboard.ranked_in_list(members, {:with_scores => false, :with_rank => true, :use_zero_index_for_rank => false})
+
+ ranked_members.size.should be(3)
+ ranked_members[0][:rank].should be(25)
+ ranked_members[1][:rank].should be(21)
+ ranked_members[2][:rank].should be(16)
+ end
+
+ it 'should allow you to remove members' do
+ rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
+
+ @leaderboard.total_members.should be(Leaderboard::DEFAULT_PAGE_SIZE)
+
+ @leaderboard.remove_member('member_1')
+
+ @leaderboard.total_members.should be(Leaderboard::DEFAULT_PAGE_SIZE - 1)
+ @leaderboard.rank_for('member_1').should be_nil
+ end
+
+ it 'should allow you to change the score for a member' do
+ @leaderboard.rank_member('member_1', 5)
+ @leaderboard.score_for('member_1').should == 5
+
+ @leaderboard.change_score_for('member_1', 5)
+ @leaderboard.score_for('member_1').should == 10
+
+ @leaderboard.change_score_for('member_1', -5)
+ @leaderboard.score_for('member_1').should == 5
+ end
+
+ it 'should allow you to check if a member exists' do
+ @leaderboard.rank_member('member_1', 10)
+
+ @leaderboard.check_member?('member_1').should be_true
+ @leaderboard.check_member?('member_2').should be_false
+ end
+
+ it 'should allow you to change the page size and have that reflected in the size of the result set' do
+ rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
+
+ @leaderboard.page_size = 5
+
+ @leaderboard.total_pages.should be(5)
+ @leaderboard.leaders(1).size.should be(5)
+ end
+
+ it 'should not allow you to set the page size to an invalid page size' do
+ rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
+
+ @leaderboard.page_size = 0
+ @leaderboard.total_pages.should be(1)
+ @leaderboard.leaders(1).size.should be(Leaderboard::DEFAULT_PAGE_SIZE)
+ end
+
+ it 'should return the correct information when calling score_and_rank_for' do
+ rank_members_in_leaderboard
+
+ data = @leaderboard.score_and_rank_for('member_1')
+ data[:member].should == 'member_1'
+ data[:score].should == 1
+ data[:rank].should == 5
+ end
+
+ it 'should allow you to remove members in a given score range' do
+ rank_members_in_leaderboard
+
+ @leaderboard.total_members.should be(5)
+
+ @leaderboard.rank_member('cheater_1', 100)
+ @leaderboard.rank_member('cheater_2', 101)
+ @leaderboard.rank_member('cheater_3', 102)
+
+ @leaderboard.total_members.should be(8)
+
+ @leaderboard.remove_members_in_score_range(100, 102)
+
+ @leaderboard.total_members.should be(5)
+
+ leaders = @leaderboard.leaders(1)
+ leaders.each do |leader|
+ leader[:score].should be < 100
+ end
+ end
+
+ it 'should allow you to merge leaderboards' do
+ foo = Leaderboard.new('foo')
+ bar = Leaderboard.new('bar')
+
+ foo.rank_member('foo_1', 1)
+ foo.rank_member('foo_2', 2)
+ bar.rank_member('bar_1', 3)
+ bar.rank_member('bar_2', 4)
+ bar.rank_member('bar_3', 5)
+
+ foobar_keys = foo.merge_leaderboards('foobar', ['bar'])
+ foobar_keys.should be(5)
+
+ foobar = Leaderboard.new('foobar')
+ foobar.total_members.should be(5)
+
+ first_leader_in_foobar = foobar.leaders(1).first
+ first_leader_in_foobar[:rank].should == 1
+ first_leader_in_foobar[:member].should == 'bar_3'
+ first_leader_in_foobar[:score].should == 5
+
+ foo.disconnect
+ bar.disconnect
+ foobar.disconnect
+ end
+
+ it 'should allow you to intersect leaderboards' do
+ foo = Leaderboard.new('foo')
+ bar = Leaderboard.new('bar')
+
+ foo.rank_member('foo_1', 1)
+ foo.rank_member('foo_2', 2)
+ foo.rank_member('bar_3', 6)
+ bar.rank_member('bar_1', 3)
+ bar.rank_member('foo_1', 4)
+ bar.rank_member('bar_3', 5)
+
+ foobar_keys = foo.intersect_leaderboards('foobar', ['bar'], {:aggregate => :max})
+ foobar_keys.should be(2)
+
+ foobar = Leaderboard.new('foobar')
+ foobar.total_members.should be(2)
+
+ first_leader_in_foobar = foobar.leaders(1).first
+ first_leader_in_foobar[:rank].should == 1
+ first_leader_in_foobar[:member].should == 'bar_3'
+ first_leader_in_foobar[:score].should == 6
+
+ foo.disconnect
+ bar.disconnect
+ foobar.disconnect
+ end
+
+ it 'should respect the with_scores option in the massage_leader_data method' do
+ rank_members_in_leaderboard(25)
+
+ @leaderboard.total_members.should be(25)
+
+ leaders = @leaderboard.leaders(1, {:with_scores => false, :with_rank => false})
+ leaders[0][:member].should_not be_nil
+ leaders[0][:score].should be_nil
+ leaders[0][:rank].should be_nil
+
+ @leaderboard.page_size = 25
+ leaders = @leaderboard.leaders(1, {:with_scores => false, :with_rank => false})
+ leaders.size.should be(25)
+
+ @leaderboard.page_size = Leaderboard::DEFAULT_PAGE_SIZE
+ leaders = @leaderboard.leaders(1, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS)
+ leaders[0][:member].should_not be_nil
+ leaders[0][:score].should_not be_nil
+ leaders[0][:rank].should_not be_nil
+
+ @leaderboard.page_size = 25
+ leaders = @leaderboard.leaders(1, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS)
+ leaders.size.should be(25)
+ end
+
+ it 'should return the correct number of pages when calling total_pages_in page size option' do
+ rank_members_in_leaderboard(25)
+
+ @leaderboard.total_pages_in(@leaderboard.leaderboard_name).should be(1)
+ @leaderboard.total_pages_in(@leaderboard.leaderboard_name, 5).should be(5)
+ end
+
+ it 'should return the correct number of members when calling leaders with a page_size option' do
+ rank_members_in_leaderboard(25)
+
+ @leaderboard.leaders(1, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 5})).size.should be(5)
+ @leaderboard.leaders(1, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 10})).size.should be(10)
+ @leaderboard.leaders(2, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 10})).size.should be(10)
+ @leaderboard.leaders(3, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 10})).size.should be(5)
+ end
+
+ it 'should return the correct number of members when calling around_me with a page_size options' do
+ rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)
+
+ leaders_around_me = @leaderboard.around_me('member_30', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 3}))
+ leaders_around_me.size.should be(3)
+ leaders_around_me[0][:member].should == 'member_31'
+ leaders_around_me[2][:member].should == 'member_29'
+ end
+
+ it 'should return the correct information when calling percentile_for' do
+ rank_members_in_leaderboard(12)
+
+ @leaderboard.percentile_for('member_1').should == 0
+ @leaderboard.percentile_for('member_2').should == 9
+ @leaderboard.percentile_for('member_3').should == 17
+ @leaderboard.percentile_for('member_4').should == 25
+ @leaderboard.percentile_for('member_12').should == 92
+ end
+
+ it 'should not throw an exception when calling around_me with a member not in the leaderboard' do
+ rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)
+
+ leaders_around_me = @leaderboard.around_me('jones', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 3}))
+ leaders_around_me.size.should be(0)
+ end
+
+ it 'should not throw an exception when calling score_and_rank_for with a member not in the leaderboard' do
+ score_and_rank_for_member = @leaderboard.score_and_rank_for('jones')
+
+ score_and_rank_for_member[:member].should == 'jones'
+ score_and_rank_for_member[:score].should == 0.0
+ score_and_rank_for_member[:rank].should be_nil
+ end
+
+ it 'should not throw an exception when calling ranked_in_list with a member not in the leaderboard' do
+ rank_members_in_leaderboard
+
+ members = ['member_1', 'member_5', 'jones']
+ ranked_members = @leaderboard.ranked_in_list(members)
+
+ ranked_members.size.should be(3)
+ ranked_members[2][:rank].should be_nil
+ end
+
+ it 'should not throw an exception when calling percentile_for with a member not in the leaderboard' do
+ percentile = @leaderboard.percentile_for('jones')
+
+ percentile.should be_nil
+ end
+
+ it 'should allow you to change the score for a member not in the leaderboard' do
+ @leaderboard.score_for('jones').should == 0.0
+ @leaderboard.change_score_for('jones', 5)
+ @leaderboard.score_for('jones').should == 5.0
+ end
+
+ it 'should return the correct page when calling page_for a given member in the leaderboard' do
+ @leaderboard.page_for('jones').should be(0)
+
+ rank_members_in_leaderboard(20)
+
+ @leaderboard.page_for('member_17').should be(1)
+ @leaderboard.page_for('member_11').should be(1)
+ @leaderboard.page_for('member_10').should be(1)
+ @leaderboard.page_for('member_1').should be(1)
+
+ @leaderboard.page_for('member_17', 10).should be(1)
+ @leaderboard.page_for('member_11', 10).should be(1)
+ @leaderboard.page_for('member_10', 10).should be(2)
+ @leaderboard.page_for('member_1', 10).should be(2)
+ end
+
+ it 'should allow you to rank multiple members with a variable number of arguments' do
+ @leaderboard.total_members.should be(0)
+ @leaderboard.rank_members('member_1', 1, 'member_10', 10)
+ @leaderboard.total_members.should be(2)
+ @leaderboard.leaders(1).first[:member].should == 'member_10'
+ end
+
+ it 'should allow you to rank multiple members with an array' do
+ @leaderboard.total_members.should be(0)
+ @leaderboard.rank_members(['member_1', 1, 'member_10', 10])
+ @leaderboard.total_members.should be(2)
+ @leaderboard.leaders(1).first[:member].should == 'member_10'
+ end
+end
View
292 spec/reverse_leaderboard_spec.rb
@@ -0,0 +1,292 @@
+require 'spec_helper'
+
+describe 'Leaderboard (reverse option)' do
+ before(:each) do
+ @redis_connection = Redis.new(:host => "127.0.0.1")
+ @leaderboard = Leaderboard.new('name', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:reverse => true}), :host => "127.0.0.1")
+ end
+
+ after(:each) do
+ @redis_connection.flushdb
+ @leaderboard.disconnect
+ @redis_connection.client.disconnect
+ end
+
+ it 'should return the correct rank when calling rank_for' do
+ rank_members_in_leaderboard(5)
+
+ @leaderboard.rank_for('member_4').should be(4)
+ @leaderboard.rank_for('member_4', true).should be(3)
+ end
+
+ it 'should return the correct list when calling leaders' do
+ rank_members_in_leaderboard(25)
+
+ @leaderboard.total_members.should be(25)
+
+ leaders = @leaderboard.leaders(1)
+
+ leaders.size.should be(25)
+ leaders[0][:member].should == 'member_1'
+ leaders[-2][:member].should == 'member_24'
+ leaders[-1][:member].should == 'member_25'
+ leaders[-1][:score].to_i.should be(25)
+ end
+
+ it 'should allow you to retrieve leaders without scores and ranks' do
+ rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
+
+ @leaderboard.total_members.should be(Leaderboard::DEFAULT_PAGE_SIZE)
+ leaders = @leaderboard.leaders(1, {:with_scores => false, :with_rank => false})
+
+ member_1 = {:member => 'member_1'}
+ leaders[0].should == member_1
+
+ member_25 = {:member => 'member_25'}
+ leaders[24].should == member_25
+ end
+
+ it 'should allow you to call leaders with various options that respect the defaults for the options not passed in' do
+ rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE + 1)
+
+ leaders = @leaderboard.leaders(1, :page_size => 1)
+ leaders.size.should be(1)
+
+ leaders = @leaderboard.leaders(1, :with_rank => false)
+ leaders.size.should be(Leaderboard::DEFAULT_PAGE_SIZE)
+ member_1 = {:member => 'member_1', :score => 1}
+ member_2 = {:member => 'member_2', :score => 2}
+ member_3 = {:member => 'member_3', :score => 3}
+ leaders[0].should == member_1
+ leaders[1].should == member_2
+ leaders[2].should == member_3
+
+ leaders = @leaderboard.leaders(1, :with_scores => false)
+ leaders.size.should be(Leaderboard::DEFAULT_PAGE_SIZE)
+ member_1 = {:member => 'member_1', :rank => 1}
+ member_2 = {:member => 'member_2', :rank => 2}
+ leaders[0].should == member_1
+ leaders[1].should == member_2
+
+ leaders = @leaderboard.leaders(1, :with_scores => false, :with_rank => false)
+ leaders.size.should be(Leaderboard::DEFAULT_PAGE_SIZE)
+ member_1 = {:member => 'member_1'}
+ member_2 = {:member => 'member_2'}
+ leaders[0].should == member_1
+ leaders[1].should == member_2
+
+ leaders = @leaderboard.leaders(1, :with_rank => false, :page_size => 1)
+ leaders.size.should be(1)
+ member_1 = {:member => 'member_1', :score => 1}
+ leaders[0].should == member_1
+ end
+
+ it 'should return the correct information when calling around_me' do
+ rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)
+
+ @leaderboard.total_members.should be(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)
+
+ leaders_around_me = @leaderboard.around_me('member_30')
+ (leaders_around_me.size / 2).should be(@leaderboard.page_size / 2)
+
+ leaders_around_me = @leaderboard.around_me('member_76')
+ leaders_around_me.size.should be(@leaderboard.page_size / 2 + 1)
+
+ leaders_around_me = @leaderboard.around_me('member_1')
+ (leaders_around_me.size / 2).should be(@leaderboard.page_size / 2)
+ end
+
+ it 'should return the correct information when calling ranked_in_list' do
+ rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
+
+ @leaderboard.total_members.should be(Leaderboard::DEFAULT_PAGE_SIZE)
+
+ members = ['member_1', 'member_5', 'member_10']
+ ranked_members = @leaderboard.ranked_in_list(members, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS)
+
+ ranked_members.size.should be(3)
+
+ ranked_members[0][:rank].should == 1
+ ranked_members[0][:score].should == 1
+
+ ranked_members[1][:rank].should == 5
+ ranked_members[1][:score].should == 5
+
+ ranked_members[2][:rank].should == 10
+ ranked_members[2][:score].should == 10
+ end
+
+ it 'should return the correct information when calling ranked_in_list without scores' do
+ rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
+
+ @leaderboard.total_members.should be(Leaderboard::DEFAULT_PAGE_SIZE)
+
+ members = ['member_1', 'member_5', 'member_10']
+ ranked_members = @leaderboard.ranked_in_list(members, {:with_scores => false, :with_rank => true, :use_zero_index_for_rank => false})
+
+ ranked_members.size.should be(3)
+
+ ranked_members[0][:rank].should be(1)
+
+ ranked_members[1][:rank].should be(5)
+
+ ranked_members[2][:rank].should be(10)
+ end
+
+ it 'should return the correct information when calling score_and_rank_for' do
+ rank_members_in_leaderboard
+
+ data = @leaderboard.score_and_rank_for('member_1')
+ data[:member].should == 'member_1'
+ data[:score].should == 1
+ data[:rank].should be(1)
+ end
+
+ it 'should allow you to remove members in a given score range' do
+ rank_members_in_leaderboard
+
+ @leaderboard.total_members.should be(5)
+
+ @leaderboard.rank_member('cheater_1', 100)
+ @leaderboard.rank_member('cheater_2', 101)
+ @leaderboard.rank_member('cheater_3', 102)
+
+ @leaderboard.total_members.should be(8)
+
+ @leaderboard.remove_members_in_score_range(100, 102)
+
+ @leaderboard.total_members.should be(5)
+
+ leaders = @leaderboard.leaders(1)
+ leaders.each do |leader|
+ leader[:score].should be < 100
+ end
+ end
+
+ it 'should allow you to merge leaderboards' do
+ foo = Leaderboard.new('foo')
+ bar = Leaderboard.new('bar')
+
+ foo.rank_member('foo_1', 1)
+ foo.rank_member('foo_2', 2)
+ bar.rank_member('bar_1', 3)
+ bar.rank_member('bar_2', 4)
+ bar.rank_member('bar_3', 5)
+
+ foobar_keys = foo.merge_leaderboards('foobar', ['bar'])
+ foobar_keys.should be(5)
+
+ foobar = Leaderboard.new('foobar')
+ foobar.total_members.should be(5)
+
+ first_leader_in_foobar = foobar.leaders(1).first
+ first_leader_in_foobar[:rank].should be(1)
+ first_leader_in_foobar[:member].should == 'bar_3'
+ first_leader_in_foobar[:score].should == 5
+
+ foo.disconnect
+ bar.disconnect
+ foobar.disconnect
+ end
+
+ it 'should allow you to intersect leaderboards' do
+ foo = Leaderboard.new('foo')
+ bar = Leaderboard.new('bar')
+
+ foo.rank_member('foo_1', 1)
+ foo.rank_member('foo_2', 2)
+ foo.rank_member('bar_3', 6)
+ bar.rank_member('bar_1', 3)
+ bar.rank_member('foo_1', 4)
+ bar.rank_member('bar_3', 5)
+
+ foobar_keys = foo.intersect_leaderboards('foobar', ['bar'], {:aggregate => :max})
+ foobar_keys.should be(2)
+
+ foobar = Leaderboard.new('foobar')
+ foobar.total_members.should be(2)
+
+ first_leader_in_foobar = foobar.leaders(1).first
+ first_leader_in_foobar[:rank].should be(1)
+ first_leader_in_foobar[:member].should == 'bar_3'
+ first_leader_in_foobar[:score].should == 6
+
+ foo.disconnect
+ bar.disconnect
+ foobar.disconnect
+ end
+
+ it 'should respect the with_scores option in the massage_leader_data method' do
+ rank_members_in_leaderboard(25)
+
+ @leaderboard.total_members.should be(25)
+
+ leaders = @leaderboard.leaders(1, {:with_scores => false, :with_rank => false})
+ leaders[0][:member].should_not be_nil
+ leaders[0][:score].should be_nil
+ leaders[0][:rank].should be_nil
+
+ @leaderboard.page_size = 25
+ leaders = @leaderboard.leaders(1, {:with_scores => false, :with_rank => false})
+ leaders.size.should be(25)
+
+ @leaderboard.page_size = Leaderboard::DEFAULT_PAGE_SIZE
+ leaders = @leaderboard.leaders(1, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS)
+ leaders[0][:member].should_not be_nil
+ leaders[0][:score].should_not be_nil
+ leaders[0][:rank].should_not be_nil
+
+ @leaderboard.page_size = 25
+ leaders = @leaderboard.leaders(1, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS)
+ leaders.size.should be(25)
+ end
+
+ it 'should return the correct number of members when calling around_me with a page_size options' do
+ rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)
+
+ leaders_around_me = @leaderboard.around_me('member_30', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 3}))
+ leaders_around_me.size.should be(3)
+ leaders_around_me[2][:member].should == 'member_31'
+ leaders_around_me[0][:member].should == 'member_29'
+ end
+
+ it 'should return the correct information when calling percentile_for' do
+ rank_members_in_leaderboard(12)
+
+ @leaderboard.percentile_for('member_1').should == 100
+ @leaderboard.percentile_for('member_2').should == 91
+ @leaderboard.percentile_for('member_3').should == 83
+ @leaderboard.percentile_for('member_4').should == 75
+ @leaderboard.percentile_for('member_12').should == 8
+ end
+
+ it 'should return the correct page when calling page_for a given member in the leaderboard' do
+ @leaderboard.page_for('jones').should be(0)
+
+ rank_members_in_leaderboard(20)
+
+ @leaderboard.page_for('member_17').should be(1)
+ @leaderboard.page_for('member_11').should be(1)
+ @leaderboard.page_for('member_10').should be(1)
+ @leaderboard.page_for('member_1').should be(1)
+
+ @leaderboard.page_for('member_10', 10).should be(1)
+ @leaderboard.page_for('member_1', 10).should be(1)
+ @leaderboard.page_for('member_17', 10).should be(2)
+ @leaderboard.page_for('member_11', 10).should be(2)
+ end
+
+ it 'should allow you to rank multiple members with a variable number of arguments' do
+ @leaderboard.total_members.should be(0)
+ @leaderboard.rank_members('member_1', 1, 'member_10', 10)
+ @leaderboard.total_members.should be(2)
+ @leaderboard.leaders(1).first[:member].should == 'member_1'
+ end
+
+ it 'should allow you to rank multiple members with an array' do
+ @leaderboard.total_members.should be(0)
+ @leaderboard.rank_members(['member_1', 1, 'member_10', 10])
+ @leaderboard.total_members.should be(2)
+ @leaderboard.leaders(1).first[:member].should == 'member_1'
+ end
+end
View
15 test/test_helper.rb → spec/spec_helper.rb
@@ -1,19 +1,12 @@
-require 'rubygems'
-require 'test/unit'
-
+require 'rspec'
require 'leaderboard'
-class LeaderboardTest < Test::Unit::TestCase
-
- def test_version
- assert_equal '2.0.5', Leaderboard::VERSION
- end
+Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
- private
-
+RSpec.configure do |config|
def rank_members_in_leaderboard(members_to_add = 5)
1.upto(members_to_add) do |index|
@leaderboard.rank_member("member_#{index}", index)
end
end
-end
+end
View
2  test/test.conf → spec/test.conf
@@ -1,4 +1,4 @@
-dir ./test/db
+dir ./spec/db
pidfile ./redis.pid
port 6379
timeout 300
View
7 spec/version_spec.rb
@@ -0,0 +1,7 @@
+require 'spec_helper'
+
+describe 'Leaderboard::VERSION' do
+ it 'should be the correct version' do
+ Leaderboard::VERSION.should == '2.0.5'
+ end
+end
View
484 test/test_leaderboard.rb
@@ -1,484 +0,0 @@
-require 'test_helper'
-
-class TestLeaderboard < LeaderboardTest
- def setup
- @redis_connection = Redis.new(:host => "127.0.0.1")
- @leaderboard = Leaderboard.new('name', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1")
- end
-
- def teardown
- @redis_connection.flushdb
- @leaderboard.disconnect
- @redis_connection.client.disconnect
- end
-
- def test_initialize_with_defaults
- assert_equal 'name', @leaderboard.leaderboard_name
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, @leaderboard.page_size
- end
-
- def test_disconnect
- assert_equal nil, @leaderboard.disconnect
- end
-
- def test_will_automatically_reconnect_after_a_disconnect
- assert_equal 0, @leaderboard.total_members
- rank_members_in_leaderboard(5)
- assert_equal 5, @leaderboard.total_members
- assert_equal nil, @leaderboard.disconnect
- assert_equal 5, @leaderboard.total_members
- end
-
- def test_page_size_is_default_page_size_if_set_to_invalid_value
- some_leaderboard = Leaderboard.new('name', {:page_size => 0})
-
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, some_leaderboard.page_size
- some_leaderboard.disconnect
- end
-
- def test_delete_leaderboard
- rank_members_in_leaderboard
-
- assert_equal true, @redis_connection.exists('name')
- @leaderboard.delete_leaderboard
- assert_equal false, @redis_connection.exists('name')
- end
-
- def test_can_pass_existing_redis_connection_to_initializer
- @leaderboard = Leaderboard.new('name', Leaderboard::DEFAULT_OPTIONS, {:redis_connection => @redis_connection})
- rank_members_in_leaderboard
-
- assert_equal 1, @redis_connection.info["connected_clients"].to_i
- end
-
- def test_rank_member_and_total_members
- @leaderboard.rank_member('member', 1)
-
- assert_equal 1, @leaderboard.total_members
- end
-
- def test_total_members_in_score_range
- rank_members_in_leaderboard(5)
-
- assert_equal 3, @leaderboard.total_members_in_score_range(2, 4)
- end
-
- def test_rank_for
- rank_members_in_leaderboard(5)
-
- assert_equal 2, @leaderboard.rank_for('member_4')
- assert_equal 1, @leaderboard.rank_for('member_4', true)
- end
-
- def test_score_for
- rank_members_in_leaderboard(5)
-
- assert_equal 4, @leaderboard.score_for('member_4')
- end
-
- def test_total_pages
- rank_members_in_leaderboard(10)
-
- assert_equal 1, @leaderboard.total_pages
-
- @redis_connection.flushdb
-
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE + 1)
-
- assert_equal 2, @leaderboard.total_pages
- end
-
- def test_leaders
- rank_members_in_leaderboard(25)
-
- assert_equal 25, @leaderboard.total_members
-
- leaders = @leaderboard.leaders(1)
-
- assert_equal 25, leaders.size
- assert_equal 'member_25', leaders[0][:member]
- assert_equal 'member_2', leaders[-2][:member]
- assert_equal 'member_1', leaders[-1][:member]
- assert_equal 1, leaders[-1][:score].to_i
- end
-
- def test_leaders_with_multiple_pages
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)
-
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1, @leaderboard.total_members
-
- leaders = @leaderboard.leaders(1)
- assert_equal @leaderboard.page_size, leaders.size
-
- leaders = @leaderboard.leaders(2)
- assert_equal @leaderboard.page_size, leaders.size
-
- leaders = @leaderboard.leaders(3)
- assert_equal @leaderboard.page_size, leaders.size
-
- leaders = @leaderboard.leaders(4)
- assert_equal 1, leaders.size
-
- leaders = @leaderboard.leaders(-5)
- assert_equal @leaderboard.page_size, leaders.size
-
- leaders = @leaderboard.leaders(10)
- assert_equal 1, leaders.size
- end
-
- def test_leaders_without_retrieving_scores_and_ranks
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
-
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, @leaderboard.total_members
- leaders = @leaderboard.leaders(1, {:with_scores => false, :with_rank => false})
-
- member_25 = {:member => 'member_25'}
- assert_equal member_25, leaders[0]
-
- member_1 = {:member => 'member_1'}
- assert_equal member_1, leaders[24]
- end
-
- def test_leaders_with_only_various_options_should_respect_other_defaults
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE + 1)
-
- leaders = @leaderboard.leaders(1, :page_size => 1)
- assert_equal 1, leaders.size
-
- leaders = @leaderboard.leaders(1, :with_rank => false)
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, leaders.size
- member_26 = {:member => 'member_26', :score => 26}
- member_25 = {:member => 'member_25', :score => 25}
- member_24 = {:member => 'member_24', :score => 24}
- assert_equal member_26, leaders[0]
- assert_equal member_25, leaders[1]
- assert_equal member_24, leaders[2]
-
- leaders = @leaderboard.leaders(1, :with_scores => false)
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, leaders.size
- member_26 = {:member => 'member_26', :rank => 1}
- member_25 = {:member => 'member_25', :rank => 2}
- assert_equal member_26, leaders[0]
- assert_equal member_25, leaders[1]
-
- leaders = @leaderboard.leaders(1, :with_scores => false, :with_rank => false)
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, leaders.size
- member_26 = {:member => 'member_26'}
- member_25 = {:member => 'member_25'}
- assert_equal member_26, leaders[0]
- assert_equal member_25, leaders[1]
-
- leaders = @leaderboard.leaders(1, :with_rank => false, :page_size => 1)
- assert_equal 1, leaders.size
- member_26 = {:member => 'member_26', :score => 26}
- assert_equal member_26, leaders[0]
- end
-
- def test_around_me
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)
-
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1, @leaderboard.total_members
-
- leaders_around_me = @leaderboard.around_me('member_30')
- assert_equal @leaderboard.page_size / 2, leaders_around_me.size / 2
-
- leaders_around_me = @leaderboard.around_me('member_1')
- assert_equal @leaderboard.page_size / 2 + 1, leaders_around_me.size
-
- leaders_around_me = @leaderboard.around_me('member_76')
- assert_equal @leaderboard.page_size / 2, leaders_around_me.size / 2
- end
-
- def test_ranked_in_list
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
-
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, @leaderboard.total_members
-
- members = ['member_1', 'member_5', 'member_10']
- ranked_members = @leaderboard.ranked_in_list(members, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS)
-
- assert_equal 3, ranked_members.size
-
- assert_equal 25, ranked_members[0][:rank]
- assert_equal 1, ranked_members[0][:score]
-
- assert_equal 21, ranked_members[1][:rank]
- assert_equal 5, ranked_members[1][:score]
-
- assert_equal 16, ranked_members[2][:rank]
- assert_equal 10, ranked_members[2][:score]
- end
-
- def test_ranked_in_list_without_scores
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
-
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, @leaderboard.total_members
-
- members = ['member_1', 'member_5', 'member_10']
- ranked_members = @leaderboard.ranked_in_list(members, {:with_scores => false, :with_rank => true, :use_zero_index_for_rank => false})
-
- assert_equal 3, ranked_members.size
-
- assert_equal 25, ranked_members[0][:rank]
-
- assert_equal 21, ranked_members[1][:rank]
-
- assert_equal 16, ranked_members[2][:rank]
- end
-
- def test_remove_member
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
-
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, @leaderboard.total_members
-
- @leaderboard.remove_member('member_1')
-
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE - 1, @leaderboard.total_members
- assert_nil @leaderboard.rank_for('member_1')
- end
-
- def test_change_score_for
- @leaderboard.rank_member('member_1', 5)
- assert_equal 5, @leaderboard.score_for('member_1')
-
- @leaderboard.change_score_for('member_1', 5)
- assert_equal 10, @leaderboard.score_for('member_1')
-
- @leaderboard.change_score_for('member_1', -5)
- assert_equal 5, @leaderboard.score_for('member_1')
- end
-
- def test_check_member
- @leaderboard.rank_member('member_1', 10)
-
- assert_equal true, @leaderboard.check_member?('member_1')
- assert_equal false, @leaderboard.check_member?('member_2')
- end
-
- def test_can_change_page_size_and_have_it_reflected_in_size_of_result_set
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
-
- @leaderboard.page_size = 5
-
- assert_equal 5, @leaderboard.total_pages
- assert_equal 5, @leaderboard.leaders(1).size
- end
-
- def test_cannot_set_page_size_to_invalid_page_size
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
-
- @leaderboard.page_size = 0
- assert_equal 1, @leaderboard.total_pages
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, @leaderboard.leaders(1).size
- end
-
- def test_score_and_rank_for
- rank_members_in_leaderboard
-
- data = @leaderboard.score_and_rank_for('member_1')
- assert_equal 'member_1', data[:member]
- assert_equal 1, data[:score]
- assert_equal 5, data[:rank]
- end
-
- def test_remove_members_in_score_range
- rank_members_in_leaderboard
-
- assert_equal 5, @leaderboard.total_members
-
- @leaderboard.rank_member('cheater_1', 100)
- @leaderboard.rank_member('cheater_2', 101)
- @leaderboard.rank_member('cheater_3', 102)
-
- assert_equal 8, @leaderboard.total_members
-
- @leaderboard.remove_members_in_score_range(100, 102)
-
- assert_equal 5, @leaderboard.total_members
-
- leaders = @leaderboard.leaders(1)
- leaders.each do |leader|
- assert leader[:score] < 100
- end
- end
-
- def test_merge_leaderboards
- foo = Leaderboard.new('foo')
- bar = Leaderboard.new('bar')
-
- foo.rank_member('foo_1', 1)
- foo.rank_member('foo_2', 2)
- bar.rank_member('bar_1', 3)
- bar.rank_member('bar_2', 4)
- bar.rank_member('bar_3', 5)
-
- foobar_keys = foo.merge_leaderboards('foobar', ['bar'])
- assert_equal 5, foobar_keys
-
- foobar = Leaderboard.new('foobar')
- assert_equal 5, foobar.total_members
-
- first_leader_in_foobar = foobar.leaders(1).first
- assert_equal 1, first_leader_in_foobar[:rank]
- assert_equal 'bar_3', first_leader_in_foobar[:member]
- assert_equal 5, first_leader_in_foobar[:score]
-
- foo.disconnect
- bar.disconnect
- foobar.disconnect
- end
-
- def test_intersect_leaderboards
- foo = Leaderboard.new('foo')
- bar = Leaderboard.new('bar')
-
- foo.rank_member('foo_1', 1)
- foo.rank_member('foo_2', 2)
- foo.rank_member('bar_3', 6)
- bar.rank_member('bar_1', 3)
- bar.rank_member('foo_1', 4)
- bar.rank_member('bar_3', 5)
-
- foobar_keys = foo.intersect_leaderboards('foobar', ['bar'], {:aggregate => :max})
- assert_equal 2, foobar_keys
-
- foobar = Leaderboard.new('foobar')
- assert_equal 2, foobar.total_members
-
- first_leader_in_foobar = foobar.leaders(1).first
- assert_equal 1, first_leader_in_foobar[:rank]
- assert_equal 'bar_3', first_leader_in_foobar[:member]
- assert_equal 6, first_leader_in_foobar[:score]
-
- foo.disconnect
- bar.disconnect
- foobar.disconnect
- end
-
- def test_massage_leader_data_respects_with_scores
- rank_members_in_leaderboard(25)
-
- assert_equal 25, @leaderboard.total_members
-
- leaders = @leaderboard.leaders(1, {:with_scores => false, :with_rank => false})
- assert_not_nil leaders[0][:member]
- assert_nil leaders[0][:score]
- assert_nil leaders[0][:rank]
-
- @leaderboard.page_size = 25
- leaders = @leaderboard.leaders(1, {:with_scores => false, :with_rank => false})
- assert_equal 25, leaders.size
-
- @leaderboard.page_size = Leaderboard::DEFAULT_PAGE_SIZE
- leaders = @leaderboard.leaders(1, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS)
- assert_not_nil leaders[0][:member]
- assert_not_nil leaders[0][:score]
- assert_not_nil leaders[0][:rank]
-
- @leaderboard.page_size = 25
- leaders = @leaderboard.leaders(1, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS)
- assert_equal 25, leaders.size
- end
-
- def test_total_pages_in_with_new_page_size
- rank_members_in_leaderboard(25)
-
- assert_equal 1, @leaderboard.total_pages_in(@leaderboard.leaderboard_name)
- assert_equal 5, @leaderboard.total_pages_in(@leaderboard.leaderboard_name, 5)
- end
-
- def test_leaders_call_with_new_page_size
- rank_members_in_leaderboard(25)
-
- assert_equal 5, @leaderboard.leaders(1, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 5})).size
- assert_equal 10, @leaderboard.leaders(1, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 10})).size
- assert_equal 10, @leaderboard.leaders(2, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 10})).size
- assert_equal 5, @leaderboard.leaders(3, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 10})).size
- end
-
- def test_around_me_call_with_new_page_size
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)
-
- leaders_around_me = @leaderboard.around_me('member_30', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 3}))
- assert_equal 3, leaders_around_me.size
- assert_equal 'member_31', leaders_around_me[0][:member]
- assert_equal 'member_29', leaders_around_me[2][:member]
- end
-
- def test_percentile_for
- rank_members_in_leaderboard(12)
-
- assert_equal 0, @leaderboard.percentile_for('member_1')
- assert_equal 9, @leaderboard.percentile_for('member_2')
- assert_equal 17, @leaderboard.percentile_for('member_3')
- assert_equal 25, @leaderboard.percentile_for('member_4')
- assert_equal 92, @leaderboard.percentile_for('member_12')
- end
-
- def test_around_me_for_invalid_member
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)
-
- leaders_around_me = @leaderboard.around_me('jones', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 3}))
- assert_equal 0, leaders_around_me.size
- end
-
- def test_score_and_rank_for_non_existent_member
- score_and_rank_for_member = @leaderboard.score_and_rank_for('jones')
-
- assert_equal 'jones', score_and_rank_for_member[:member]
- assert_equal 0.0, score_and_rank_for_member[:score]
- assert_nil score_and_rank_for_member[:rank]
- end
-
- def test_ranked_in_list_for_non_existent_member
- rank_members_in_leaderboard
-
- members = ['member_1', 'member_5', 'jones']
- ranked_members = @leaderboard.ranked_in_list(members)
-
- assert_equal 3, ranked_members.size
- assert_nil ranked_members[2][:rank]
- end
-
- def test_percentile_for_non_existent_member
- percentile = @leaderboard.percentile_for('jones')
-
- assert_nil percentile
- end
-
- def test_change_score_for_non_existent_member
- assert_equal 0.0, @leaderboard.score_for('jones')
- @leaderboard.change_score_for('jones', 5)
- assert_equal 5.0, @leaderboard.score_for('jones')
- end
-
- def test_page_for
- assert_equal 0, @leaderboard.page_for('jones')
-
- rank_members_in_leaderboard(20)
-
- assert_equal 1, @leaderboard.page_for('member_17')
- assert_equal 1, @leaderboard.page_for('member_11')
- assert_equal 1, @leaderboard.page_for('member_10')
- assert_equal 1, @leaderboard.page_for('member_1')
-
- assert_equal 1, @leaderboard.page_for('member_17', 10)
- assert_equal 1, @leaderboard.page_for('member_11', 10)
- assert_equal 2, @leaderboard.page_for('member_10', 10)
- assert_equal 2, @leaderboard.page_for('member_1', 10)
- end
-
- def test_rank_members
- assert_equal 0, @leaderboard.total_members
- @leaderboard.rank_members('member_1', 1, 'member_10', 10)
- assert_equal 2, @leaderboard.total_members
- assert_equal 'member_10', @leaderboard.leaders(1).first[:member]
- end
-
- def test_rank_members_with_array
- assert_equal 0, @leaderboard.total_members
- @leaderboard.rank_members(['member_1', 1, 'member_10', 10])
- assert_equal 2, @leaderboard.total_members
- assert_equal 'member_10', @leaderboard.leaders(1).first[:member]
- end
-end
View
484 test/test_rev_leaderboard.rb
@@ -1,484 +0,0 @@
-require 'test_helper'
-
-class TestRevLeaderboard < LeaderboardTest
- def setup
- @redis_connection = Redis.new(:host => "127.0.0.1")
- @leaderboard = Leaderboard.new('name', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:reverse => true}), :host => "127.0.0.1")
- end
-
- def teardown
- @redis_connection.flushdb
- @leaderboard.disconnect
- @redis_connection.client.disconnect
- end
-
- def test_initialize_with_defaults
- assert_equal 'name', @leaderboard.leaderboard_name
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, @leaderboard.page_size
- end
-
- def test_disconnect
- assert_equal nil, @leaderboard.disconnect
- end
-
- def test_will_automatically_reconnect_after_a_disconnect
- assert_equal 0, @leaderboard.total_members
- rank_members_in_leaderboard(5)
- assert_equal 5, @leaderboard.total_members
- assert_equal nil, @leaderboard.disconnect
- assert_equal 5, @leaderboard.total_members
- end
-
- def test_page_size_is_default_page_size_if_set_to_invalid_value
- some_leaderboard = Leaderboard.new('name', {:page_size => 0})
-
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, some_leaderboard.page_size
- some_leaderboard.disconnect
- end
-
- def test_delete_leaderboard
- rank_members_in_leaderboard
-
- assert_equal true, @redis_connection.exists('name')
- @leaderboard.delete_leaderboard
- assert_equal false, @redis_connection.exists('name')
- end
-
- def test_can_pass_existing_redis_connection_to_initializer
- @leaderboard = Leaderboard.new('name', Leaderboard::DEFAULT_OPTIONS, {:redis_connection => @redis_connection})
- rank_members_in_leaderboard
-
- assert_equal 1, @redis_connection.info["connected_clients"].to_i
- end
-
- def test_rank_member_and_total_members
- @leaderboard.rank_member('member', 1)
-
- assert_equal 1, @leaderboard.total_members
- end
-
- def test_total_members_in_score_range
- rank_members_in_leaderboard(5)
-
- assert_equal 3, @leaderboard.total_members_in_score_range(2, 4)
- end
-
- def test_rank_for
- rank_members_in_leaderboard(5)
-
- assert_equal 4, @leaderboard.rank_for('member_4')
- assert_equal 3, @leaderboard.rank_for('member_4', true)
- end
-
- def test_score_for
- rank_members_in_leaderboard(5)
-
- assert_equal 4, @leaderboard.score_for('member_4')
- end
-
- def test_total_pages
- rank_members_in_leaderboard(10)
-
- assert_equal 1, @leaderboard.total_pages
-
- @redis_connection.flushdb
-
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE + 1)
-
- assert_equal 2, @leaderboard.total_pages
- end
-
- def test_leaders
- rank_members_in_leaderboard(25)
-
- assert_equal 25, @leaderboard.total_members
-
- leaders = @leaderboard.leaders(1)
-
- assert_equal 25, leaders.size
- assert_equal 'member_1', leaders[0][:member]
- assert_equal 'member_24', leaders[-2][:member]
- assert_equal 'member_25', leaders[-1][:member]
- assert_equal 25, leaders[-1][:score].to_i
- end
-
- def test_leaders_with_multiple_pages
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)
-
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1, @leaderboard.total_members
-
- leaders = @leaderboard.leaders(1)
- assert_equal @leaderboard.page_size, leaders.size
-
- leaders = @leaderboard.leaders(2)
- assert_equal @leaderboard.page_size, leaders.size
-
- leaders = @leaderboard.leaders(3)
- assert_equal @leaderboard.page_size, leaders.size
-
- leaders = @leaderboard.leaders(4)
- assert_equal 1, leaders.size
-
- leaders = @leaderboard.leaders(-5)
- assert_equal @leaderboard.page_size, leaders.size
-
- leaders = @leaderboard.leaders(10)
- assert_equal 1, leaders.size
- end
-
- def test_leaders_without_retrieving_scores_and_ranks
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
-
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, @leaderboard.total_members
- leaders = @leaderboard.leaders(1, {:with_scores => false, :with_rank => false})
-
- member_1 = {:member => 'member_1'}
- assert_equal member_1, leaders[0]
-
- member_25 = {:member => 'member_25'}
- assert_equal member_25, leaders[24]
- end
-
- def test_leaders_with_only_various_options_should_respect_other_defaults
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE + 1)
-
- leaders = @leaderboard.leaders(1, :page_size => 1)
- assert_equal 1, leaders.size
-
- leaders = @leaderboard.leaders(1, :with_rank => false)
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, leaders.size
- member_1 = {:member => 'member_1', :score => 1}
- member_2 = {:member => 'member_2', :score => 2}
- member_3 = {:member => 'member_3', :score => 3}
- assert_equal member_1, leaders[0]
- assert_equal member_2, leaders[1]
- assert_equal member_3, leaders[2]
-
- leaders = @leaderboard.leaders(1, :with_scores => false)
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, leaders.size
- member_1 = {:member => 'member_1', :rank => 1}
- member_2 = {:member => 'member_2', :rank => 2}
- assert_equal member_1, leaders[0]
- assert_equal member_2, leaders[1]
-
- leaders = @leaderboard.leaders(1, :with_scores => false, :with_rank => false)
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, leaders.size
- member_1 = {:member => 'member_1'}
- member_2 = {:member => 'member_2'}
- assert_equal member_1, leaders[0]
- assert_equal member_2, leaders[1]
-
- leaders = @leaderboard.leaders(1, :with_rank => false, :page_size => 1)
- assert_equal 1, leaders.size
- member_1 = {:member => 'member_1', :score => 1}
- assert_equal member_1, leaders[0]
- end
-
- def test_around_me
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)
-
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1, @leaderboard.total_members
-
- leaders_around_me = @leaderboard.around_me('member_30')
- assert_equal @leaderboard.page_size / 2, leaders_around_me.size / 2
-
- leaders_around_me = @leaderboard.around_me('member_76')
- assert_equal @leaderboard.page_size / 2 + 1, leaders_around_me.size
-
- leaders_around_me = @leaderboard.around_me('member_1')
- assert_equal @leaderboard.page_size / 2, leaders_around_me.size / 2
- end
-
- def test_ranked_in_list
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
-
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, @leaderboard.total_members
-
- members = ['member_1', 'member_5', 'member_10']
- ranked_members = @leaderboard.ranked_in_list(members, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS)
-
- assert_equal 3, ranked_members.size
-
- assert_equal 1, ranked_members[0][:rank]
- assert_equal 1, ranked_members[0][:score]
-
- assert_equal 5, ranked_members[1][:rank]
- assert_equal 5, ranked_members[1][:score]
-
- assert_equal 10, ranked_members[2][:rank]
- assert_equal 10, ranked_members[2][:score]
- end
-
- def test_ranked_in_list_without_scores
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
-
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, @leaderboard.total_members
-
- members = ['member_1', 'member_5', 'member_10']
- ranked_members = @leaderboard.ranked_in_list(members, {:with_scores => false, :with_rank => true, :use_zero_index_for_rank => false})
-
- assert_equal 3, ranked_members.size
-
- assert_equal 1, ranked_members[0][:rank]
-
- assert_equal 5, ranked_members[1][:rank]
-
- assert_equal 10, ranked_members[2][:rank]
- end
-
- def test_remove_member
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
-
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, @leaderboard.total_members
-
- @leaderboard.remove_member('member_1')
-
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE - 1, @leaderboard.total_members
- assert_nil @leaderboard.rank_for('member_1')
- end
-
- def test_change_score_for
- @leaderboard.rank_member('member_1', 5)
- assert_equal 5, @leaderboard.score_for('member_1')
-
- @leaderboard.change_score_for('member_1', 5)
- assert_equal 10, @leaderboard.score_for('member_1')
-
- @leaderboard.change_score_for('member_1', -5)
- assert_equal 5, @leaderboard.score_for('member_1')
- end
-
- def test_check_member
- @leaderboard.rank_member('member_1', 10)
-
- assert_equal true, @leaderboard.check_member?('member_1')
- assert_equal false, @leaderboard.check_member?('member_2')
- end
-
- def test_can_change_page_size_and_have_it_reflected_in_size_of_result_set
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
-
- @leaderboard.page_size = 5
-
- assert_equal 5, @leaderboard.total_pages
- assert_equal 5, @leaderboard.leaders(1).size
- end
-
- def test_cannot_set_page_size_to_invalid_page_size
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
-
- @leaderboard.page_size = 0
- assert_equal 1, @leaderboard.total_pages
- assert_equal Leaderboard::DEFAULT_PAGE_SIZE, @leaderboard.leaders(1).size
- end
-
- def test_score_and_rank_for
- rank_members_in_leaderboard
-
- data = @leaderboard.score_and_rank_for('member_1')
- assert_equal 'member_1', data[:member]
- assert_equal 1, data[:score]
- assert_equal 1, data[:rank]
- end
-
- def test_remove_members_in_score_range
- rank_members_in_leaderboard
-
- assert_equal 5, @leaderboard.total_members
-
- @leaderboard.rank_member('cheater_1', 100)
- @leaderboard.rank_member('cheater_2', 101)
- @leaderboard.rank_member('cheater_3', 102)
-
- assert_equal 8, @leaderboard.total_members
-
- @leaderboard.remove_members_in_score_range(100, 102)
-
- assert_equal 5, @leaderboard.total_members
-
- leaders = @leaderboard.leaders(1)
- leaders.each do |leader|
- assert leader[:score] < 100
- end
- end
-
- def test_merge_leaderboards
- foo = Leaderboard.new('foo')
- bar = Leaderboard.new('bar')
-
- foo.rank_member('foo_1', 1)
- foo.rank_member('foo_2', 2)
- bar.rank_member('bar_1', 3)
- bar.rank_member('bar_2', 4)
- bar.rank_member('bar_3', 5)
-
- foobar_keys = foo.merge_leaderboards('foobar', ['bar'])
- assert_equal 5, foobar_keys
-
- foobar = Leaderboard.new('foobar')
- assert_equal 5, foobar.total_members
-
- first_leader_in_foobar = foobar.leaders(1).first
- assert_equal 1, first_leader_in_foobar[:rank]
- assert_equal 'bar_3', first_leader_in_foobar[:member]
- assert_equal 5, first_leader_in_foobar[:score]
-
- foo.disconnect
- bar.disconnect
- foobar.disconnect
- end
-
- def test_intersect_leaderboards
- foo = Leaderboard.new('foo')
- bar = Leaderboard.new('bar')
-
- foo.rank_member('foo_1', 1)
- foo.rank_member('foo_2', 2)
- foo.rank_member('bar_3', 6)
- bar.rank_member('bar_1', 3)
- bar.rank_member('foo_1', 4)
- bar.rank_member('bar_3', 5)
-
- foobar_keys = foo.intersect_leaderboards('foobar', ['bar'], {:aggregate => :max})
- assert_equal 2, foobar_keys
-
- foobar = Leaderboard.new('foobar')
- assert_equal 2, foobar.total_members
-
- first_leader_in_foobar = foobar.leaders(1).first
- assert_equal 1, first_leader_in_foobar[:rank]
- assert_equal 'bar_3', first_leader_in_foobar[:member]
- assert_equal 6, first_leader_in_foobar[:score]
-
- foo.disconnect
- bar.disconnect
- foobar.disconnect
- end
-
- def test_massage_leader_data_respects_with_scores
- rank_members_in_leaderboard(25)
-
- assert_equal 25, @leaderboard.total_members
-
- leaders = @leaderboard.leaders(1, {:with_scores => false, :with_rank => false})
- assert_not_nil leaders[0][:member]
- assert_nil leaders[0][:score]
- assert_nil leaders[0][:rank]
-
- @leaderboard.page_size = 25
- leaders = @leaderboard.leaders(1, {:with_scores => false, :with_rank => false})
- assert_equal 25, leaders.size
-
- @leaderboard.page_size = Leaderboard::DEFAULT_PAGE_SIZE
- leaders = @leaderboard.leaders(1, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS)
- assert_not_nil leaders[0][:member]
- assert_not_nil leaders[0][:score]
- assert_not_nil leaders[0][:rank]
-
- @leaderboard.page_size = 25
- leaders = @leaderboard.leaders(1, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS)
- assert_equal 25, leaders.size
- end
-
- def test_total_pages_in_with_new_page_size
- rank_members_in_leaderboard(25)
-
- assert_equal 1, @leaderboard.total_pages_in(@leaderboard.leaderboard_name)
- assert_equal 5, @leaderboard.total_pages_in(@leaderboard.leaderboard_name, 5)
- end
-
- def test_leaders_call_with_new_page_size
- rank_members_in_leaderboard(25)
-
- assert_equal 5, @leaderboard.leaders(1, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 5})).size
- assert_equal 10, @leaderboard.leaders(1, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 10})).size
- assert_equal 10, @leaderboard.leaders(2, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 10})).size
- assert_equal 5, @leaderboard.leaders(3, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 10})).size
- end
-
- def test_around_me_call_with_new_page_size
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)
-
- leaders_around_me = @leaderboard.around_me('member_30', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 3}))
- assert_equal 3, leaders_around_me.size
- assert_equal 'member_31', leaders_around_me[2][:member]
- assert_equal 'member_29', leaders_around_me[0][:member]
- end
-
- def test_percentile_for
- rank_members_in_leaderboard(12)
-
- assert_equal 100, @leaderboard.percentile_for('member_1')
- assert_equal 91, @leaderboard.percentile_for('member_2')
- assert_equal 83, @leaderboard.percentile_for('member_3')
- assert_equal 75, @leaderboard.percentile_for('member_4')
- assert_equal 8, @leaderboard.percentile_for('member_12')
- end
-
- def test_around_me_for_invalid_member
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)
-
- leaders_around_me = @leaderboard.around_me('jones', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 3}))
- assert_equal 0, leaders_around_me.size
- end
-
- def test_score_and_rank_for_non_existent_member
- score_and_rank_for_member = @leaderboard.score_and_rank_for('jones')
-
- assert_equal 'jones', score_and_rank_for_member[:member]
- assert_equal 0.0, score_and_rank_for_member[:score]
- assert_nil score_and_rank_for_member[:rank]
- end
-
- def test_ranked_in_list_for_non_existent_member
- rank_members_in_leaderboard
-
- members = ['member_1', 'member_5', 'jones']
- ranked_members = @leaderboard.ranked_in_list(members)
-
- assert_equal 3, ranked_members.size
- assert_nil ranked_members[2][:rank]
- end
-
- def test_percentile_for_non_existent_member
- percentile = @leaderboard.percentile_for('jones')
-
- assert_nil percentile
- end
-
- def test_change_score_for_non_existent_member
- assert_equal 0.0, @leaderboard.score_for('jones')
- @leaderboard.change_score_for('jones', 5)
- assert_equal 5.0, @leaderboard.score_for('jones')
- end
-
- def test_page_for
- assert_equal 0, @leaderboard.page_for('jones')
-
- rank_members_in_leaderboard(20)
-
- assert_equal 1, @leaderboard.page_for('member_17')
- assert_equal 1, @leaderboard.page_for('member_11')
- assert_equal 1, @leaderboard.page_for('member_10')
- assert_equal 1, @leaderboard.page_for('member_1')
-
- assert_equal 1, @leaderboard.page_for('member_10', 10)
- assert_equal 1, @leaderboard.page_for('member_1', 10)
- assert_equal 2, @leaderboard.page_for('member_17', 10)
- assert_equal 2, @leaderboard.page_for('member_11', 10)
- end
-
- def test_rank_members
- assert_equal 0, @leaderboard.total_members
- @leaderboard.rank_members('member_1', 1, 'member_10', 10)
- assert_equal 2, @leaderboard.total_members
- assert_equal 'member_1', @leaderboard.leaders(1).first[:member]
- end
-
- def test_rank_members_with_array
- assert_equal 0, @leaderboard.total_members
- @leaderboard.rank_members(['member_1', 1, 'member_10', 10])
- assert_equal 2, @leaderboard.total_members
- assert_equal 'member_1', @leaderboard.leaders(1).first[:member]
- end
-end

No commit comments for this range

Something went wrong with that request. Please try again.