Replace the usage of reorder with RANDOM() posgtresql function, for all & sample

This commit is contained in:
Bertocq
2017-11-09 12:57:16 +01:00
parent 607423c1a8
commit 5a188474c5

View File

@@ -153,7 +153,7 @@ section "Creating Users" do
level = [1, 2, 3].sample
if level >= 2
user.update(residence_verified_at: Time.current, confirmed_phone: Faker::PhoneNumber.phone_number,
document_number: unique_document_number, document_type: "1", geozone: Geozone.reorder("RANDOM()").first)
document_number: unique_document_number, document_type: "1", geozone: Geozone.all.sample)
end
if level == 3
user.update(verified_at: Time.current, document_number: unique_document_number)
@@ -182,27 +182,27 @@ end
section "Creating Debates" do
tags = Faker::Lorem.words(25)
30.times do
author = User.reorder("RANDOM()").first
author = User.all.sample
description = "<p>#{Faker::Lorem.paragraphs.join('</p><p>')}</p>"
debate = Debate.create!(author: author,
title: Faker::Lorem.sentence(3).truncate(60),
created_at: rand((Time.current - 1.week)..Time.current),
description: description,
tag_list: tags.sample(3).join(','),
geozone: Geozone.reorder("RANDOM()").first,
geozone: Geozone.all.sample,
terms_of_service: "1")
end
tags = ActsAsTaggableOn::Tag.where(kind: 'category')
30.times do
author = User.reorder("RANDOM()").first
author = User.all.sample
description = "<p>#{Faker::Lorem.paragraphs.join('</p><p>')}</p>"
debate = Debate.create!(author: author,
title: Faker::Lorem.sentence(3).truncate(60),
created_at: rand((Time.current - 1.week)..Time.current),
description: description,
tag_list: tags.sample(3).join(','),
geozone: Geozone.reorder("RANDOM()").first,
geozone: Geozone.all.sample,
terms_of_service: "1")
end
end
@@ -210,7 +210,7 @@ end
section "Creating Proposals" do
tags = Faker::Lorem.words(25)
30.times do
author = User.reorder("RANDOM()").first
author = User.all.sample
description = "<p>#{Faker::Lorem.paragraphs.join('</p><p>')}</p>"
proposal = Proposal.create!(author: author,
title: Faker::Lorem.sentence(3).truncate(60),
@@ -221,7 +221,7 @@ section "Creating Proposals" do
description: description,
created_at: rand((Time.current - 1.week)..Time.current),
tag_list: tags.sample(3).join(','),
geozone: Geozone.reorder("RANDOM()").first,
geozone: Geozone.all.sample,
terms_of_service: "1")
end
end
@@ -229,7 +229,7 @@ end
section "Creating Archived Proposals" do
tags = Faker::Lorem.words(25)
5.times do
author = User.reorder("RANDOM()").first
author = User.all.sample
description = "<p>#{Faker::Lorem.paragraphs.join('</p><p>')}</p>"
proposal = Proposal.create!(author: author,
title: Faker::Lorem.sentence(3).truncate(60),
@@ -239,7 +239,7 @@ section "Creating Archived Proposals" do
external_url: Faker::Internet.url,
description: description,
tag_list: tags.sample(3).join(','),
geozone: Geozone.reorder("RANDOM()").first,
geozone: Geozone.all.sample,
terms_of_service: "1",
created_at: Setting["months_to_archive_proposals"].to_i.months.ago)
end
@@ -248,7 +248,7 @@ end
section "Creating Successful Proposals" do
tags = Faker::Lorem.words(25)
10.times do
author = User.reorder("RANDOM()").first
author = User.all.sample
description = "<p>#{Faker::Lorem.paragraphs.join('</p><p>')}</p>"
proposal = Proposal.create!(author: author,
title: Faker::Lorem.sentence(3).truncate(60),
@@ -259,14 +259,14 @@ section "Creating Successful Proposals" do
description: description,
created_at: rand((Time.current - 1.week)..Time.current),
tag_list: tags.sample(3).join(','),
geozone: Geozone.reorder("RANDOM()").first,
geozone: Geozone.all.sample,
terms_of_service: "1",
cached_votes_up: Setting["votes_for_proposal_success"])
end
tags = ActsAsTaggableOn::Tag.where(kind: 'category')
30.times do
author = User.reorder("RANDOM()").first
author = User.all.sample
description = "<p>#{Faker::Lorem.paragraphs.join('</p><p>')}</p>"
proposal = Proposal.create!(author: author,
title: Faker::Lorem.sentence(3).truncate(60),
@@ -277,15 +277,15 @@ section "Creating Successful Proposals" do
description: description,
created_at: rand((Time.current - 1.week)..Time.current),
tag_list: tags.sample(3).join(','),
geozone: Geozone.reorder("RANDOM()").first,
geozone: Geozone.all.sample,
terms_of_service: "1")
end
end
section "Commenting Debates" do
100.times do
author = User.reorder("RANDOM()").first
debate = Debate.reorder("RANDOM()").first
author = User.all.sample
debate = Debate.all.sample
Comment.create!(user: author,
created_at: rand(debate.created_at..Time.current),
commentable: debate,
@@ -295,8 +295,8 @@ end
section "Commenting Proposals" do
100.times do
author = User.reorder("RANDOM()").first
proposal = Proposal.reorder("RANDOM()").first
author = User.all.sample
proposal = Proposal.all.sample
Comment.create!(user: author,
created_at: rand(proposal.created_at..Time.current),
commentable: proposal,
@@ -306,8 +306,8 @@ end
section "Commenting Comments" do
200.times do
author = User.reorder("RANDOM()").first
parent = Comment.reorder("RANDOM()").first
author = User.all.sample
parent = Comment.all.sample
Comment.create!(user: author,
created_at: rand(parent.created_at..Time.current),
commentable_id: parent.commentable_id,
@@ -320,42 +320,42 @@ end
section "Voting Debates, Proposals & Comments" do
not_org_users = User.where(['users.id NOT IN(?)', User.organizations.pluck(:id)])
100.times do
voter = not_org_users.level_two_or_three_verified.reorder("RANDOM()").first
voter = not_org_users.level_two_or_three_verified.all.sample
vote = [true, false].sample
debate = Debate.reorder("RANDOM()").first
debate = Debate.all.sample
debate.vote_by(voter: voter, vote: vote)
end
100.times do
voter = not_org_users.reorder("RANDOM()").first
voter = not_org_users.all.sample
vote = [true, false].sample
comment = Comment.reorder("RANDOM()").first
comment = Comment.all.sample
comment.vote_by(voter: voter, vote: vote)
end
100.times do
voter = not_org_users.level_two_or_three_verified.reorder("RANDOM()").first
proposal = Proposal.reorder("RANDOM()").first
voter = not_org_users.level_two_or_three_verified.all.sample
proposal = Proposal.all.sample
proposal.vote_by(voter: voter, vote: true)
end
end
section "Flagging Debates & Comments" do
40.times do
debate = Debate.reorder("RANDOM()").first
flagger = User.where(["users.id <> ?", debate.author_id]).reorder("RANDOM()").first
debate = Debate.all.sample
flagger = User.where(["users.id <> ?", debate.author_id]).all.sample
Flag.flag(flagger, debate)
end
40.times do
comment = Comment.reorder("RANDOM()").first
flagger = User.where(["users.id <> ?", comment.user_id]).reorder("RANDOM()").first
comment = Comment.all.sample
flagger = User.where(["users.id <> ?", comment.user_id]).all.sample
Flag.flag(flagger, comment)
end
40.times do
proposal = Proposal.reorder("RANDOM()").first
flagger = User.where(["users.id <> ?", proposal.author_id]).reorder("RANDOM()").first
proposal = Proposal.all.sample
flagger = User.where(["users.id <> ?", proposal.author_id]).all.sample
Flag.flag(flagger, proposal)
end
end
@@ -363,8 +363,8 @@ end
section "Creating Spending Proposals" do
tags = Faker::Lorem.words(10)
60.times do
geozone = Geozone.reorder("RANDOM()").first
author = User.reorder("RANDOM()").first
geozone = Geozone.all.sample
author = User.all.sample
description = "<p>#{Faker::Lorem.paragraphs.join('</p><p>')}</p>"
feasible_explanation = "<p>#{Faker::Lorem.paragraphs.join('</p><p>')}</p>"
valuation_finished = [true, false].sample
@@ -386,7 +386,7 @@ end
section "Creating Valuation Assignments" do
(1..17).to_a.sample.times do
SpendingProposal.reorder("RANDOM()").first.valuators << Valuator.first
SpendingProposal.all.sample.valuators << Valuator.first
end
end
@@ -420,10 +420,10 @@ end
section "Creating Investments" do
tags = Faker::Lorem.words(10)
100.times do
heading = Budget::Heading.reorder("RANDOM()").first
heading = Budget::Heading.all.sample
investment = Budget::Investment.create!(
author: User.reorder("RANDOM()").first,
author: User.all.sample,
heading: heading,
group: heading.group,
budget: heading.group.budget,
@@ -450,9 +450,9 @@ end
section "Winner Investments" do
budget = Budget.where(phase: "finished").last
100.times do
heading = budget.headings.reorder("RANDOM()").first
heading = budget.headings.all.sample
investment = Budget::Investment.create!(
author: User.reorder("RANDOM()").first,
author: User.all.sample,
heading: heading,
group: heading.group,
budget: heading.group.budget,
@@ -474,7 +474,7 @@ end
section "Creating Valuation Assignments" do
(1..50).to_a.sample.times do
Budget::Investment.reorder("RANDOM()").first.valuators << Valuator.first
Budget::Investment.all.sample.valuators << Valuator.first
end
end
@@ -517,8 +517,8 @@ section "Creating proposal notifications" do
100.times do |i|
ProposalNotification.create!(title: "Proposal notification title #{i}",
body: "Proposal notification body #{i}",
author: User.reorder("RANDOM()").first,
proposal: Proposal.reorder("RANDOM()").first)
author: User.all.sample,
proposal: Proposal.all.sample)
end
end
@@ -624,7 +624,7 @@ end
section "Creating Poll Questions from Proposals" do
3.times do
proposal = Proposal.reorder("RANDOM()").first
proposal = Proposal.all.sample
poll = Poll.current.first
question = Poll::Question.create(poll: poll)
Faker::Lorem.words((2..4).to_a.sample).each do |answer|
@@ -637,7 +637,7 @@ end
section "Creating Successful Proposals" do
10.times do
proposal = Proposal.reorder("RANDOM()").first
proposal = Proposal.all.sample
poll = Poll.current.first
question = Poll::Question.create(poll: poll)
Faker::Lorem.words((2..4).to_a.sample).each do |answer|
@@ -650,8 +650,8 @@ end
section "Commenting Poll Questions" do
30.times do
author = User.reorder("RANDOM()").first
question = Poll::Question.reorder("RANDOM()").first
author = User.all.sample
question = Poll::Question.all.sample
Comment.create!(user: author,
created_at: rand(question.created_at..Time.current),
commentable: question,