Spaces:
Running
Running
Download tests/unit/test_save_reference.py from iamfebin/algospaced-dsa: direct link, hf CLI and curl.
- Browser
- Download file 3.84 kB
-
https://huggingface.co/spaces/iamfebin/algospaced-dsa/resolve/main/tests/unit/test_save_reference.py
- Command line
-
hf download hf://spaces/iamfebin/algospaced-dsa/tests/unit/test_save_reference.py
-
curl -L -o test_save_reference.py https://huggingface.co/spaces/iamfebin/algospaced-dsa/resolve/main/tests/unit/test_save_reference.py
3.84 kB
| 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 | |