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 9 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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ dump.rdb
.DS_Store
.ruby-gemset
*.pdf
!spec/fixtures/files/*.pdf
/spec/example_failures.txt

# Track, don't ignore, RSpec comparison PDFs
!spec/fixtures/files/*.pdf

# Ignore Docker stuff (see issues #503, #603)
Dockerfile
docker-compose.yml
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -798,4 +798,4 @@ DEPENDENCIES
webmock (~> 3.24)

BUNDLED WITH
2.5.21
2.5.22
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_08_30_015517) do
ActiveRecord::Schema[7.1].define(version: 2024_10_02_205346) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down
Binary file modified spec/fixtures/files/distribution_pickup.pdf
Binary file not shown.
Binary file modified spec/fixtures/files/distribution_program_address.pdf
Binary file not shown.
Binary file modified spec/fixtures/files/distribution_same_address.pdf
Binary file not shown.
117 changes: 26 additions & 91 deletions spec/pdfs/distribution_pdf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,9 @@
end

describe DistributionPdf do
let(:organization) {
create(:organization,
name: "Essentials Bank 1",
street: "1500 Remount Road",
city: "Front Royal",
state: "VA",
zipcode: "22630",
email: "[email protected]")
}

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

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) }
before(:each) do
@organization, @storage_location, @item1, @item2, @item3, @item4 = create_organization_storage_items
Copy link
Collaborator

Choose a reason for hiding this comment

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

There's no need to do this via instance variables. Here's how I'd do it to keep the RSpec looking the same:

let(:storage_creation) { create_organization_storage_items }
let(:organization) { storage_creation.organization }
let(:item1) { storage_creation.items[0] }
# etc.

See below where you can change this method to make it easier to work with.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed in 6da7702

end

describe "pdf item and column displays" do
let(:org_hiding_packages_and_values) {
Expand All @@ -30,27 +17,28 @@
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) }
let(:distribution) { create(:distribution, organization: @organization, storage_location: @storage_location) }

before(:each) do
create_line_items_request(distribution)
create_line_items_request(distribution, @item1, @item2, @item3, @item4)
end

specify "#request_data" do
results = described_class.new(organization, distribution).request_data
specify "#request_data with custom units feature" do
Flipper.enable(:enable_packs)
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],
["Item 4", "120 packs", "", "$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
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"],
Expand Down Expand Up @@ -146,104 +134,51 @@
end

describe "address pdf output" do
let(:partner) {
create(:partner, :uninvited, without_profile: true,
name: "Leslie Sue",
organization: organization)
}
let(:profile_name) { "Jaqueline Kihn DDS" }
let(:profile_email) { "[email protected]" }
# 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) }
before(:each) do
@partner = create_partner(@organization)
@expected_pickup_file_path, @expected_same_address_file_path, @expected_different_address_file_path = create_file_paths
end

context "when the partner has no addresses" do
before(:each) do
create(:partner_profile,
partner_id: partner.id,
primary_contact_name: profile_name,
primary_contact_email: profile_email,
address1: "",
address2: "",
city: "",
state: "",
zip_code: "",
program_address1: "",
program_address2: "",
program_city: "",
program_state: "",
program_zip_code: "")
create_profile_no_address(@partner)
end
it "doesn't print any address if the delivery type is pickup" do
compare_pdf(create_dist(:pick_up), expected_pickup_file)
compare_pdf(@organization, create_dist(@partner, @organization, @storage_location, @item1, @item2, @item3, @item4, :pick_up), @expected_pickup_file_path)
end
it "doesn't print any address if the delivery type is delivery" do
compare_pdf(create_dist(:delivery), expected_pickup_file)
compare_pdf(@organization, create_dist(@partner, @organization, @storage_location, @item1, @item2, @item3, @item4, :delivery), @expected_pickup_file_path)
end
it "doesn't print any address if the delivery type is shipped" do
compare_pdf(create_dist(:shipped), expected_pickup_file)
compare_pdf(@organization, create_dist(@partner, @organization, @storage_location, @item1, @item2, @item3, @item4, :shipped), @expected_pickup_file_path)
end
end
context "when the partner doesn't have a different program address" do
before(:each) do
create_profile_without_program_address
create_profile_without_program_address(@partner)
end
it "prints the address if the delivery type is delivery" do
compare_pdf(create_dist(:delivery), expected_same_address_file)
compare_pdf(@organization, create_dist(@partner, @organization, @storage_location, @item1, @item2, @item3, @item4, :delivery), @expected_same_address_file_path)
end
it "prints the address if the delivery type is shipped" do
compare_pdf(create_dist(:shipped), expected_same_address_file)
compare_pdf(@organization, create_dist(@partner, @organization, @storage_location, @item1, @item2, @item3, @item4, :shipped), @expected_same_address_file_path)
end
it "doesn't print the address if the delivery type is pickup" do
compare_pdf(create_dist(:pick_up), expected_pickup_file)
compare_pdf(@organization, create_dist(@partner, @organization, @storage_location, @item1, @item2, @item3, @item4, :pick_up), @expected_pickup_file_path)
end
end
context "when the partner has a different program/delivery address" do
before(:each) do
create_profile_with_program_address
create_profile_with_program_address(@partner)
end
it "prints the delivery address if the delivery type is delivery" do
compare_pdf(create_dist(:delivery), expected_different_address_file)
compare_pdf(@organization, create_dist(@partner, @organization, @storage_location, @item1, @item2, @item3, @item4, :delivery), @expected_different_address_file_path)
end
it "prints the delivery address if the delivery type is shipped" do
compare_pdf(create_dist(:shipped), expected_different_address_file)
compare_pdf(@organization, create_dist(@partner, @organization, @storage_location, @item1, @item2, @item3, @item4, :shipped), @expected_different_address_file_path)
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
# 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"
compare_pdf(@organization, create_dist(@partner, @organization, @storage_location, @item1, @item2, @item3, @item4, :pick_up), @expected_pickup_file_path)
end
end
end
Expand Down
121 changes: 98 additions & 23 deletions spec/support/distribution_pdf_helper.rb
Original file line number Diff line number Diff line change
@@ -1,51 +1,126 @@
module DistributionPDFHelper
private def create_profile(program_address1, program_address2, program_city, program_state, program_zip)
create(:partner_profile,
def create_organization_storage_items
org = FactoryBot.create(:organization,
Copy link
Collaborator

Choose a reason for hiding this comment

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

If this setup is being used outside RSpec, it shouldn't be in spec/support and it shouldn't use FactoryBot. It should work more like the seeds file where you create data manually. Probably should live in its own folder under the /lib directory.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed in ea7e241

name: "Essentials Bank 1",
street: "1500 Remount Road",
city: "Front Royal",
state: "VA",
zipcode: "22630",
email: "[email protected]")

storage_location = FactoryBot.create(:storage_location, organization: org)
item1 = FactoryBot.create(:item, name: "Item 1", package_size: 50, value_in_cents: 100)
item2 = FactoryBot.create(:item, name: "Item 2", value_in_cents: 200)
item3 = FactoryBot.create(:item, name: "Item 3", value_in_cents: 300)
item4 = FactoryBot.create(:item, name: "Item 4", package_size: 25, value_in_cents: 400)

[org, storage_location, item1, item2, item3, item4]
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd define a struct above, something like

StorageCreation = Data.define(:organization, :storage_location, :items)

and here you could return

StorageCreation.new(org, storage_location, [item1, item2, item3, item4])

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed in 6da7702

end

def create_partner(organization)
FactoryBot.create(:partner, :uninvited, without_profile: true,
name: "Leslie Sue",
organization: organization)
end

def create_file_paths
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are you actually creating file paths here? Or just returning the ones you have specified?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

renamed to get_file_paths in 6da7702

expected_pickup_file_path = Rails.root.join("spec", "fixtures", "files", "distribution_pickup.pdf")
expected_same_address_file_path = Rails.root.join("spec", "fixtures", "files", "distribution_same_address.pdf")
expected_different_address_file_path = Rails.root.join("spec", "fixtures", "files", "distribution_program_address.pdf")
[expected_pickup_file_path, expected_same_address_file_path, expected_different_address_file_path]
end

private def create_profile(partner:, program_address1:, program_address2:, program_city:, program_state:, program_zip:,
address1: "Example Address 1", city: "Example City", state: "Example State", zip: "12345")

FactoryBot.create(:partner_profile,
partner_id: partner.id,
primary_contact_name: profile_name,
primary_contact_email: profile_email,
address1: "Example Address 1",
primary_contact_name: "Jaqueline Kihn DDS",
primary_contact_email: "[email protected]",
address1: address1,
address2: "",
city: "Example City",
state: "Example State",
zip_code: "12345",
city: city,
state: state,
zip_code: zip,
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("", "", "", "", "")
def create_profile_no_address(partner)
create_profile(partner: partner, program_address1: "", program_address2: "", program_city: "", program_state: "", program_zip: "", address1: "", city: "", state: "", zip: "")
end

def create_profile_with_program_address
create_profile("Example Program Address 1", "", "Example Program City", "Example Program State", 54321)
def create_profile_without_program_address(partner)
create_profile(partner: partner, program_address1: "", program_address2: "", program_city: "", program_state: "", program_zip: "")
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}])
def create_profile_with_program_address(partner)
create_profile(partner: partner, program_address1: "Example Program Address 1", program_address2: "", program_city: "Example Program City", program_state: "Example Program State", program_zip: 54321)
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)
def create_line_items_request(distribution, item1, item2, item3, item4)
FactoryBot.create(:line_item, itemizable: distribution, item: item1, quantity: 50)
FactoryBot.create(:line_item, itemizable: distribution, item: item2, quantity: 100)
FactoryBot.create(:item_unit, item: item4, name: "pack")
FactoryBot.create(:request, :with_item_requests, distribution: distribution,
request_items: [
{"item_id" => item2.id, "quantity" => 30},
{"item_id" => item3.id, "quantity" => 50},
{"item_id" => item4.id, "quantity" => 120, "request_unit" => "pack"}
])
end

def create_dist(partner, organization, storage_location, item1, item2, item3, item4, delivery_method)
Time.zone = "America/Los_Angeles"
dist = FactoryBot.create(:distribution, partner: partner, delivery_method: delivery_method, issued_at: DateTime.new(2024, 7, 4, 0, 0, 0, "-07:00"), organization: organization, storage_location: storage_location)
create_line_items_request(dist, item1, item2, item3, item4)
dist
end

def compare_pdf(distribution, expected_file)
def compare_pdf(organization, distribution, expected_file_path)
pdf = DistributionPdf.new(organization, distribution)
begin
pdf_file = pdf.compute_and_render
expect(pdf_file).to eq(expected_file)

# Run the following from Rails sandbox console (bin/rails/console --sandbox) to regenerate these comparison PDFs:
# => load "spec/support/distribution_pdf_helper.rb"
# => Rails::ConsoleMethods.send(:prepend, DistributionPDFHelper)
# => create_comparison_pdfs
expect(pdf_file).to eq(IO.binread(expected_file_path))
rescue RSpec::Expectations::ExpectationNotMetError => e
File.binwrite(Rails.root.join("tmp", "failed_match_distribution_" + 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

private def create_comparison_pdf(organization, storage_location, item1, item2, item3, item4, profile_create_method, expected_file_path, delivery_method)
partner = create_partner(organization)
profile = profile_create_method.bind_call(Class.new.extend(DistributionPDFHelper), partner)
dist = create_dist(partner, organization, storage_location, item1, item2, item3, item4, delivery_method)
pdf_file = DistributionPdf.new(organization, dist).compute_and_render
File.binwrite(expected_file_path, pdf_file)
profile.destroy
dist.destroy
partner.destroy
end

# helper function that can be called from Rails console to generate comparison PDFs
def create_comparison_pdfs
org, storage_location, item1, item2, item3, item4 = create_organization_storage_items
expected_pickup_file_path, expected_same_address_file_path, expected_different_address_file_path = create_file_paths

create_comparison_pdf(org, storage_location, item1, item2, item3, item4, DistributionPDFHelper.instance_method(:create_profile_no_address), expected_pickup_file_path, :pick_up)
create_comparison_pdf(org, storage_location, item1, item2, item3, item4, DistributionPDFHelper.instance_method(:create_profile_without_program_address), expected_same_address_file_path, :shipped)
create_comparison_pdf(org, storage_location, item1, item2, item3, item4, DistributionPDFHelper.instance_method(:create_profile_with_program_address), expected_different_address_file_path, :delivery)

storage_location.destroy
item1.destroy
item2.destroy
item3.destroy
item4.destroy
org.destroy
end
end