summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2017-02-06 15:07:13 +0100
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2017-02-06 15:07:13 +0100
commita7420b77bd9b7038af3702d3665faab317048d3a (patch)
treeeb96b3ec5c303aa6ea2573122586b397b1207fa9
parent81eb5c7fd79897d63e733e99b8fc4424dc8ea38a (diff)
downloadgitlab-ce-a7420b77bd9b7038af3702d3665faab317048d3a.tar.gz
Add specs for standalone pagination for serializers
-rw-r--r--app/serializers/pipeline_serializer.rb7
-rw-r--r--lib/gitlab/serializer/pagination.rb9
-rw-r--r--spec/lib/gitlab/serializer/pagination_spec.rb46
-rw-r--r--spec/serializers/pipeline_serializer_spec.rb4
4 files changed, 61 insertions, 5 deletions
diff --git a/app/serializers/pipeline_serializer.rb b/app/serializers/pipeline_serializer.rb
index 731ccfe90b3..245298db3c3 100644
--- a/app/serializers/pipeline_serializer.rb
+++ b/app/serializers/pipeline_serializer.rb
@@ -12,10 +12,11 @@ class PipelineSerializer < BaseSerializer
end
def represent(resource, opts = {})
- if paginated?
- raise InvalidResourceError unless resource.respond_to?(:page)
-
+ if resource.is_a?(ActiveRecord::Relation)
resource = resource.includes(project: :namespace)
+ end
+
+ if paginated?
super(@paginator.paginate(resource), opts)
else
super(resource, opts)
diff --git a/lib/gitlab/serializer/pagination.rb b/lib/gitlab/serializer/pagination.rb
index d9372f3dcd6..bf2c0acc729 100644
--- a/lib/gitlab/serializer/pagination.rb
+++ b/lib/gitlab/serializer/pagination.rb
@@ -1,6 +1,7 @@
module Gitlab
module Serializer
class Pagination
+ class InvalidResourceError < StandardError; end
include ::API::Helpers::Pagination
def initialize(request, response)
@@ -8,6 +9,14 @@ module Gitlab
@response = response
end
+ def paginate(resource)
+ if resource.respond_to?(:page)
+ super(resource)
+ else
+ raise InvalidResourceError
+ end
+ end
+
private
# Methods needed by `API::Helpers::Pagination`
diff --git a/spec/lib/gitlab/serializer/pagination_spec.rb b/spec/lib/gitlab/serializer/pagination_spec.rb
new file mode 100644
index 00000000000..43d9782c3b7
--- /dev/null
+++ b/spec/lib/gitlab/serializer/pagination_spec.rb
@@ -0,0 +1,46 @@
+require 'spec_helper'
+
+describe Gitlab::Serializer::Pagination do
+ let(:request) { spy('request') }
+ let(:response) { spy('response') }
+
+ before do
+ allow(request)
+ .to receive(:query_parameters)
+ .and_return(params)
+ end
+
+ let(:pagination) { described_class.new(request, response) }
+
+ describe '#paginate' do
+ subject { pagination.paginate(resource) }
+
+ let(:resource) { User.all }
+ let(:params) { { page: 1, per_page: 2 } }
+
+ context 'when a multiple resources are present in relation' do
+ before { create_list(:user, 3) }
+
+ it 'correctly paginates the resource' do
+ expect(subject.count).to be 2
+ end
+
+ it 'appends relevant headers' do
+ expect(response).to receive(:[]=).with('X-Total', '3')
+ expect(response).to receive(:[]=).with('X-Total-Pages', '2')
+ expect(response).to receive(:[]=).with('X-Per-Page', '2')
+
+ subject
+ end
+ end
+
+ context 'when an invalid resource is about to be paginated' do
+ let(:resource) { create(:user) }
+
+ it 'raises error' do
+ expect { subject }.to raise_error(
+ described_class::InvalidResourceError)
+ end
+ end
+ end
+end
diff --git a/spec/serializers/pipeline_serializer_spec.rb b/spec/serializers/pipeline_serializer_spec.rb
index 14553acd00c..2aaef03cb93 100644
--- a/spec/serializers/pipeline_serializer_spec.rb
+++ b/spec/serializers/pipeline_serializer_spec.rb
@@ -58,8 +58,8 @@ describe PipelineSerializer do
let(:pagination) { { page: 1, per_page: 1 } }
it 'raises error' do
- expect { subject }
- .to raise_error(PipelineSerializer::InvalidResourceError)
+ expect { subject }.to raise_error(
+ Gitlab::Serializer::Pagination::InvalidResourceError)
end
end
end