Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #4138 fix delivery address #4540

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4bb8332
REFACTOR group examples, remove rubocop disable lines,
jimmyli97 Jul 18, 2024
77b2a55
RED add rspec to test address output in distribution PDFs
jimmyli97 Jul 18, 2024
dcdc0f8
GREEN Fix #4138 address output changes based on delivery method
jimmyli97 Jul 18, 2024
aa38b4b
BUGFIX pdf now writes when test fails
jimmyli97 Jul 23, 2024
f1d7273
RED add rspec for when partner has no addresses
jimmyli97 Jul 23, 2024
d64acc4
GREEN don't print address if no address or delivery address
jimmyli97 Jul 23, 2024
4557128
Merge branch 'main' into 4138-fix-delivery-address
jimmyli97 Sep 12, 2024
0d53723
Add comments clarifying gitignore and comparison pdf helper rspec
jimmyli97 Sep 12, 2024
d722bca
Update bundler version
jimmyli97 Sep 12, 2024
4516951
REFACTOR replace helper test with environment variable
jimmyli97 Sep 24, 2024
ee159ab
Merge branch 'main' into 4138-fix-delivery-address
jimmyli97 Sep 24, 2024
cec74ce
Merge branch 'main' into 4138-fix-delivery-address
jimmyli97 Oct 17, 2024
c26f94b
update schema date and bundler version
jimmyli97 Oct 17, 2024
df4b975
RED remove env variable code, stub out console helper method
jimmyli97 Oct 17, 2024
26ece6e
GREEN add Rails console method for generating comparison pdfs, update…
jimmyli97 Oct 17, 2024
a21c7f2
Merge branch 'main' into 4138-fix-delivery-address
jimmyli97 Nov 27, 2024
6da7702
Replace instance vars with structs and let
jimmyli97 Nov 27, 2024
8bc117e
FIX destroy request so db is clean, use destroy! instead so exception…
jimmyli97 Nov 27, 2024
ea7e241
Replace FactoryBot with calling ActiveModel.create, move methods to lib/
jimmyli97 Nov 28, 2024
39e8cb4
Fix formatting when address is incomplete, add rspecs, create and del…
jimmyli97 Dec 3, 2024
29e59df
Merge branch 'main' into 4138-fix-delivery-address
jimmyli97 Dec 3, 2024
4c6cd65
FIX replace with space if partner primary contact name/email/phone is…
jimmyli97 Dec 3, 2024
b00d1b0
FIX spacing between issued to and delivery address
jimmyli97 Dec 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ dump.rdb
.DS_Store
.ruby-gemset
*.pdf
!spec/fixtures/files/*.pdf
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you adding this if you've added these files? Shouldn't they be tracked?

Copy link
Contributor Author

@jimmyli97 jimmyli97 Sep 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line 51 tells git to ignore all .pdf files so line 52 specifies an exception (by prefixing an exclamation mark) to track the pdf files in spec/fixtures/files

added a comment to clarify in 0d53723

/spec/example_failures.txt

# Ignore Docker stuff (see issues #503, #603)
Expand Down
34 changes: 25 additions & 9 deletions app/pdfs/distribution_pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,39 @@ def compute_and_render
text @distribution.partner.name
move_up 24

profile = @distribution.partner.profile

text "Partner Primary Contact:", style: :bold, align: :right
font_size 12
text @distribution.partner.profile.primary_contact_name, align: :right
text profile.primary_contact_name, align: :right
font_size 10
text @distribution.partner.profile.primary_contact_email, align: :right
text @distribution.partner.profile.primary_contact_phone, align: :right
text profile.primary_contact_email, align: :right
text profile.primary_contact_phone, align: :right
move_down 10

if %w(shipped delivered).include?(@distribution.delivery_method)
if @distribution.delivery? || @distribution.shipped?
if profile.program_address1.blank?
address1 = profile.address1
address2 = profile.address2
city = profile.city
state = profile.state
zip_code = profile.zip_code
else
address1 = profile.program_address1
address2 = profile.program_address2
city = profile.program_city
state = profile.program_state
zip_code = profile.program_zip_code.to_s
end

move_up 10
text "Delivery address:", style: :bold
font_size 10
text @distribution.partner.profile.address1
text @distribution.partner.profile.address2
text @distribution.partner.profile.city
text @distribution.partner.profile.state
text @distribution.partner.profile.zip_code
text address1
text address2
text city
text state
text zip_code
move_up 40

text "Issued on:", style: :bold, align: :right
Expand Down
Binary file added spec/fixtures/files/distribution_pickup.pdf
Binary file not shown.
Binary file not shown.
Binary file added spec/fixtures/files/distribution_same_address.pdf
Binary file not shown.
271 changes: 179 additions & 92 deletions spec/pdfs/distribution_pdf_spec.rb
Original file line number Diff line number Diff line change
@@ -1,54 +1,100 @@
# avoid Rubocop failing with an infinite loop when it checks this cop
# rubocop:disable Layout/ArrayAlignment
require_relative("../support/distribution_pdf_helper")

RSpec.configure do |c|
c.include DistributionPDFHelper
end

describe DistributionPdf do
let(:organization) { create(:organization) }
let(:distribution) { create(:distribution, organization: organization) }
let(:item1) { FactoryBot.create(:item, name: "Item 1", package_size: 50, value_in_cents: 100) }
let(:item2) { FactoryBot.create(:item, name: "Item 2", value_in_cents: 200) }
let(:item3) { FactoryBot.create(:item, name: "Item 3", value_in_cents: 300) }
let(:item4) { FactoryBot.create(:item, name: "Item 4", package_size: 25, value_in_cents: 400) }

let(:org_hiding_packages_and_values) do
FactoryBot.create(:organization, name: DEFAULT_TEST_ORGANIZATION_NAME,
hide_value_columns_on_receipt: true, hide_package_column_on_receipt: true)
end
let(:org_hiding_packages) { FactoryBot.create(:organization, name: DEFAULT_TEST_ORGANIZATION_NAME, hide_package_column_on_receipt: true) }
let(:org_hiding_values) { FactoryBot.create(:organization, name: DEFAULT_TEST_ORGANIZATION_NAME, hide_value_columns_on_receipt: true) }

before(:each) do
create(:line_item, itemizable: distribution, item: item1, quantity: 50)
create(:line_item, itemizable: distribution, item: item2, quantity: 100)
create(:request, distribution: distribution,
request_items: [{"item_id" => item2.id, "quantity" => 30},
{"item_id" => item3.id, "quantity" => 50}, {"item_id" => item4.id, "quantity" => 120}])
end
let(:organization) {
create(:organization,
name: "Essentials Bank 1",
street: "1500 Remount Road",
city: "Front Royal",
state: "VA",
zipcode: "22630",
email: "[email protected]")
}

specify "#request_data" do
results = described_class.new(organization, distribution).request_data
expect(results).to eq([
["Items Received", "Requested", "Received", "Value/item", "In-Kind Value Received", "Packages"],
["Item 1", "", 50, "$1.00", "$50.00", "1"],
["Item 2", 30, 100, "$2.00", "$200.00", nil],
["Item 3", 50, "", "$3.00", nil, nil],
["Item 4", 120, "", "$4.00", nil, nil],
["", "", "", "", ""],
["Total Items Received", 200, 150, "", "$250.00", ""]
])
end
let(:storage_location) { create(:storage_location, organization: organization) }

specify "#non_request_data" do
results = described_class.new(organization, distribution).non_request_data
expect(results).to eq([
["Items Received", "Value/item", "In-Kind Value", "Quantity", "Packages"],
["Item 1", "$1.00", "$50.00", 50, "1"],
["Item 2", "$2.00", "$200.00", 100, nil],
["", "", "", "", ""],
["Total Items Received", "", "$250.00", 150, ""]
])
end
let(:item1) { create(:item, name: "Item 1", package_size: 50, value_in_cents: 100) }
let(:item2) { create(:item, name: "Item 2", value_in_cents: 200) }
let(:item3) { create(:item, name: "Item 3", value_in_cents: 300) }
let(:item4) { create(:item, name: "Item 4", package_size: 25, value_in_cents: 400) }

describe "pdf item and column displays" do
let(:org_hiding_packages_and_values) {
create(:organization, name: DEFAULT_TEST_ORGANIZATION_NAME,
hide_value_columns_on_receipt: true, hide_package_column_on_receipt: true)
}
let(:org_hiding_packages) { create(:organization, name: DEFAULT_TEST_ORGANIZATION_NAME, hide_package_column_on_receipt: true) }
let(:org_hiding_values) { create(:organization, name: DEFAULT_TEST_ORGANIZATION_NAME, hide_value_columns_on_receipt: true) }

let(:distribution) { create(:distribution, organization: organization, storage_location: storage_location) }

context "with request data" do
describe "#hide_columns" do
before(:each) do
create_line_items_request(distribution)
end

specify "#request_data" do
results = described_class.new(organization, distribution).request_data
expect(results).to eq([
["Items Received", "Requested", "Received", "Value/item", "In-Kind Value Received", "Packages"],
["Item 1", "", 50, "$1.00", "$50.00", "1"],
["Item 2", 30, 100, "$2.00", "$200.00", nil],
["Item 3", 50, "", "$3.00", nil, nil],
["Item 4", 120, "", "$4.00", nil, nil],
["", "", "", "", ""],
["Total Items Received", 200, 150, "", "$250.00", ""]
])
end

specify "#non_request_data" do
results = described_class.new(organization, distribution).non_request_data
expect(results).to eq([
["Items Received", "Value/item", "In-Kind Value", "Quantity", "Packages"],
["Item 1", "$1.00", "$50.00", 50, "1"],
["Item 2", "$2.00", "$200.00", 100, nil],
["", "", "", "", ""],
["Total Items Received", "", "$250.00", 150, ""]
])
end

context "with request data" do
describe "#hide_columns" do
it "hides value and package columns when true on organization" do
pdf = described_class.new(org_hiding_packages_and_values, distribution)
data = pdf.request_data
pdf.hide_columns(data)
expect(data).to eq([
["Items Received", "Requested", "Received"],
["Item 1", "", 50],
["Item 2", 30, 100],
["Item 3", 50, ""],
["Item 4", 120, ""],
["", "", ""],
["Total Items Received", 200, 150]
])
end

it "hides value columns when true on organization" do
pdf = described_class.new(org_hiding_values, distribution)
data = pdf.request_data
pdf.hide_columns(data)
expect(data).to eq([
["Items Received", "Requested", "Received", "Packages"],
["Item 1", "", 50, "1"],
["Item 2", 30, 100, nil],
["Item 3", 50, "", nil],
["Item 4", 120, "", nil],
["", "", ""],
["Total Items Received", 200, 150, ""]
])
end
end
end

context "with non request data" do
it "hides value and package columns when true on organization" do
pdf = described_class.new(org_hiding_packages_and_values, distribution)
data = pdf.request_data
Expand Down Expand Up @@ -79,56 +125,97 @@
])
end
end
context "regardles of request data" do
describe "#hide_columns" do
it "hides package column when true on organization" do
pdf = described_class.new(org_hiding_packages, distribution)
data = pdf.request_data
pdf.hide_columns(data)
expect(data).to eq([
["Items Received", "Requested", "Received", "Value/item", "In-Kind Value Received"],
["Item 1", "", 50, "$1.00", "$50.00"],
["Item 2", 30, 100, "$2.00", "$200.00"],
["Item 3", 50, "", "$3.00", nil],
["Item 4", 120, "", "$4.00", nil],
["", "", "", "", ""],
["Total Items Received", 200, 150, "", "$250.00"]
])
end
end
end
end

context "with non request data" do
it "hides value and package columns when true on organization" do
pdf = described_class.new(org_hiding_packages_and_values, distribution)
data = pdf.request_data
pdf.hide_columns(data)
expect(data).to eq([
["Items Received", "Requested", "Received"],
["Item 1", "", 50],
["Item 2", 30, 100],
["Item 3", 50, ""],
["Item 4", 120, ""],
["", "", ""],
["Total Items Received", 200, 150]
])
end
describe "address pdf output" do
let(:partner) {
create(:partner, :uninvited, without_profile: true,
name: "Leslie Sue",
organization: organization)
}
# there is a helper test at the bottom to regenerate these PDFs easily
let(:expected_pickup_file_path) { Rails.root.join("spec", "fixtures", "files", "distribution_pickup.pdf") }
let(:expected_pickup_file) { IO.binread(expected_pickup_file_path) }
let(:expected_same_address_file_path) { Rails.root.join("spec", "fixtures", "files", "distribution_same_address.pdf") }
let(:expected_same_address_file) { IO.binread(expected_same_address_file_path) }
let(:expected_different_address_file_path) { Rails.root.join("spec", "fixtures", "files", "distribution_program_address.pdf") }
let(:expected_different_address_file) { IO.binread(expected_different_address_file_path) }

it "hides value columns when true on organization" do
pdf = described_class.new(org_hiding_values, distribution)
data = pdf.request_data
pdf.hide_columns(data)
expect(data).to eq([
["Items Received", "Requested", "Received", "Packages"],
["Item 1", "", 50, "1"],
["Item 2", 30, 100, nil],
["Item 3", 50, "", nil],
["Item 4", 120, "", nil],
["", "", ""],
["Total Items Received", 200, 150, ""]
])
context "when the organization doesn't have a different program address" do
before(:each) do
create_profile_without_program_address
end
it "prints the address if the delivery type is delivery" do
compare_pdf(create_dist(:delivery), expected_same_address_file)
end
it "prints the address if the delivery type is shipped" do
compare_pdf(create_dist(:shipped), expected_same_address_file)
end
it "doesn't print the address if the delivery type is pickup" do
compare_pdf(create_dist(:pick_up), expected_pickup_file)
end
end
end
context "regardles of request data" do
describe "#hide_columns" do
it "hides package column when true on organization" do
pdf = described_class.new(org_hiding_packages, distribution)
data = pdf.request_data
pdf.hide_columns(data)
expect(data).to eq([
["Items Received", "Requested", "Received", "Value/item", "In-Kind Value Received"],
["Item 1", "", 50, "$1.00", "$50.00"],
["Item 2", 30, 100, "$2.00", "$200.00"],
["Item 3", 50, "", "$3.00", nil],
["Item 4", 120, "", "$4.00", nil],
["", "", "", "", ""],
["Total Items Received", 200, 150, "", "$250.00"]
])
context "when the organization has a different program/delivery address" do
before(:each) do
create_profile_with_program_address
end
it "prints the delivery address if the delivery type is delivery" do
compare_pdf(create_dist(:delivery), expected_different_address_file)
end
it "prints the delivery address if the delivery type is shipped" do
compare_pdf(create_dist(:shipped), expected_different_address_file)
end
it "doesn't print any address if the delivery type is pickup" do
compare_pdf(create_dist(:pick_up), expected_pickup_file)
end
end
# this test is a helper function to regenerate expected PDFs, only commit with it skipped
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's really confusing. If you want a helper method, why not define it as a method, and people can run it from the Rails console?

Copy link
Contributor Author

@jimmyli97 jimmyli97 Sep 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to generate the pdf files correctly, the helper method generating the file needs to have the same test environment setup as the actual test itself. This way you don't have to repeat code setting up the test environment. I did research but I could not figure out a better way to do it without duplicating code.

you can call this method from the terminal by calling the test's line number e.g. bundle exec rspec spec/pdfs/distribution_pdf_spec.rb:227.

I added a comment clarifying this in 0d53723

alternatively I could delete the method if you want, but I found it super helpful because I could just run that line whenever I made changes to the pdf generation and regenerate the test files. so if anyone in the future makes changes they could do the same thing.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should definitely be a way to do this without duplicating code. Maybe it's the setup that needs to be extracted to a base method that's called from both places. And worse comes to worst, if you do need to duplicate a few lines of code, it's still a lot less messy than a test that's not actually a test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I came up with a solution which checks for an environment variable and if it's set to true to overwrite the expected PDFs in 4516951 . I think it's a lot cleaner, let me know if that works.

# rubocop:disable Lint/LiteralAsCondition
if false
# rubocop:enable Lint/LiteralAsCondition
it "skip this helper function for regenerating the expected pdfs", type: :request do
user = create(:user, organization: organization)
sign_in(user)

profile = create_profile_without_program_address

dist = create_dist(:pick_up)
get print_distribution_path(dist)
File.binwrite(expected_pickup_file_path, response.body)
dist.destroy

dist = create_dist(:shipped)
get print_distribution_path(dist)
File.binwrite(expected_same_address_file_path, response.body)
dist.destroy

profile.destroy
create_profile_with_program_address

dist = create_dist(:shipped)
get print_distribution_path(dist)
File.binwrite(expected_different_address_file_path, response.body)

raise "Do not commit this helper function"
end
end
end
end
# rubocop:enable Layout/ArrayAlignment
51 changes: 51 additions & 0 deletions spec/support/distribution_pdf_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module DistributionPDFHelper
private def create_profile(program_address1, program_address2, program_city, program_state, program_zip)
create(:partner_profile,
partner_id: partner.id,
primary_contact_name: "Jaqueline Kihn DDS",
primary_contact_email: "[email protected]",
address1: "Example Address 1",
address2: "",
city: "Example City",
state: "Example State",
zip_code: "12345",
program_address1: program_address1,
program_address2: program_address2,
program_city: program_city,
program_state: program_state,
program_zip_code: program_zip)
end

def create_profile_without_program_address
create_profile("", "", "", "", "")
end

def create_profile_with_program_address
create_profile("Example Program Address 1", "", "Example Program City", "Example Program State", 54321)
end

def create_line_items_request(distribution)
create(:line_item, itemizable: distribution, item: item1, quantity: 50)
create(:line_item, itemizable: distribution, item: item2, quantity: 100)
create(:request, distribution: distribution,
request_items: [{"item_id" => item2.id, "quantity" => 30},
{"item_id" => item3.id, "quantity" => 50}, {"item_id" => item4.id, "quantity" => 120}])
end

def create_dist(delivery_method)
dist = create(:distribution, partner: partner, delivery_method: delivery_method, issued_at: DateTime.new(2024, 7, 4, 0, 0), organization: organization, storage_location: storage_location)
create_line_items_request(dist)
dist
end

def compare_pdf(distribution, expected_file)
pdf = DistributionPdf.new(organization, distribution)
begin
pdf_file = pdf.compute_and_render
expect(pdf_file).to eq(expected_file)
rescue RSpec::Expectations::ExpectationNotMetError => e
File.binwrite(Rails.root.join("tmp", "failed_match_distribution_" + delivery_method.to_s + "_" + Time.current.to_s + ".pdf"), pdf_file)
raise e.class, "PDF does not match, written to tmp/", cause: nil
end
end
end