Add unit tests, smoke test script, fix CI to debian-host + test job
- tests/test_catalog.py: 5 unit tests for PypiCatalog (fetch, cache, sort prefix, completions) - tests/test_proxy.py: 5 unit tests for BicepProxy (framing, injection, list result, passthrough) - tests/conftest.py: pytest asyncio_mode=auto config - scripts/smoke_test.sh: end-to-end TCP + health smoke test script - .gitea/workflows/ci.yml: split into test + build-and-deploy jobs (test blocks deploy) - runs-on: debian-host (was ubuntu-latest = broken) - test job installs deps + runs pytest before building image - pyproject.toml: [project.optional-dependencies] dev = pytest + pytest-asyncio
This commit is contained in:
105
tests/test_catalog.py
Normal file
105
tests/test_catalog.py
Normal file
@@ -0,0 +1,105 @@
|
||||
"""Unit tests for the pypi-server.i80.dk catalog fetcher."""
|
||||
|
||||
import asyncio
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from ilsp.python_lsp.catalog import PypiCatalog
|
||||
|
||||
|
||||
MOCK_HTML = """
|
||||
<!DOCTYPE html>
|
||||
<html><head><title>Simple Index</title></head>
|
||||
<body>
|
||||
<a href="/simple/azure-toolbox-database/">azure-toolbox-database</a>
|
||||
<a href="/simple/devops-gitea-sdk/">devops-gitea-sdk</a>
|
||||
<a href="/simple/toolbox-tests-framework/">toolbox-tests-framework</a>
|
||||
</body></html>
|
||||
"""
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def reset_catalog():
|
||||
PypiCatalog._packages = []
|
||||
PypiCatalog._last_refresh = 0
|
||||
yield
|
||||
|
||||
|
||||
class _MockResponse:
|
||||
def __init__(self, html: str):
|
||||
self._html = html
|
||||
self.status = 200
|
||||
|
||||
async def text(self):
|
||||
return self._html
|
||||
|
||||
def raise_for_status(self):
|
||||
pass
|
||||
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, *_):
|
||||
pass
|
||||
|
||||
|
||||
class _MockSession:
|
||||
def __init__(self, html: str):
|
||||
self._html = html
|
||||
|
||||
def get(self, url):
|
||||
return _MockResponse(self._html)
|
||||
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, *_):
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fetch_parses_packages():
|
||||
with patch("ilsp.python_lsp.catalog.aiohttp.ClientSession", return_value=_MockSession(MOCK_HTML)):
|
||||
packages = await PypiCatalog._fetch()
|
||||
|
||||
assert len(packages) == 3
|
||||
names = [p["name"] for p in packages]
|
||||
assert "azure-toolbox-database" in names
|
||||
assert "devops-gitea-sdk" in names
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_packages_have_sort_prefix():
|
||||
with patch("ilsp.python_lsp.catalog.aiohttp.ClientSession", return_value=_MockSession(MOCK_HTML)):
|
||||
packages = await PypiCatalog._fetch()
|
||||
|
||||
for pkg in packages:
|
||||
assert pkg["sort_prefix"] == "0_i80_"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_packages_triggers_refresh_when_empty():
|
||||
with patch("ilsp.python_lsp.catalog.aiohttp.ClientSession", return_value=_MockSession(MOCK_HTML)):
|
||||
packages = await PypiCatalog.get_packages()
|
||||
|
||||
assert len(packages) == 3
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_packages_uses_cache():
|
||||
PypiCatalog._packages = [{"name": "cached-pkg", "sort_prefix": "0_i80_", "detail": "x"}]
|
||||
PypiCatalog._last_refresh = 9_999_999_999
|
||||
|
||||
packages = await PypiCatalog.get_packages()
|
||||
assert packages[0]["name"] == "cached-pkg"
|
||||
|
||||
|
||||
def test_as_completion_items():
|
||||
PypiCatalog._packages = [
|
||||
{"name": "my-pkg", "detail": "i80", "sort_prefix": "0_i80_"},
|
||||
]
|
||||
items = PypiCatalog.as_completion_items()
|
||||
assert len(items) == 1
|
||||
assert items[0].label == "my-pkg"
|
||||
assert items[0].sort_text == "0_i80_my-pkg"
|
||||
Reference in New Issue
Block a user