-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(testing): add testing for ontology-builder
feat(testing): add testing for ontology-builder
- Loading branch information
Showing
8 changed files
with
123 additions
and
1 deletion.
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
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
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 |
---|---|---|
@@ -1,3 +1,2 @@ | ||
lint: | ||
pre-commit run --all-files | ||
|
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,2 @@ | ||
unit-tests: | ||
python -m pytest tests |
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,4 @@ | ||
# Run Tests | ||
|
||
1. `pip install -r requirements.txt -r requirements-dev.txt` | ||
2. `make unit-tests` |
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,4 @@ | ||
[tool.pytest.ini_options] | ||
pythonpath = [ | ||
"src" | ||
] |
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 @@ | ||
pytest |
85 changes: 85 additions & 0 deletions
85
tools/ontology-builder/tests/test_all_ontology_generator.py
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,85 @@ | ||
import os | ||
import urllib.request | ||
from unittest.mock import MagicMock, patch | ||
|
||
import pytest | ||
from all_ontology_generator import _download_ontologies, _parse_ontologies | ||
|
||
|
||
@pytest.fixture | ||
def mock_ontology_info(tmpdir): | ||
# Create a temporary ontology info yaml file | ||
onto_info_yml = tmpdir.join("ontology_info.yml") | ||
onto_info_yml.write("ontology_name:\n source: http://example.com\n version: v1\n filetype: owl\n") | ||
return str(onto_info_yml) | ||
|
||
|
||
@pytest.fixture | ||
def mock_raw_ontology_dir(tmpdir): | ||
# Create a temporary directory for raw ontology files | ||
raw_ontology_dir = tmpdir.mkdir("raw_ontology") | ||
return str(raw_ontology_dir) | ||
|
||
|
||
@pytest.fixture | ||
def mock_parsed_ontology_file(tmpdir): | ||
# Create a temporary gzipped json file for parsed ontology data | ||
parsed_ontology_file = tmpdir.join("parsed_ontologies.json.gz") | ||
return str(parsed_ontology_file) | ||
|
||
|
||
def test_download_ontologies(mock_ontology_info, mock_raw_ontology_dir): | ||
# Mocking urllib.request.urlretrieve, urllib.request.urlopen | ||
with patch("urllib.request.urlretrieve") as mock_urlretrieve, patch("urllib.request.urlopen") as mock_urlopen: | ||
# Mock HTTP response | ||
mock_response = MagicMock() | ||
mock_response.code = 200 | ||
mock_urlopen.return_value = mock_response | ||
|
||
# Call the function | ||
_download_ontologies(onto_info_yml=mock_ontology_info, output_dir=mock_raw_ontology_dir) | ||
|
||
mock_urlretrieve.assert_called_once() | ||
|
||
|
||
def test_parse_ontologies(mock_raw_ontology_dir, mock_parsed_ontology_file): | ||
# Mocking _load_ontology_object and _extract_ontology_term_metadata | ||
with patch("all_ontology_generator._load_ontology_object") as mock_load_ontology, patch( | ||
"all_ontology_generator._extract_ontology_term_metadata" | ||
) as mock_extract_metadata: | ||
# Mock return values | ||
mock_load_ontology.return_value = MagicMock(name="ontology_object") | ||
mock_extract_metadata.return_value = {"term_id": {"label": "Term Label", "deprecated": False, "ancestors": []}} | ||
|
||
# Call the function | ||
_parse_ontologies(working_dir=mock_raw_ontology_dir, output_json_file=mock_parsed_ontology_file) | ||
|
||
# Assert _load_ontology_object is called for each ontology file | ||
assert mock_load_ontology.call_count == len(os.listdir(mock_raw_ontology_dir)) | ||
|
||
# Assert _extract_ontology_term_metadata is called for each ontology object | ||
assert mock_extract_metadata.call_count == len(os.listdir(mock_raw_ontology_dir)) | ||
|
||
|
||
def test_download_ontologies_http_error(mock_ontology_info, mock_raw_ontology_dir): | ||
# Mocking urllib.request.urlopen to raise HTTPError | ||
with patch("urllib.request.urlopen") as mock_urlopen: | ||
mock_urlopen.side_effect = urllib.error.HTTPError( | ||
url="http://example.com", code=404, msg="Not Found", hdrs={}, fp=None | ||
) | ||
|
||
# Assertion | ||
with pytest.raises(Exception) as exc_info: | ||
_download_ontologies(onto_info_yml=mock_ontology_info, output_dir=mock_raw_ontology_dir) | ||
assert "returns status code 404" in str(exc_info.value) | ||
|
||
|
||
def test_download_ontologies_url_error(mock_ontology_info, mock_raw_ontology_dir): | ||
# Mocking urllib.request.urlopen to raise URLError | ||
with patch("urllib.request.urlopen") as mock_urlopen: | ||
mock_urlopen.side_effect = urllib.error.URLError(reason="Connection refused") | ||
|
||
# Assertion | ||
with pytest.raises(Exception) as exc_info: | ||
_download_ontologies(onto_info_yml=mock_ontology_info, output_dir=mock_raw_ontology_dir) | ||
assert "fails due to Connection refused" in str(exc_info.value) |