Spaces:
Running
Running
File size: 3,837 Bytes
1993562 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | from fastapi.testclient import TestClient
import cache
import db
from main import app
client = TestClient(app)
class TestSaveReference:
def test_save_reference_by_problem_id(self):
"""Test saving a reference solution using an existing problem ID."""
test_code = (
"# Test Custom Solution for Two Sum\n"
"class Solution:\n"
" def twoSum(self, nums, target):\n"
" return [0, 1]"
)
res = client.post(
"/api/reviews/save-reference",
headers={"Authorization": "Bearer sandbox"},
json={
"problem_id": "prob-two-sum",
"code": test_code,
},
)
assert res.status_code == 200
data = res.json()
assert data["success"] is True
assert data["problem_id"] == "prob-two-sum"
# Verify the problem record has the updated code
prob = db.get_problem("prob-two-sum", "sandbox-user-id")
assert prob is not None
assert prob.get("reference_code") == test_code
def test_save_reference_by_problem_number_string(self):
"""Test saving a reference solution using numeric problem_id string e.g. '1'."""
test_code = "# Number string solution\nclass Solution:\n pass"
res = client.post(
"/api/reviews/save-reference",
headers={"Authorization": "Bearer sandbox"},
json={
"problem_id": "1",
"problem_number": 1,
"problem_name": "Two Sum",
"code": test_code,
},
)
assert res.status_code == 200
data = res.json()
assert data["success"] is True
# Check that it resolved to the problem
probs = db.get_problems_by_number(1, "sandbox-user-id")
assert len(probs) > 0
assert probs[0]["reference_code"] == test_code
def test_save_reference_creates_new_problem_if_not_exists(self):
"""Test that saving a reference for a problem not yet in personal table creates it."""
unique_num = 99999
unique_name = "Unique Test Problem 99999"
test_code = "# Brand new solution\nclass Solution:\n pass"
res = client.post(
"/api/reviews/save-reference",
headers={"Authorization": "Bearer sandbox"},
json={
"problem_id": f"nc-{unique_num}",
"problem_number": unique_num,
"problem_name": unique_name,
"pattern": "Dynamic Programming",
"difficulty": "Hard",
"optimal_time": "O(N)",
"optimal_space": "O(N)",
"code": test_code,
},
)
assert res.status_code == 200
data = res.json()
assert data["success"] is True
new_prob_id = data["problem_id"]
prob = db.get_problem(new_prob_id, "sandbox-user-id")
assert prob is not None
assert prob["name"] == unique_name
assert prob["problem_number"] == unique_num
assert prob["reference_code"] == test_code
def test_save_reference_invalidates_cache(self, monkeypatch):
"""Test that due_reviews and all_problems_reviews cache are cleared."""
deleted_keys = []
monkeypatch.setattr(cache, "delete_cache", lambda k: deleted_keys.append(k))
res = client.post(
"/api/reviews/save-reference",
headers={"Authorization": "Bearer sandbox"},
json={
"problem_id": "prob-two-sum",
"code": "# Cache check\npass",
},
)
assert res.status_code == 200
assert "due_reviews:sandbox-user-id" in deleted_keys
assert "all_problems_reviews:sandbox-user-id" in deleted_keys
|