Replay git (mirror + LFS), labels, milestones, issues & PRs (with comments), and releases (with assets) entity-by-entity over the shared Forgejo/Gitea REST API. Two-pass issue/PR creation rewrites #NNN and URL cross-references to the target number space; deleted-branch PR heads are recreated as refs/heads/_pr/<n>. CLI: codeberg-to-gitea migrate owner/name (CODEBERG_TOKEN/GITEA_TOKEN) Verified end-to-end on AlexKucera/helm: 283 issues, 1 PR, 19 labels, 219 comments, git history, with cross-refs correctly rewritten.
74 lines
2.2 KiB
Python
74 lines
2.2 KiB
Python
"""Tests for the API client (pagination + helpers) using a mock transport.
|
|
|
|
No network: httpx.MockTransport answers all requests deterministically.
|
|
"""
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
from codeberg_to_gitea_migrator.clients import ApiClient, ApiError
|
|
|
|
|
|
def _client(handler) -> ApiClient:
|
|
transport = httpx.MockTransport(handler)
|
|
real = ApiClient("https://example.com", "tok")
|
|
# Swap in the mock transport but keep the auth header.
|
|
real._client = httpx.Client(
|
|
base_url=real.api_root,
|
|
headers={"Authorization": "token tok"},
|
|
transport=transport,
|
|
)
|
|
return real
|
|
|
|
|
|
def test_paged_follows_pages_until_short_batch() -> None:
|
|
pages = {
|
|
1: [{"id": i} for i in range(50)],
|
|
2: [{"id": i} for i in range(50, 70)], # < limit → stop
|
|
}
|
|
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
page = int(request.url.params["page"])
|
|
return httpx.Response(200, json=pages.get(page, []))
|
|
|
|
with _client(handler) as c:
|
|
items = c.get_all("/repos/o/r/labels")
|
|
assert len(items) == 70
|
|
assert items[0]["id"] == 0
|
|
assert items[-1]["id"] == 69
|
|
|
|
|
|
def test_get_drops_none_params_and_stringifies_bools() -> None:
|
|
seen = {}
|
|
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
seen["params"] = dict(request.url.params)
|
|
return httpx.Response(200, json={"ok": True})
|
|
|
|
with _client(handler) as c:
|
|
c.get("/repos/o/r", state=None, private=True, draft=False, name="x")
|
|
assert seen["params"] == {"private": "true", "draft": "false", "name": "x"}
|
|
|
|
|
|
def test_raises_on_4xx() -> None:
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
return httpx.Response(404, text="not found")
|
|
|
|
with _client(handler) as c:
|
|
with pytest.raises(ApiError) as exc_info:
|
|
c.get("/repos/o/r/missing")
|
|
assert exc_info.value.status == 404
|
|
|
|
|
|
def test_post_sends_json_body() -> None:
|
|
seen = {}
|
|
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
seen["body"] = request.read().decode()
|
|
return httpx.Response(201, json={"id": 7})
|
|
|
|
with _client(handler) as c:
|
|
result = c.post("/repos/o/r/issues", body={"title": "x"})
|
|
assert result == {"id": 7}
|
|
assert '"title":"x"' in seen["body"]
|