dab47f0cb3
- 添加 .env.local 支持,app.sh 和 dev-migrate.sh 自动覆盖 - Docker Compose 使用 profiles 区分 dev/prod 环境 - 改进认证 dev session 判断逻辑,使用 test account 配置 - 修复 CoinPackageCard 重复代码问题 - 清理 opencode 配置,移除敏感信息 - 新增 infra/docker/README.md 文档 - 修复 ruff/pyright/flutter lint 错误 - 更新测试用例移除已删除的 country 字段
119 lines
4.1 KiB
Python
119 lines
4.1 KiB
Python
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from schemas.shared.user import (
|
|
NotificationSettings,
|
|
PreferenceSettings,
|
|
ProfileSettingsV1,
|
|
parse_profile_settings,
|
|
)
|
|
|
|
|
|
class TestParseProfileSettings:
|
|
def test_parse_profile_settings_with_full_settings(self) -> None:
|
|
raw = {
|
|
"version": 1,
|
|
"preferences": {
|
|
"language": "en-US",
|
|
"timezone": "America/New_York",
|
|
},
|
|
"privacy": {"profile_visibility": "private"},
|
|
"notification": {
|
|
"allow_notifications": True,
|
|
"allow_vibration": False,
|
|
},
|
|
}
|
|
result = parse_profile_settings(raw)
|
|
|
|
assert isinstance(result, ProfileSettingsV1)
|
|
assert result.version == 1
|
|
assert isinstance(result.preferences, PreferenceSettings)
|
|
assert result.preferences.language == "en-US"
|
|
assert result.preferences.timezone == "America/New_York"
|
|
assert isinstance(result.notification, NotificationSettings)
|
|
assert result.notification.allow_notifications is True
|
|
assert result.notification.allow_vibration is False
|
|
|
|
def test_parse_profile_settings_with_empty_settings(self) -> None:
|
|
result = parse_profile_settings(None)
|
|
|
|
assert isinstance(result, ProfileSettingsV1)
|
|
assert result.version == 1
|
|
assert isinstance(result.preferences, PreferenceSettings)
|
|
assert result.preferences.language == "zh-CN"
|
|
assert result.preferences.timezone == "Asia/Shanghai"
|
|
assert isinstance(result.notification, NotificationSettings)
|
|
assert result.notification.allow_notifications is True
|
|
assert result.notification.allow_vibration is True
|
|
|
|
def test_parse_profile_settings_with_partial_preferences(self) -> None:
|
|
raw = {
|
|
"preferences": {
|
|
"language": "en-US",
|
|
},
|
|
}
|
|
result = parse_profile_settings(raw)
|
|
|
|
assert result.preferences.language == "en-US"
|
|
assert result.preferences.timezone == "Asia/Shanghai"
|
|
|
|
def test_parse_profile_settings_with_partial_notification(self) -> None:
|
|
raw = {
|
|
"notification": {
|
|
"allow_notifications": False,
|
|
},
|
|
}
|
|
result = parse_profile_settings(raw)
|
|
|
|
assert isinstance(result.notification, NotificationSettings)
|
|
assert result.notification.allow_notifications is False
|
|
assert result.notification.allow_vibration is True
|
|
|
|
def test_parse_profile_settings_with_legacy_notification_format(self) -> None:
|
|
raw = {
|
|
"notification": {
|
|
"push_enabled": True,
|
|
},
|
|
}
|
|
result = parse_profile_settings(raw)
|
|
|
|
assert isinstance(result.notification, NotificationSettings)
|
|
assert result.notification.allow_notifications is True
|
|
assert result.notification.allow_vibration is True
|
|
|
|
def test_parse_profile_settings_invalid_language_uses_default(self) -> None:
|
|
raw = {
|
|
"preferences": {
|
|
"language": "invalid-language",
|
|
},
|
|
}
|
|
with pytest.raises(ValueError, match="language must be a valid BCP-47 tag"):
|
|
parse_profile_settings(raw)
|
|
|
|
def test_parse_profile_settings_invalid_timezone_uses_default(self) -> None:
|
|
raw = {
|
|
"preferences": {
|
|
"timezone": "Invalid/Timezone",
|
|
},
|
|
}
|
|
with pytest.raises(ValueError, match="timezone must be a valid IANA timezone"):
|
|
parse_profile_settings(raw)
|
|
|
|
def test_profile_settings_v1_model_dump(self) -> None:
|
|
settings = ProfileSettingsV1(
|
|
preferences=PreferenceSettings(
|
|
language="en-US",
|
|
timezone="UTC",
|
|
),
|
|
notification=NotificationSettings(
|
|
allow_notifications=True,
|
|
allow_vibration=False,
|
|
),
|
|
)
|
|
dumped = settings.model_dump(mode="json")
|
|
|
|
assert dumped["preferences"]["language"] == "en-US"
|
|
assert dumped["notification"]["allow_notifications"] is True
|
|
assert dumped["notification"]["allow_vibration"] is False
|