Skip to content

Commit

Permalink
Add tests for request spec
Browse files Browse the repository at this point in the history
  • Loading branch information
samnang committed Jan 13, 2025
1 parent 155e6d9 commit 8e41583
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
5 changes: 3 additions & 2 deletions app/request_schemas/v1/beneficiary_stats_request_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class BeneficiaryStatsRequestSchema < ApplicationRequestSchema
].freeze

params do
optional(:filter).value(:hash)
optional(:filter).value(:hash) do
optional(:status).filled(included_in?: Contact.status.values)
optional(:gender).filled(Types::UpcaseString, included_in?: Contact.gender.values)
optional(:date_of_birth).filled(:date)
Expand All @@ -43,6 +43,7 @@ class BeneficiaryStatsRequestSchema < ApplicationRequestSchema
optional(:"address.administrative_division_level_3_name").filled(:string)
optional(:"address.administrative_division_level_4_code").filled(:string)
optional(:"address.administrative_division_level_4_name").filled(:string)
end

required(:group_by).value(array[:string])
end
Expand All @@ -67,7 +68,7 @@ def output
result = super

result[:filter_fields] = result.fetch(:filter, {}).each_with_object({}) do |(filter, value), filters|
filters[FIELDS.fetch(filter)] = value
filters[FIELDS.fetch(filter.to_s)] = value
end

result[:group_by_fields] = result[:group_by].map do |group|
Expand Down
48 changes: 48 additions & 0 deletions spec/request_schemas/v1/beneficiary_stats_request_schema_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require "rails_helper"

module V1
RSpec.describe BeneficiaryStatsRequestSchema, type: :request_schema do
it "validates the address" do
expect(
validate_schema(input_params: { group_by: [ "address.administrative_division_level_2_code" ] })
).not_to have_valid_field(:group_by)

expect(
validate_schema(input_params: { group_by: [ "address.iso_region_code", "address.administrative_division_level_3_code" ] })
).not_to have_valid_field(:group_by)

expect(
validate_schema(input_params: { group_by: [ "address.iso_region_code", "address.administrative_division_level_2_code", "address.administrative_division_level_3_code" ] })
).to have_valid_field(:group_by)
end

it "handles post processing" do
result = validate_schema(
input_params: {
filter: {
gender: "M",
"iso_country_code": "KH"
},
group_by: [ "iso_country_code", "gender", "address.iso_region_code" ]
}
).output

expect(result[:filter_fields]).to include(
BeneficiaryStatsRequestSchema::FIELDS.fetch("gender") => "M",
BeneficiaryStatsRequestSchema::FIELDS.fetch("iso_country_code") => "KH"
)
expect(result[:group_by_fields]).to contain_exactly(
BeneficiaryStatsRequestSchema::FIELDS.fetch("iso_country_code"),
BeneficiaryStatsRequestSchema::FIELDS.fetch("gender"),
BeneficiaryStatsRequestSchema::FIELDS.fetch("address.iso_region_code")
)
end

def validate_schema(input_params:, options: {})
BeneficiaryStatsRequestSchema.new(
input_params:,
options: options.reverse_merge(account: build_stubbed(:account))
)
end
end
end

0 comments on commit 8e41583

Please sign in to comment.