Skip to content

Commit

Permalink
GL-465 Incorporate pseudo exemptions in serialized output
Browse files Browse the repository at this point in the history
  • Loading branch information
jebw committed May 21, 2024
1 parent b4c828c commit ca9dc50
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def index
.new(presented_assessments,
include: %w[geographical_area
excluded_geographical_areas
exemptions
theme
regulation
measure_type])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def excluded_geographical_area_ids
end

def exemptions
certificates + additional_codes
@exemptions ||= (certificates + additional_codes + pseudo_exemptions)
end

def certificates
Expand All @@ -77,6 +77,10 @@ def additional_codes
Array.wrap(additional_code)
end

def pseudo_exemptions
ExemptionPresenter.wrap(@category_assessment.exemptions)
end

def regulation
RegulationPresenter.new(super)
end
Expand Down
15 changes: 15 additions & 0 deletions app/presenters/api/v2/green_lanes/exemption_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Api
module V2
module GreenLanes
class ExemptionPresenter < WrapDelegator
def id
code
end

def formatted_description
description
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class CategoryAssessmentSerializer
include JSONAPI::Serializer

set_type :category_assessment

set_id :id

has_many :exemptions, serializer: lambda { |record, _params|
Expand All @@ -14,6 +13,8 @@ class CategoryAssessmentSerializer
GreenLanes::CertificateSerializer
when AdditionalCode
AdditionalCodeSerializer
when ExemptionPresenter
GreenLanes::ExemptionSerializer
else
raise 'Unknown type'
end
Expand Down
15 changes: 15 additions & 0 deletions app/serializers/api/v2/green_lanes/exemption_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Api
module V2
module GreenLanes
class ExemptionSerializer
include JSONAPI::Serializer

set_id :code

attributes :code,
:description,
:formatted_description
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,13 @@
it { is_expected.to match_array certificates << additional_code }
end

context 'with neither' do
context 'with pseudo exemption' do
before { assessment.add_exemption create(:green_lanes_exemption) }

it { is_expected.to include instance_of Api::V2::GreenLanes::ExemptionPresenter }
end

context 'with no exemptions' do
it { is_expected.to be_empty }
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/presenters/api/v2/green_lanes/exemption_presenter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
RSpec.describe Api::V2::GreenLanes::ExemptionPresenter do
subject { described_class.new(exemption) }

let(:exemption) { create :green_lanes_exemption }

it { is_expected.to have_attributes id: exemption.code }
it { is_expected.to have_attributes code: exemption.code }
it { is_expected.to have_attributes description: exemption.description }
it { is_expected.to have_attributes formatted_description: exemption.description }
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
).serializable_hash.as_json
end

before { category_assessment.add_exemption exemption }

let(:category_assessment) { create :category_assessment, measure: }
let(:certificate) { create :certificate, :exemption, :with_certificate_type, :with_description }
let(:measure) { create :measure, :with_additional_code, :with_base_regulation, certificate: }
let(:exemption) { create :green_lanes_exemption }

let :presented do
Api::V2::GreenLanes::CategoryAssessmentPresenter.wrap(category_assessment).first
Expand All @@ -28,6 +31,7 @@
data: [
{ id: certificate.id, type: 'certificate' },
{ id: measure.additional_code.id.to_s, type: 'additional_code' },
{ id: exemption.code, type: 'exemption' },
],
},
geographical_area: {
Expand Down Expand Up @@ -76,6 +80,15 @@
formatted_description: be_a(String),
},
},
{
id: exemption.code,
type: 'exemption',
attributes: {
code: exemption.code,
description: be_a(String),
formatted_description: be_a(String),
},
},
{
id: measure.geographical_area_id,
type: 'geographical_area',
Expand Down
22 changes: 22 additions & 0 deletions spec/serializers/api/v2/green_lanes/exemption_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
RSpec.describe Api::V2::GreenLanes::ExemptionSerializer do
subject { described_class.new(presented).serializable_hash.as_json }

let(:exemption) { create :green_lanes_exemption }
let(:presented) { Api::V2::GreenLanes::ExemptionPresenter.new exemption }

let(:expected_pattern) do
{
data: {
id: exemption.code,
type: 'exemption',
attributes: {
code: exemption.code,
description: exemption.description,
formatted_description: exemption.description,
},
},
}
end

it { is_expected.to include_json(expected_pattern) }
end
20 changes: 20 additions & 0 deletions spec/services/green_lanes/permutation_calculator_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@
it { is_expected.to eq(measures.map { |m| [m] }) }
end

context 'with related measures' do
let(:measures) { [measure, measure2] }

let :measure do
create :measure, :with_additional_code, :with_measure_type, :with_base_regulation
end

let :measure2 do
create :measure, measure_type_id: measure.measure_type_id,
generating_regulation: measure.generating_regulation,
additional_code_sid: measure.additional_code_sid,
additional_code_id: measure.additional_code_id,
additional_code_type_id: measure.additional_code_type_id,
geographical_area_id: measure.geographical_area_id
end

it { is_expected.to have_attributes length: 1 }
it { expect(permutations[0]).to eq_pk measures }
end

context 'with mixture of related and unrelated' do
let :measures do
measures = create_list(:measure, 2, :with_measure_type, :with_base_regulation)
Expand Down

0 comments on commit ca9dc50

Please sign in to comment.