Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the flaky seeds for comments #13013

Merged
merged 7 commits into from
Jul 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions decidim-comments/app/models/decidim/comments/seed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ def comments_for(resource)

next if [true, false].sample

[comment1, comment2].compact.each do |comment|
create_votes(comment)
end
create_votes(comment1) if comment1
create_votes(comment2) if comment2
end
end

Expand All @@ -50,8 +49,8 @@ def comments_for(resource)
#
# @return [Decidim::Comments::Comment]
def create_comment(resource, root_commentable = nil)
author = user
user_group = user_group(author)
author = random_user
user_group = random_user_group(author)

params = {
commentable: resource,
Expand Down Expand Up @@ -79,25 +78,30 @@ def create_comment(resource, root_commentable = nil)
# @return nil
def create_votes(comment)
rand(0..12).times do
author = user
user_group = user_group(author)

CommentVote.find_or_create_by(
comment:,
author: [author, user_group].compact.sample,
weight: [1, -1].sample
)
user = random_user
user_group = random_user_group(user)
author = [user, user_group].compact.sample
next if CommentVote.where(comment:, author:).any?

CommentVote.create!(comment:, author:, weight: [1, -1].sample)
end

nil
rescue ActiveRecord::AssociationTypeMismatch
nil # in case there is a mismatch, we ignore the error as it is not important for the seeding
end

def user
Decidim::User.where(organization:).all.sample
def random_user
user = Decidim::User.where(organization:).not_deleted.not_blocked.confirmed.sample

user.valid? ? user : random_user
end

def user_group(user)
[true, false].sample ? Decidim::UserGroups::ManageableUserGroups.for(user).verified.sample : nil
def random_user_group(user)
user_group = Decidim::UserGroups::ManageableUserGroups.for(user).verified.sample
return nil unless user_group&.valid?

[true, false].sample ? user_group : nil
end
end
end
Expand Down
Loading