2016-11-10 20:16:54 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe PipelineSerializer do
|
2016-12-15 14:57:35 +00:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
2016-11-10 20:16:54 +00:00
|
|
|
let(:serializer) do
|
|
|
|
described_class.new(user: user)
|
|
|
|
end
|
|
|
|
|
2017-01-27 12:02:59 +00:00
|
|
|
subject { serializer.represent(resource) }
|
2016-12-28 12:07:30 +00:00
|
|
|
|
2016-12-15 14:57:35 +00:00
|
|
|
describe '#represent' do
|
2016-12-28 12:07:30 +00:00
|
|
|
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
|
|
|
|
|
2017-02-03 10:41:23 +00:00
|
|
|
context 'when resource is not paginatable' do
|
2016-12-28 12:07:30 +00:00
|
|
|
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
|
2017-02-06 14:07:13 +00:00
|
|
|
expect { subject }.to raise_error(
|
|
|
|
Gitlab::Serializer::Pagination::InvalidResourceError)
|
2016-12-28 12:07:30 +00:00
|
|
|
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')
|
2016-11-10 20:16:54 +00:00
|
|
|
|
2016-12-28 12:07:30 +00:00
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-12-15 14:57:35 +00:00
|
|
|
end
|
2017-04-04 21:52:59 +00:00
|
|
|
|
|
|
|
context 'number of queries' do
|
|
|
|
let(:resource) { Ci::Pipeline.all }
|
2017-04-06 13:35:40 +00:00
|
|
|
let(:project) { create(:empty_project) }
|
2017-04-04 21:52:59 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
Ci::Pipeline::AVAILABLE_STATUSES.each do |status|
|
|
|
|
create_pipeline(status)
|
|
|
|
end
|
2017-04-06 13:14:40 +00:00
|
|
|
|
|
|
|
RequestStore.begin!
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
RequestStore.end!
|
|
|
|
RequestStore.clear!
|
2017-04-04 21:52:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "verifies number of queries" do
|
|
|
|
recorded = ActiveRecord::QueryRecorder.new { subject }
|
2017-04-06 13:35:40 +00:00
|
|
|
expect(recorded.count).to be_within(1).of(50)
|
|
|
|
expect(recorded.cached_count).to eq(0)
|
2017-04-04 21:52:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_pipeline(status)
|
2017-04-06 13:35:40 +00:00
|
|
|
create(:ci_empty_pipeline, project: project, status: status).tap do |pipeline|
|
2017-04-04 21:52:59 +00:00
|
|
|
Ci::Build::AVAILABLE_STATUSES.each do |status|
|
|
|
|
create_build(pipeline, status, status)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_build(pipeline, stage, status)
|
|
|
|
create(:ci_build, :tags, :triggered, :artifacts,
|
|
|
|
pipeline: pipeline, stage: stage,
|
|
|
|
name: stage, status: status)
|
|
|
|
end
|
|
|
|
end
|
2017-03-10 15:16:48 +00:00
|
|
|
end
|
2017-03-06 15:09:48 +00:00
|
|
|
|
2017-03-10 15:16:48 +00:00
|
|
|
describe '#represent_status' do
|
|
|
|
context 'when represents only status' do
|
2017-03-21 13:21:13 +00:00
|
|
|
let(:resource) { create(:ci_pipeline) }
|
|
|
|
let(:status) { resource.detailed_status(double('user')) }
|
2017-03-10 15:16:48 +00:00
|
|
|
|
|
|
|
subject { serializer.represent_status(resource) }
|
2017-03-06 15:09:48 +00:00
|
|
|
|
|
|
|
it 'serializes only status' do
|
2017-03-11 14:30:25 +00:00
|
|
|
expect(subject[:text]).to eq(status.text)
|
|
|
|
expect(subject[:label]).to eq(status.label)
|
|
|
|
expect(subject[:icon]).to eq(status.icon)
|
2017-03-10 15:16:48 +00:00
|
|
|
expect(subject[:favicon]).to eq(status.favicon)
|
2017-03-06 15:09:48 +00:00
|
|
|
end
|
|
|
|
end
|
2016-12-15 14:57:35 +00:00
|
|
|
end
|
2016-11-10 20:16:54 +00:00
|
|
|
end
|