|
1 | 1 | 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:} } |
5 | 9 |
|
6 | | - let(:params) { {organization_id: organization.id, start_date: nil, end_date: nil} } |
| 10 | + include_examples "distribution_by_county" |
7 | 11 |
|
8 | | - describe "call" do |
| 12 | + before do |
| 13 | + create(:storage_location, organization: organization) |
| 14 | + end |
| 15 | + |
| 16 | + describe "get_breakdown" do |
9 | 17 | 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) |
11 | 19 | breakdown = DistributionSummaryByCountyQuery.call(**params) |
12 | 20 | expect(breakdown.size).to eq(1) |
13 | 21 | expect(breakdown[0].quantity).to eq(100) |
14 | 22 | expect(breakdown[0].value).to be_within(0.01).of(105000.0) |
15 | 23 | end |
16 | 24 |
|
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) |
26 | 34 | end |
| 35 | + end |
27 | 36 |
|
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 |
37 | 49 | end |
38 | | - end |
39 | 50 |
|
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) |
50 | 53 | 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) |
80 | 60 | end |
81 | 61 | 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 |
82 | 65 | end |
83 | 66 | end |
84 | 67 | end |
0 commit comments