2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe TopTopic do
|
2016-01-08 05:53:52 -05:00
|
|
|
describe "#sorted_periods" do
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when verifying enum sequence" do
|
2016-01-08 05:53:52 -05:00
|
|
|
before { @sorted_periods = TopTopic.sorted_periods }
|
|
|
|
|
|
|
|
it "'daily' should be at 1st position" do
|
|
|
|
expect(@sorted_periods[:daily]).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "'all' should be at 6th position" do
|
|
|
|
expect(@sorted_periods[:all]).to eq(6)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
it { is_expected.to belong_to :topic }
|
2013-12-23 18:50:36 -05:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe ".refresh!" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:t1) { Fabricate(:topic) }
|
|
|
|
fab!(:t2) { Fabricate(:topic) }
|
2013-12-23 18:50:36 -05:00
|
|
|
|
|
|
|
it "begins blank" do
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(TopTopic.all).to be_blank
|
2013-12-23 18:50:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context "after calculating" do
|
|
|
|
before { TopTopic.refresh! }
|
|
|
|
|
|
|
|
it "should have top topics" do
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(TopTopic.pluck(:topic_id)).to match_array([t1.id, t2.id])
|
2013-12-23 18:50:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-20 11:36:18 -05:00
|
|
|
describe "#compute_top_score_for" do
|
2023-11-09 17:47:59 -05:00
|
|
|
fab!(:user)
|
|
|
|
fab!(:coding_horror)
|
2016-01-20 11:36:18 -05:00
|
|
|
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:topic_1) { Fabricate(:topic, posts_count: 10, like_count: 28) }
|
|
|
|
fab!(:t1_post_1) { Fabricate(:post, topic: topic_1, like_count: 28, post_number: 1) }
|
2016-01-20 11:36:18 -05:00
|
|
|
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:topic_2) { Fabricate(:topic, posts_count: 10, like_count: 20) }
|
|
|
|
fab!(:t2_post_1) { Fabricate(:post, topic: topic_2, like_count: 10, post_number: 1) }
|
|
|
|
fab!(:t2_post_2) { Fabricate(:post, topic: topic_2, like_count: 10) }
|
2016-01-20 11:36:18 -05:00
|
|
|
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:topic_3) { Fabricate(:topic, posts_count: 10) }
|
|
|
|
fab!(:t3_post_1) { Fabricate(:post, topic_id: topic_3.id) }
|
2016-01-20 11:36:18 -05:00
|
|
|
let!(:t3_view_1) { TopicViewItem.add(topic_3.id, "127.0.0.1", user) }
|
|
|
|
let!(:t3_view_2) { TopicViewItem.add(topic_3.id, "127.0.0.2", coding_horror) }
|
|
|
|
|
|
|
|
# Note: all topics has 10 posts so we can skip "0 - ((10 - topics.posts_count) / 20) * #{period}_op_likes_count" calculation
|
|
|
|
|
|
|
|
it "should compute top score" do
|
|
|
|
# Default Formula: log(views_count) * {2} + op_likes_count * {0.5} + LEAST(likes_count / posts_count, {3}) + 10 + log(posts_count)
|
|
|
|
#
|
|
|
|
# topic_1 => 0 + 14 + 3 + 10 + 0 => 27
|
|
|
|
# topic_2 => 0 + 5 + 3 + 10 + 0.301029995664 => 18.301029995664
|
|
|
|
# topic_3 => 0.602059991328 + 0 + 0 + 10 + 0 => 10.602059991328
|
|
|
|
|
|
|
|
TopTopic.refresh!
|
|
|
|
top_topics = TopTopic.all
|
|
|
|
|
2023-02-12 23:39:45 -05:00
|
|
|
expect(top_topics.where(topic_id: topic_1.id).pick(:yearly_score)).to eq(27)
|
|
|
|
expect(top_topics.where(topic_id: topic_2.id).pick(:yearly_score)).to be_within(
|
2019-10-21 06:32:27 -04:00
|
|
|
0.0000000001,
|
|
|
|
).of(18.301029995664)
|
2023-02-12 23:39:45 -05:00
|
|
|
expect(top_topics.where(topic_id: topic_3.id).pick(:yearly_score)).to be_within(
|
2019-10-21 06:32:27 -04:00
|
|
|
0.0000000001,
|
|
|
|
).of(10.602059991328)
|
2016-01-20 11:36:18 -05:00
|
|
|
|
|
|
|
# when 'top_topics_formula_log_views_multiplier' setting is changed
|
|
|
|
SiteSetting.top_topics_formula_log_views_multiplier = 4
|
|
|
|
SiteSetting.top_topics_formula_first_post_likes_multiplier = 0.5 # unchanged
|
|
|
|
SiteSetting.top_topics_formula_least_likes_per_post_multiplier = 3 # unchanged
|
|
|
|
|
|
|
|
# New Formula: log(views_count) * {4} + op_likes_count * {0.5} + LEAST(likes_count / posts_count, {3}) + 10 + log(posts_count)
|
|
|
|
#
|
|
|
|
# topic_1 => 0 + 14 + 3 + 10 + 0 => 27
|
|
|
|
# topic_2 => 0 + 5 + 3 + 10 + 0.301029995664 => 18.301029995664
|
|
|
|
# topic_3 => 1.2041199826559 + 0 + 0 + 10 + 0 => 11.2041199826559
|
|
|
|
|
|
|
|
TopTopic.refresh!
|
|
|
|
top_topics = TopTopic.all
|
|
|
|
|
2023-02-12 23:39:45 -05:00
|
|
|
expect(top_topics.where(topic_id: topic_1.id).pick(:yearly_score)).to eq(27)
|
|
|
|
expect(top_topics.where(topic_id: topic_2.id).pick(:yearly_score)).to be_within(
|
2019-11-26 00:39:14 -05:00
|
|
|
0.0000000001,
|
|
|
|
).of(18.301029995664)
|
2023-02-12 23:39:45 -05:00
|
|
|
expect(top_topics.where(topic_id: topic_3.id).pick(:yearly_score)).to be_within(
|
2019-11-26 00:39:14 -05:00
|
|
|
0.0000000001,
|
|
|
|
).of(11.2041199826559)
|
2016-01-20 11:36:18 -05:00
|
|
|
|
|
|
|
# when 'top_topics_formula_first_post_likes_multiplier' setting is changed
|
|
|
|
SiteSetting.top_topics_formula_log_views_multiplier = 2 # unchanged
|
|
|
|
SiteSetting.top_topics_formula_first_post_likes_multiplier = 2
|
|
|
|
SiteSetting.top_topics_formula_least_likes_per_post_multiplier = 3 # unchanged
|
|
|
|
|
|
|
|
# New Formula: log(views_count) * {2} + op_likes_count * {2} + LEAST(likes_count / posts_count, {3}) + 10 + log(posts_count)
|
|
|
|
#
|
|
|
|
# topic_1 => 0 + 56 + 3 + 10 + 0 => 69
|
|
|
|
# topic_2 => 0 + 20 + 3 + 10 + 0.301029995664 => 33.301029995664
|
|
|
|
# topic_3 => 0.602059991328 + 0 + 0 + 10 + 0 => 10.602059991328
|
|
|
|
|
|
|
|
TopTopic.refresh!
|
|
|
|
top_topics = TopTopic.all
|
|
|
|
|
2023-02-12 23:39:45 -05:00
|
|
|
expect(top_topics.where(topic_id: topic_1.id).pick(:yearly_score)).to eq(69)
|
|
|
|
expect(top_topics.where(topic_id: topic_2.id).pick(:yearly_score)).to be_within(
|
2019-11-26 00:39:14 -05:00
|
|
|
0.0000000001,
|
|
|
|
).of(33.301029995664)
|
2023-02-12 23:39:45 -05:00
|
|
|
expect(top_topics.where(topic_id: topic_3.id).pick(:yearly_score)).to be_within(
|
2019-11-26 00:39:14 -05:00
|
|
|
0.0000000001,
|
|
|
|
).of(10.602059991328)
|
2016-01-20 11:36:18 -05:00
|
|
|
|
|
|
|
# when 'top_topics_formula_least_likes_per_post_multiplier' setting is changed
|
|
|
|
SiteSetting.top_topics_formula_log_views_multiplier = 2 # unchanged
|
|
|
|
SiteSetting.top_topics_formula_first_post_likes_multiplier = 0.5 # unchanged
|
|
|
|
SiteSetting.top_topics_formula_least_likes_per_post_multiplier = 6
|
|
|
|
|
|
|
|
# New Formula: log(views_count) * {2} + op_likes_count * {0.5} + LEAST(likes_count / posts_count, {6}) + 10 + log(posts_count)
|
|
|
|
#
|
|
|
|
# topic_1 => 0 + 14 + 6 + 10 + 0 => 30
|
|
|
|
# topic_2 => 0 + 5 + 6 + 10 + 0.301029995664 => 21.301029995664
|
|
|
|
# topic_3 => 0.602059991328 + 0 + 0 + 10 + 0 => 10.602059991328
|
|
|
|
|
|
|
|
TopTopic.refresh!
|
|
|
|
top_topics = TopTopic.all
|
|
|
|
|
2023-02-12 23:39:45 -05:00
|
|
|
expect(top_topics.where(topic_id: topic_1.id).pick(:yearly_score)).to eq(30)
|
|
|
|
expect(top_topics.where(topic_id: topic_2.id).pick(:yearly_score)).to be_within(
|
2019-11-26 00:39:14 -05:00
|
|
|
0.0000000001,
|
|
|
|
).of(21.301029995664)
|
2023-02-12 23:39:45 -05:00
|
|
|
expect(top_topics.where(topic_id: topic_3.id).pick(:yearly_score)).to be_within(
|
2019-11-26 00:39:14 -05:00
|
|
|
0.0000000001,
|
|
|
|
).of(10.602059991328)
|
2016-01-20 11:36:18 -05:00
|
|
|
|
|
|
|
# handles invalid string value
|
|
|
|
SiteSetting.top_topics_formula_log_views_multiplier = "not good"
|
|
|
|
SiteSetting.top_topics_formula_first_post_likes_multiplier = "not good"
|
|
|
|
SiteSetting.top_topics_formula_least_likes_per_post_multiplier = "not good"
|
|
|
|
|
|
|
|
TopTopic.refresh!
|
|
|
|
top_topics = TopTopic.all
|
|
|
|
|
2023-02-12 23:39:45 -05:00
|
|
|
expect(top_topics.where(topic_id: topic_1.id).pick(:yearly_score)).to eq(27)
|
|
|
|
expect(top_topics.where(topic_id: topic_2.id).pick(:yearly_score)).to be_within(
|
2019-11-26 00:39:14 -05:00
|
|
|
0.0000000001,
|
|
|
|
).of(18.301029995664)
|
2023-02-12 23:39:45 -05:00
|
|
|
expect(top_topics.where(topic_id: topic_3.id).pick(:yearly_score)).to be_within(
|
2019-11-26 00:39:14 -05:00
|
|
|
0.0000000001,
|
|
|
|
).of(10.602059991328)
|
2016-01-20 11:36:18 -05:00
|
|
|
end
|
|
|
|
end
|
2013-12-23 18:50:36 -05:00
|
|
|
end
|