Skip to content

Commit

Permalink
DEV - make methods module instead of instance
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyli97 committed Jan 9, 2025
1 parent 91c8687 commit 2bbeeaf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 39 deletions.
25 changes: 12 additions & 13 deletions lib/test_helpers/pdf_comparison_test_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module PDFComparisonTestFactory
StorageCreation = Data.define(:organization, :storage_location, :items)
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)

def create_organization_storage_items
def self.create_organization_storage_items
org = Organization.create!(
name: "Essentials Bank 1",
short_name: "db",
Expand Down Expand Up @@ -30,11 +30,11 @@ def create_organization_storage_items
StorageCreation.new(org, storage_location, [item1, item2, item3, item4])
end

def create_partner(organization)
def self.create_partner(organization)
Partner.create!(name: "Leslie Sue", organization: organization, email: "[email protected]")
end

def get_file_paths
def self.get_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")
Expand All @@ -43,9 +43,8 @@ def get_file_paths
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)
end

private def create_profile(partner:, program_address1:, program_address2:, program_city:, program_state:, program_zip:,
private_class_method def self.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", primary_contact_name: "Jaqueline Kihn DDS", primary_contact_email: "[email protected]")

Partners::Profile.create!(
partner_id: partner.id,
essentials_bank_id: partner.organization.id,
Expand All @@ -64,27 +63,27 @@ def get_file_paths
)
end

def create_profile_no_address(partner)
def self.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)
def self.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(partner)
def self.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_profile_with_incomplete_address(partner)
def self.create_profile_with_incomplete_address(partner)
create_profile(partner: partner, program_address1: "Example Program Address 1", program_address2: "", program_city: "", program_state: "", program_zip: "")
end

def create_profile_no_contact_with_program_address(partner)
def self.create_profile_no_contact_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, primary_contact_name: "", primary_contact_email: "")
end

def create_line_items_request(distribution, partner, storage_creation)
def self.create_line_items_request(distribution, partner, storage_creation)
LineItem.create!(itemizable: distribution, item: storage_creation.items[0], quantity: 50)
LineItem.create!(itemizable: distribution, item: storage_creation.items[1], quantity: 100)
storage_creation.organization.request_units.find_or_create_by!(name: "pack")
Expand All @@ -105,7 +104,7 @@ def create_line_items_request(distribution, partner, storage_creation)
)
end

def create_dist(partner, storage_creation, delivery_method)
def self.create_dist(partner, storage_creation, delivery_method)
Time.zone = "America/Los_Angeles"
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)
create_line_items_request(dist, partner, storage_creation)
Expand All @@ -125,7 +124,7 @@ def create_dist(partner, storage_creation, delivery_method)
end

# helper function that can be called from Rails console to generate comparison PDFs
def create_comparison_pdfs
def self.create_comparison_pdfs
storage_creation = create_organization_storage_items
file_paths = get_file_paths

Expand Down
45 changes: 20 additions & 25 deletions spec/pdfs/distribution_pdf_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
require_relative("../support/distribution_pdf_helper")
require_relative("../../lib/test_helpers/pdf_comparison_test_factory")

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

describe DistributionPdf do
let(:storage_creation) { create_organization_storage_items }
let(:storage_creation) { PDFComparisonTestFactory.create_organization_storage_items }
let(:organization) { storage_creation.organization }
let(:storage_location) { storage_creation.storage_location }

Expand All @@ -23,7 +18,7 @@
let(:partner) { create(:partner) }

before(:each) do
create_line_items_request(distribution, partner, storage_creation)
PDFComparisonTestFactory.create_line_items_request(distribution, partner, storage_creation)
end

specify "#request_data with custom units feature" do
Expand Down Expand Up @@ -137,8 +132,8 @@
end

describe "address pdf output" do
let(:partner) { create_partner(organization) }
let(:file_paths) { get_file_paths }
let(:partner) { PDFComparisonTestFactory.create_partner(organization) }
let(:file_paths) { PDFComparisonTestFactory.get_file_paths }
let(:expected_different_address_file_path) { file_paths.expected_different_address_file_path }
let(:expected_pickup_file_path) { file_paths.expected_pickup_file_path }
let(:expected_same_address_file_path) { file_paths.expected_same_address_file_path }
Expand All @@ -147,53 +142,53 @@

context "when the partner has no addresses" do
before(:each) do
create_profile_no_address(partner)
PDFComparisonTestFactory.create_profile_no_address(partner)
end
it "doesn't print any address if the delivery type is pickup" do
compare_pdf(organization, create_dist(partner, storage_creation, :pick_up), expected_pickup_file_path)
DistributionPDFHelper.compare_pdf(organization, PDFComparisonTestFactory.create_dist(partner, storage_creation, :pick_up), expected_pickup_file_path)
end
it "doesn't print any address if the delivery type is delivery" do
compare_pdf(organization, create_dist(partner, storage_creation, :delivery), expected_pickup_file_path)
DistributionPDFHelper.compare_pdf(organization, PDFComparisonTestFactory.create_dist(partner, storage_creation, :delivery), expected_pickup_file_path)
end
it "doesn't print any address if the delivery type is shipped" do
compare_pdf(organization, create_dist(partner, storage_creation, :shipped), expected_pickup_file_path)
DistributionPDFHelper.compare_pdf(organization, PDFComparisonTestFactory.create_dist(partner, storage_creation, :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(partner)
PDFComparisonTestFactory.create_profile_without_program_address(partner)
end
it "prints the address if the delivery type is delivery" do
compare_pdf(organization, create_dist(partner, storage_creation, :delivery), expected_same_address_file_path)
DistributionPDFHelper.compare_pdf(organization, PDFComparisonTestFactory.create_dist(partner, storage_creation, :delivery), expected_same_address_file_path)
end
it "prints the address if the delivery type is shipped" do
compare_pdf(organization, create_dist(partner, storage_creation, :shipped), expected_same_address_file_path)
DistributionPDFHelper.compare_pdf(organization, PDFComparisonTestFactory.create_dist(partner, storage_creation, :shipped), expected_same_address_file_path)
end
it "doesn't print the address if the delivery type is pickup" do
compare_pdf(organization, create_dist(partner, storage_creation, :pick_up), expected_pickup_file_path)
DistributionPDFHelper.compare_pdf(organization, PDFComparisonTestFactory.create_dist(partner, storage_creation, :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(partner)
PDFComparisonTestFactory.create_profile_with_program_address(partner)
end
it "prints the delivery address if the delivery type is delivery" do
compare_pdf(organization, create_dist(partner, storage_creation, :delivery), expected_different_address_file_path)
DistributionPDFHelper.compare_pdf(organization, PDFComparisonTestFactory.create_dist(partner, storage_creation, :delivery), expected_different_address_file_path)
end
it "prints the delivery address if the delivery type is shipped" do
compare_pdf(organization, create_dist(partner, storage_creation, :shipped), expected_different_address_file_path)
DistributionPDFHelper.compare_pdf(organization, PDFComparisonTestFactory.create_dist(partner, storage_creation, :shipped), expected_different_address_file_path)
end
it "doesn't print any address if the delivery type is pickup" do
compare_pdf(organization, create_dist(partner, storage_creation, :pick_up), expected_pickup_file_path)
DistributionPDFHelper.compare_pdf(organization, PDFComparisonTestFactory.create_dist(partner, storage_creation, :pick_up), expected_pickup_file_path)
end
end
it "formats output correctly when the partner delivery address is incomplete" do
create_profile_with_incomplete_address(partner)
compare_pdf(organization, create_dist(partner, storage_creation, :delivery), expected_incomplete_address_file_path)
PDFComparisonTestFactory.create_profile_with_incomplete_address(partner)
DistributionPDFHelper.compare_pdf(organization, PDFComparisonTestFactory.create_dist(partner, storage_creation, :delivery), expected_incomplete_address_file_path)
end
it "formats output correctly when the partner profile contact info does not exist" do
create_profile_no_contact_with_program_address(partner)
compare_pdf(organization, create_dist(partner, storage_creation, :delivery), expected_no_contact_file_path)
PDFComparisonTestFactory.create_profile_no_contact_with_program_address(partner)
DistributionPDFHelper.compare_pdf(organization, PDFComparisonTestFactory.create_dist(partner, storage_creation, :delivery), expected_no_contact_file_path)
end
end
end
2 changes: 1 addition & 1 deletion spec/support/distribution_pdf_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module DistributionPDFHelper
def compare_pdf(organization, distribution, expected_file_path)
def self.compare_pdf(organization, distribution, expected_file_path)
pdf = DistributionPdf.new(organization, distribution)
begin
pdf_file = pdf.compute_and_render
Expand Down

0 comments on commit 2bbeeaf

Please sign in to comment.