Select Git revision
test_acmc.py
mjbonifa authored
test_acmc.py 3.22 KiB
import pytest
import argparse
import sys
import shutil
import logging
from pathlib import Path
from acmc import trud, omop, main, logging_config as lc
# setup logging
logger = lc.setup_logger()
@pytest.fixture
def tmp_dir():
# Setup tmp directory
temp_dir = Path("./tests/tmp")
temp_dir.mkdir(parents=True, exist_ok=True)
# Yield the directory path to the test function
yield temp_dir
# Remove the directory after the test finishes
shutil.rmtree(temp_dir)
@pytest.fixture
def logger():
logger = logging.getLogger('acmc_logger')
logger.setLevel(logging.DEBUG)
stream_handler = logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)
def test_phen_init_local_specified(tmp_dir, monkeypatch, caplog):
with caplog.at_level(logging.DEBUG):
phen_path = tmp_dir / "phen"
monkeypatch.setattr(sys, "argv", ["main.py", "phen", "init", "-d", str(phen_path.resolve())])
# Mock input() to return "yes" to the question about reinitialising the directory
monkeypatch.setattr("builtins.input", lambda _: "y")
main.main()
assert "Phenotype initialised successfully" in caplog.text
def test_phen_workflow(tmp_dir, monkeypatch, caplog):
with caplog.at_level(logging.DEBUG):
phen_path = tmp_dir / "phen"
phen_path = phen_path.resolve()
monkeypatch.setattr(sys, "argv", ["main.py", "phen", "init", "-d", str(phen_path.resolve())])
# Mock input() to return "yes" to the question about reinitialising the directory
monkeypatch.setattr("builtins.input", lambda _: "y")
main.main()
assert "Phenotype initialised successfully" in caplog.text
with caplog.at_level(logging.DEBUG):
# validate phenotype
# copy examples across
shutil.rmtree(phen_path / 'codes')
ex_path = Path('./examples').resolve()
for item in ex_path.iterdir():
source = ex_path / item.name
destination = phen_path / item.name
if source.is_dir():
shutil.copytree(source, destination)
else:
shutil.copy(source, destination)
shutil.copy( phen_path / 'config1.json', phen_path / 'config.json')
monkeypatch.setattr(sys, "argv", ["main.py", "phen", "validate", "-d", str(phen_path.resolve())])
main.main()
assert "Phenotype validated successfully" in caplog.text
# map phenotype
for code_type in ["read2", "read3", "snomed"]:
with caplog.at_level(logging.DEBUG):
monkeypatch.setattr(sys, "argv", ["main.py", "phen", "map", "-d", str(phen_path.resolve()), "-t", code_type])
main.main()
assert "Phenotype processed successfully" in caplog.text
# publish phenotype
with caplog.at_level(logging.DEBUG):
monkeypatch.setattr(sys, "argv", ["main.py", "phen", "publish", "-d", str(phen_path.resolve())])
main.main()
assert "Phenotype published successfully" in caplog.text
# copy phenotype'
with caplog.at_level(logging.DEBUG):
monkeypatch.setattr(sys, "argv", ["main.py", "phen", "copy", "-d", str(phen_path.resolve()), "-td", str(tmp_dir.resolve()), "-v", "v1.0.3"])
main.main()
assert "Phenotype copied successfully" in caplog.text
# diff phenotype
with caplog.at_level(logging.DEBUG):
old_path = tmp_dir / "v1.0.3"
monkeypatch.setattr(sys, "argv", ["main.py", "phen", "diff", "-d", str(phen_path.resolve()), "-old", str(old_path.resolve())])
main.main()
assert "Phenotypes diff'd successfully" in caplog.text