summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/multi_collection_paginator_spec.rb
diff options
context:
space:
mode:
authorBob Van Landuyt <bob@vanlanduyt.co>2017-10-04 22:26:51 +0200
committerBob Van Landuyt <bob@vanlanduyt.co>2017-10-05 11:10:57 +0200
commitdda023d66d09b8a3a43a5599bde42ac52eb6fd06 (patch)
treeaa585d3884c5f577bd605e21c1554d02bf5f1ca5 /spec/lib/gitlab/multi_collection_paginator_spec.rb
parent57bd3bb34a19bf812fd6a74f394a69c491b05dd0 (diff)
downloadgitlab-ce-dda023d66d09b8a3a43a5599bde42ac52eb6fd06.tar.gz
Optimize queries and pagination in `GroupDescendantsFinder`
Diffstat (limited to 'spec/lib/gitlab/multi_collection_paginator_spec.rb')
-rw-r--r--spec/lib/gitlab/multi_collection_paginator_spec.rb46
1 files changed, 46 insertions, 0 deletions
diff --git a/spec/lib/gitlab/multi_collection_paginator_spec.rb b/spec/lib/gitlab/multi_collection_paginator_spec.rb
new file mode 100644
index 00000000000..385cfa63dda
--- /dev/null
+++ b/spec/lib/gitlab/multi_collection_paginator_spec.rb
@@ -0,0 +1,46 @@
+require 'spec_helper'
+
+describe Gitlab::MultiCollectionPaginator do
+ subject(:paginator) { described_class.new(Project.all, Group.all, per_page: 3) }
+
+ it 'combines both collections' do
+ project = create(:project)
+ group = create(:group)
+
+ expect(paginator.paginate(1)).to eq([project, group])
+ end
+
+ it 'includes elements second collection if first collection is empty' do
+ group = create(:group)
+
+ expect(paginator.paginate(1)).to eq([group])
+ end
+
+ context 'with a full first page' do
+ let!(:all_groups) { create_list(:group, 4) }
+ let!(:all_projects) { create_list(:project, 4) }
+
+ it 'knows the total count of the collection' do
+ expect(paginator.total_count).to eq(8)
+ end
+
+ it 'fills the first page with elements of the first collection' do
+ expect(paginator.paginate(1)).to eq(all_projects.take(3))
+ end
+
+ it 'fils the second page with a mixture of of the first & second collection' do
+ first_collection_element = all_projects.last
+ second_collection_elements = all_groups.take(2)
+
+ expected_collection = [first_collection_element] + second_collection_elements
+
+ expect(paginator.paginate(2)).to eq(expected_collection)
+ end
+
+ it 'fils the last page with elements from the second collection' do
+ expected_collection = all_groups[-2..-1]
+
+ expect(paginator.paginate(3)).to eq(expected_collection)
+ end
+ end
+end