Convert random seed to a small value

We are trying out a modulus function to return investments in random
order https://github.com/consul/consul/pull/2131

However we ran into the gotcha of having a seed value too big for the
modulus function to work as expected

If the seed is bigger than the investment id, the records are returned
ordered by id

By dividing the seed by a big number, this problem seems to get fixed
This commit is contained in:
rgarcia
2018-03-01 22:54:49 +01:00
parent 59805e9b73
commit ce3cb045f8
2 changed files with 10 additions and 1 deletions

View File

@@ -112,7 +112,8 @@ module Budgets
def set_random_seed
if params[:order] == 'random' || params[:order].blank?
seed = params[:random_seed] || session[:random_seed] || rand(-100000..100000)
params[:random_seed] ||= Float(seed) rescue 0
params[:random_seed] = Float(seed) / 1000000 rescue 0
session[:random_seed] = params[:random_seed]
else
params[:random_seed] = nil
end

View File

@@ -610,7 +610,15 @@ feature 'Budget Investments' do
end
expect(@first_user_investments_order).to eq(@second_user_investments_order)
scenario "Convert seed to a value small enough for the modulus function to return investments in random order", :focus do
12.times { |i| create(:budget_investment, heading: heading, id: i) }
visit budget_investments_path(budget, heading_id: heading.id, random_seed: '12')
order = investments_order
orderd_by_id = Budget::Investment.order(:id).limit(10).pluck(:title)
expect(order).to_not eq(orderd_by_id)
end
def investments_order