Skip to content

Commit

Permalink
improving tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanDietzMorris committed Jan 21, 2025
1 parent 9a6652d commit 39cb957
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 7 deletions.
33 changes: 32 additions & 1 deletion tests/graph_specs/testing-graph-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,35 @@ graphs:
output_format:
sources:
- source_id: CTD
- source_id: HGNC
- source_id: HGNC

- graph_id: Testing_Graph_2
graph_name: Testing Graph 2
graph_description: 'This is a graph spec that can be used for testing with a subgraph.'
graph_url: ''
output_format:
sources:
- source_id: GtoPdb
subgraphs:
- graph_id: Testing_Graph

- graph_id: Testing_Graph_3
graph_name: Testing Graph 3
graph_description: 'This is a graph spec that can be used for testing an invalid subgraph.'
graph_url: ''
output_format:
sources:
- source_id: GtoPdb
subgraphs:
- graph_id: Invalid_Graph

- graph_id: Testing_Graph_4
graph_name: Testing Graph 4
graph_description: 'This is a graph spec that can be used for testing an invalid subgraph version.'
graph_url: ''
output_format:
sources:
- source_id: GtoPdb
subgraphs:
- graph_id: Testing_Graph
graph_version: abc123
54 changes: 48 additions & 6 deletions tests/test_graph_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest
import requests.exceptions

from unittest.mock import MagicMock
from Common.build_manager import GraphBuilder, GraphSpecError


Expand All @@ -18,10 +19,16 @@ def reset_graph_spec_config():
def get_testing_graph_spec_dir():
# this is ORION/tests/graph_specs not ORION/graph_specs
testing_specs_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'graph_specs')
print(testing_specs_dir)
return testing_specs_dir


def get_source_data_manager_mock():
s_d_mock = MagicMock()
s_d_mock.get_latest_source_version = MagicMock()
s_d_mock.get_latest_source_version.side_effect = lambda arg: arg + '_v1'
return s_d_mock


def test_empty_graph_spec_config():
clear_graph_spec_config()
with pytest.raises(GraphSpecError):
Expand All @@ -42,24 +49,22 @@ def test_invalid_graph_spec_url_config():
graph_builder = GraphBuilder()


# the graph spec is loaded up properly but doesn't attempt to determine versions when unspecified
def test_valid_graph_spec_config():
reset_graph_spec_config()
os.environ['ORION_GRAPH_SPEC'] = 'testing-graph-spec.yaml'
graph_builder = GraphBuilder(graph_specs_dir=get_testing_graph_spec_dir())
assert len(graph_builder.graph_specs)

testing_graph_spec = graph_builder.graph_specs.get('Testing_Graph', None)
assert testing_graph_spec is not None

assert len(testing_graph_spec.sources) == 2

for source in testing_graph_spec.sources:
assert source.version is None
assert source.source_version is None


# graph spec sources are able to return versions once source_version(s) are set
def test_graph_spec_lazy_versions():
reset_graph_spec_config()
os.environ['ORION_GRAPH_SPEC'] = 'testing-graph-spec.yaml'
graph_builder = GraphBuilder(graph_specs_dir=get_testing_graph_spec_dir())
testing_graph_spec = graph_builder.graph_specs.get('Testing_Graph', None)
for source in testing_graph_spec.sources:
Expand All @@ -70,4 +75,41 @@ def test_graph_spec_lazy_versions():
assert source.version is not None


# mock the source_data_manager to return deterministic source_versions
# then see if a graph with a subgraph can properly determine graph versions
def test_graph_spec_subgraph_version():
reset_graph_spec_config()
graph_builder = GraphBuilder(graph_specs_dir=get_testing_graph_spec_dir())
graph_builder.source_data_manager = get_source_data_manager_mock()

testing_graph_spec = graph_builder.graph_specs.get('Testing_Graph_2', None)
assert testing_graph_spec.graph_version is None
graph_builder.determine_graph_version(testing_graph_spec)
for source in testing_graph_spec.sources:
assert source.source_version == source.id + "_v1"
testing_graph_spec_sub_graph = graph_builder.graph_specs.get('Testing_Graph', None)
for source in testing_graph_spec_sub_graph.sources:
assert source.source_version == source.id + "_v1"
assert testing_graph_spec_sub_graph.graph_version == "0f69593274e4bf24"
assert testing_graph_spec.graph_version == "a802ebc12e481d8a"


# make sure a graph spec with an invalid subgraph fails with the appropriate exception
def test_graph_spec_invalid_subgraph():
reset_graph_spec_config()
graph_builder = GraphBuilder(graph_specs_dir=get_testing_graph_spec_dir())
graph_builder.source_data_manager = get_source_data_manager_mock()
testing_graph_spec = graph_builder.graph_specs.get('Testing_Graph_3', None)
assert testing_graph_spec.graph_version is None
with pytest.raises(GraphSpecError):
graph_builder.determine_graph_version(testing_graph_spec)


# make sure a graph spec with an invalid subgraph version (which is otherwise valid) fails to build
def test_graph_spec_invalid_subgraph_version():
reset_graph_spec_config()
graph_builder = GraphBuilder(graph_specs_dir=get_testing_graph_spec_dir())
graph_builder.source_data_manager = get_source_data_manager_mock()
testing_graph_spec = graph_builder.graph_specs.get('Testing_Graph_4', None)
graph_builder.determine_graph_version(testing_graph_spec)
assert graph_builder.build_graph(testing_graph_spec) is False

0 comments on commit 39cb957

Please sign in to comment.