Skip to content

Commit

Permalink
Change query spec to be closer to original spec
Browse files Browse the repository at this point in the history
  • Loading branch information
coalest committed Dec 16, 2024
1 parent 6b8d7c1 commit f14d05d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 65 deletions.
2 changes: 1 addition & 1 deletion app/queries/distribution_summary_by_county_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class DistributionSummaryByCountyQuery
SQL

class << self
def call(organization_id:, start_date:, end_date:)
def call(organization_id:, start_date: nil, end_date: nil)
params = {
organization_id: organization_id,
start_date: start_date || "1000-01-01",
Expand Down
111 changes: 47 additions & 64 deletions spec/queries/distribution_summary_by_county_query_spec.rb
Original file line number Diff line number Diff line change
@@ -1,84 +1,67 @@
RSpec.describe DistributionSummaryByCountyQuery do
let(:organization) { create(:organization, name: "Some Unique Name") }
let(:item_1) { create(:item, value_in_cents: 1050, organization: organization) }
let(:now) { Time.current.to_datetime }
let(:year) { Time.current.year }
let(:issued_at_last_year) { Time.current.change(year: year - 1).to_datetime }
let(:distributions) { [] }
let(:organization_id) { organization.id }
let(:start_date) { nil }
let(:end_date) { nil }
let(:params) { {organization_id:, start_date:, end_date:} }

let(:params) { {organization_id: organization.id, start_date: nil, end_date: nil} }
include_examples "distribution_by_county"

describe "call" do
before do
create(:storage_location, organization: organization)
end

describe "get_breakdown" do
it "will have 100% unspecified shows if no served_areas" do
create(:distribution, :with_items, item: item_1, organization: organization)
create(:distribution, :with_items, item: item_1, organization: user.organization)
breakdown = DistributionSummaryByCountyQuery.call(**params)
expect(breakdown.size).to eq(1)
expect(breakdown[0].quantity).to eq(100)
expect(breakdown[0].value).to be_within(0.01).of(105000.0)
end

context "with matching partner served areas" do
let!(:partner_1) do
create(:partner, organization:, without_profile: true) do |p|
p.profile = create(:partner_profile, partner: p, organization:) do |pp|
pp.served_areas = create_list(:partners_served_area, 4, partner_profile: pp, client_share: 25) do |sa|
sa.county = create(:county)
end
end
end
it "divides the item numbers and values according to the partner profile" do
create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1)
breakdown = DistributionSummaryByCountyQuery.call(**params)
expect(breakdown.size).to eq(5)
expect(breakdown[4].quantity).to eq(0)
expect(breakdown[4].value).to be_within(0.01).of(0)
3.times do |i|
expect(breakdown[i].quantity).to eq(25)
expect(breakdown[i].value).to be_within(0.01).of(26250.0)
end
end

it "divides the item numbers and values according to the partner profile" do
create(:distribution, :with_items, item: item_1, organization: organization, partner: partner_1)
breakdown = DistributionSummaryByCountyQuery.call(**params)
expect(breakdown.size).to eq(5)
expect(breakdown[4].quantity).to eq(0)
expect(breakdown[4].value).to be_within(0.01).of(0)
3.times do |i|
expect(breakdown[i].quantity).to eq(25)
expect(breakdown[i].value).to be_within(0.01).of(26250.0)
it "handles multiple partners with overlapping service areas properly" do
create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1, issued_at: issued_at_present)
create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_2, issued_at: issued_at_present)
breakdown = DistributionSummaryByCountyQuery.call(**params)
num_with_45 = 0
num_with_20 = 0
num_with_0 = 0
# The result will have at least 1 45 and at least 1 20, and 1 0. Anything else will be either 45 or 25 or 20
breakdown.each do |sa|
if sa.quantity == 45
expect(sa.value).to be_within(0.01).of(47250.0)
num_with_45 += 1
end
end

context "with overlapping served areas" do
let!(:partner_2) do
create(:partner, organization:, without_profile: true) do |p|
p.profile = create(:partner_profile, partner: p, organization:) do |pp|
pp.served_areas = create_list(:partners_served_area, 5, partner_profile: pp, client_share: 20) do |sa, i|
# create one overlapping service area
sa.county = i.zero? ? partner_1.profile.served_areas[0].county : create(:county)
end
end
end
if sa.quantity == 25
expect(sa.value).to be_within(0.01).of(26250.0)
end

it "handles multiple partners with overlapping service areas properly" do
create(:distribution, :with_items, item: item_1, organization: organization, partner: partner_1, issued_at: now)
create(:distribution, :with_items, item: item_1, organization: organization, partner: partner_2, issued_at: now)
breakdown = DistributionSummaryByCountyQuery.call(**params)
num_with_45 = 0
num_with_20 = 0
num_with_0 = 0
# The result will have at least 1 45 and at least 1 20, and 1 0. Anything else will be either 45 or 25 or 20
breakdown.each do |sa|
if sa.quantity == 45
expect(sa.value).to be_within(0.01).of(47250.0)
num_with_45 += 1
end

if sa.quantity == 25
expect(sa.value).to be_within(0.01).of(26250.0)
end
if sa.quantity == 20
expect(sa.value).to be_within(0.01).of(21000.0)
num_with_20 += 1
end
if sa.quantity == 0
expect(sa.value).to be_within(0.01).of(0)
end
end
expect(num_with_45).to be > 0
expect(num_with_20).to be > 0
expect(num_with_0).to eq 0
if sa.quantity == 20
expect(sa.value).to be_within(0.01).of(21000.0)
num_with_20 += 1
end
if sa.quantity == 0
expect(sa.value).to be_within(0.01).of(0)
end
end
expect(num_with_45).to be > 0
expect(num_with_20).to be > 0
expect(num_with_0).to eq 0
end
end
end

0 comments on commit f14d05d

Please sign in to comment.