Skip to content

Commit

Permalink
stash progress
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyli97 committed Jul 4, 2024
1 parent 468facd commit afb1fc9
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 2 deletions.
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
/spec/example_failures.txt

# Ignore Docker stuff (see issues #503, #603)
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/partners.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
next if evaluator.try(:without_profile)

# Create associated records
create(:partner_profile, partner_id: partner.id)
create(:partner_profile, partner_id: partner.id, address1: "Example Address 1", program_address2: "Example Address 2", city: "Example City", state: "Example State", zip_code: 12345)
create(:partners_user, email: partner.email, name: partner.name, partner: partner)
end
end
Expand Down
Binary file added spec/fixtures/files/distribution_pickup.pdf
Binary file not shown.
Binary file added spec/fixtures/files/distribution_same_address.pdf
Binary file not shown.
54 changes: 53 additions & 1 deletion spec/pdfs/distribution_pdf_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
require_relative("../support/distribution_pdf_helper")

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

# avoid Rubocop failing with an infinite loop when it checks this cop
# rubocop:disable Layout/ArrayAlignment
describe DistributionPdf do
let(:organization) { create(:organization) }
let(:distribution) { create(:distribution, organization: organization) }
let(:partner) { create(:partner, organization: organization) }
let(:storage_location) { create(:storage_location, 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) }
Expand All @@ -15,7 +23,17 @@
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) }

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

before(:each) do
TestInventory.create_inventory(organization, {
storage_location.id => {
item1.id => 100,
item2.id => 100,
item3.id => 100,
item4.id => 100
}
})
create(:line_item, itemizable: distribution, item: item1, quantity: 50)
create(:line_item, itemizable: distribution, item: item2, quantity: 100)
create(:request, distribution: distribution,
Expand Down Expand Up @@ -130,5 +148,39 @@
end
end
end
# OFFENSE remove system annotation
describe "#compute_and_render", type: :system do
let(:expected_pickup_file_md5) { Digest::MD5.file(Rails.root.join("spec", "fixtures", "files", "distribution_pickup.pdf")).hexdigest }
# let(:expected_pickup_file_md5) { "d" }
context "when the organization doesn't have a different program address" do
let(:expected_file_md5) { Digest::MD5.file(Rails.root.join("spec", "fixtures", "files", "distribution_same_address.pdf")).hexdigest }
# let(:expected_file_md5) { "d" }
it "prints the address if the delivery type is delivery" do
set_delivery_and_compare(described_class, :delivery, organization, distribution, expected_file_md5)
end
it "prints the address if the delivery type is shipped" do
set_delivery_and_compare(described_class, :shipped, organization, distribution, expected_file_md5)
end
it "doesn't print the address if the delivery type is pickup" do
set_delivery_and_compare(described_class, :pick_up, organization, distribution, expected_pickup_file_md5)
end
end
context "when the organization has a different program/delivery address" do
# let(:expected_file_md5) { Digest::MD5.file(Rails.root.join("spec", "fixtures", "files", "distribution_different_program_address.pdf")).hexdigest }
let(:expected_file_md5) { "FAILING TEST DOESNT PRINT PROGRAM ADDRESS" }
before(:each) {
partner.profile.update!(program_address1: "Example Program Address 1", program_address2: "Example Program Address 2", program_city: "Example Program City", program_state: "Example Program State", program_zip_code: 54321)
}
it "prints the delivery address if the delivery type is delivery" do
set_delivery_and_compare(described_class, :delivery, organization, distribution, expected_file_md5)
end
it "prints the delivery address if the delivery type is shipped" do
set_delivery_and_compare(described_class, :shipped, organization, distribution, expected_file_md5)
end
it "doesn't print any address if the delivery type is pickup" do
set_delivery_and_compare(described_class, :pick_up, organization, distribution, expected_pickup_file_md5)
end
end
end
end
# rubocop:enable Layout/ArrayAlignment
18 changes: 18 additions & 0 deletions spec/support/distribution_pdf_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module DistributionPDFHelper
def set_delivery_and_compare(described_class, delivery_method, organization, distribution, expected_file_md5)
distribution.update!(delivery_method: delivery_method, issued_at: DateTime.new(2024, 7, 4, 0, 0))

# user = create(:user, organization: organization)
# sign_in(user)
# visit distributions_path
# binding.pry
#
# use this along with RSpec type: :system to update expected PDF
get print_distribution_path(distribution)
File.write(Rails.root.join("spec", "fixtures", "files", "distribution_expected_RENAME.pdf"), response.body)

pdf = described_class.new(organization, distribution)
output_file_md5 = Digest::MD5.hexdigest pdf.compute_and_render
expect(output_file_md5).to eq(expected_file_md5)
end
end

0 comments on commit afb1fc9

Please sign in to comment.