Skip to content

Commit f14d05d

Browse files
committed
Change query spec to be closer to original spec
1 parent 6b8d7c1 commit f14d05d

File tree

2 files changed

+48
-65
lines changed

2 files changed

+48
-65
lines changed

app/queries/distribution_summary_by_county_query.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class DistributionSummaryByCountyQuery
5656
SQL
5757

5858
class << self
59-
def call(organization_id:, start_date:, end_date:)
59+
def call(organization_id:, start_date: nil, end_date: nil)
6060
params = {
6161
organization_id: organization_id,
6262
start_date: start_date || "1000-01-01",
Lines changed: 47 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,67 @@
11
RSpec.describe DistributionSummaryByCountyQuery do
2-
let(:organization) { create(:organization, name: "Some Unique Name") }
3-
let(:item_1) { create(:item, value_in_cents: 1050, organization: organization) }
4-
let(:now) { Time.current.to_datetime }
2+
let(:year) { Time.current.year }
3+
let(:issued_at_last_year) { Time.current.change(year: year - 1).to_datetime }
4+
let(:distributions) { [] }
5+
let(:organization_id) { organization.id }
6+
let(:start_date) { nil }
7+
let(:end_date) { nil }
8+
let(:params) { {organization_id:, start_date:, end_date:} }
59

6-
let(:params) { {organization_id: organization.id, start_date: nil, end_date: nil} }
10+
include_examples "distribution_by_county"
711

8-
describe "call" do
12+
before do
13+
create(:storage_location, organization: organization)
14+
end
15+
16+
describe "get_breakdown" do
917
it "will have 100% unspecified shows if no served_areas" do
10-
create(:distribution, :with_items, item: item_1, organization: organization)
18+
create(:distribution, :with_items, item: item_1, organization: user.organization)
1119
breakdown = DistributionSummaryByCountyQuery.call(**params)
1220
expect(breakdown.size).to eq(1)
1321
expect(breakdown[0].quantity).to eq(100)
1422
expect(breakdown[0].value).to be_within(0.01).of(105000.0)
1523
end
1624

17-
context "with matching partner served areas" do
18-
let!(:partner_1) do
19-
create(:partner, organization:, without_profile: true) do |p|
20-
p.profile = create(:partner_profile, partner: p, organization:) do |pp|
21-
pp.served_areas = create_list(:partners_served_area, 4, partner_profile: pp, client_share: 25) do |sa|
22-
sa.county = create(:county)
23-
end
24-
end
25-
end
25+
it "divides the item numbers and values according to the partner profile" do
26+
create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1)
27+
breakdown = DistributionSummaryByCountyQuery.call(**params)
28+
expect(breakdown.size).to eq(5)
29+
expect(breakdown[4].quantity).to eq(0)
30+
expect(breakdown[4].value).to be_within(0.01).of(0)
31+
3.times do |i|
32+
expect(breakdown[i].quantity).to eq(25)
33+
expect(breakdown[i].value).to be_within(0.01).of(26250.0)
2634
end
35+
end
2736

28-
it "divides the item numbers and values according to the partner profile" do
29-
create(:distribution, :with_items, item: item_1, organization: organization, partner: partner_1)
30-
breakdown = DistributionSummaryByCountyQuery.call(**params)
31-
expect(breakdown.size).to eq(5)
32-
expect(breakdown[4].quantity).to eq(0)
33-
expect(breakdown[4].value).to be_within(0.01).of(0)
34-
3.times do |i|
35-
expect(breakdown[i].quantity).to eq(25)
36-
expect(breakdown[i].value).to be_within(0.01).of(26250.0)
37+
it "handles multiple partners with overlapping service areas properly" do
38+
create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1, issued_at: issued_at_present)
39+
create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_2, issued_at: issued_at_present)
40+
breakdown = DistributionSummaryByCountyQuery.call(**params)
41+
num_with_45 = 0
42+
num_with_20 = 0
43+
num_with_0 = 0
44+
# 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
45+
breakdown.each do |sa|
46+
if sa.quantity == 45
47+
expect(sa.value).to be_within(0.01).of(47250.0)
48+
num_with_45 += 1
3749
end
38-
end
3950

40-
context "with overlapping served areas" do
41-
let!(:partner_2) do
42-
create(:partner, organization:, without_profile: true) do |p|
43-
p.profile = create(:partner_profile, partner: p, organization:) do |pp|
44-
pp.served_areas = create_list(:partners_served_area, 5, partner_profile: pp, client_share: 20) do |sa, i|
45-
# create one overlapping service area
46-
sa.county = i.zero? ? partner_1.profile.served_areas[0].county : create(:county)
47-
end
48-
end
49-
end
51+
if sa.quantity == 25
52+
expect(sa.value).to be_within(0.01).of(26250.0)
5053
end
51-
52-
it "handles multiple partners with overlapping service areas properly" do
53-
create(:distribution, :with_items, item: item_1, organization: organization, partner: partner_1, issued_at: now)
54-
create(:distribution, :with_items, item: item_1, organization: organization, partner: partner_2, issued_at: now)
55-
breakdown = DistributionSummaryByCountyQuery.call(**params)
56-
num_with_45 = 0
57-
num_with_20 = 0
58-
num_with_0 = 0
59-
# 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
60-
breakdown.each do |sa|
61-
if sa.quantity == 45
62-
expect(sa.value).to be_within(0.01).of(47250.0)
63-
num_with_45 += 1
64-
end
65-
66-
if sa.quantity == 25
67-
expect(sa.value).to be_within(0.01).of(26250.0)
68-
end
69-
if sa.quantity == 20
70-
expect(sa.value).to be_within(0.01).of(21000.0)
71-
num_with_20 += 1
72-
end
73-
if sa.quantity == 0
74-
expect(sa.value).to be_within(0.01).of(0)
75-
end
76-
end
77-
expect(num_with_45).to be > 0
78-
expect(num_with_20).to be > 0
79-
expect(num_with_0).to eq 0
54+
if sa.quantity == 20
55+
expect(sa.value).to be_within(0.01).of(21000.0)
56+
num_with_20 += 1
57+
end
58+
if sa.quantity == 0
59+
expect(sa.value).to be_within(0.01).of(0)
8060
end
8161
end
62+
expect(num_with_45).to be > 0
63+
expect(num_with_20).to be > 0
64+
expect(num_with_0).to eq 0
8265
end
8366
end
8467
end

0 commit comments

Comments
 (0)