docbrown/spec/workers/follows/update_points_worker_spec.rb
Ben Halpern 0ec2a40fe0
Test variants for which posts are fetched on initial "hot articles" home feed query. (#11982)
* Declare winner in update points worker

* Remove tests

* Test variants for which posts are fetched on home feed

* Add field test

* Rewrite field tests

* Change user to @user

* Fix tests

* Fix user thing

* Check for existence of user before setting experiment

* Fix experiment -> @experiment

* Change spec

* Fix tests

* Fix tests

* Fix tests

* Fix rubocop issues

* Remove unneeded offset value

* Fix up specs

* Final adjustments

* Add comments

* Update app/services/articles/feeds/large_forem_experimental.rb

Co-authored-by: Molly Struve <mollylbs@gmail.com>

* Update app/services/articles/feeds/large_forem_experimental.rb

Co-authored-by: Molly Struve <mollylbs@gmail.com>

* Update app/services/articles/feeds/large_forem_experimental.rb

Co-authored-by: Fernando Valverde <fdov88@gmail.com>

* Update app/services/articles/feeds/large_forem_experimental.rb

Co-authored-by: Fernando Valverde <fdov88@gmail.com>

* Better initial user query

Co-authored-by: Molly Struve <mollylbs@gmail.com>
Co-authored-by: Fernando Valverde <fdov88@gmail.com>
2021-01-05 12:29:46 -05:00

78 lines
2.8 KiB
Ruby

require "rails_helper"
RSpec.describe Follows::UpdatePointsWorker, type: :worker do
include_examples "#enqueues_on_correct_queue", "low_priority", 1
describe "#perform" do
let(:worker) { subject }
let(:user) { create(:user) }
let(:tag) { create(:tag, name: "tag") }
let(:second_tag) { create(:tag, name: "secondtag") }
let(:third_tag) { create(:tag, name: "thirdtag") }
let(:article) { create(:article, tags: [tag.name]) }
let(:second_article) { create(:article, tags: [tag.name]) }
let(:reaction) { create(:reaction, reactable: article, user: user) }
let(:page_view) { create(:page_view, user: user, article: article, time_tracked_in_seconds: 100) }
before do
user.follow(second_tag)
user.follow(tag)
end
it "calculates scores" do
follow = Follow.last
follow.update_column(:explicit_points, 2.2)
worker.perform(reaction.reactable_id, reaction.user_id)
follow.reload
expect(follow.implicit_points).to be > 0
expect(follow.reload.points.round(2)).to eq (follow.implicit_points + follow.explicit_points).round(2)
end
it "has higher score with more long page views" do
follow = Follow.last
worker.perform(reaction.reactable_id, reaction.user_id)
follow.reload
first_implicit_score = follow.implicit_points
create(:page_view, user: user, article: second_article, time_tracked_in_seconds: 100)
worker.perform(reaction.reactable_id, reaction.user_id)
follow.reload
expect(follow.implicit_points).to be > first_implicit_score
end
it "has higher score with more reactions" do
follow = Follow.last
worker.perform(reaction.reactable_id, reaction.user_id)
follow.reload
first_implicit_score = follow.implicit_points
create(:reaction, reactable: second_article, user: user)
worker.perform(reaction.reactable_id, reaction.user_id)
follow.reload
expect(follow.implicit_points).to be > first_implicit_score
end
it "bumps down tag follow points not included in this calc" do
follow = Follow.first
worker.perform(reaction.reactable_id, reaction.user_id)
expect(follow.reload.points.round(2)).to eq(0.98)
end
it "applies inverse bonus to slightly penalize more popular tags" do
follow = Follow.last
tag.update_column(:hotness_score, 1000)
second_tag.update_column(:hotness_score, 100)
worker.perform(reaction.reactable_id, reaction.user_id)
follow.reload
original_points = follow.points
tag.update_column(:hotness_score, 50)
tag.reload
worker.perform(reaction.reactable_id, reaction.user_id)
expect(follow.reload.points).to be > original_points # should be higher because tag is now less popular
end
end
end