forked from gloriachin/Translator_component_toolkit
-
Notifications
You must be signed in to change notification settings - Fork 5
add fix for JSON passing into query method, add tests #27
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
sierra-moxon
wants to merge
3
commits into
main
Choose a base branch
from
predicate_query
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
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
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,84 @@ | ||
| """Tests for TCT.trapi module, focused on the predicate_query branch changes: | ||
| - build_query defaults to returning a dict (return_json=False) | ||
| - query() accepts a dict instead of a JSON string | ||
| """ | ||
|
|
||
| import json | ||
| import inspect | ||
|
|
||
| import pytest | ||
|
|
||
| from TCT.trapi import build_query, query | ||
|
|
||
|
|
||
| # Test data | ||
| EXAMPLE_QUERIES = [ | ||
| { | ||
| 'subject_ids': ['NCBIGene:3845'], | ||
| 'object_categories': ['biolink:Gene'], | ||
| 'predicates': ['biolink:physically_interacts_with'], | ||
| }, | ||
| { | ||
| 'subject_ids': ['NCBIGene:3845'], | ||
| 'object_categories': ['biolink:Gene'], | ||
| 'predicates': [ | ||
| 'biolink:positively_correlated_with', | ||
| 'biolink:physically_interacts_with', | ||
| ], | ||
| }, | ||
| ] | ||
|
|
||
|
|
||
| def test_build_query_returns_dict_by_default(): | ||
| q = EXAMPLE_QUERIES[0] | ||
| result = build_query(q['subject_ids'], q['object_categories'], q['predicates']) | ||
| assert isinstance(result, dict) | ||
|
|
||
|
|
||
| def test_build_query_default_is_explicitly_false(): | ||
| sig = inspect.signature(build_query) | ||
| assert sig.parameters['return_json'].default is False | ||
|
|
||
|
|
||
| def test_build_query_returns_json_string_when_requested(): | ||
| q = EXAMPLE_QUERIES[0] | ||
| result = build_query(q['subject_ids'], q['object_categories'], q['predicates'], return_json=True) | ||
| assert isinstance(result, str) | ||
| parsed = json.loads(result) | ||
| assert isinstance(parsed, dict) | ||
|
|
||
|
|
||
| def test_build_query_dict_and_json_are_equivalent(): | ||
| q = EXAMPLE_QUERIES[0] | ||
| dict_result = build_query(q['subject_ids'], q['object_categories'], q['predicates'], return_json=False) | ||
| json_result = build_query(q['subject_ids'], q['object_categories'], q['predicates'], return_json=True) | ||
| assert dict_result == json.loads(json_result) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("example_query", EXAMPLE_QUERIES) | ||
| def test_build_query_structure(example_query): | ||
| result = build_query( | ||
| example_query['subject_ids'], | ||
| example_query['object_categories'], | ||
| example_query['predicates'], | ||
| ) | ||
| assert 'message' in result | ||
| qg = result['message']['query_graph'] | ||
| assert qg['edges']['e00']['predicates'] == example_query['predicates'] | ||
| assert qg['edges']['e00']['subject'] == 'n00' | ||
| assert qg['edges']['e00']['object'] == 'n01' | ||
| assert qg['nodes']['n00']['ids'] == example_query['subject_ids'] | ||
| assert qg['nodes']['n01']['categories'] == example_query['object_categories'] | ||
|
|
||
|
|
||
| def test_query_signature_expects_dict(): | ||
| sig = inspect.signature(query) | ||
| assert sig.parameters['query'].annotation is dict | ||
|
|
||
|
|
||
| def test_build_query_output_matches_query_input_type(): | ||
| """build_query's default output type should match what query() expects.""" | ||
| q = EXAMPLE_QUERIES[0] | ||
| result = build_query(q['subject_ids'], q['object_categories'], q['predicates']) | ||
| sig = inspect.signature(query) | ||
| assert isinstance(result, sig.parameters['query'].annotation) |
Oops, something went wrong.
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.