diff --git a/tests/covalent_tests/results_manager_tests/root_dispatch_id_test.py b/tests/covalent_tests/results_manager_tests/root_dispatch_id_test.py new file mode 100644 index 00000000..25eb4532 --- /dev/null +++ b/tests/covalent_tests/results_manager_tests/root_dispatch_id_test.py @@ -0,0 +1,191 @@ + + + + + + + + + + + + + + + + + + + + + + + +import tempfile + +import pytest + +import covalent as ct +from covalent._data_store.datastore import DataStore +from covalent._data_store.models import Lattice +from covalent._results_manager import result as result_module +from covalent._results_manager.result import Result, initialize_result_object +from covalent._results_manager import write_result_to_db +from covalent._workflow.lattice import Lattice as LatticeClass + + +@pytest.fixture +def test_db(): + + + return DataStore( + db_URL="sqlite+pysqlite:///:memory:", + initialize_db=True, + ) + + +@pytest.fixture(autouse=True) +def patch_db(test_db, monkeypatch): + monkeypatch.setattr(result_module, "workflow_db", test_db) + monkeypatch.setattr(write_result_to_db, "workflow_db", test_db) + + + + monkeypatch.setattr(result_module, "insert_electron_dependency_data", lambda *a, **k: None) + monkeypatch.setattr( + write_result_to_db, "insert_electron_dependency_data", lambda *a, **k: None + ) + return test_db + + +def _make_json_lattice(name="workflow"): + results_dir = tempfile.mkdtemp() + + @ct.electron + def task(x): + return x + + @ct.lattice(results_dir=results_dir) + def workflow(x): + return task(x) + + workflow.__name__ = name + workflow.build_graph(2) + return workflow.serialize_to_json() + + +def _make_result_directly(dispatch_id="dispatch_x"): + json_lattice = _make_json_lattice() + lattice = LatticeClass.deserialize_from_json(json_lattice) + result = Result( + lattice=lattice, + results_dir=lattice.metadata["results_dir"], + dispatch_id=dispatch_id, + ) + result._initialize_nodes() + return result + + + + + +def test_root_dispatch_id_defaults_to_own_dispatch_id(): + result = _make_result_directly(dispatch_id="dispatch_abc") + assert result.root_dispatch_id == "dispatch_abc" + + +def test_root_dispatch_id_is_read_only(): + result = _make_result_directly(dispatch_id="dispatch_abc") + with pytest.raises(AttributeError): + result.root_dispatch_id = "something_else" + + + + + +def test_persist_without_electron_id_keeps_null(test_db): + result = _make_result_directly(dispatch_id="dispatch_top") + result.persist() + + with test_db.session() as session: + lattice_row = session.query(Lattice).filter_by(dispatch_id="dispatch_top").first() + assert lattice_row is not None + assert lattice_row.electron_id is None + + +def test_persist_with_electron_id_writes_it(test_db): + result = _make_result_directly(dispatch_id="dispatch_sub") + result.persist(electron_id=42) + + with test_db.session() as session: + lattice_row = session.query(Lattice).filter_by(dispatch_id="dispatch_sub").first() + assert lattice_row is not None + assert lattice_row.electron_id == 42 + + + + + +def test_initialize_result_object_toplevel(test_db): + json_lattice = _make_json_lattice() + result = initialize_result_object(json_lattice) + + assert result.dispatch_id is not None + assert result.root_dispatch_id == result.dispatch_id + + with test_db.session() as session: + lattice_row = ( + session.query(Lattice).filter_by(dispatch_id=result.dispatch_id).first() + ) + assert lattice_row is not None + assert lattice_row.electron_id is None + + +def test_initialize_result_object_unique_dispatch_ids(): + json_lattice = _make_json_lattice() + r1 = initialize_result_object(json_lattice) + r2 = initialize_result_object(json_lattice) + assert r1.dispatch_id != r2.dispatch_id + + +def test_initialize_result_object_sublattice_inherits_root(test_db): + parent = initialize_result_object(_make_json_lattice(name="parent")) + + sub = initialize_result_object( + _make_json_lattice(name="sub"), + parent_result_object=parent, + parent_electron_id=7, + ) + + + assert sub.dispatch_id != parent.dispatch_id + + assert sub.root_dispatch_id == parent.root_dispatch_id + assert sub.root_dispatch_id == parent.dispatch_id + + + with test_db.session() as session: + lattice_row = ( + session.query(Lattice).filter_by(dispatch_id=sub.dispatch_id).first() + ) + assert lattice_row is not None + assert lattice_row.electron_id == 7 + + +def test_initialize_result_object_nested_sublattice_inherits_root(test_db): + root = initialize_result_object(_make_json_lattice(name="root")) + mid = initialize_result_object( + _make_json_lattice(name="mid"), + parent_result_object=root, + parent_electron_id=1, + ) + leaf = initialize_result_object( + _make_json_lattice(name="leaf"), + parent_result_object=mid, + parent_electron_id=2, + ) + + assert mid.root_dispatch_id == root.dispatch_id + assert leaf.root_dispatch_id == root.dispatch_id + assert leaf.dispatch_id != root.dispatch_id + assert leaf.dispatch_id != mid.dispatch_id diff --git a/mimo_test_command.sh b/mimo_test_command.sh new file mode 100755 --- /dev/null +++ b/mimo_test_command.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + + + + +set -euo pipefail +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +cd /testbed + + + + + + + + +python -m pytest -q \ + tests/covalent_tests/data_store/data_store_test.py \ + tests/covalent_tests/workflow/lattice_serialization_test.py + + +python -m pytest -q \ + tests/covalent_tests/results_manager_tests/root_dispatch_id_test.py