-
Notifications
You must be signed in to change notification settings - Fork 5
Added fetch function as an out-of-band utility to easily fetch invoices #231
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| from unittest import TestCase, mock | ||
| import pandas | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from unittest.mock import MagicMock | ||
|
|
||
| from process_report.tests import util as test_utils | ||
|
|
||
|
|
@@ -17,6 +21,33 @@ def test_filter_exported_columns(self): | |
|
|
||
| self.assertTrue(result_invoice.equals(answer_invoice)) | ||
|
|
||
| @mock.patch("pandas.read_csv") | ||
| def test_fetch_with_mock_s3_bucket(self, mock_read_csv: "MagicMock") -> None: | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're conditionally importing the MagicMock due to the type hints in this function.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Followup on the comment above. I suppose it's fine to keep this type hint string |
||
| """Test that fetch() method loads data correctly when S3 bucket is mocked.""" | ||
| test_invoice = test_utils.new_base_invoice( | ||
| name="TestInvoice", invoice_month="2024-08" | ||
| ) | ||
| expected_data = pandas.DataFrame( | ||
| { | ||
| "Invoice Month": ["2024-08", "2024-08"], | ||
| "Project - Allocation": ["P1", "P2"], | ||
| "Manager (PI)": ["[email protected]", "[email protected]"], | ||
| "Cost": [100.00, 150.00], | ||
| } | ||
| ) | ||
| mock_read_csv.return_value = expected_data | ||
|
|
||
| mock_s3_bucket = mock.MagicMock() | ||
| mock_s3_bucket.download_file.return_value = None | ||
|
|
||
| test_invoice.fetch(mock_s3_bucket) | ||
|
|
||
| self.assertIsNotNone(test_invoice.data) | ||
| self.assertTrue(test_invoice.data.equals(expected_data)) | ||
| mock_s3_bucket.download_file.assert_called_once_with( | ||
| "Invoices/2024-08/TestInvoice 2024-08.csv", "TestInvoice 2024-08.csv" | ||
| ) | ||
|
|
||
|
|
||
| class TestUploadToS3(TestCase): | ||
| @mock.patch("process_report.util.get_invoice_bucket") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the use of this check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://docs.python.org/3/library/typing.html#typing.TYPE_CHECKING
Using this pattern essentially lets you import types only when running type checkers or using code editors, not during actual program execution. This avoids slow imports or circular dependencies while still keeping type hints working properly.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. That is interesting. People come up with tools for everything.
I'd argue that this change is beyond the scope of this issue. Introducing the use (or in this case, the implication) of type checkers is seperate from adding a fetch function, and should be done in its own PR.
Do note also, by using
TYPE_CHECKINGhere, there's pressure to refactor the entire repo to follow this pattern as well. At least in our repos, whenever a code pattern or tool is proposed to a repo, we will want that feature to be applied uniformly across the entire repo, not just in one-off locations. This generally makes the repo more cohesive and easier to maintain.That being said. If you'd like to propose using
TYPE_CHECKINGand encourage the use of type checkers, I would suggest making a new PR for it and ask people on their opinion. I, for one, am ambivalent.