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 ...@@ -6,6 +6,7 @@ class BaseSerializer
def represent(resource, opts = {}) def represent(resource, opts = {})
self.class.entity_class self.class.entity_class
.represent(resource, opts.merge(request: @request)) .represent(resource, opts.merge(request: @request))
.as_json
end end
def self.entity(entity_class) def self.entity(entity_class)
......
require 'spec_helper' require 'spec_helper'
describe AnalyticsBuildSerializer do describe AnalyticsBuildSerializer do
let(:serializer) do
described_class
.new.represent(resource)
end
let(:json) { serializer.as_json }
let(:resource) { create(:ci_build) } let(:resource) { create(:ci_build) }
subject { described_class.new.represent(resource) }
context 'when there is a single object provided' do context 'when there is a single object provided' do
it 'contains important elements of analyticsBuild' do it 'contains important elements of analyticsBuild' do
expect(json) expect(subject)
.to include(:name, :branch, :short_sha, :date, :total_time, :url, :author) .to include(:name, :branch, :short_sha, :date, :total_time, :url, :author)
end end
end end
......
require 'spec_helper' require 'spec_helper'
describe AnalyticsIssueSerializer do describe AnalyticsIssueSerializer do
let(:serializer) do subject do
described_class described_class
.new(project: project, entity: :merge_request) .new(project: project, entity: :merge_request)
.represent(resource) .represent(resource)
end end
let(:user) { create(:user) } let(:user) { create(:user) }
let(:json) { serializer.as_json }
let(:project) { create(:project) } let(:project) { create(:project) }
let(:resource) do let(:resource) do
{ {
...@@ -23,7 +22,7 @@ describe AnalyticsIssueSerializer do ...@@ -23,7 +22,7 @@ describe AnalyticsIssueSerializer do
context 'when there is a single object provided' do context 'when there is a single object provided' do
it 'contains important elements of the issue' 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 end
end end
require 'spec_helper' require 'spec_helper'
describe AnalyticsMergeRequestSerializer do describe AnalyticsMergeRequestSerializer do
let(:serializer) do subject do
described_class described_class
.new(project: project, entity: :merge_request) .new(project: project, entity: :merge_request)
.represent(resource) .represent(resource)
end end
let(:user) { create(:user) } let(:user) { create(:user) }
let(:json) { serializer.as_json }
let(:project) { create(:project) } let(:project) { create(:project) }
let(:resource) do let(:resource) do
{ {
...@@ -24,7 +23,7 @@ describe AnalyticsMergeRequestSerializer do ...@@ -24,7 +23,7 @@ describe AnalyticsMergeRequestSerializer do
context 'when there is a single object provided' do context 'when there is a single object provided' do
it 'contains important elements of the merge request' 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 end
end end
require 'spec_helper' require 'spec_helper'
describe AnalyticsStageSerializer do describe AnalyticsStageSerializer do
let(:serializer) do subject do
described_class described_class.new.represent(resource)
.new.represent(resource)
end end
let(:json) { serializer.as_json } let(:resource) do
let(:resource) { Gitlab::CycleAnalytics::CodeStage.new(project: double, options: {}) } Gitlab::CycleAnalytics::CodeStage.new(project: double, options: {})
end
before do before do
allow_any_instance_of(Gitlab::CycleAnalytics::BaseStage).to receive(:median).and_return(1.12) allow_any_instance_of(Gitlab::CycleAnalytics::BaseStage).to receive(:median).and_return(1.12)
...@@ -15,10 +15,10 @@ describe AnalyticsStageSerializer do ...@@ -15,10 +15,10 @@ describe AnalyticsStageSerializer do
end end
it 'it generates payload for single object' do it 'it generates payload for single object' do
expect(json).to be_kind_of Hash expect(subject).to be_kind_of Hash
end end
it 'contains important elements of AnalyticsStage' do it 'contains important elements of AnalyticsStage' do
expect(json).to include(:title, :description, :value) expect(subject).to include(:title, :description, :value)
end end
end end
require 'spec_helper' require 'spec_helper'
describe AnalyticsSummarySerializer do describe AnalyticsSummarySerializer do
let(:serializer) do subject do
described_class described_class.new.represent(resource)
.new.represent(resource)
end end
let(:json) { serializer.as_json }
let(:project) { create(:empty_project) } let(:project) { create(:empty_project) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:resource) do let(:resource) do
Gitlab::CycleAnalytics::Summary::Issue.new(project: double, Gitlab::CycleAnalytics::Summary::Issue
from: 1.day.ago, .new(project: double, from: 1.day.ago, current_user: user)
current_user: user)
end end
before do 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 end
it 'it generates payload for single object' do it 'it generates payload for single object' do
expect(json).to be_kind_of Hash expect(subject).to be_kind_of Hash
end end
it 'contains important elements of AnalyticsStage' do it 'contains important elements of AnalyticsStage' do
expect(json).to include(:title, :value) expect(subject).to include(:title, :value)
end end
end end
require 'spec_helper' require 'spec_helper'
describe EnvironmentSerializer do describe EnvironmentSerializer do
let(:serializer) do let(:user) { create(:user) }
let(:project) { create(:project) }
let(:json) do
described_class described_class
.new(user: user, project: project) .new(user: user, project: project)
.represent(resource) .represent(resource)
end end
let(:json) { serializer.as_json }
let(:user) { create(:user) }
let(:project) { create(:project) }
context 'when there is a single object provided' do context 'when there is a single object provided' do
before do before do
create(:ci_build, :manual, name: 'manual1', create(:ci_build, :manual, name: 'manual1',
......
...@@ -7,11 +7,7 @@ describe PipelineSerializer do ...@@ -7,11 +7,7 @@ describe PipelineSerializer do
described_class.new(user: user) described_class.new(user: user)
end end
let(:entity) do subject { serializer.represent(resource) }
serializer.represent(resource)
end
subject { entity.as_json }
describe '#represent' do describe '#represent' do
context 'when used without pagination' 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