Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions act/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ def _daysago_type(value: str) -> int:


def _token_type(value: str) -> str:
# Allow empty for optional --targeting-token; --token stays required=True.
return value


def _required_token_type(value: str) -> str:
if not value.strip():
raise argparse.ArgumentTypeError("token must be a non-empty string")
return value
Expand Down Expand Up @@ -69,9 +74,22 @@ def build_parser() -> argparse.ArgumentParser:
)
parser.add_argument(
"--token",
type=_token_type,
type=_required_token_type,
required=True,
help="RedCap API token (required, non-empty)",
help=(
"Observational REDCap API token (BOOST_TOKEN) for reports 43327/41693"
),
)
parser.add_argument(
"--targeting-token",
type=_token_type,
default="",
dest="targeting_token",
help=(
"Intervention/Targeting REDCap API token for report 53846 "
"(defaults to TARGETING_TOKEN or DEBRIEF_FITNESS_TOKEN env). "
"Pass empty string to skip INT report."
),
)
parser.add_argument(
"--daysago",
Expand Down Expand Up @@ -106,12 +124,24 @@ def main(argv: list[str] | None = None) -> int:
_configure_logging()
args = build_parser().parse_args(argv)

targeting_token = (
(args.targeting_token or "").strip()
or os.environ.get("TARGETING_TOKEN", "").strip()
or os.environ.get("DEBRIEF_FITNESS_TOKEN", "").strip()
)
if not targeting_token:
logging.warning(
"No Targeting token (--targeting-token / TARGETING_TOKEN); "
"INT report 53846 will be skipped"
)

p = Pipe(
token=args.token,
daysago=args.daysago,
system=args.system,
rebuild_manifest_only=args.rebuild_manifest_only,
reconcile_manifest_only=args.reconcile_manifest_only,
targeting_token=targeting_token or None,
)

try:
Expand Down
17 changes: 17 additions & 0 deletions act/tests/test_pipeline_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,15 @@ def __init__(
system,
rebuild_manifest_only=False,
reconcile_manifest_only=False,
targeting_token=None,
):
call_state["pipe_args"] = {
"token": token,
"daysago": daysago,
"system": system,
"rebuild_manifest_only": rebuild_manifest_only,
"reconcile_manifest_only": reconcile_manifest_only,
"targeting_token": targeting_token,
}

def run_pipe(self):
Expand Down Expand Up @@ -287,6 +289,8 @@ def plot_session(self):
_install_module(monkeypatch, "act.utils.pipe", pipe_mod)
_install_module(monkeypatch, "act.utils.group", group_mod)

monkeypatch.setenv("TARGETING_TOKEN", "")
monkeypatch.setenv("DEBRIEF_FITNESS_TOKEN", "")
monkeypatch.setattr(
sys,
"argv",
Expand All @@ -312,6 +316,7 @@ def plot_session(self):
"system": "local",
"rebuild_manifest_only": False,
"reconcile_manifest_only": False,
"targeting_token": None,
}
assert call_state["run_pipe"] == 1
assert call_state["group_systems"] == ["local", "person", "local", "session"]
Expand All @@ -328,13 +333,15 @@ def __init__(
system,
rebuild_manifest_only=False,
reconcile_manifest_only=False,
targeting_token=None,
):
call_state["pipe_args"] = {
"token": token,
"daysago": daysago,
"system": system,
"rebuild_manifest_only": rebuild_manifest_only,
"reconcile_manifest_only": reconcile_manifest_only,
"targeting_token": targeting_token,
}

def run_pipe(self):
Expand Down Expand Up @@ -363,6 +370,8 @@ def plot_session(self):
_install_module(monkeypatch, "act.utils.pipe", pipe_mod)
_install_module(monkeypatch, "act.utils.group", group_mod)

monkeypatch.setenv("TARGETING_TOKEN", "")
monkeypatch.setenv("DEBRIEF_FITNESS_TOKEN", "")
monkeypatch.setattr(
sys,
"argv",
Expand All @@ -388,6 +397,7 @@ def plot_session(self):
"system": "local",
"rebuild_manifest_only": True,
"reconcile_manifest_only": False,
"targeting_token": None,
}
assert call_state["run_pipe"] == 1
assert call_state["group_inits"] == 0
Expand All @@ -404,13 +414,15 @@ def __init__(
system,
rebuild_manifest_only=False,
reconcile_manifest_only=False,
targeting_token=None,
):
call_state["pipe_args"] = {
"token": token,
"daysago": daysago,
"system": system,
"rebuild_manifest_only": rebuild_manifest_only,
"reconcile_manifest_only": reconcile_manifest_only,
"targeting_token": targeting_token,
}

def run_pipe(self):
Expand Down Expand Up @@ -442,6 +454,8 @@ def __init__(self, system):
_install_module(monkeypatch, "act.utils.pipe", pipe_mod)
_install_module(monkeypatch, "act.utils.group", group_mod)

monkeypatch.setenv("TARGETING_TOKEN", "")
monkeypatch.setenv("DEBRIEF_FITNESS_TOKEN", "")
monkeypatch.setattr(
sys,
"argv",
Expand All @@ -467,6 +481,7 @@ def __init__(self, system):
"system": "local",
"rebuild_manifest_only": False,
"reconcile_manifest_only": True,
"targeting_token": None,
}
assert call_state["run_pipe"] == 1
assert call_state["group_inits"] == 0
Expand All @@ -481,6 +496,7 @@ def __init__(
system,
rebuild_manifest_only=False,
reconcile_manifest_only=False,
targeting_token=None,
):
pass

Expand Down Expand Up @@ -542,6 +558,7 @@ def __init__(
system,
rebuild_manifest_only=False,
reconcile_manifest_only=False,
targeting_token=None,
):
pass

Expand Down
52 changes: 52 additions & 0 deletions act/tests/test_report_merge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Unit tests for multi-report boost↔lab merge (no network).

Loads comparison_utils by path so conftest's ID_COMPARISONS stub does not apply.
"""
from __future__ import annotations

import importlib.util
from pathlib import Path

import pandas as pd

_PATH = Path(__file__).resolve().parents[1] / "utils" / "comparison_utils.py"
_SPEC = importlib.util.spec_from_file_location("comparison_utils_real", _PATH)
_MOD = importlib.util.module_from_spec(_SPEC)
assert _SPEC.loader is not None
_SPEC.loader.exec_module(_MOD)
_merge = _MOD.ID_COMPARISONS._merge_report_frames


def test_merge_later_source_overrides_lab_conflict():
a = pd.DataFrame(
[
{"boost_id": "8080", "lab_id": "9999", "_source": "obs:43327"},
{"boost_id": "7222", "lab_id": "1417", "_source": "obs:43327"},
]
)
b = pd.DataFrame(
[
{"boost_id": "8080", "lab_id": "1501", "_source": "int:53846"},
]
)
merged = _merge([a, b])
by_boost = {str(r.boost_id): str(r.lab_id) for r in merged.itertuples()}
assert by_boost["8080"] == "1501"
assert by_boost["7222"] == "1417"


def test_merge_skips_empty_lab():
a = pd.DataFrame(
[
{"boost_id": "9002", "lab_id": "", "_source": "int:53846"},
{"boost_id": "8078", "lab_id": "1433", "_source": "int:53846"},
]
)
merged = _merge([a])
assert list(merged["boost_id"]) == ["8078"]


def test_normalize_id_cell_strips_float_dot_zero():
assert _MOD._normalize_id_cell(8078.0) == "8078"
assert _MOD._normalize_id_cell("nan") == ""
assert _MOD._normalize_id_cell("7178, 8066") == "7178, 8066"
Loading
Loading