From 25108f4c7075bea872fba37a8f4316e0f6c0e8eb Mon Sep 17 00:00:00 2001 From: filipefurtad0 Date: Fri, 10 Mar 2023 15:29:43 +0000 Subject: [PATCH] Removes all fixture files; asserts on downloaded file Changes helper method to assert there is one file only load_file_txt will be useful in other spec files too, and could probably be moved to spec/support/file_helper.rb --- .../sales_tax_by_order/sales_tax_by_order.csv | 5 - .../sales_tax_by_order.xlsx | Bin 4280 -> 0 bytes spec/support/downloads_helper.rb | 1 + .../sales_tax_totals_by_order_spec.rb | 136 ++++++++++-------- 4 files changed, 74 insertions(+), 68 deletions(-) delete mode 100644 spec/fixtures/reports/sales_tax_by_order/sales_tax_by_order.csv delete mode 100644 spec/fixtures/reports/sales_tax_by_order/sales_tax_by_order.xlsx diff --git a/spec/fixtures/reports/sales_tax_by_order/sales_tax_by_order.csv b/spec/fixtures/reports/sales_tax_by_order/sales_tax_by_order.csv deleted file mode 100644 index 14eb8c3a39..0000000000 --- a/spec/fixtures/reports/sales_tax_by_order/sales_tax_by_order.csv +++ /dev/null @@ -1,5 +0,0 @@ -Distributor,Order Cycle,Order number,Tax Category,Tax Rate Name,Tax Rate,Total excl. tax ($),Tax,Total incl. tax ($),First Name,Last Name,Code,Email -Distributor,oc1,ORDER_NUMBER_1,tax_category,State,0.015,115.0,1.73,116.73,cfname,clname,ABC123,order1@example.com -Distributor,oc1,ORDER_NUMBER_1,tax_category,Country,0.025,115.0,2.88,117.88,cfname,clname,ABC123,order1@example.com -Distributor,oc1,ORDER_NUMBER_2,tax_category,State,0.015,215.0,3.23,218.23,c2fname,c2lname,DEF456,order2@example.com -Distributor,oc1,ORDER_NUMBER_2,tax_category,Country,0.025,215.0,5.38,220.38,c2fname,c2lname,DEF456,order2@example.com diff --git a/spec/fixtures/reports/sales_tax_by_order/sales_tax_by_order.xlsx b/spec/fixtures/reports/sales_tax_by_order/sales_tax_by_order.xlsx deleted file mode 100644 index ad2870fd259b2caf949a22b49ffdfb0fdac92054..0000000000000000000000000000000000000000 GI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^g_Mv&o}P2)fRLj@B)y zen2`&VAo2={4H=fcZmEofjonaDY7t(u)$;UxNd+Noh{e#SIFc2x)9lg72F2blhbHyb75zC3vzg?NU#KsAC+`nuvti22TBf-_R@& zjLRaON6>Lr5?AifM zL`2GGt%LJFFsB_pa z0xj(JKQq;3;N{Wd0tmy__J0~gF0))#>kAeetUCP8^0(T3neeijUJx>1=kR|M{wMh_ zgD)%71(<~RFYtv~M!W%%V#zJRA-rS`ui Z;opI+2__&s{{=DjrGb5F+E>oM{R>%3KR^Hg diff --git a/spec/support/downloads_helper.rb b/spec/support/downloads_helper.rb index a9cf3cf60d..2a6bc73b0c 100644 --- a/spec/support/downloads_helper.rb +++ b/spec/support/downloads_helper.rb @@ -9,6 +9,7 @@ module DownloadsHelper def downloaded_filename wait_for_download + expect(downloaded_filenames.length).to eq(1) # downloads folder should contain 1 file downloaded_filenames.first end diff --git a/spec/system/admin/reports/sales_tax/sales_tax_totals_by_order_spec.rb b/spec/system/admin/reports/sales_tax/sales_tax_totals_by_order_spec.rb index d06a141114..718ed1e3d3 100644 --- a/spec/system/admin/reports/sales_tax/sales_tax_totals_by_order_spec.rb +++ b/spec/system/admin/reports/sales_tax/sales_tax_totals_by_order_spec.rb @@ -30,7 +30,7 @@ describe "Sales Tax Totals By order" do } let!(:state_zone){ create(:zone_with_state_member) } let!(:country_zone){ create(:zone_with_member) } - let!(:tax_category){ create(:tax_category, name: 'tax_category') } + let!(:tax_category){ create(:tax_category, name: 'GST Food') } let!(:state_tax_rate){ create(:tax_rate, zone: state_zone, tax_category: tax_category, name: 'State', amount: 0.015) @@ -47,9 +47,12 @@ describe "Sales Tax Totals By order" do let!(:variant){ create(:variant) } let!(:product){ variant.product } - let!(:supplier){ create(:supplier_enterprise, name: 'Supplier', charges_sales_tax: true) } + let!(:supplier){ + create(:supplier_enterprise, name: 'SupplierEnterprise', charges_sales_tax: true) + } let!(:distributor){ - create(:distributor_enterprise_with_tax, name: 'Distributor', charges_sales_tax: true) + create(:distributor_enterprise_with_tax, name: 'DistributorEnterpriseWithTax', + charges_sales_tax: true) } let!(:distributor_fee){ create(:enterprise_fee, :flat_rate, amount: 5, @@ -112,10 +115,10 @@ describe "Sales Tax Totals By order" do expect(page.find("table.report__table thead tr").text).to have_content(table_header) expect(page.find("table.report__table tbody").text).to have_content([ - "Distributor", + "DistributorEnterpriseWithTax", "oc1", "ORDER_NUMBER_1", - "tax_category", + "GST Food", "State", "1.5 %", "115.0", @@ -128,10 +131,10 @@ describe "Sales Tax Totals By order" do ].join(" ")) expect(page.find("table.report__table tbody").text).to have_content([ - "Distributor", + "DistributorEnterpriseWithTax", "oc1", "ORDER_NUMBER_1", - "tax_category", + "GST Food", "Country", "2.5 %", "115.0", @@ -174,10 +177,10 @@ describe "Sales Tax Totals By order" do expect(page.find("table.report__table thead tr").text).to have_content(table_header) expect(page.find("table.report__table tbody").text).to have_content([ - "Distributor", + "DistributorEnterpriseWithTax", "oc1", "ORDER_NUMBER_1", - "tax_category", + "GST Food", "State", "1.5 %", "110.5", @@ -190,10 +193,10 @@ describe "Sales Tax Totals By order" do ].join(" ")) expect(page.find("table.report__table tbody").text).to have_content([ - "Distributor", + "DistributorEnterpriseWithTax", "oc1", "ORDER_NUMBER_1", - "tax_category", + "GST Food", "Country", "2.5 %", "110.5", @@ -225,10 +228,10 @@ describe "Sales Tax Totals By order" do let!(:table_raw_selector){ "table.report__table tbody tr" } let(:customer1_country_tax_rate_row){ [ - "Distributor", + "DistributorEnterpriseWithTax", "oc1", "ORDER_NUMBER_1", - "tax_category", + "GST Food", "Country", "2.5 %", "115.0", @@ -242,10 +245,10 @@ describe "Sales Tax Totals By order" do } let(:customer1_state_tax_rate_row){ [ - "Distributor", + "DistributorEnterpriseWithTax", "oc1", "ORDER_NUMBER_1", - "tax_category", + "GST Food", "State", "1.5 %", "115.0", @@ -272,10 +275,10 @@ describe "Sales Tax Totals By order" do let(:customer2_country_tax_rate_row){ [ - "Distributor", + "DistributorEnterpriseWithTax", "oc1", "ORDER_NUMBER_2", - "tax_category", + "GST Food", "Country", "2.5 %", "215.0", @@ -289,10 +292,10 @@ describe "Sales Tax Totals By order" do } let(:customer2_state_tax_rate_row){ [ - "Distributor", + "DistributorEnterpriseWithTax", "oc1", "ORDER_NUMBER_2", - "tax_category", + "GST Food", "State", "1.5 %", "215.0", @@ -425,56 +428,63 @@ describe "Sales Tax Totals By order" do end describe "downloading" do - context "csv files" do - let(:report_file_csv) do - CSV.read("spec/fixtures/reports/sales_tax_by_order/sales_tax_by_order.csv") - end + shared_examples "reports generated as" do |output_type, extension, expect_totals| + context output_type.to_s do + it "downloads the file" do + select output_type, from: "report_format" - it 'downloads the file' do - expect(downloaded_filenames.length).to eq(0) # downloads folder should be empty - select "CSV", from: "report_format" - click_on "Go" - wait_for_download - expect(downloaded_filenames.length).to eq(1) # downloads folder should contain 1 file - expect(downloaded_filename).to match(/.*\.csv/) - expect(CSV.read(downloaded_filename)).to eq(report_file_csv) + expect { generate_report }.to change { downloaded_filenames.length }.from(0).to(1) + + expect(downloaded_filename).to match(/.*\.#{extension}/) + + downloaded_file_txt = load_file_txt(extension, downloaded_filename) + + expect(downloaded_file_txt).to include "DistributorEnterpriseWithTax", "oc1", + "ORDER_NUMBER_1", "GST Food" + expect(downloaded_file_txt).to include "State", "cfname", "clname", "ABC123", + "order1@example.com" + # order1, first tax rate line + expect(downloaded_file_txt).to match /115.0\s+1.73\s+116.73/ + # order1, second tax rate line + expect(downloaded_file_txt).to match /115.0\s+2.88\s+117.88/ + # order2, first tax rate line + expect(downloaded_file_txt).to match /215.0\s+3.23\s+218.23/ + # order2, second tax rate line + expect(downloaded_file_txt).to match /215.0\s+5.38\s+220.38/ + + expectation = expect_totals ? :to : :not_to + # correct totals with white-space between them + expect(downloaded_file_txt).public_send(expectation, + match(/TOTAL\s+215.0\s+8.61\s+223.61/)) + # correct totals with white-space between them + expect(downloaded_file_txt).public_send(expectation, + match(/TOTAL\s+115.0\s+4.61\s+119.61/)) + end end end - context "xlsx files" do - let(:report_file_xlsx) do - File.open("spec/fixtures/reports/sales_tax_by_order/sales_tax_by_order.xlsx") - end + it_behaves_like "reports generated as", "CSV", "csv", false + it_behaves_like "reports generated as", "Spreadsheet", "xlsx", true + it_behaves_like "reports generated as", "PDF", "pdf", true + end + end - it 'downloads the file' do - expect(downloaded_filenames.length).to eq(0) # downloads folder should be empty - select "Spreadsheet", from: "report_format" - find("#display_summary_row").uncheck - click_on "Go" - wait_for_download - expect(downloaded_filenames.length).to eq(1) # downloads folder should contain 1 file - expect(downloaded_filename).to match(/.*\.xlsx/) - downloaded_content = extract_xlsx_rows(downloaded_filename, 1..5) - fixture_content = extract_xlsx_rows(report_file_xlsx, 1..5) - expect(downloaded_content).to eq(fixture_content) - end + def generate_report + click_on "Go" + wait_for_download + end - def extract_xlsx_rows(file, range) - xlsx = Roo::Excelx.new(file) - range.map { |i| xlsx.row(i) } - end - end - - context "pdf files" do - it 'downloads the file' do - expect(downloaded_filenames.length).to eq(0) # downloads folder should be empty - select "PDF", from: "report_format" - click_on "Go" - wait_for_download - expect(downloaded_filenames.length).to eq(1) # downloads folder should contain 1 file - expect(downloaded_filename).to match(/.*\.pdf/) - end - end + def load_file_txt(extension, downloaded_filename) + case extension + when "csv" + CSV.read(downloaded_filename).join(" ") + when "xlsx" + xlsx = Roo::Excelx.new(downloaded_filename) + xlsx.map(&:to_a).join(" ") + when "pdf" + # Load PDF pages and contents join into one big string + pdf = PDF::Reader.new(downloaded_filename) + pdf.pages.map(&:text).join(" ") end end end