-
Notifications
You must be signed in to change notification settings - Fork 38
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
Add tests for warning_messages.py
file
#62
base: main
Are you sure you want to change the base?
Conversation
@0xedward has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
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.
Thanks for working on this! :) Sorry about the delay getting to this
Would you be able to fix the type checking errors?
sapp/tests/warning_messages_test.py:36:4 Missing return annotation [3]: Returning `None` but no return type is specified.
sapp/tests/warning_messages_test.py:40:16 Undefined attribute [16]: Optional type has no attribute `message`.
sapp/tests/warning_messages_test.py:54:4 Missing return annotation [3]: Returning `None` but no return type is specified.
sapp/tests/warning_messages_test.py:63:69 Incompatible parameter type [6]: Expected `int` for 3rd positional only parameter to call `upsert_entry` but got `str`.
@0xedward has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
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.
Thanks for adding this test! A few tweaks and then it's ready to land!
@0xedward has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
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.
Thanks for making the changes! :) I had one suggestion for managing nested contexts.
Would you also be able to re-run the linters locally and commit those changes?
sapp/tests/warning_messages_test.py
Outdated
temp = tempfile.NamedTemporaryFile(mode="w+") | ||
json.dump(test_metadata, temp) | ||
temp.flush() |
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.
Would we be able to use a context manager to manage closing the file and the db session here?
Since we will have nested with
statements here, we might want to use contextlib.ExitStack
For example, the following pseudocode:
with ExitStack() as stack:
temp_file = stack.enter_context(tempfile.NamedTemporaryFile(mode="w+")
json.dump(test_metadata, temp_file)
...
session = stack.enter_context(self.db.make_session())
try:
...
code1001 = (
session.query(WarningMessage).filter_by(code="1001").one_or_none()
)
...
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.
added
ea05063
to
df8ab6c
Compare
@0xedward has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
sapp/tests/warning_messages_test.py
Outdated
with ExitStack() as stack: | ||
temp = stack.enter_context(tempfile.NamedTemporaryFile(mode="w+")) | ||
json.dump(test_metadata, temp) | ||
temp.read() |
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's the point of the read here, if we are not using the result?
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.
If we don't call .read()
, we are not able to use the file content of it later on the line number 41
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.
It looks like temp.read()
reads the contents we dumped with json.dump(test_metadata, temp)
. Without the call, temp
will be empty when we call path.Pathlib.read_text(temp.name)
.
It looks like we might be able to avoid calling temp.read()
if we set the mode during instantiation of temp
to 'w+b'
, but then we cannot write a string to temp
, we would have to encode test_metadata
as byte object to be able to write it to temp
. Would you prefer if we approach it this way instead @arthaud?
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 honestly still don't understand why we would need that .read()
.
My only idea is that .read()
flushes some buffer and the next read actually works? What if we use temp.flush()
instead?
Also, not sure w+b
will change anything.
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.
temp.flush()
works as well, I can change it to that 😄
@0xedward has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
Pre-submission checklist
black .
usort format .
flake8
pip install -r requirements-dev.txt
and completed the following:./scripts/run-tests.sh
and made sure all tests are passingSummary
Added tests for the file
/sapp/warning_messages.py
Test Plan
Relevant issue: MLH-Fellowship#11