-
Notifications
You must be signed in to change notification settings - Fork 44
Add tests for warning_messages.py file
#62
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
Open
arpancodes
wants to merge
10
commits into
facebook:main
Choose a base branch
from
arpancodes:warning-messages-test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7c1607f
Add tests for `warning_messages.py` file
arpancodes 393479c
add test for `upsert_entry` method
arpancodes cf8a37c
fix type check errors
arpancodes d78aad5
added test for updation, test for existence and added tempfile module
arpancodes df8ab6c
add copyright comments and context manager
arpancodes a9a3bbc
add `.read()` to the temp file before using it.
arpancodes 3760368
Merge branch 'main' of github.com:facebook/sapp into warning-messages…
arpancodes 6ef7815
rename `temp` file
arpancodes ab72990
add purpose of `.read()`
arpancodes 1bf0553
change `.read()` to `flush()`
arpancodes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| # Copyright (c) Facebook, Inc. and its affiliates. | ||
| # | ||
| # This source code is licensed under the MIT license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| import json | ||
| import pathlib | ||
| import tempfile | ||
| from contextlib import ExitStack | ||
| from unittest import TestCase | ||
|
|
||
| from sqlalchemy import Table | ||
|
|
||
| from ..db import DB, DBType | ||
| from ..models import WarningMessage, create as create_model | ||
| from ..warning_messages import update_warning_messages, upsert_entry | ||
|
|
||
| test_metadata = { | ||
| "codes": { | ||
| "1001": "Updated warning message from test", | ||
| "1002": "Updated warning message from test 2", | ||
| }, | ||
| "codes2": { | ||
| "2001": "Updated warning message from another test", | ||
| "2002": "Updated warning message from another test 2", | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| class WarningMessagesTest(TestCase): | ||
| def setUp(self) -> None: | ||
| self.db = DB(DBType.MEMORY) | ||
| create_model(self.db) | ||
|
|
||
| def test_update_warning_messages(self) -> None: | ||
| with ExitStack() as stack: | ||
| temporary_meta_file = stack.enter_context( | ||
| tempfile.NamedTemporaryFile(mode="w+") | ||
| ) | ||
| json.dump(test_metadata, temporary_meta_file) | ||
| temporary_meta_file.flush() # flush the buffer | ||
| session = stack.enter_context(self.db.make_session()) | ||
| update_warning_messages(self.db, pathlib.Path(temporary_meta_file.name)) | ||
| code1001 = ( | ||
| session.query(WarningMessage).filter_by(code="1001").one_or_none() | ||
| ) | ||
| code1002 = ( | ||
| session.query(WarningMessage).filter_by(code="1002").one_or_none() | ||
| ) | ||
| self.assertIsNotNone(code1001) | ||
| self.assertEqual( | ||
| code1001.message, | ||
| test_metadata["codes"]["1001"], | ||
| ) | ||
| self.assertIsNotNone(code1002) | ||
| self.assertEqual( | ||
| code1002.message, | ||
| test_metadata["codes"]["1002"], | ||
| ) | ||
|
|
||
| def test_upsert_entry(self) -> None: | ||
| warning_messages_table = Table( | ||
| WarningMessage.__tablename__, WarningMessage.metadata | ||
| ) | ||
| codes = test_metadata["codes2"] | ||
| with self.db.make_session() as session: | ||
| with session.connection() as database_connection: | ||
| for code, message in codes.items(): | ||
| # Test for insertion | ||
| upsert_entry( | ||
arpancodes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| database_connection, warning_messages_table, int(code), message | ||
| ) | ||
|
|
||
| self.assertEqual( | ||
| database_connection.execute( | ||
| warning_messages_table.select().where( | ||
| warning_messages_table.c.code == "2001" | ||
| ) | ||
| ) | ||
| .first() | ||
| .message, | ||
| "Updated warning message from another test", | ||
| ) | ||
| self.assertEqual( | ||
| database_connection.execute( | ||
| warning_messages_table.select().where( | ||
| warning_messages_table.c.code == "2002" | ||
| ) | ||
| ) | ||
| .first() | ||
| .message, | ||
| "Updated warning message from another test 2", | ||
| ) | ||
| # Test for Updation | ||
| upsert_entry( | ||
| database_connection, warning_messages_table, 2001, "Testing Update" | ||
| ) | ||
| self.assertEqual( | ||
| database_connection.execute( | ||
| warning_messages_table.select().where( | ||
| warning_messages_table.c.code == "2001" | ||
| ) | ||
| ) | ||
| .first() | ||
| .message, | ||
| "Testing Update", | ||
| ) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.