Commit 20838cdd authored by Etienne Baqué's avatar Etienne Baqué

Added serialization to create JSON object for Evidence

Added serializers which will create the Summary JSON.
Added spec files for the serializer and entities.
parent 8d6b6c78
# frozen_string_literal: true
class EvidenceAuthorEntity < Grape::Entity
expose :id
expose :name
expose :email
end
# frozen_string_literal: true
class EvidenceIssueEntity < Grape::Entity
expose :id
expose :title
expose :description
expose :author, using: EvidenceAuthorEntity
expose :state
expose :confidential
expose :created_at
expose :due_date
end
# frozen_string_literal: true
class EvidenceMilestoneEntity < Grape::Entity
expose :id
expose :title
expose :description
expose :state
expose :created_at
expose :due_date
expose :issues, using: EvidenceIssueEntity
end
# frozen_string_literal: true
class EvidenceProjectEntity < Grape::Entity
expose :id
expose :name
expose :description
expose :created_at
end
# frozen_string_literal: true
class EvidenceReleaseEntity < Grape::Entity
expose :id
expose :tag
expose :name
expose :description
expose :created_at
expose :project, using: EvidenceProjectEntity
expose :milestones, using: EvidenceMilestoneEntity
end
# frozen_string_literal: true
class EvidenceReleaseSerializer < BaseSerializer
entity EvidenceReleaseEntity
end
# frozen_string_literal: true
require 'spec_helper'
describe EvidenceAuthorEntity do
let(:entity) { described_class.new(build(:author)) }
subject { entity.as_json }
it 'exposes the expected fields' do
expect(subject.keys).to contain_exactly(:id, :name, :email)
end
end
# frozen_string_literal: true
require 'spec_helper'
describe EvidenceIssueEntity do
let(:entity) { described_class.new(build(:issue)) }
subject { entity.as_json }
it 'exposes the expected fields' do
expect(subject.keys).to contain_exactly(:id, :title, :description, :author, :state, :confidential, :created_at, :due_date)
end
end
# frozen_string_literal: true
require 'spec_helper'
describe EvidenceMilestoneEntity do
let(:milestone) { build(:milestone) }
let(:entity) { described_class.new(milestone) }
subject { entity.as_json }
it 'exposes the expected fields' do
expect(subject.keys).to contain_exactly(:id, :title, :description, :state, :created_at, :due_date, :issues)
end
context 'when there issues linked to this milestone' do
let(:issue_1) { build(:issue) }
let(:issue_2) { build(:issue) }
let(:milestone) { build(:milestone, issues: [issue_1, issue_2]) }
it 'exposes these issues' do
expect(subject[:issues]).to contain_exactly(
EvidenceIssueEntity.new(issue_1).as_json,
EvidenceIssueEntity.new(issue_2).as_json
)
end
end
context 'when the release has no milestone' do
let(:milestone) { build(:milestone, issues: []) }
it 'exposes an empty array for milestones' do
expect(subject[:issues]).to be_empty
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe EvidenceProjectEntity do
let(:entity) { described_class.new(build(:project)) }
subject { entity.as_json }
it 'exposes the expected fields' do
expect(subject.keys).to contain_exactly(:id, :name, :description, :created_at)
end
end
# frozen_string_literal: true
require 'spec_helper'
describe EvidenceReleaseEntity do
let(:release) { build(:release) }
let(:entity) { described_class.new(release) }
subject { entity.as_json }
it 'exposes the expected fields' do
expect(subject.keys).to contain_exactly(:id, :tag, :name, :description, :created_at, :project, :milestones)
end
context 'when the release has milestones' do
let(:project) { create(:project) }
let(:milestone_1) { build(:milestone, project: project) }
let(:milestone_2) { build(:milestone, project: project) }
let(:release) { build(:release, project: project, milestones: [milestone_1, milestone_2]) }
it 'exposes these milestones' do
expect(subject[:milestones]).to contain_exactly(
EvidenceMilestoneEntity.new(milestone_1).as_json,
EvidenceMilestoneEntity.new(milestone_2).as_json
)
end
end
context 'when the release has no milestone' do
let(:release) { build(:release, milestones: []) }
it 'exposes an empty array for milestones' do
expect(subject[:milestones]).to be_empty
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe EvidenceReleaseSerializer do
it 'represents an EvidenceReleaseEntity entity' do
expect(described_class.entity_class).to eq(EvidenceReleaseEntity)
end
end
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