Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
gitlab-ce
Commits
a7420b77
Commit
a7420b77
authored
Feb 06, 2017
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add specs for standalone pagination for serializers
parent
81eb5c7f
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
61 additions
and
5 deletions
+61
-5
app/serializers/pipeline_serializer.rb
app/serializers/pipeline_serializer.rb
+4
-3
lib/gitlab/serializer/pagination.rb
lib/gitlab/serializer/pagination.rb
+9
-0
spec/lib/gitlab/serializer/pagination_spec.rb
spec/lib/gitlab/serializer/pagination_spec.rb
+46
-0
spec/serializers/pipeline_serializer_spec.rb
spec/serializers/pipeline_serializer_spec.rb
+2
-2
No files found.
app/serializers/pipeline_serializer.rb
View file @
a7420b77
...
@@ -12,10 +12,11 @@ class PipelineSerializer < BaseSerializer
...
@@ -12,10 +12,11 @@ class PipelineSerializer < BaseSerializer
end
end
def
represent
(
resource
,
opts
=
{})
def
represent
(
resource
,
opts
=
{})
if
paginated?
if
resource
.
is_a?
(
ActiveRecord
::
Relation
)
raise
InvalidResourceError
unless
resource
.
respond_to?
(
:page
)
resource
=
resource
.
includes
(
project: :namespace
)
resource
=
resource
.
includes
(
project: :namespace
)
end
if
paginated?
super
(
@paginator
.
paginate
(
resource
),
opts
)
super
(
@paginator
.
paginate
(
resource
),
opts
)
else
else
super
(
resource
,
opts
)
super
(
resource
,
opts
)
...
...
lib/gitlab/serializer/pagination.rb
View file @
a7420b77
module
Gitlab
module
Gitlab
module
Serializer
module
Serializer
class
Pagination
class
Pagination
class
InvalidResourceError
<
StandardError
;
end
include
::
API
::
Helpers
::
Pagination
include
::
API
::
Helpers
::
Pagination
def
initialize
(
request
,
response
)
def
initialize
(
request
,
response
)
...
@@ -8,6 +9,14 @@ module Gitlab
...
@@ -8,6 +9,14 @@ module Gitlab
@response
=
response
@response
=
response
end
end
def
paginate
(
resource
)
if
resource
.
respond_to?
(
:page
)
super
(
resource
)
else
raise
InvalidResourceError
end
end
private
private
# Methods needed by `API::Helpers::Pagination`
# Methods needed by `API::Helpers::Pagination`
...
...
spec/lib/gitlab/serializer/pagination_spec.rb
0 → 100644
View file @
a7420b77
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
spec/serializers/pipeline_serializer_spec.rb
View file @
a7420b77
...
@@ -58,8 +58,8 @@ describe PipelineSerializer do
...
@@ -58,8 +58,8 @@ describe PipelineSerializer do
let
(
:pagination
)
{
{
page:
1
,
per_page:
1
}
}
let
(
:pagination
)
{
{
page:
1
,
per_page:
1
}
}
it
'raises error'
do
it
'raises error'
do
expect
{
subject
}
expect
{
subject
}
.
to
raise_error
(
.
to
raise_error
(
PipelineSerializer
::
InvalidResourceError
)
Gitlab
::
Serializer
::
Pagination
::
InvalidResourceError
)
end
end
end
end
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment