Skip to content

Commit f4e0ae2

Browse files
committed
RED Make methods module instead of instance
* Move helper compare_pdf method into Rspec group * Failing tests are due to an error introduced by merging main branch with "Items received YTD" displaying zero
1 parent 91c8687 commit f4e0ae2

File tree

4 files changed

+50
-56
lines changed

4 files changed

+50
-56
lines changed

app/pdfs/distribution_pdf.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require "prawn/table"
2+
13
# Configures a Prawn PDF template for generating Distribution manifests
24
class DistributionPdf
35
include Prawn::View

lib/test_helpers/pdf_comparison_test_factory.rb

+12-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module PDFComparisonTestFactory
22
StorageCreation = Data.define(:organization, :storage_location, :items)
33
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)
44

5-
def create_organization_storage_items
5+
def self.create_organization_storage_items
66
org = Organization.create!(
77
name: "Essentials Bank 1",
88
short_name: "db",
@@ -30,11 +30,11 @@ def create_organization_storage_items
3030
StorageCreation.new(org, storage_location, [item1, item2, item3, item4])
3131
end
3232

33-
def create_partner(organization)
33+
def self.create_partner(organization)
3434
Partner.create!(name: "Leslie Sue", organization: organization, email: "[email protected]")
3535
end
3636

37-
def get_file_paths
37+
def self.get_file_paths
3838
expected_pickup_file_path = Rails.root.join("spec", "fixtures", "files", "distribution_pickup.pdf")
3939
expected_same_address_file_path = Rails.root.join("spec", "fixtures", "files", "distribution_same_address.pdf")
4040
expected_different_address_file_path = Rails.root.join("spec", "fixtures", "files", "distribution_program_address.pdf")
@@ -43,9 +43,8 @@ def get_file_paths
4343
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)
4444
end
4545

46-
private def create_profile(partner:, program_address1:, program_address2:, program_city:, program_state:, program_zip:,
46+
private_class_method def self.create_profile(partner:, program_address1:, program_address2:, program_city:, program_state:, program_zip:,
4747
address1: "Example Address 1", city: "Example City", state: "Example State", zip: "12345", primary_contact_name: "Jaqueline Kihn DDS", primary_contact_email: "[email protected]")
48-
4948
Partners::Profile.create!(
5049
partner_id: partner.id,
5150
essentials_bank_id: partner.organization.id,
@@ -64,27 +63,27 @@ def get_file_paths
6463
)
6564
end
6665

67-
def create_profile_no_address(partner)
66+
def self.create_profile_no_address(partner)
6867
create_profile(partner: partner, program_address1: "", program_address2: "", program_city: "", program_state: "", program_zip: "", address1: "", city: "", state: "", zip: "")
6968
end
7069

71-
def create_profile_without_program_address(partner)
70+
def self.create_profile_without_program_address(partner)
7271
create_profile(partner: partner, program_address1: "", program_address2: "", program_city: "", program_state: "", program_zip: "")
7372
end
7473

75-
def create_profile_with_program_address(partner)
74+
def self.create_profile_with_program_address(partner)
7675
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)
7776
end
7877

79-
def create_profile_with_incomplete_address(partner)
78+
def self.create_profile_with_incomplete_address(partner)
8079
create_profile(partner: partner, program_address1: "Example Program Address 1", program_address2: "", program_city: "", program_state: "", program_zip: "")
8180
end
8281

83-
def create_profile_no_contact_with_program_address(partner)
82+
def self.create_profile_no_contact_with_program_address(partner)
8483
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: "")
8584
end
8685

87-
def create_line_items_request(distribution, partner, storage_creation)
86+
def self.create_line_items_request(distribution, partner, storage_creation)
8887
LineItem.create!(itemizable: distribution, item: storage_creation.items[0], quantity: 50)
8988
LineItem.create!(itemizable: distribution, item: storage_creation.items[1], quantity: 100)
9089
storage_creation.organization.request_units.find_or_create_by!(name: "pack")
@@ -105,7 +104,7 @@ def create_line_items_request(distribution, partner, storage_creation)
105104
)
106105
end
107106

108-
def create_dist(partner, storage_creation, delivery_method)
107+
def self.create_dist(partner, storage_creation, delivery_method)
109108
Time.zone = "America/Los_Angeles"
110109
dist = Distribution.create!(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)
111110
create_line_items_request(dist, partner, storage_creation)
@@ -125,7 +124,7 @@ def create_dist(partner, storage_creation, delivery_method)
125124
end
126125

127126
# helper function that can be called from Rails console to generate comparison PDFs
128-
def create_comparison_pdfs
127+
def self.create_comparison_pdfs
129128
storage_creation = create_organization_storage_items
130129
file_paths = get_file_paths
131130

spec/pdfs/distribution_pdf_spec.rb

+36-26
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
require_relative("../support/distribution_pdf_helper")
21
require_relative("../../lib/test_helpers/pdf_comparison_test_factory")
32

4-
RSpec.configure do |c|
5-
c.include DistributionPDFHelper
6-
c.include PDFComparisonTestFactory
7-
end
8-
93
describe DistributionPdf do
10-
let(:storage_creation) { create_organization_storage_items }
4+
let(:storage_creation) { PDFComparisonTestFactory.create_organization_storage_items }
115
let(:organization) { storage_creation.organization }
126
let(:storage_location) { storage_creation.storage_location }
137

@@ -23,7 +17,7 @@
2317
let(:partner) { create(:partner) }
2418

2519
before(:each) do
26-
create_line_items_request(distribution, partner, storage_creation)
20+
PDFComparisonTestFactory.create_line_items_request(distribution, partner, storage_creation)
2721
end
2822

2923
specify "#request_data with custom units feature" do
@@ -137,8 +131,24 @@
137131
end
138132

139133
describe "address pdf output" do
140-
let(:partner) { create_partner(organization) }
141-
let(:file_paths) { get_file_paths }
134+
def compare_pdf(distribution, expected_file_path)
135+
pdf = DistributionPdf.new(organization, distribution)
136+
begin
137+
pdf_file = pdf.compute_and_render
138+
139+
# Run the following from Rails sandbox console (bin/rails/console --sandbox) to regenerate these comparison PDFs:
140+
# => load "lib/test_helpers/pdf_comparison_test_factory.rb"
141+
# => Rails::ConsoleMethods.send(:prepend, PDFComparisonTestFactory)
142+
# => create_comparison_pdfs
143+
expect(pdf_file).to eq(IO.binread(expected_file_path))
144+
rescue RSpec::Expectations::ExpectationNotMetError => e
145+
File.binwrite(Rails.root.join("tmp", "failed_match_distribution_" + distribution.delivery_method.to_s + "_" + Time.current.to_s + ".pdf"), pdf_file)
146+
raise e.class, "PDF does not match, written to tmp/", cause: nil
147+
end
148+
end
149+
150+
let(:partner) { PDFComparisonTestFactory.create_partner(organization) }
151+
let(:file_paths) { PDFComparisonTestFactory.get_file_paths }
142152
let(:expected_different_address_file_path) { file_paths.expected_different_address_file_path }
143153
let(:expected_pickup_file_path) { file_paths.expected_pickup_file_path }
144154
let(:expected_same_address_file_path) { file_paths.expected_same_address_file_path }
@@ -147,53 +157,53 @@
147157

148158
context "when the partner has no addresses" do
149159
before(:each) do
150-
create_profile_no_address(partner)
160+
PDFComparisonTestFactory.create_profile_no_address(partner)
151161
end
152162
it "doesn't print any address if the delivery type is pickup" do
153-
compare_pdf(organization, create_dist(partner, storage_creation, :pick_up), expected_pickup_file_path)
163+
compare_pdf(PDFComparisonTestFactory.create_dist(partner, storage_creation, :pick_up), expected_pickup_file_path)
154164
end
155165
it "doesn't print any address if the delivery type is delivery" do
156-
compare_pdf(organization, create_dist(partner, storage_creation, :delivery), expected_pickup_file_path)
166+
compare_pdf(PDFComparisonTestFactory.create_dist(partner, storage_creation, :delivery), expected_pickup_file_path)
157167
end
158168
it "doesn't print any address if the delivery type is shipped" do
159-
compare_pdf(organization, create_dist(partner, storage_creation, :shipped), expected_pickup_file_path)
169+
compare_pdf(PDFComparisonTestFactory.create_dist(partner, storage_creation, :shipped), expected_pickup_file_path)
160170
end
161171
end
162172
context "when the partner doesn't have a different program address" do
163173
before(:each) do
164-
create_profile_without_program_address(partner)
174+
PDFComparisonTestFactory.create_profile_without_program_address(partner)
165175
end
166176
it "prints the address if the delivery type is delivery" do
167-
compare_pdf(organization, create_dist(partner, storage_creation, :delivery), expected_same_address_file_path)
177+
compare_pdf(PDFComparisonTestFactory.create_dist(partner, storage_creation, :delivery), expected_same_address_file_path)
168178
end
169179
it "prints the address if the delivery type is shipped" do
170-
compare_pdf(organization, create_dist(partner, storage_creation, :shipped), expected_same_address_file_path)
180+
compare_pdf(PDFComparisonTestFactory.create_dist(partner, storage_creation, :shipped), expected_same_address_file_path)
171181
end
172182
it "doesn't print the address if the delivery type is pickup" do
173-
compare_pdf(organization, create_dist(partner, storage_creation, :pick_up), expected_pickup_file_path)
183+
compare_pdf(PDFComparisonTestFactory.create_dist(partner, storage_creation, :pick_up), expected_pickup_file_path)
174184
end
175185
end
176186
context "when the partner has a different program/delivery address" do
177187
before(:each) do
178-
create_profile_with_program_address(partner)
188+
PDFComparisonTestFactory.create_profile_with_program_address(partner)
179189
end
180190
it "prints the delivery address if the delivery type is delivery" do
181-
compare_pdf(organization, create_dist(partner, storage_creation, :delivery), expected_different_address_file_path)
191+
compare_pdf(PDFComparisonTestFactory.create_dist(partner, storage_creation, :delivery), expected_different_address_file_path)
182192
end
183193
it "prints the delivery address if the delivery type is shipped" do
184-
compare_pdf(organization, create_dist(partner, storage_creation, :shipped), expected_different_address_file_path)
194+
compare_pdf(PDFComparisonTestFactory.create_dist(partner, storage_creation, :shipped), expected_different_address_file_path)
185195
end
186196
it "doesn't print any address if the delivery type is pickup" do
187-
compare_pdf(organization, create_dist(partner, storage_creation, :pick_up), expected_pickup_file_path)
197+
compare_pdf(PDFComparisonTestFactory.create_dist(partner, storage_creation, :pick_up), expected_pickup_file_path)
188198
end
189199
end
190200
it "formats output correctly when the partner delivery address is incomplete" do
191-
create_profile_with_incomplete_address(partner)
192-
compare_pdf(organization, create_dist(partner, storage_creation, :delivery), expected_incomplete_address_file_path)
201+
PDFComparisonTestFactory.create_profile_with_incomplete_address(partner)
202+
compare_pdf(PDFComparisonTestFactory.create_dist(partner, storage_creation, :delivery), expected_incomplete_address_file_path)
193203
end
194204
it "formats output correctly when the partner profile contact info does not exist" do
195-
create_profile_no_contact_with_program_address(partner)
196-
compare_pdf(organization, create_dist(partner, storage_creation, :delivery), expected_no_contact_file_path)
205+
PDFComparisonTestFactory.create_profile_no_contact_with_program_address(partner)
206+
compare_pdf(PDFComparisonTestFactory.create_dist(partner, storage_creation, :delivery), expected_no_contact_file_path)
197207
end
198208
end
199209
end

spec/support/distribution_pdf_helper.rb

-17
This file was deleted.

0 commit comments

Comments
 (0)