-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement basic automated tests targeting the
validate_json
function
- Loading branch information
Showing
3 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
This directory contains file related to testing code written in the `nmdc_runtime/util.py` file. | ||
|
||
I named the directory "`test_util`" in an attempt to follow the naming convention of the other test directories. | ||
In its name, "`test`" is a verb and "`util`" is a noun (i.e. "to test the utility"). This is in contrast to the file | ||
`../test_util.py`, in whose name "`test`" is an adjective and "`util`" is a noun (i.e. "a test-related utility"). |
Empty file.
This file contains 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,99 @@ | ||
from nmdc_runtime.api.db.mongo import get_mongo_db | ||
from nmdc_runtime.util import validate_json | ||
|
||
# Tip: At the time of this writing, you can run the tests in this file without running other tests in this repo, | ||
# by issuing the following command from the root directory of the repository: | ||
# ``` | ||
# $ pytest tests/test_util/test_util.py | ||
# ``` | ||
|
||
|
||
def test_validate_json(): | ||
# Get a reference to the MongoDB database, since the `validate_json` function requires | ||
# it to be passed in as a parameter. | ||
mdb = get_mongo_db() | ||
|
||
# Define a reusable dictionary that matches the value the `validate_json` function | ||
# returns when it considers the input to be valid. | ||
ok_result = {"result": "All Okay!"} | ||
|
||
# Test: An empty outer dictionary is valid. | ||
database_dict = {} | ||
result = validate_json(in_docs=database_dict, mdb=mdb) | ||
assert result == ok_result | ||
|
||
# Test: An empty collection is valid. | ||
database_dict = {"study_set": []} | ||
result = validate_json(in_docs=database_dict, mdb=mdb) | ||
assert result == ok_result | ||
|
||
# Test: Two empty collections is valid. | ||
database_dict = {"biosample_set": [], "study_set": []} | ||
result = validate_json(in_docs=database_dict, mdb=mdb) | ||
assert result == ok_result | ||
|
||
# Test: A schema-compliant document is valid. | ||
database_dict = { | ||
"study_set": [ | ||
{ | ||
"id": "nmdc:sty-00-000001", | ||
"type": "nmdc:Study", | ||
"study_category": "research_study", | ||
} | ||
] | ||
} | ||
result = validate_json(in_docs=database_dict, mdb=mdb) | ||
assert result == ok_result | ||
|
||
# Test: Multiple schema-compliant documents are valid. | ||
database_dict = { | ||
"study_set": [ | ||
{ | ||
"id": "nmdc:sty-00-000001", | ||
"type": "nmdc:Study", | ||
"study_category": "research_study", | ||
}, | ||
{ | ||
"id": "nmdc:sty-00-000002", | ||
"type": "nmdc:Study", | ||
"study_category": "research_study", | ||
}, | ||
] | ||
} | ||
result = validate_json(in_docs=database_dict, mdb=mdb) | ||
assert result == ok_result | ||
|
||
# Test: The function reports an error for the schema-defiant document. | ||
database_dict = { | ||
"study_set": [ | ||
{ | ||
"id": "nmdc:OTHER-00-000001", | ||
"type": "nmdc:Study", | ||
"study_category": "research_study", | ||
}, | ||
] | ||
} | ||
result = validate_json(in_docs=database_dict, mdb=mdb) | ||
assert result["result"] == "errors" | ||
assert "study_set" in result["detail"] | ||
assert len(result["detail"]["study_set"]) == 1 | ||
|
||
# Test: The function reports an error for each schema-defiant document. | ||
database_dict = { | ||
"study_set": [ | ||
{ | ||
"id": "nmdc:OTHER-00-000001", | ||
"type": "nmdc:Study", | ||
"study_category": "research_study", | ||
}, | ||
{ | ||
"id": "nmdc:OTHER-00-000002", | ||
"type": "nmdc:Study", | ||
"study_category": "research_study", | ||
}, | ||
] | ||
} | ||
result = validate_json(in_docs=database_dict, mdb=mdb) | ||
assert result["result"] == "errors" | ||
assert "study_set" in result["detail"] | ||
assert len(result["detail"]["study_set"]) == 2 |