Skip to content

Commit 257cf8f

Browse files
committed
Fix flaky query spec
1 parent c86f218 commit 257cf8f

File tree

1 file changed

+58
-53
lines changed

1 file changed

+58
-53
lines changed
Lines changed: 58 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,6 @@
11
RSpec.describe DistributionSummaryByCountyQuery do
22
let(:organization) { create(:organization, name: "Some Unique Name") }
33
let(:item_1) { create(:item, value_in_cents: 1050, organization: organization) }
4-
let(:partner_1) do
5-
create(:partner, organization:, without_profile: true) do |p|
6-
p.profile = create(:partner_profile, partner: p, organization:) do |pp|
7-
pp.served_areas = create_list(:partners_served_area, 4, partner_profile: pp, client_share: 25) do |sa|
8-
sa.county = create(:county)
9-
end
10-
end
11-
end
12-
end
13-
let(:partner_2) do
14-
create(:partner, organization:, without_profile: true) do |p|
15-
p.profile = create(:partner_profile, partner: p, organization:) do |pp|
16-
pp.served_areas = create_list(:partners_served_area, 5, partner_profile: pp, client_share: 20) do |sa, i|
17-
# create one overlapping service area
18-
sa.county = i.zero? ? partner_1.profile.served_areas[0].county : create(:county)
19-
end
20-
end
21-
end
22-
end
23-
244
let(:now) { Time.current.to_datetime }
255

266
let(:params) { {organization_id: organization.id, start_date: nil, end_date: nil} }
@@ -34,46 +14,71 @@
3414
expect(breakdown[0].value).to be_within(0.01).of(105000.0)
3515
end
3616

37-
it "divides the item numbers and values according to the partner profile" do
38-
create(:distribution, :with_items, item: item_1, organization: organization, partner: partner_1)
39-
breakdown = DistributionSummaryByCountyQuery.call(**params)
40-
expect(breakdown.size).to eq(5)
41-
expect(breakdown[4].quantity).to eq(0)
42-
expect(breakdown[4].value).to be_within(0.01).of(0)
43-
3.times do |i|
44-
expect(breakdown[i].quantity).to eq(25)
45-
expect(breakdown[i].value).to be_within(0.01).of(26250.0)
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
4626
end
47-
end
4827

49-
it "handles multiple partners with overlapping service areas properly" do
50-
create(:distribution, :with_items, item: item_1, organization: organization, partner: partner_1, issued_at: now)
51-
create(:distribution, :with_items, item: item_1, organization: organization, partner: partner_2, issued_at: now)
52-
breakdown = DistributionSummaryByCountyQuery.call(**params)
53-
num_with_45 = 0
54-
num_with_20 = 0
55-
num_with_0 = 0
56-
# 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
57-
breakdown.each do |sa|
58-
if sa.quantity == 45
59-
expect(sa.value).to be_within(0.01).of(47250.0)
60-
num_with_45 += 1
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)
6137
end
38+
end
6239

63-
if sa.quantity == 25
64-
expect(sa.value).to be_within(0.01).of(26250.0)
65-
end
66-
if sa.quantity == 20
67-
expect(sa.value).to be_within(0.01).of(21000.0)
68-
num_with_20 += 1
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
6950
end
70-
if sa.quantity == 0
71-
expect(sa.value).to be_within(0.01).of(0)
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
7280
end
7381
end
74-
expect(num_with_45).to be > 0
75-
expect(num_with_20).to be > 0
76-
expect(num_with_0).to eq 0
7782
end
7883
end
7984
end

0 commit comments

Comments
 (0)