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

Add tests for warning_messages.py file #62

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

arpancodes
Copy link

Pre-submission checklist

  • I've ran the following linters locally and fixed lint errors related to the files I modified in this PR
    • black .
    • usort format .
    • flake8
  • I've installed dev dependencies pip install -r requirements-dev.txt and completed the following:
    • I've ran tests with ./scripts/run-tests.sh and made sure all tests are passing

Summary

Added tests for the file /sapp/warning_messages.py

Test Plan

Relevant issue: MLH-Fellowship#11

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 9, 2021
@arpancodes arpancodes marked this pull request as ready for review November 10, 2021 16:48
@facebook-github-bot
Copy link
Contributor

@0xedward has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@0xedward 0xedward left a 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`.

@arpancodes arpancodes requested a review from 0xedward November 18, 2021 15:07
@facebook-github-bot
Copy link
Contributor

@0xedward has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link

@gbleaney gbleaney left a 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!

sapp/tests/warning_messages_test.py Outdated Show resolved Hide resolved
sapp/tests/warning_messages_test.py Outdated Show resolved Hide resolved
sapp/tests/warning_messages_test.py Show resolved Hide resolved
@arpancodes arpancodes requested a review from gbleaney November 21, 2021 20:29
@arpancodes
Copy link
Author

@0xedward and @gbleaney thanks for the review, I have made the requested changes. Please have a look at it again 😄

@facebook-github-bot
Copy link
Contributor

@0xedward has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@0xedward 0xedward left a 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 Show resolved Hide resolved
Comment on lines 30 to 32
temp = tempfile.NamedTemporaryFile(mode="w+")
json.dump(test_metadata, temp)
temp.flush()
Copy link
Contributor

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()
        )
    ...

Copy link
Author

Choose a reason for hiding this comment

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

added

@arpancodes arpancodes force-pushed the warning-messages-test branch from ea05063 to df8ab6c Compare December 6, 2021 15:57
@facebook-github-bot
Copy link
Contributor

@0xedward has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

with ExitStack() as stack:
temp = stack.enter_context(tempfile.NamedTemporaryFile(mode="w+"))
json.dump(test_metadata, temp)
temp.read()
Copy link
Contributor

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?

Copy link
Author

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

Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Author

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 😄

sapp/tests/warning_messages_test.py Outdated Show resolved Hide resolved
@arpancodes arpancodes requested a review from 0xedward December 9, 2021 08:23
@0xedward 0xedward requested a review from arthaud December 9, 2021 21:26
@facebook-github-bot
Copy link
Contributor

@0xedward has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants