Skip to content

Commit

Permalink
Implement basic automated tests targeting the validate_json function
Browse files Browse the repository at this point in the history
  • Loading branch information
eecavanna committed Dec 12, 2024
1 parent 82ce38e commit ae83dc9
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/test_util/README.md
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 added tests/test_util/__init__.py
Empty file.
99 changes: 99 additions & 0 deletions tests/test_util/test_util.py
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

0 comments on commit ae83dc9

Please sign in to comment.