Skip to content

Commit d62f919

Browse files
committed
Add regression test for Picklist pdf generation
- Generate the pdf and add to fixture - Rename content in PDFComparisonTestFactory to be more domain specific - Add compare_pdf to spec to compare the file to the
1 parent 7c4cca6 commit d62f919

4 files changed

Lines changed: 111 additions & 17 deletions

File tree

lib/test_helpers/pdf_comparison_test_factory.rb

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module PDFComparisonTestFactory
44
extend ActiveSupport::Testing::TimeHelpers
55

66
StorageCreation = Data.define(:organization, :storage_location, :items)
7-
FilePaths = Data.define(:expected_pickup_file_path, :expected_same_address_file_path, :expected_different_address_file_path, :expected_incomplete_address_file_path, :expected_no_contact_file_path)
7+
FilePaths = Data.define(:expected_pickup_file_path, :expected_same_address_file_path, :expected_different_address_file_path, :expected_incomplete_address_file_path, :expected_no_contact_file_path, :expected_picklist_file_path)
88

99
def self.get_logo_file
1010
Rack::Test::UploadedFile.new(Rails.root.join("spec/fixtures/files/logo.jpg"), "image/jpeg")
@@ -41,17 +41,30 @@ def self.create_partner(organization)
4141
Partner.create!(name: "Leslie Sue", organization: organization, email: "leslie1@gmail.com")
4242
end
4343

44+
def self.create_partner_with_quota(organization)
45+
Partner.create!(name: "Leslie Sue", organization: organization, email: "leslie1@gmail.com", quota: 100)
46+
end
47+
4448
def self.get_file_paths
4549
expected_pickup_file_path = Rails.root.join("spec", "fixtures", "files", "distribution_pickup.pdf")
4650
expected_same_address_file_path = Rails.root.join("spec", "fixtures", "files", "distribution_same_address.pdf")
4751
expected_different_address_file_path = Rails.root.join("spec", "fixtures", "files", "distribution_program_address.pdf")
4852
expected_incomplete_address_file_path = Rails.root.join("spec", "fixtures", "files", "distribution_incomplete_address.pdf")
4953
expected_no_contact_file_path = Rails.root.join("spec", "fixtures", "files", "distribution_no_contact.pdf")
50-
FilePaths.new(expected_pickup_file_path, expected_same_address_file_path, expected_different_address_file_path, expected_incomplete_address_file_path, expected_no_contact_file_path)
54+
expected_picklist_file_path = Rails.root.join("spec", "fixtures", "files", "picklist.pdf")
55+
56+
FilePaths.new(
57+
expected_pickup_file_path,
58+
expected_same_address_file_path,
59+
expected_different_address_file_path,
60+
expected_incomplete_address_file_path,
61+
expected_no_contact_file_path,
62+
expected_picklist_file_path
63+
)
5164
end
5265

5366
private_class_method def self.create_profile(partner:, program_address1:, program_address2:, program_city:, program_state:, program_zip:,
54-
address1: "Example Address 1", city: "Example City", state: "Example State", zip: "12345", primary_contact_name: "Jaqueline Kihn DDS", primary_contact_email: "van@durgan.example")
67+
address1: "Example Address 1", city: "Example City", state: "Example State", zip: "12345", primary_contact_name: "Jaqueline Kihn DDS", primary_contact_email: "van@durgan.example", pick_up_name: nil, pick_up_email: nil, pick_up_phone: nil)
5568
Partners::Profile.create!(
5669
partner_id: partner.id,
5770
essentials_bank_id: partner.organization.id,
@@ -66,7 +79,10 @@ def self.get_file_paths
6679
program_address2: program_address2,
6780
program_city: program_city,
6881
program_state: program_state,
69-
program_zip_code: program_zip
82+
program_zip_code: program_zip,
83+
pick_up_name: pick_up_name,
84+
pick_up_email: pick_up_email,
85+
pick_up_phone: pick_up_phone
7086
)
7187
end
7288

@@ -90,9 +106,26 @@ def self.create_profile_no_contact_with_program_address(partner)
90106
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, primary_contact_name: "", primary_contact_email: "")
91107
end
92108

93-
def self.create_line_items_request(distribution, partner, storage_creation)
109+
def self.create_profile_with_pickup_person(partner)
110+
create_profile(
111+
partner: partner,
112+
pick_up_name: "Pickup Person",
113+
pick_up_email: "pickup@example.com",
114+
pick_up_phone: "1234567890",
115+
program_address1: "Example Program Address 1",
116+
program_address2: "",
117+
program_city: "Example Program City",
118+
program_state: "Example Program State",
119+
program_zip: 54321
120+
)
121+
end
122+
123+
def self.create_line_items_for_distribution(distribution, storage_creation)
94124
LineItem.create!(itemizable: distribution, item: storage_creation.items[0], quantity: 50)
95125
LineItem.create!(itemizable: distribution, item: storage_creation.items[1], quantity: 100)
126+
end
127+
128+
def self.create_line_items_request(partner:, storage_creation:, distribution: nil)
96129
storage_creation.organization.request_units.find_or_create_by!(name: "pack")
97130
ItemUnit.find_or_create_by!(item: storage_creation.items[3], name: "pack")
98131
req1 = Partners::ItemRequest.new(item: storage_creation.items[1], quantity: 30, name: storage_creation.items[1].name, partner_key: storage_creation.items[1].partner_key)
@@ -107,30 +140,48 @@ def self.create_line_items_request(distribution, partner, storage_creation)
107140
{"item_id" => storage_creation.items[2].id, "quantity" => 50},
108141
{"item_id" => storage_creation.items[3].id, "quantity" => 120, "request_unit" => "pack"}
109142
],
110-
item_requests: [req1, req2, req3]
143+
item_requests: [req1, req2, req3],
144+
created_at: Time.zone.local(2024, 12, 30, 0, 0, 0)
111145
)
112146
end
113147

114148
def self.create_dist(partner, storage_creation, delivery_method)
115149
Time.zone = "America/Los_Angeles"
116150
dist = Distribution.create!(id: 123, partner: partner, delivery_method: delivery_method, issued_at: DateTime.new(2024, 7, 4, 0, 0, 0, "-07:00"), organization: storage_creation.organization, storage_location: storage_creation.storage_location)
117-
create_line_items_request(dist, partner, storage_creation)
151+
create_line_items_for_distribution(dist, storage_creation)
152+
create_line_items_request(distribution: dist, partner: partner, storage_creation: storage_creation)
118153
dist
119154
end
120155

121-
def self.render_pdf_at_year_end(organization, distribution)
156+
def self.render_distribution_pdf_at_year_end(organization, distribution)
122157
travel_to(Time.zone.local(2024, 12, 30, 0, 0, 0)) do
123158
return DistributionPdf.new(organization, distribution).compute_and_render
124159
end
125160
end
126161

127-
private_class_method def self.create_comparison_pdf(storage_creation, profile_create_method, expected_file_path, delivery_method)
162+
def self.render_picklist_pdf(organization, requests)
163+
PicklistsPdf.new(organization, requests).compute_and_render
164+
end
165+
166+
private_class_method def self.create_distribution_comparison_pdf(storage_creation, profile_create_method, expected_file_path, delivery_method)
128167
# Partner creation must be rolled back otherwise Items requested YTD will accumulate
129168
ActiveRecord::Base.transaction(requires_new: true) do
130169
partner = create_partner(storage_creation.organization)
131170
PDFComparisonTestFactory.public_send(profile_create_method, partner)
132171
dist = create_dist(partner, storage_creation, delivery_method)
133-
pdf_file = render_pdf_at_year_end(storage_creation.organization, dist)
172+
pdf_file = render_distribution_pdf_at_year_end(storage_creation.organization, dist)
173+
File.binwrite(expected_file_path, pdf_file)
174+
raise ActiveRecord::Rollback
175+
end
176+
end
177+
178+
private_class_method def self.create_picklist_comparison_pdf(storage_creation, partner_create_method, profile_create_method, expected_file_path)
179+
# Partner creation must be rolled back otherwise Items requested YTD will accumulate
180+
ActiveRecord::Base.transaction(requires_new: true) do
181+
partner = PDFComparisonTestFactory.public_send(partner_create_method, storage_creation.organization)
182+
PDFComparisonTestFactory.public_send(profile_create_method, partner)
183+
request = create_line_items_request(partner: partner, storage_creation: storage_creation)
184+
pdf_file = render_picklist_pdf(storage_creation.organization, [request])
134185
File.binwrite(expected_file_path, pdf_file)
135186
raise ActiveRecord::Rollback
136187
end
@@ -151,11 +202,13 @@ def self.create_comparison_pdfs
151202
ActiveRecord::Base.transaction do
152203
storage_creation = create_organization_storage_items(logo)
153204

154-
create_comparison_pdf(storage_creation, :create_profile_no_address, file_paths.expected_pickup_file_path, :pick_up)
155-
create_comparison_pdf(storage_creation, :create_profile_without_program_address, file_paths.expected_same_address_file_path, :shipped)
156-
create_comparison_pdf(storage_creation, :create_profile_with_program_address, file_paths.expected_different_address_file_path, :delivery)
157-
create_comparison_pdf(storage_creation, :create_profile_with_incomplete_address, file_paths.expected_incomplete_address_file_path, :delivery)
158-
create_comparison_pdf(storage_creation, :create_profile_no_contact_with_program_address, file_paths.expected_no_contact_file_path, :delivery)
205+
create_distribution_comparison_pdf(storage_creation, :create_profile_no_address, file_paths.expected_pickup_file_path, :pick_up)
206+
create_distribution_comparison_pdf(storage_creation, :create_profile_without_program_address, file_paths.expected_same_address_file_path, :shipped)
207+
create_distribution_comparison_pdf(storage_creation, :create_profile_with_program_address, file_paths.expected_different_address_file_path, :delivery)
208+
create_distribution_comparison_pdf(storage_creation, :create_profile_with_incomplete_address, file_paths.expected_incomplete_address_file_path, :delivery)
209+
create_distribution_comparison_pdf(storage_creation, :create_profile_no_contact_with_program_address, file_paths.expected_no_contact_file_path, :delivery)
210+
211+
create_picklist_comparison_pdf(storage_creation, :create_partner_with_quota, :create_profile_with_pickup_person, file_paths.expected_picklist_file_path)
159212

160213
raise ActiveRecord::Rollback
161214
end

spec/fixtures/files/picklist.pdf

32.4 KB
Binary file not shown.

spec/pdfs/distribution_pdf_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
let(:partner) { create(:partner) }
1818

1919
before(:each) do
20-
PDFComparisonTestFactory.create_line_items_request(distribution, partner, storage_creation)
20+
PDFComparisonTestFactory.create_line_items_for_distribution(distribution, storage_creation)
21+
PDFComparisonTestFactory.create_line_items_request(distribution: distribution, partner: partner, storage_creation: storage_creation)
2122
end
2223

2324
specify "#request_data with custom units feature" do
@@ -133,7 +134,7 @@
133134

134135
describe "address pdf output" do
135136
def compare_pdf(distribution, expected_file_path)
136-
pdf_file = PDFComparisonTestFactory.render_pdf_at_year_end(organization, distribution)
137+
pdf_file = PDFComparisonTestFactory.render_distribution_pdf_at_year_end(organization, distribution)
137138
begin
138139
# Run the following from Rails sandbox console (bin/rails/console --sandbox) to regenerate these comparison PDFs:
139140
# => load "lib/test_helpers/pdf_comparison_test_factory.rb"

spec/pdfs/picklists_pdf_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require_relative("../../lib/test_helpers/pdf_comparison_test_factory")
2+
13
describe PicklistsPdf do
24
let(:organization) { create(:organization) }
35
let(:item1) { create(:item, name: "Item 1", organization: organization) }
@@ -141,4 +143,42 @@
141143
])
142144
end
143145
end
146+
147+
describe "picklist pdf output" do
148+
def compare_picklist_pdf(requests, expected_file_path)
149+
pdf_file = PDFComparisonTestFactory.render_picklist_pdf(organization, requests)
150+
begin
151+
# Run the following from Rails sandbox console (bin/rails/console --sandbox) to regenerate these comparison PDFs:
152+
# => load "lib/test_helpers/pdf_comparison_test_factory.rb"
153+
# => Flipper.enable(:enable_packs)
154+
# => PDFComparisonTestFactory.create_comparison_pdfs
155+
expect(pdf_file).to eq(IO.binread(expected_file_path))
156+
rescue RSpec::Expectations::ExpectationNotMetError => e
157+
Rails.root.join("tmp", "failed_match_picklist_" + expected_file_path.to_s.split("/").last + ".pdf").binwrite(pdf_file)
158+
raise e.class, "PDF does not match, written to tmp/", cause: nil
159+
end
160+
end
161+
162+
# The generated PDFs (PDFs to use for comparison) are expecting the packs feature to be enabled.
163+
before(:each) do
164+
Flipper.enable(:enable_packs)
165+
end
166+
167+
let(:storage_creation) { PDFComparisonTestFactory.create_organization_storage_items }
168+
let(:organization) { storage_creation.organization }
169+
let(:partner) { PDFComparisonTestFactory.create_partner_with_quota(organization) }
170+
let(:file_paths) { PDFComparisonTestFactory.get_file_paths }
171+
let(:expected_picklist_file_path) { file_paths.expected_picklist_file_path }
172+
173+
context "when generating picklist PDF with comprehensive data" do
174+
before(:each) do
175+
PDFComparisonTestFactory.create_profile_with_pickup_person(partner)
176+
end
177+
178+
it "compares against expected PDF file" do
179+
request = PDFComparisonTestFactory.create_line_items_request(partner: partner, storage_creation: storage_creation)
180+
compare_picklist_pdf([request], expected_picklist_file_path)
181+
end
182+
end
183+
end
144184
end

0 commit comments

Comments
 (0)