|
1 | 1 | RSpec.describe DistributionSummaryByCountyQuery do |
2 | 2 | let(:organization) { create(:organization, name: "Some Unique Name") } |
3 | 3 | 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 | | - |
24 | 4 | let(:now) { Time.current.to_datetime } |
25 | 5 |
|
26 | 6 | let(:params) { {organization_id: organization.id, start_date: nil, end_date: nil} } |
|
34 | 14 | expect(breakdown[0].value).to be_within(0.01).of(105000.0) |
35 | 15 | end |
36 | 16 |
|
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 |
46 | 26 | end |
47 | | - end |
48 | 27 |
|
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) |
61 | 37 | end |
| 38 | + end |
62 | 39 |
|
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 |
69 | 50 | 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 |
72 | 80 | end |
73 | 81 | 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 |
77 | 82 | end |
78 | 83 | end |
79 | 84 | end |
0 commit comments