forked from rubyforgood/human-essentials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GREEN add Rails console method for generating comparison pdfs, update…
… comparison PDFs
- Loading branch information
Showing
5 changed files
with
104 additions
and
85 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
end | ||
|
||
describe "pdf item and column displays" do | ||
let(:org_hiding_packages_and_values) { | ||
|
@@ -30,15 +17,15 @@ | |
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 with custom units feature" do | ||
Flipper.enable(:enable_packs) | ||
results = described_class.new(organization, distribution).request_data | ||
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"], | ||
|
@@ -51,7 +38,7 @@ | |
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"], | ||
|
@@ -147,70 +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]" } | ||
let(:expected_pickup_file_path) { Rails.root.join("spec", "fixtures", "files", "distribution_pickup.pdf") } | ||
let(:expected_same_address_file_path) { Rails.root.join("spec", "fixtures", "files", "distribution_same_address.pdf") } | ||
let(:expected_different_address_file_path) { Rails.root.join("spec", "fixtures", "files", "distribution_program_address.pdf") } | ||
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_path) | ||
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_path) | ||
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_path) | ||
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_path) | ||
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_path) | ||
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_path) | ||
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_path) | ||
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_path) | ||
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_path) | ||
compare_pdf(@organization, create_dist(@partner, @organization, @storage_location, @item1, @item2, @item3, @item4, :pick_up), @expected_pickup_file_path) | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,42 @@ | ||
module DistributionPDFHelper | ||
def create_profile(program_address1, program_address2, program_city, program_state, program_zip, | ||
address1 = "Example Address 1", city = "Example City", state = "Example State", zip = "12345") | ||
def create_organization_storage_items | ||
org = FactoryBot.create(:organization, | ||
name: "Essentials Bank 1", | ||
street: "1500 Remount Road", | ||
city: "Front Royal", | ||
state: "VA", | ||
zipcode: "22630", | ||
email: "[email protected]") | ||
|
||
create(:partner_profile, | ||
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] | ||
end | ||
|
||
def create_partner(organization) | ||
FactoryBot.create(:partner, :uninvited, without_profile: true, | ||
name: "Leslie Sue", | ||
organization: organization) | ||
end | ||
|
||
def create_file_paths | ||
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, | ||
primary_contact_name: "Jaqueline Kihn DDS", | ||
primary_contact_email: "[email protected]", | ||
address1: address1, | ||
address2: "", | ||
city: city, | ||
|
@@ -18,58 +49,78 @@ def create_profile(program_address1, program_address2, program_city, program_sta | |
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_without_program_address(partner) | ||
create_profile(partner: partner, program_address1: "", program_address2: "", program_city: "", program_state: "", program_zip: "") | ||
end | ||
|
||
def create_profile_with_program_address | ||
create_profile("Example Program Address 1", "", "Example Program City", "Example Program State", 54321) | ||
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_line_items_request(distribution) | ||
create(:line_item, itemizable: distribution, item: item1, quantity: 50) | ||
create(:line_item, itemizable: distribution, item: item2, quantity: 100) | ||
create(:item_unit, item: item4, name: "pack") | ||
create(:request, :with_item_requests, distribution: distribution, | ||
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(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_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_path) | ||
def compare_pdf(organization, distribution, expected_file_path) | ||
pdf = DistributionPdf.new(organization, distribution) | ||
begin | ||
pdf_file = pdf.compute_and_render | ||
|
||
# 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 | ||
|
||
# helper function you can call from Rails console to create comparison pdfs | ||
def create_comparison_pdfs | ||
# create_inventory | ||
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 | ||
|
||
# create_profile no address | ||
pdf_file = DistributionPdf.new(organization, create_dist(:pickup)).compute_and_render | ||
File.binwrite(:expected_pickup_file_path, pdf_file) | ||
# 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_profile without_program_address | ||
pdf_file = DistributionPdf.new(organization, create_dist(:delivery)).compute_and_render | ||
File.binwrite(:expected_same_address_file_path, pdf_file) | ||
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) | ||
|
||
# create_profile_with_program_address | ||
pdf_file = DistributionPdf.new(organization, create_dist(:shipped)).compute_and_render | ||
File.binwrite(:expected_different_address_file_path, pdf_file) | ||
storage_location.destroy | ||
item1.destroy | ||
item2.destroy | ||
item3.destroy | ||
item4.destroy | ||
org.destroy | ||
end | ||
end |