From 39cb95701780fc078bd0005578f18e24e48e0984 Mon Sep 17 00:00:00 2001 From: Evan Morris Date: Tue, 21 Jan 2025 01:27:47 -0500 Subject: [PATCH] improving tests --- tests/graph_specs/testing-graph-spec.yaml | 33 +++++++++++++- tests/test_graph_spec.py | 54 ++++++++++++++++++++--- 2 files changed, 80 insertions(+), 7 deletions(-) diff --git a/tests/graph_specs/testing-graph-spec.yaml b/tests/graph_specs/testing-graph-spec.yaml index f619130f..53f74173 100644 --- a/tests/graph_specs/testing-graph-spec.yaml +++ b/tests/graph_specs/testing-graph-spec.yaml @@ -7,4 +7,35 @@ graphs: output_format: sources: - source_id: CTD - - source_id: HGNC \ No newline at end of file + - 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 \ No newline at end of file diff --git a/tests/test_graph_spec.py b/tests/test_graph_spec.py index 21cb25f0..1051fcda 100644 --- a/tests/test_graph_spec.py +++ b/tests/test_graph_spec.py @@ -2,6 +2,7 @@ import pytest import requests.exceptions +from unittest.mock import MagicMock from Common.build_manager import GraphBuilder, GraphSpecError @@ -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): @@ -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: @@ -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