mirror of
https://github.com/discourse/discourse.git
synced 2024-12-15 11:33:40 +08:00
f88dced0b7
This previously was a hot path in topic view. Avoids an expensive active record operation and instead perform SQL directly which is far more targeted and efficient
53 lines
1.3 KiB
Ruby
53 lines
1.3 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'rails_helper'
|
|
|
|
describe TopicViewPostsSerializer do
|
|
|
|
it 'should return the right attributes' do
|
|
|
|
user = Fabricate(:user)
|
|
post = Fabricate(:post)
|
|
topic = post.topic
|
|
|
|
reviewable = Fabricate(:reviewable_flagged_post, created_by: user, target: post, topic: topic)
|
|
|
|
ReviewableScore.create!(
|
|
reviewable_id: reviewable.id,
|
|
user_id: user.id,
|
|
reviewable_score_type: 0,
|
|
status: ReviewableScore.statuses[:pending]
|
|
)
|
|
|
|
ReviewableScore.create!(
|
|
reviewable_id: reviewable.id,
|
|
user_id: user.id,
|
|
reviewable_score_type: 0,
|
|
status: ReviewableScore.statuses[:ignored]
|
|
)
|
|
|
|
topic_view = TopicView.new(topic, user, post_ids: [post.id])
|
|
|
|
serializer = TopicViewPostsSerializer.new(
|
|
topic_view,
|
|
scope: Guardian.new(Fabricate(:admin)),
|
|
root: false
|
|
)
|
|
|
|
body = JSON.parse(serializer.to_json)
|
|
|
|
posts = body["post_stream"]["posts"]
|
|
|
|
expect(posts.count).to eq(1)
|
|
expect(posts.first["id"]).to eq(post.id)
|
|
|
|
expect(posts.first["reviewable_score_count"]).to eq(2)
|
|
expect(posts.first["reviewable_score_pending_count"]).to eq(1)
|
|
|
|
expect(body["post_stream"]["stream"]).to eq(nil)
|
|
expect(body["post_stream"]["timeline_lookup"]).to eq(nil)
|
|
expect(body["post_stream"]["gaps"]).to eq(nil)
|
|
|
|
end
|
|
end
|