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
f4315b72
Commit
f4315b72
authored
Dec 28, 2016
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add specs for pipeline serializer with pagination
parent
5838b160
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
92 additions
and
6 deletions
+92
-6
app/serializers/pipeline_serializer.rb
app/serializers/pipeline_serializer.rb
+5
-2
spec/serializers/pipeline_serializer_spec.rb
spec/serializers/pipeline_serializer_spec.rb
+87
-4
No files found.
app/serializers/pipeline_serializer.rb
View file @
f4315b72
class
PipelineSerializer
<
BaseSerializer
class
PipelineSerializer
<
BaseSerializer
entity
PipelineEntity
entity
PipelineEntity
class
InvalidResourceError
<
StandardError
;
end
include
API
::
Helpers
::
Pagination
include
API
::
Helpers
::
Pagination
Struct
.
new
(
'Pagination'
,
:request
,
:response
)
Struct
.
new
(
'Pagination'
,
:request
,
:response
)
def
represent
(
resource
,
opts
=
{})
def
represent
(
resource
,
opts
=
{})
if
paginate?
if
paginated?
raise
InvalidResourceError
unless
resource
.
respond_to?
(
:page
)
super
(
paginate
(
resource
),
opts
)
super
(
paginate
(
resource
),
opts
)
else
else
super
(
resource
,
opts
)
super
(
resource
,
opts
)
end
end
end
end
def
paginate?
def
paginate
d
?
defined?
(
@pagination
)
defined?
(
@pagination
)
end
end
...
...
spec/serializers/pipeline_serializer_spec.rb
View file @
f4315b72
...
@@ -2,17 +2,100 @@ require 'spec_helper'
...
@@ -2,17 +2,100 @@ require 'spec_helper'
describe
PipelineSerializer
do
describe
PipelineSerializer
do
let
(
:user
)
{
create
(
:user
)
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:pipeline
)
{
create
(
:ci_empty_pipeline
)
}
let
(
:serializer
)
do
let
(
:serializer
)
do
described_class
.
new
(
user:
user
)
described_class
.
new
(
user:
user
)
end
end
let
(
:entity
)
do
serializer
.
represent
(
resource
)
end
subject
{
entity
.
as_json
}
describe
'#represent'
do
describe
'#represent'
do
subject
{
serializer
.
represent
(
pipeline
)
}
context
'when used without pagination'
do
it
'created a not paginated serializer'
do
expect
(
serializer
).
not_to
be_paginated
end
context
'when a single object is being serialized'
do
let
(
:resource
)
{
create
(
:ci_empty_pipeline
)
}
it
'serializers the pipeline object'
do
expect
(
subject
[
:id
]).
to
eq
resource
.
id
end
end
context
'when multiple objects are being serialized'
do
let
(
:resource
)
{
create_list
(
:ci_pipeline
,
2
)
}
it
'serializers the array of pipelines'
do
expect
(
subject
).
not_to
be_empty
end
end
end
context
'when used with pagination'
do
let
(
:request
)
{
spy
(
'request'
)
}
let
(
:response
)
{
spy
(
'response'
)
}
let
(
:pagination
)
{
{}
}
before
do
allow
(
request
)
.
to
receive
(
:query_parameters
)
.
and_return
(
pagination
)
end
let
(
:serializer
)
do
described_class
.
new
(
user:
user
)
.
with_pagination
(
request
,
response
)
end
it
'created a paginated serializer'
do
expect
(
serializer
).
to
be_paginated
end
context
'when resource does is not paginatable'
do
context
'when a single pipeline object is being serialized'
do
let
(
:resource
)
{
create
(
:ci_empty_pipeline
)
}
let
(
:pagination
)
{
{
page:
1
,
per_page:
1
}
}
it
'raises error'
do
expect
{
subject
}
.
to
raise_error
(
PipelineSerializer
::
InvalidResourceError
)
end
end
end
context
'when resource is paginatable relation'
do
let
(
:resource
)
{
Ci
::
Pipeline
.
all
}
let
(
:pagination
)
{
{
page:
1
,
per_page:
2
}
}
context
'when a single pipeline object is present in relation'
do
before
{
create
(
:ci_empty_pipeline
)
}
it
'serializes pipeline relation'
do
expect
(
subject
.
first
).
to
have_key
:id
end
end
context
'when a multiple pipeline objects are being serialized'
do
before
{
create_list
(
:ci_empty_pipeline
,
3
)
}
it
'serializes appropriate number of objects'
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'
)
it
'serializers the pipeline object'
do
subject
expect
(
subject
.
as_json
).
to
include
:id
end
end
end
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