|
| 1 | +import json |
| 2 | +from pathlib import Path |
| 3 | +from typing import TypeVar |
| 4 | + |
| 5 | +import httpx |
| 6 | +import pytest |
| 7 | + |
| 8 | +from githubkit import GitHub |
| 9 | +from githubkit.versions.latest.models import FullRepository |
| 10 | + |
| 11 | +T = TypeVar("T") |
| 12 | + |
| 13 | + |
| 14 | +FAKE_RESPONSE = json.loads((Path(__file__).parent / "fake_response.json").read_text()) |
| 15 | + |
| 16 | + |
| 17 | +def target_sync_func(github: GitHub): |
| 18 | + resp = github.rest.repos.get("owner", "repo") |
| 19 | + return resp.parsed_data |
| 20 | + |
| 21 | + |
| 22 | +def mock_transport_handler(request: httpx.Request) -> httpx.Response: |
| 23 | + if request.method == "GET" and request.url.path == "/repos/owner/repo": |
| 24 | + return httpx.Response(status_code=200, json=FAKE_RESPONSE) |
| 25 | + raise RuntimeError(f"Unexpected request: {request.method} {request.url.path}") |
| 26 | + |
| 27 | + |
| 28 | +def test_sync_mock(): |
| 29 | + g = GitHub("xxxxx", transport=httpx.MockTransport(mock_transport_handler)) |
| 30 | + repo = target_sync_func(g) |
| 31 | + assert isinstance(repo, FullRepository) |
| 32 | + |
| 33 | + |
| 34 | +async def target_async_func(github: GitHub): |
| 35 | + resp = await github.rest.repos.async_get("owner", "repo") |
| 36 | + return resp.parsed_data |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.anyio |
| 40 | +async def test_async_mock(): |
| 41 | + g = GitHub("xxxxx", async_transport=httpx.MockTransport(mock_transport_handler)) |
| 42 | + repo = await target_async_func(g) |
| 43 | + assert isinstance(repo, FullRepository) |
0 commit comments