diff options
author | Markus Koller <mkoller@gitlab.com> | 2019-09-02 13:12:20 +0200 |
---|---|---|
committer | Markus Koller <mkoller@gitlab.com> | 2019-09-10 15:24:28 +0200 |
commit | 60755fbc406bd25ab526339899f97a2b27aeb272 (patch) | |
tree | a60f6fa9beb0c4ccdc27c5f7734c14bc34ad2d21 /spec/lib/gitlab/noteable_metadata_spec.rb | |
parent | 08b0613302ec813c0735e2c0447a3f7683d7ab87 (diff) | |
download | gitlab-ce-60755fbc406bd25ab526339899f97a2b27aeb272.tar.gz |
Optimize queries for snippet listings
- Avoid N+1 queries for authors and comment counts
- Avoid an additional snippet existence query
Diffstat (limited to 'spec/lib/gitlab/noteable_metadata_spec.rb')
-rw-r--r-- | spec/lib/gitlab/noteable_metadata_spec.rb | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/spec/lib/gitlab/noteable_metadata_spec.rb b/spec/lib/gitlab/noteable_metadata_spec.rb new file mode 100644 index 00000000000..b12a1825f04 --- /dev/null +++ b/spec/lib/gitlab/noteable_metadata_spec.rb @@ -0,0 +1,29 @@ +# 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 |