Files
social-app/backend/tests/unit/v1/app/test_router.py
T

120 lines
3.4 KiB
Python
Raw Normal View History

from __future__ import annotations
import json
import pytest
from v1.app import router
def _write_manifest(tmp_path, data: dict):
manifest_file = tmp_path / "manifest.json"
manifest_file.write_text(json.dumps(data), encoding="utf-8")
return manifest_file
@pytest.mark.asyncio
async def test_check_updates_returns_none_when_manifest_missing(monkeypatch) -> None:
monkeypatch.setattr(
router, "_manifest_file_path", lambda: router.PROJECT_ROOT / "missing.json"
)
result = await router.check_updates(
current_version_code=2,
current_version_name="0.1.1",
platform="android",
channel="release",
)
assert result.has_update is False
assert result.update_type == "none"
assert result.latest_version_name == "0.1.1"
assert result.latest_version_code == 2
@pytest.mark.asyncio
async def test_check_updates_returns_optional_update(monkeypatch, tmp_path) -> None:
manifest_file = _write_manifest(
tmp_path,
{
"releases": [
{
"platform": "android",
"channel": "release",
"version_name": "0.1.2",
"version_code": 3,
"min_supported_version_code": 2,
"file_name": "social-app-android-v0.1.2+3-release.apk",
"release_notes": "优化体验",
}
]
},
)
monkeypatch.setattr(router, "_manifest_file_path", lambda: manifest_file)
monkeypatch.setattr(
router.config.app_version,
"download_base_url",
"https://download.example.com",
raising=False,
)
monkeypatch.setattr(
router.config.app_version,
"release_path_prefix",
"releases",
raising=False,
)
result = await router.check_updates(
current_version_code=2,
current_version_name="0.1.1",
platform="android",
channel="release",
)
assert result.has_update is True
assert result.update_type == "optional"
assert result.latest_version_name == "0.1.2"
assert result.latest_version_code == 3
assert result.min_supported_version_code == 2
assert (
result.download_url
== "https://download.example.com/releases/social-app-android-v0.1.2+3-release.apk"
)
@pytest.mark.asyncio
async def test_check_updates_returns_required_update(monkeypatch, tmp_path) -> None:
manifest_file = _write_manifest(
tmp_path,
{
"releases": [
{
"platform": "android",
"channel": "release",
"version_name": "0.1.3",
"version_code": 5,
"min_supported_version_code": 4,
"file_name": "social-app-android-v0.1.3+5-release.apk",
}
]
},
)
monkeypatch.setattr(router, "_manifest_file_path", lambda: manifest_file)
monkeypatch.setattr(
router.config.app_version,
"download_base_url",
"https://download.example.com",
raising=False,
)
result = await router.check_updates(
current_version_code=3,
current_version_name="0.1.1",
platform="android",
channel="release",
)
assert result.has_update is True
assert result.update_type == "required"
assert result.min_supported_version_code == 4