gitlab-org--gitlab-foss/spec/lib/gitlab/noteable_metadata_spec.rb
Markus Koller 60755fbc40
Optimize queries for snippet listings
- Avoid N+1 queries for authors and comment counts
- Avoid an additional snippet existence query
2019-09-10 15:24:28 +02:00

29 lines
948 B
Ruby

# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::NoteableMetadata do
subject { Class.new { include Gitlab::NoteableMetadata }.new }
it 'returns an empty Hash if an empty collection is provided' do
expect(subject.noteable_meta_data(Snippet.none, 'Snippet')).to eq({})
end
it 'raises an error when given a collection with no limit' do
expect { subject.noteable_meta_data(Snippet.all, 'Snippet') }.to raise_error(/must have a limit/)
end
context 'snippets' do
let!(:snippet) { create(:personal_snippet) }
let!(:other_snippet) { create(:personal_snippet) }
let!(:note) { create(:note, noteable: snippet) }
it 'aggregates stats on snippets' do
data = subject.noteable_meta_data(Snippet.all.limit(10), 'Snippet')
expect(data.count).to eq(2)
expect(data[snippet.id].user_notes_count).to eq(1)
expect(data[other_snippet.id].user_notes_count).to eq(0)
end
end
end