When updating article scores, use new score for blackbox (#13009)

Fixes #13008

Add a test case for blackbox stability

This failed as expected on master and passed on this branch, when a
reaction (which would change the article's calculated score during
update) is added, the hotness should be stable, and not modified
during the second call to `#update_score`.
This commit is contained in:
Daniel Uber 2021-03-18 14:20:27 -05:00 committed by GitHub
parent fdf91eb5f2
commit d6b4e83190
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 2 deletions

View file

@ -375,8 +375,8 @@ class Article < ApplicationRecord
end
def update_score
new_score = reactions.sum(:points) + Reaction.where(reactable_id: user_id, reactable_type: "User").sum(:points)
update_columns(score: new_score,
self.score = reactions.sum(:points) + Reaction.where(reactable_id: user_id, reactable_type: "User").sum(:points)
update_columns(score: score,
comment_score: comments.sum(:score),
hotness_score: BlackBox.article_hotness_score(self),
spaminess_rating: BlackBox.calculate_spaminess(self))

View file

@ -1008,4 +1008,13 @@ RSpec.describe Article, type: :model do
expect(article.plain_html).not_to include("highlight__panel")
end
end
describe "#update_score" do
it "stably sets the correct blackbox values" do
create(:reaction, reactable: article, points: 1)
article.update_score
expect { article.update_score }.not_to change { article.reload.hotness_score }
end
end
end