2022-02-08 23:51:13 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe UserStatCountUpdater do
|
2023-11-09 17:47:59 -05:00
|
|
|
fab!(:user)
|
2022-02-08 23:51:13 -05:00
|
|
|
fab!(:user_stat) { user.user_stat }
|
2023-11-09 17:47:59 -05:00
|
|
|
fab!(:post)
|
2022-02-09 08:48:18 -05:00
|
|
|
fab!(:post_2) { Fabricate(:post, topic: post.topic) }
|
2022-02-08 23:51:13 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
@orig_logger = Rails.logger
|
|
|
|
Rails.logger = @fake_logger = FakeLogger.new
|
2022-02-10 20:00:58 -05:00
|
|
|
SiteSetting.verbose_user_stat_count_logging = true
|
2022-02-08 23:51:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
after { Rails.logger = @orig_logger }
|
|
|
|
|
|
|
|
it "should log the exception when a negative count is inserted" do
|
|
|
|
UserStatCountUpdater.decrement!(post, user_stat: user_stat)
|
|
|
|
|
2022-02-09 08:48:18 -05:00
|
|
|
expect(@fake_logger.warnings.last).to match("topic_count")
|
2022-02-10 20:00:58 -05:00
|
|
|
expect(@fake_logger.warnings.last).to match(post.id.to_s)
|
2022-02-09 08:48:18 -05:00
|
|
|
|
|
|
|
UserStatCountUpdater.decrement!(post_2, user_stat: user_stat)
|
|
|
|
|
|
|
|
expect(@fake_logger.warnings.last).to match("post_count")
|
2022-02-10 20:00:58 -05:00
|
|
|
expect(@fake_logger.warnings.last).to match(post_2.id.to_s)
|
2022-02-08 23:51:13 -05:00
|
|
|
end
|
2022-02-15 20:49:11 -05:00
|
|
|
|
|
|
|
it "should log the exception when a negative count will be inserted but 0 is used instead" do
|
|
|
|
UserStatCountUpdater.set!(user_stat: user_stat, count: -10, count_column: :post_count)
|
|
|
|
|
|
|
|
expect(@fake_logger.warnings.last).to match("post_count")
|
|
|
|
expect(@fake_logger.warnings.last).to match("using 0")
|
|
|
|
expect(@fake_logger.warnings.last).to match("user #{user_stat.user_id}")
|
|
|
|
expect(user_stat.reload.post_count).to eq(0)
|
|
|
|
end
|
2022-02-08 23:51:13 -05:00
|
|
|
end
|