From 2bbeeafec01a17301c4e8f6e139e0d3ea5ed9664 Mon Sep 17 00:00:00 2001 From: Jimmy Li Date: Thu, 9 Jan 2025 03:39:05 +0000 Subject: [PATCH] DEV - make methods module instead of instance --- .../pdf_comparison_test_factory.rb | 25 +++++------ spec/pdfs/distribution_pdf_spec.rb | 45 +++++++++---------- spec/support/distribution_pdf_helper.rb | 2 +- 3 files changed, 33 insertions(+), 39 deletions(-) diff --git a/lib/test_helpers/pdf_comparison_test_factory.rb b/lib/test_helpers/pdf_comparison_test_factory.rb index 5a6b2f5353..c87cb660ba 100644 --- a/lib/test_helpers/pdf_comparison_test_factory.rb +++ b/lib/test_helpers/pdf_comparison_test_factory.rb @@ -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", @@ -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: "leslie1@gmail.com") 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") @@ -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: "van@durgan.example") - Partners::Profile.create!( partner_id: partner.id, essentials_bank_id: partner.organization.id, @@ -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") @@ -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) @@ -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 diff --git a/spec/pdfs/distribution_pdf_spec.rb b/spec/pdfs/distribution_pdf_spec.rb index 01aa086690..46bbfdf99d 100644 --- a/spec/pdfs/distribution_pdf_spec.rb +++ b/spec/pdfs/distribution_pdf_spec.rb @@ -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 } @@ -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 @@ -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 } @@ -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 diff --git a/spec/support/distribution_pdf_helper.rb b/spec/support/distribution_pdf_helper.rb index 39ea40d4c0..8315d0e33f 100644 --- a/spec/support/distribution_pdf_helper.rb +++ b/spec/support/distribution_pdf_helper.rb @@ -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