test: PR#27新增单元测试和e2e测试
All checks were successful
PR Preview / teardown-preview (pull_request) Has been skipped
Test / unit-test (push) Successful in 5s
Test / build-check (push) Successful in 3s
PR Preview / test (pull_request) Successful in 5s
PR Preview / deploy-preview (pull_request) Successful in 17s
Test / e2e-test (push) Successful in 53s

后端: auto_translate/title_case 14个测试
前端: EDITOR_ONLY_TAGS、drop单复数、已下架过滤 11个测试
E2E: en_name title case、删除用户转移配方、管理配方登录引导

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-12 10:26:44 +00:00
parent 0f7ae6ecc7
commit 6f1b9f3f68
3 changed files with 384 additions and 0 deletions

108
backend/test_translate.py Normal file
View File

@@ -0,0 +1,108 @@
"""Tests for translate.py auto_translate and main.py title_case."""
import pytest
from backend.translate import auto_translate
# ---------------------------------------------------------------------------
# title_case (inlined here since it's a trivial helper in main.py)
# ---------------------------------------------------------------------------
def title_case(s: str) -> str:
return s.strip().title() if s else s
class TestTitleCase:
def test_basic(self):
assert title_case("pain relief") == "Pain Relief"
def test_single_word(self):
assert title_case("sleep") == "Sleep"
def test_preserves_already_cased(self):
assert title_case("Pain Relief") == "Pain Relief"
def test_empty_string(self):
assert title_case("") == ""
def test_none(self):
assert title_case(None) is None
def test_strips_whitespace(self):
assert title_case(" hello world ") == "Hello World"
# ---------------------------------------------------------------------------
# auto_translate
# ---------------------------------------------------------------------------
class TestAutoTranslate:
def test_empty_string(self):
assert auto_translate("") == ""
def test_single_keyword(self):
assert auto_translate("失眠") == "Insomnia"
def test_compound_name(self):
result = auto_translate("助眠配方")
assert "Sleep" in result
assert "Blend" in result
def test_head_pain(self):
result = auto_translate("头痛")
# 头痛 is a single keyword → Headache
assert "Headache" in result
def test_shoulder_neck_massage(self):
result = auto_translate("肩颈按摩")
assert "Neck" in result or "Shoulder" in result
assert "Massage" in result
def test_no_duplicate_words(self):
# 肩颈 → "Neck & Shoulder", but should not duplicate if sub-keys match
result = auto_translate("肩颈护理")
words = result.split()
# No exact duplicate consecutive words
for i in range(len(words) - 1):
if words[i] == words[i + 1]:
pytest.fail(f"Duplicate word '{words[i]}' in '{result}'")
def test_skincare_blend(self):
result = auto_translate("皮肤修复")
assert "Skin" in result
assert "Repair" in result
def test_foot_soak(self):
result = auto_translate("泡脚配方")
assert "Foot Soak" in result or "Foot" in result
def test_ascii_passthrough(self):
# Embedded ASCII letters are preserved
result = auto_translate("DIY面膜")
assert "DIY" in result or "Diy" in result
assert "Face Mask" in result or "Mask" in result
def test_pure_chinese_returns_english(self):
result = auto_translate("薰衣草精华")
# Should not return original Chinese; should have English words
assert any(c.isascii() and c.isalpha() for c in result)
def test_fallback_for_unknown(self):
# Completely unknown chars get skipped; if nothing matches, returns original
result = auto_translate("㊗㊗㊗")
assert result == "㊗㊗㊗"
def test_children_sleep(self):
result = auto_translate("儿童助眠")
assert "Children" in result
assert "Sleep" in result
def test_menstrual_pain(self):
result = auto_translate("痛经调理")
assert "Menstrual Pain" in result or "Menstrual" in result
assert "Therapy" in result
def test_result_is_title_cased(self):
result = auto_translate("排毒按摩")
# Each word should start with uppercase
for word in result.split():
if word == "&":
continue
assert word[0].isupper(), f"'{word}' in '{result}' is not title-cased"