|
| 1 | +import logging |
| 2 | +import tempfile |
| 3 | +from pathlib import Path |
| 4 | +from types import SimpleNamespace |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.parametrize("status_code", [502, 504]) |
| 10 | +def test_push_finalize_logs_on_5xx_real_diff(caplog, status_code): |
| 11 | + # geodiff is required – if missing, skip the test gracefully |
| 12 | + pytest.importorskip("pygeodiff") |
| 13 | + from pygeodiff import GeoDiff |
| 14 | + |
| 15 | + from mergin.client_push import push_project_finalize |
| 16 | + from mergin.common import ClientError |
| 17 | + |
| 18 | + # --- exact paths according to your repo structure --- |
| 19 | + data_dir = Path(__file__).resolve().parent / "test_data" |
| 20 | + base = data_dir / "base.gpkg" |
| 21 | + modified = data_dir / "inserted_1_A.gpkg" |
| 22 | + |
| 23 | + # if something is missing, print the directory contents and FAIL |
| 24 | + if not base.exists() or not modified.exists(): |
| 25 | + print("[DEBUG] data_dir:", data_dir) |
| 26 | + print("[DEBUG] data_dir.exists():", data_dir.exists()) |
| 27 | + if data_dir.exists(): |
| 28 | + print("[DEBUG] contents:", [p.name for p in sorted(data_dir.iterdir())]) |
| 29 | + pytest.fail(f"Expected files are not available: base={base.exists()} inserted_1_A={modified.exists()}") |
| 30 | + |
| 31 | + # --- create a real .diff (changeset) --- |
| 32 | + with tempfile.TemporaryDirectory(prefix="geodiff-") as tmpdir: |
| 33 | + tmpdir = Path(tmpdir) |
| 34 | + diff_path = tmpdir / "base_to_inserted_1_A.diff" |
| 35 | + |
| 36 | + gd = GeoDiff() |
| 37 | + gd.create_changeset(str(base), str(modified), str(diff_path)) |
| 38 | + diff_size = diff_path.stat().st_size |
| 39 | + file_size = modified.stat().st_size |
| 40 | + |
| 41 | + # --- logger captured by caplog --- |
| 42 | + logger = logging.getLogger("mergin_test") |
| 43 | + logger.setLevel(logging.DEBUG) |
| 44 | + logger.propagate = True |
| 45 | + |
| 46 | + # --- fake mc.post: /finish -> 5xx, /cancel -> ok --- |
| 47 | + def fake_post(url, *args, **kwargs): |
| 48 | + if "/finish/" in url: |
| 49 | + err = ClientError("Gateway error") |
| 50 | + setattr(err, "http_error", status_code) |
| 51 | + raise err |
| 52 | + if "/cancel/" in url: |
| 53 | + return SimpleNamespace(msg="cancelled") |
| 54 | + return SimpleNamespace(msg="ok") |
| 55 | + |
| 56 | + # --- fake future/executor (no exceptions) --- |
| 57 | + fake_future = SimpleNamespace(done=lambda: True, exception=lambda: None, running=lambda: False) |
| 58 | + fake_executor = SimpleNamespace(shutdown=lambda wait=True: None) |
| 59 | + |
| 60 | + # mp.fpath_meta should resolve the relative diff path to an absolute path |
| 61 | + def fpath_meta(rel): |
| 62 | + return str(diff_path) if rel == diff_path.name else rel |
| 63 | + |
| 64 | + # --- minimal job – only what finalize() uses --- |
| 65 | + job = SimpleNamespace( |
| 66 | + executor=fake_executor, |
| 67 | + futures=[fake_future], |
| 68 | + transferred_size=1234, |
| 69 | + total_size=1234, |
| 70 | + upload_queue_items=[1], |
| 71 | + transaction_id="tx-1", |
| 72 | + mp=SimpleNamespace( |
| 73 | + log=logger, |
| 74 | + update_metadata=lambda *a, **k: None, |
| 75 | + apply_push_changes=lambda *a, **k: None, |
| 76 | + fpath_meta=fpath_meta, |
| 77 | + ), |
| 78 | + mc=SimpleNamespace(post=fake_post), |
| 79 | + changes={ |
| 80 | + "added": [], |
| 81 | + "updated": [{ |
| 82 | + "path": modified.name, |
| 83 | + "size": file_size, |
| 84 | + "diff": {"path": diff_path.name, "size": diff_size}, |
| 85 | + "chunks": [1], |
| 86 | + }], |
| 87 | + "removed": [], |
| 88 | + }, |
| 89 | + tmp_dir=SimpleNamespace(cleanup=lambda: None), |
| 90 | + ) |
| 91 | + |
| 92 | + # capture ERROR-level logs from "mergin_test" |
| 93 | + caplog.set_level(logging.ERROR, logger="mergin_test") |
| 94 | + |
| 95 | + # --- call production logic (expecting 5xx) --- |
| 96 | + with pytest.raises(ClientError): |
| 97 | + push_project_finalize(job) |
| 98 | + |
| 99 | + text = caplog.text |
| 100 | + |
| 101 | + assert f"Push failed with HTTP error {status_code}" in text |
| 102 | + assert "Upload details:" in text |
| 103 | + assert "Files:" in text |
| 104 | + assert modified.name in text |
| 105 | + assert f"size={file_size}" in text |
| 106 | + assert f"diff_size={diff_size}" in text |
| 107 | + # helper may compute changes, or log changes=n/a – accept both |
| 108 | + assert ("changes=" in text) or ("changes=n/a" in text) |
0 commit comments