Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5fd4988985 | ||
|
|
4598289f06 |
+1
-1
@@ -283,7 +283,7 @@
|
||||
| wrong_because | character varying | YES | |
|
||||
| edited_at | timestamp without time zone | YES | |
|
||||
|
||||
## sentence_hints_used ⚠️ устаревшая (не используется)
|
||||
## sentence_hints_used
|
||||
|
||||
| Колонка | Тип | NULL | Default |
|
||||
|---------|-----|------|---------|
|
||||
|
||||
@@ -7,6 +7,39 @@ from database import get_pool
|
||||
router = APIRouter(prefix="/sentences", tags=["sentences"])
|
||||
|
||||
|
||||
@router.get("/{sentence_id}/hint")
|
||||
async def get_sentence_hint(sentence_id: int, request: Request):
|
||||
"""Получить правильное предложение в качестве подсказки (одноразово)"""
|
||||
telegram_id, _ = await require_auth(request)
|
||||
|
||||
pool = await get_pool()
|
||||
async with pool.acquire() as conn:
|
||||
row = await conn.fetchrow(
|
||||
"SELECT sentence_pl FROM sentences WHERE id = $1", sentence_id
|
||||
)
|
||||
if not row:
|
||||
raise HTTPException(404, "Sentence not found")
|
||||
|
||||
# 🔧 ФИКС (24.09.2026): sentence_hint_no_limit — лимит 1 раз на предложение.
|
||||
already_used = await conn.fetchval(
|
||||
"SELECT 1 FROM sentence_hints_used WHERE user_id = $1 AND sentence_id = $2",
|
||||
telegram_id, sentence_id
|
||||
)
|
||||
if already_used:
|
||||
raise HTTPException(403, "Hint already used for this sentence")
|
||||
|
||||
# Записываем использование
|
||||
await conn.execute("""
|
||||
INSERT INTO sentence_hints_used (user_id, sentence_id)
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT (user_id, sentence_id) DO NOTHING
|
||||
""", telegram_id, sentence_id)
|
||||
|
||||
return {
|
||||
"sentence_pl": row["sentence_pl"],
|
||||
}
|
||||
|
||||
|
||||
def load_sentences_from_csv(filename):
|
||||
"""Загружает предложения из CSV файла (формат: polish,english,russian,ukrainian)."""
|
||||
sentences = []
|
||||
|
||||
@@ -53,6 +53,7 @@ USER_TABLES = [
|
||||
"user_seen_bubble_levels",
|
||||
"user_word_stats",
|
||||
"xp_log",
|
||||
"sentence_hints_used",
|
||||
"typing_hints_used",
|
||||
"chat_history",
|
||||
"chat_usage",
|
||||
|
||||
+28
-69
@@ -1966,85 +1966,44 @@ async def test_sentences_stats(smoke_user):
|
||||
assert passed
|
||||
|
||||
|
||||
async def test_sentences_show_answer_medium(smoke_user, smoke_conn):
|
||||
"""POST /sentences/show-answer — Medium: лимит 2 (200, 200, 403)."""
|
||||
async def test_sentences_hint(smoke_user, smoke_conn):
|
||||
"""GET /sentences/{id}/hint → sentence_pl, лимит 1 раз."""
|
||||
client = smoke_user
|
||||
|
||||
await smoke_conn.execute(
|
||||
"UPDATE users SET difficulty = 'medium' WHERE telegram_id = $1", SMOKE_USER_ID
|
||||
)
|
||||
# Берём предложение, которое юзер ещё НЕ проходил и НЕ брал подсказку
|
||||
sentence_id = await smoke_conn.fetchval("""
|
||||
SELECT id FROM sentences
|
||||
WHERE id NOT IN (
|
||||
SELECT sentence_id FROM sentence_hints_used WHERE user_id = $1
|
||||
)
|
||||
ORDER BY id LIMIT 1
|
||||
""", SMOKE_USER_ID)
|
||||
|
||||
sentence_id = await smoke_conn.fetchval("SELECT id FROM sentences ORDER BY id LIMIT 1")
|
||||
if not sentence_id:
|
||||
pytest.skip("Нет предложений для show-answer теста")
|
||||
pytest.skip("Нет предложений для hint-теста")
|
||||
return
|
||||
|
||||
# Чистим list_opened, чтобы начать с 0
|
||||
await smoke_conn.execute(
|
||||
"DELETE FROM sentence_progress WHERE user_id = $1 AND sentence_id = $2",
|
||||
# 1. Первый запрос — 200
|
||||
resp1 = await client.get(f"/sentences/{sentence_id}/hint")
|
||||
ok1 = resp1.status_code == 200
|
||||
body1 = resp1.json() if ok1 else {}
|
||||
has_sentence = bool(body1.get("sentence_pl"))
|
||||
|
||||
# 2. Проверка в БД
|
||||
recorded = await smoke_conn.fetchval(
|
||||
"SELECT 1 FROM sentence_hints_used WHERE user_id = $1 AND sentence_id = $2",
|
||||
SMOKE_USER_ID, sentence_id,
|
||||
)
|
||||
) is not None
|
||||
|
||||
resp1 = await client.post("/sentences/show-answer", json={"sentence_id": sentence_id})
|
||||
resp2 = await client.post("/sentences/show-answer", json={"sentence_id": sentence_id})
|
||||
resp3 = await client.post("/sentences/show-answer", json={"sentence_id": sentence_id})
|
||||
# 3. Второй запрос — 403 (лимит)
|
||||
resp2 = await client.get(f"/sentences/{sentence_id}/hint")
|
||||
limit_ok = resp2.status_code == 403
|
||||
|
||||
passed = resp1.status_code == 200 and resp2.status_code == 200 and resp3.status_code == 403
|
||||
passed = ok1 and has_sentence and recorded and limit_ok
|
||||
RESULTS.append(ScenarioResult(
|
||||
"sentences_show_answer_medium", "sentences", passed,
|
||||
f"medium: 1={resp1.status_code}, 2={resp2.status_code}, 3={resp3.status_code} (ожидали 200/200/403)",
|
||||
{"first": resp1.status_code, "second": resp2.status_code, "third": resp3.status_code},
|
||||
))
|
||||
assert passed
|
||||
|
||||
|
||||
async def test_sentences_show_answer_hard(smoke_user, smoke_conn):
|
||||
"""POST /sentences/show-answer — Hard: сразу 403."""
|
||||
client = smoke_user
|
||||
|
||||
await smoke_conn.execute(
|
||||
"UPDATE users SET difficulty = 'hard' WHERE telegram_id = $1", SMOKE_USER_ID
|
||||
)
|
||||
|
||||
sentence_id = await smoke_conn.fetchval("SELECT id FROM sentences ORDER BY id LIMIT 1")
|
||||
if not sentence_id:
|
||||
pytest.skip("Нет предложений для show-answer теста")
|
||||
return
|
||||
|
||||
resp = await client.post("/sentences/show-answer", json={"sentence_id": sentence_id})
|
||||
|
||||
passed = resp.status_code == 403
|
||||
RESULTS.append(ScenarioResult(
|
||||
"sentences_show_answer_hard", "sentences", passed,
|
||||
f"hard: status={resp.status_code} (ожидали 403)",
|
||||
{"status": resp.status_code},
|
||||
))
|
||||
assert passed
|
||||
|
||||
|
||||
async def test_sentences_show_answer_easy(smoke_user, smoke_conn):
|
||||
"""POST /sentences/show-answer — Easy: без лимита (несколько 200)."""
|
||||
client = smoke_user
|
||||
|
||||
await smoke_conn.execute(
|
||||
"UPDATE users SET difficulty = 'easy' WHERE telegram_id = $1", SMOKE_USER_ID
|
||||
)
|
||||
|
||||
sentence_id = await smoke_conn.fetchval("SELECT id FROM sentences ORDER BY id LIMIT 1")
|
||||
if not sentence_id:
|
||||
pytest.skip("Нет предложений для show-answer теста")
|
||||
return
|
||||
|
||||
statuses = []
|
||||
for _ in range(3):
|
||||
r = await client.post("/sentences/show-answer", json={"sentence_id": sentence_id})
|
||||
statuses.append(r.status_code)
|
||||
|
||||
passed = all(s == 200 for s in statuses)
|
||||
RESULTS.append(ScenarioResult(
|
||||
"sentences_show_answer_easy", "sentences", passed,
|
||||
f"easy: statuses={statuses} (ожидали все 200)",
|
||||
{"statuses": statuses},
|
||||
"sentences_hint", "sentences", passed,
|
||||
f"first={resp1.status_code}, recorded={recorded}, second={resp2.status_code} (ожидали 403)",
|
||||
{"first_status": resp1.status_code, "recorded": recorded, "second_status": resp2.status_code},
|
||||
))
|
||||
assert passed
|
||||
|
||||
|
||||
Reference in New Issue
Block a user