Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extends bulk print invoice spec to assert on file contents #11836

Conversation

filipefurtad0
Copy link
Contributor

@filipefurtad0 filipefurtad0 commented Nov 20, 2023

What? Why?

Checks for the contents of bulk generated pdf files. This came up here. Thank you for bringing this up @abdellani 🙏

Rather than converting the what the browser displays to text (as invoice_print_spec.rb does) it:

  • extracts the invoice pdf file name from the URL
  • uses the name to open and convert the *.pdf file
  • asserts on its contents

What should we test?

  • green build

Release notes

Changelog Category (reviewers may add a label for the release notes):

  • User facing changes
  • API changes (V0, V1, DFC or Webhook)
  • Technical changes only
  • Feature toggled

The title of the pull request will be included in the release notes.

Dependencies

Documentation updates

@filipefurtad0 filipefurtad0 self-assigned this Nov 20, 2023
@filipefurtad0 filipefurtad0 added the technical changes only These pull requests do not contain user facing changes and are grouped in release notes label Nov 20, 2023
Copy link
Member

@mkllnk mkllnk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting idea. This seems a lot more efficient than downloading the file. The downside is that it's very specific to invoices and how we store them. But that's okay for now.

Comment on lines 937 to 944
def extract_pdf_content
pdf_href = page.all('a').pluck('href')
page.find(class: "button", text: "VIEW FILE").click
invoice_file_number = pdf_href[0][45..59]
invoice_path = "tmp/invoices/#{invoice_file_number}.pdf"
reader = PDF::Reader.new(invoice_path)
@invoice_content = reader.pages.map(&:text)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block should be within the test description instead of being a global method. Otherwise it will leak into other specs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block should be within the test description

Ok, I've created a context for the test and moved the method it in there - I'm not 100% sure this is what you meant though?

within ".modal-content" do
expect(page).to have_link(class: "button", text: "VIEW FILE", href: /invoices/)

extract_pdf_content # extracts content do variable invoice_content
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to return the content from the method:

Suggested change
extract_pdf_content # extracts content do variable invoice_content
invoice_content = extract_pdf_content

Then the test can store it however it wants and you don't have to know the name of the variable.

This way, we don't need to use it as a global method

Clarifies context naming
Copy link
Member

@mkllnk mkllnk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly. That's what I meant.

invoice_file_number = pdf_href[0][45..59]
invoice_path = "tmp/invoices/#{invoice_file_number}.pdf"
reader = PDF::Reader.new(invoice_path)
invoice_content = reader.pages.map(&:text)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
invoice_content = reader.pages.map(&:text)
reader.pages.map(&:text)


expect(page).to have_content "Bulk Invoice created"
expect(page).to have_link(class: "button", text: "VIEW FILE", href: /invoices/)
invoice_content = extract_pdf_content # extracts content do variable invoice_content
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now you don't need the comment any more.

Suggested change
invoice_content = extract_pdf_content # extracts content do variable invoice_content
invoice_content = extract_pdf_content

Copy link
Collaborator

@rioug rioug left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting approach, well done !

@rioug rioug merged commit 8b5e543 into openfoodfoundation:master Nov 24, 2023
50 checks passed
Copy link
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I reviewed this and had a draft improvement to add, but forgot to indicate I was working on it!

I'll add in a new PR.

page.find("#listing_orders tbody tr:nth-child(1) input[name='bulk_ids[]']").click
page.find("#listing_orders tbody tr:nth-child(2) input[name='bulk_ids[]']").click
context "can bulk print invoices" do
def extract_pdf_content
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think with Maikel's change, this method can safely live at the bottom of the file again. But it's a single-use method anyway so it seems ok to be here.

Comment on lines +577 to +579
pdf_href = page.all('a').pluck('href')
page.find(class: "button", text: "VIEW FILE").click
invoice_file_number = pdf_href[0][45..59]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like these lines are referring to the same link, but in a different way. In fact, I think the second line (page.find..click) isn't necessary: we're viewing the temporary file directly so don't need to trigger a download.

@dacook dacook mentioned this pull request Nov 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
technical changes only These pull requests do not contain user facing changes and are grouped in release notes
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants