diff --git a/tests/test_tea_job.py b/tests/test_tea_job.py index 61f30de..bd2eb81 100644 --- a/tests/test_tea_job.py +++ b/tests/test_tea_job.py @@ -1,5 +1,6 @@ """The tea_eeg job's graph, sensor, config classes and synthetic EEG scope.""" +import os from pathlib import Path import dagster as dg @@ -7,7 +8,7 @@ import pytest from pydantic import ValidationError from sqlalchemy import inspect -from models import definitions +from models import definitions, execution from models.definitions import ( ExecutionConfig, ScopeConfig, @@ -143,8 +144,8 @@ def test_relate_is_a_no_op_until_phase_2(): assert tea_jobs.relate() is None -@pytest.mark.parametrize("platform", ["sqlite"], indirect=True) -def test_tea_eeg_first_n_analyses_exactly_n(platform, tmp_path, monkeypatch): +@pytest.fixture +def synthetic_tea(platform, tmp_path, monkeypatch): tea.build(tmp_path / "hospital_tea.db") fixtures = Path(__file__).parents[2] / "tests" / "fixtures" drawing = tmp_path / "pipeline.json" @@ -162,13 +163,62 @@ def test_tea_eeg_first_n_analyses_exactly_n(platform, tmp_path, monkeypatch): monkeypatch.setattr(destination, "DATA_DIR", tmp_path) destination.engine.cache_clear() try: - table = "bronze.eeg_analysis" - with destination.engine().connect() as con: - has_analysis = inspect(con).has_table("eeg_analysis", schema="bronze") - before = platform.count(table) if has_analysis else 0 - pending = len(eeg.pending()) - tea_jobs.analyze(eeg.Scope(mode="first_n", n=3), rebuild_gold=False) - assert platform.count(table) == before + 3 - assert len(eeg.pending()) == pending - 3 + yield platform finally: destination.engine.cache_clear() + + +@pytest.mark.parametrize("platform", ["sqlite"], indirect=True) +def test_tea_eeg_first_n_analyses_exactly_n(synthetic_tea): + table = "bronze.eeg_analysis" + with destination.engine().connect() as con: + has_analysis = inspect(con).has_table("eeg_analysis", schema="bronze") + before = synthetic_tea.count(table) if has_analysis else 0 + pending = len(eeg.pending()) + tea_jobs.analyze(eeg.Scope(mode="first_n", n=3), rebuild_gold=False) + assert synthetic_tea.count(table) == before + 3 + assert len(eeg.pending()) == pending - 3 + + +@pytest.mark.skipif( + not os.environ.get("DASK_TEST_SCHEDULER"), reason="DASK_TEST_SCHEDULER is not set" +) +@pytest.mark.parametrize("platform", ["sqlite"], indirect=True) +def test_tea_eeg_remote_mode_gives_the_same_analysis(synthetic_tea): + def snapshot(): + analyses = synthetic_tea.query( + "select edf, valid, level, age_months, clean_fraction, bad_channels " + "from bronze.eeg_analysis order by edf" + ) + metrics = synthetic_tea.query( + "select edf, code, value from bronze.eeg_metrics order by edf, code" + ) + return analyses, metrics + + assert tea_jobs.analyze(eeg.Scope(mode="first_n", n=2), rebuild_gold=False) == 2 + before, metrics_before = snapshot() + ids = [study["id"] for study in eeg.studies() if study["analysed"]] + assert len(ids) == 2 + + @dg.op + def remote_analysis(context): + remote = cluster.Execution(mode="remote", scheduler=os.environ["DASK_TEST_SCHEDULER"]) + with execution.running(context, remote): + return tea_jobs.analyze(eeg.Scope(mode="ids", ids=ids), rebuild_gold=False) + + @dg.job + def reanalyze(): + remote_analysis() + + result = reanalyze.execute_in_process() + assert result.success + assert result.output_for_node("remote_analysis") == 2 + after, metrics_after = snapshot() + + def close(value): + return pytest.approx(value, rel=1e-12) if isinstance(value, float) else value + + assert [tuple(row) for row in after] == [tuple(map(close, row)) for row in before] + assert [tuple(row) for row in metrics_after] == [ + tuple(map(close, row)) for row in metrics_before + ]