test: add 106 unit + smoke tests, 93% coverage
- pytest + respx + pytest-cov added as dev dependencies - tests/smoke/test_registration.py: verify all 66 tools registered and unique - tests/unit/: 12 files covering every tool — HTTP method, URL, params/body, errors - tests/config.test.json + conftest.py: zero-network test setup via GITEA_CONFIG - CLAUDE.md: developer guide with testing philosophy, pyramid, and commands - server/gitea_mcp.py: get_commit, list_pr_files tools added - README.md: tool count corrected to 66
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
import json
|
||||
import pytest
|
||||
import httpx
|
||||
import respx
|
||||
from server.gitea_mcp import (
|
||||
list_my_repos, search_repos, get_repo, create_repo, create_org_repo,
|
||||
update_repo, delete_repo, fork_repo, set_repo_topics,
|
||||
)
|
||||
|
||||
BASE = "https://gitea.test/api/v1"
|
||||
|
||||
|
||||
@respx.mock
|
||||
def test_list_my_repos_defaults():
|
||||
route = respx.get(f"{BASE}/user/repos").mock(return_value=httpx.Response(200, json=[]))
|
||||
list_my_repos()
|
||||
assert route.called
|
||||
params = dict(route.calls[0].request.url.params)
|
||||
assert params["limit"] == "50"
|
||||
assert params["page"] == "1"
|
||||
|
||||
|
||||
@respx.mock
|
||||
def test_list_my_repos_custom_page():
|
||||
route = respx.get(f"{BASE}/user/repos").mock(return_value=httpx.Response(200, json=[]))
|
||||
list_my_repos(limit=10, page=3)
|
||||
assert route.called
|
||||
params = dict(route.calls[0].request.url.params)
|
||||
assert params["limit"] == "10"
|
||||
assert params["page"] == "3"
|
||||
|
||||
|
||||
@respx.mock
|
||||
def test_search_repos():
|
||||
route = respx.get(f"{BASE}/repos/search").mock(
|
||||
return_value=httpx.Response(200, json={"data": []})
|
||||
)
|
||||
result = search_repos("myquery")
|
||||
assert route.called
|
||||
params = dict(route.calls[0].request.url.params)
|
||||
assert params["q"] == "myquery"
|
||||
assert params["limit"] == "20"
|
||||
|
||||
|
||||
@respx.mock
|
||||
def test_get_repo():
|
||||
route = respx.get(f"{BASE}/repos/owner/myrepo").mock(
|
||||
return_value=httpx.Response(200, json={"name": "myrepo", "full_name": "owner/myrepo"})
|
||||
)
|
||||
result = get_repo("owner", "myrepo")
|
||||
assert route.called
|
||||
assert result["name"] == "myrepo"
|
||||
|
||||
|
||||
@respx.mock
|
||||
def test_get_repo_not_found():
|
||||
respx.get(f"{BASE}/repos/owner/missing").mock(
|
||||
return_value=httpx.Response(404, json={"message": "not found"})
|
||||
)
|
||||
with pytest.raises(RuntimeError, match="404"):
|
||||
get_repo("owner", "missing")
|
||||
|
||||
|
||||
@respx.mock
|
||||
def test_create_repo_defaults():
|
||||
route = respx.post(f"{BASE}/user/repos").mock(
|
||||
return_value=httpx.Response(201, json={"name": "newrepo"})
|
||||
)
|
||||
result = create_repo("newrepo")
|
||||
assert route.called
|
||||
body = json.loads(route.calls[0].request.content)
|
||||
assert body["name"] == "newrepo"
|
||||
assert body["private"] is True
|
||||
assert body["auto_init"] is True
|
||||
assert "description" not in body
|
||||
|
||||
|
||||
@respx.mock
|
||||
def test_create_repo_with_options():
|
||||
route = respx.post(f"{BASE}/user/repos").mock(
|
||||
return_value=httpx.Response(201, json={"name": "pub"})
|
||||
)
|
||||
create_repo("pub", description="A public repo", private=False, gitignores="Python")
|
||||
body = json.loads(route.calls[0].request.content)
|
||||
assert body["description"] == "A public repo"
|
||||
assert body["private"] is False
|
||||
assert body["gitignores"] == "Python"
|
||||
|
||||
|
||||
@respx.mock
|
||||
def test_create_org_repo():
|
||||
route = respx.post(f"{BASE}/orgs/myorg/repos").mock(
|
||||
return_value=httpx.Response(201, json={"name": "orgrepo"})
|
||||
)
|
||||
result = create_org_repo("myorg", "orgrepo")
|
||||
assert route.called
|
||||
body = json.loads(route.calls[0].request.content)
|
||||
assert body["name"] == "orgrepo"
|
||||
|
||||
|
||||
@respx.mock
|
||||
def test_update_repo():
|
||||
route = respx.patch(f"{BASE}/repos/owner/repo").mock(
|
||||
return_value=httpx.Response(200, json={"name": "repo"})
|
||||
)
|
||||
update_repo("owner", "repo", description="updated")
|
||||
assert route.called
|
||||
body = json.loads(route.calls[0].request.content)
|
||||
assert body["description"] == "updated"
|
||||
|
||||
|
||||
@respx.mock
|
||||
def test_delete_repo():
|
||||
route = respx.delete(f"{BASE}/repos/owner/repo").mock(
|
||||
return_value=httpx.Response(204)
|
||||
)
|
||||
result = delete_repo("owner", "repo")
|
||||
assert route.called
|
||||
assert result == {"success": True}
|
||||
|
||||
|
||||
@respx.mock
|
||||
def test_fork_repo():
|
||||
route = respx.post(f"{BASE}/repos/upstream/repo/forks").mock(
|
||||
return_value=httpx.Response(202, json={"name": "repo"})
|
||||
)
|
||||
fork_repo("upstream", "repo")
|
||||
assert route.called
|
||||
|
||||
|
||||
@respx.mock
|
||||
def test_fork_repo_to_org():
|
||||
route = respx.post(f"{BASE}/repos/upstream/repo/forks").mock(
|
||||
return_value=httpx.Response(202, json={"name": "myfork"})
|
||||
)
|
||||
fork_repo("upstream", "repo", organization="myorg", new_repo_name="myfork")
|
||||
body = json.loads(route.calls[0].request.content)
|
||||
assert body["organization"] == "myorg"
|
||||
assert body["name"] == "myfork"
|
||||
|
||||
|
||||
@respx.mock
|
||||
def test_set_repo_topics():
|
||||
route = respx.put(f"{BASE}/repos/owner/repo/topics").mock(
|
||||
return_value=httpx.Response(204)
|
||||
)
|
||||
result = set_repo_topics("owner", "repo", ["python", "mcp"])
|
||||
assert route.called
|
||||
body = json.loads(route.calls[0].request.content)
|
||||
assert body["topics"] == ["python", "mcp"]
|
||||
assert result == {"success": True}
|
||||
Reference in New Issue
Block a user