Commit 42e3f0fd authored by Grzegorz Bizon's avatar Grzegorz Bizon

Return serializable hash from a represented serializer

parent 256a7735
......@@ -6,6 +6,7 @@ class BaseSerializer
def represent(resource, opts = {})
self.class.entity_class
.represent(resource, opts.merge(request: @request))
.as_json
end
def self.entity(entity_class)
......
require 'spec_helper'
describe AnalyticsBuildSerializer do
let(:serializer) do
described_class
.new.represent(resource)
end
let(:json) { serializer.as_json }
let(:resource) { create(:ci_build) }
subject { described_class.new.represent(resource) }
context 'when there is a single object provided' do
it 'contains important elements of analyticsBuild' do
expect(json)
expect(subject)
.to include(:name, :branch, :short_sha, :date, :total_time, :url, :author)
end
end
......
require 'spec_helper'
describe AnalyticsIssueSerializer do
let(:serializer) do
subject do
described_class
.new(project: project, entity: :merge_request)
.represent(resource)
end
let(:user) { create(:user) }
let(:json) { serializer.as_json }
let(:project) { create(:project) }
let(:resource) do
{
......@@ -23,7 +22,7 @@ describe AnalyticsIssueSerializer do
context 'when there is a single object provided' do
it 'contains important elements of the issue' do
expect(json).to include(:title, :iid, :created_at, :total_time, :url, :author)
expect(subject).to include(:title, :iid, :created_at, :total_time, :url, :author)
end
end
end
require 'spec_helper'
describe AnalyticsMergeRequestSerializer do
let(:serializer) do
subject do
described_class
.new(project: project, entity: :merge_request)
.represent(resource)
end
let(:user) { create(:user) }
let(:json) { serializer.as_json }
let(:project) { create(:project) }
let(:resource) do
{
......@@ -24,7 +23,7 @@ describe AnalyticsMergeRequestSerializer do
context 'when there is a single object provided' do
it 'contains important elements of the merge request' do
expect(json).to include(:title, :iid, :created_at, :total_time, :url, :author, :state)
expect(subject).to include(:title, :iid, :created_at, :total_time, :url, :author, :state)
end
end
end
require 'spec_helper'
describe AnalyticsStageSerializer do
let(:serializer) do
described_class
.new.represent(resource)
subject do
described_class.new.represent(resource)
end
let(:json) { serializer.as_json }
let(:resource) { Gitlab::CycleAnalytics::CodeStage.new(project: double, options: {}) }
let(:resource) do
Gitlab::CycleAnalytics::CodeStage.new(project: double, options: {})
end
before do
allow_any_instance_of(Gitlab::CycleAnalytics::BaseStage).to receive(:median).and_return(1.12)
......@@ -15,10 +15,10 @@ describe AnalyticsStageSerializer do
end
it 'it generates payload for single object' do
expect(json).to be_kind_of Hash
expect(subject).to be_kind_of Hash
end
it 'contains important elements of AnalyticsStage' do
expect(json).to include(:title, :description, :value)
expect(subject).to include(:title, :description, :value)
end
end
require 'spec_helper'
describe AnalyticsSummarySerializer do
let(:serializer) do
described_class
.new.represent(resource)
subject do
described_class.new.represent(resource)
end
let(:json) { serializer.as_json }
let(:project) { create(:empty_project) }
let(:user) { create(:user) }
let(:resource) do
Gitlab::CycleAnalytics::Summary::Issue.new(project: double,
from: 1.day.ago,
current_user: user)
Gitlab::CycleAnalytics::Summary::Issue
.new(project: double, from: 1.day.ago, current_user: user)
end
before do
allow_any_instance_of(Gitlab::CycleAnalytics::Summary::Issue).to receive(:value).and_return(1.12)
allow_any_instance_of(Gitlab::CycleAnalytics::Summary::Issue)
.to receive(:value).and_return(1.12)
end
it 'it generates payload for single object' do
expect(json).to be_kind_of Hash
expect(subject).to be_kind_of Hash
end
it 'contains important elements of AnalyticsStage' do
expect(json).to include(:title, :value)
expect(subject).to include(:title, :value)
end
end
require 'spec_helper'
describe EnvironmentSerializer do
let(:serializer) do
let(:user) { create(:user) }
let(:project) { create(:project) }
let(:json) do
described_class
.new(user: user, project: project)
.represent(resource)
end
let(:json) { serializer.as_json }
let(:user) { create(:user) }
let(:project) { create(:project) }
context 'when there is a single object provided' do
before do
create(:ci_build, :manual, name: 'manual1',
......
......@@ -7,11 +7,7 @@ describe PipelineSerializer do
described_class.new(user: user)
end
let(:entity) do
serializer.represent(resource)
end
subject { entity.as_json }
subject { serializer.represent(resource) }
describe '#represent' do
context 'when used without pagination' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment