Spaces:
Sleeping
Sleeping
Download test_supabase_submissions_logger.py from refaelz/legacy-code-challenge: direct link, hf CLI and curl.
- Browser
- Download file 5.37 kB
-
https://huggingface.co/spaces/refaelz/legacy-code-challenge/resolve/main/test_supabase_submissions_logger.py
- Command line
-
hf download hf://spaces/refaelz/legacy-code-challenge/test_supabase_submissions_logger.py
-
curl -L -o test_supabase_submissions_logger.py https://huggingface.co/spaces/refaelz/legacy-code-challenge/resolve/main/test_supabase_submissions_logger.py
5.37 kB
| import json | |
| import os | |
| import unittest | |
| from unittest.mock import MagicMock, patch | |
| from urllib.error import HTTPError | |
| import supabase_submissions_logger as logger | |
| class SupabaseSubmissionLoggerTests(unittest.TestCase): | |
| def setUp(self): | |
| self.record = { | |
| "submission_id": "attempt-001", | |
| "timestamp": "2026-11-04T10:00:00", | |
| "student": { | |
| "name": "Alice", | |
| "auth_provider": "huggingface", | |
| "provider_user_id": "alice", | |
| }, | |
| "challenge": { | |
| "challenge_id": "boltons:utils", | |
| "repo_url": "https://github.com/mahmoud/boltons", | |
| "target_file": "utils.py", | |
| "bug_description": "The function drops the final item.", | |
| "challenge_summary": "Repair the iteration behavior.", | |
| "buggy_code": "def items(): return [1]", | |
| "expected_fixed_code": "def items(): return [1, 2]", | |
| }, | |
| "submission": { | |
| "attempt_number": 1, | |
| "hints_used": 2, | |
| "student_code": "print(1)", | |
| "student_diff": "- return [1]\n+ return [1, 2]", | |
| "chat_history": [{ | |
| "question": "Why is the last value missing?", | |
| "response": "Check the loop boundary.", | |
| "gave_hint": True, | |
| }], | |
| }, | |
| "tests": { | |
| "before_passed": 1, | |
| "before_total": 5, | |
| "passed_tests": 4, | |
| "total_tests": 5, | |
| "test_output": "Test 1: PASS", | |
| "per_test_before": [{"num": 1, "status": "FAIL"}], | |
| "per_test_after": [{"num": 1, "status": "PASS"}], | |
| }, | |
| "scoring": { | |
| "llm_score": 80, | |
| "total_score": 76, | |
| "hint_penalty": 4, | |
| "llm_explanation": "The fix restores the final value.", | |
| "submission_outcome": "partial_fix", | |
| }, | |
| } | |
| def test_missing_configuration_is_reported_without_network(self): | |
| with patch.dict(os.environ, {}, clear=True): | |
| self.assertEqual( | |
| logger.upload_submission_to_supabase(self.record), | |
| (False, "Missing SUPABASE_URL or Supabase server secret"), | |
| ) | |
| def test_upload_sends_full_payload_and_uses_idempotent_upsert(self): | |
| response = MagicMock() | |
| response.__enter__.return_value = response | |
| with patch.dict( | |
| os.environ, | |
| { | |
| "SUPABASE_URL": "https://demo.supabase.co", | |
| "SUPABASE_SECRET_KEY": "sb_secret_test", | |
| }, | |
| clear=True, | |
| ), patch.object(logger, "urlopen", return_value=response) as send: | |
| self.assertEqual(logger.upload_submission_to_supabase(self.record), (True, "")) | |
| request = send.call_args.args[0] | |
| headers = {key.lower(): value for key, value in request.header_items()} | |
| payload = json.loads(request.data) | |
| self.assertEqual(request.get_method(), "POST") | |
| self.assertIn("on_conflict=submission_id", request.full_url) | |
| self.assertEqual(headers["apikey"], "sb_secret_test") | |
| self.assertNotIn("authorization", headers) | |
| self.assertEqual(payload["provider_user_id"], "alice") | |
| report = payload["review_report"] | |
| self.assertIn("WHAT WAS THE BUG?", report) | |
| self.assertIn("The function drops the final item.", report) | |
| self.assertIn("STUDENT'S CHANGES (DIFF)", report) | |
| self.assertIn("Test 1: FAIL -> PASS", report) | |
| self.assertIn("Final score: 76", report) | |
| self.assertIn("Check the loop boundary.", report) | |
| stored_record = payload["payload"] | |
| self.assertEqual(stored_record.pop("cloud")["supabase"]["status"], "success") | |
| self.assertEqual(stored_record, self.record) | |
| def test_legacy_service_role_key_uses_bearer_header(self): | |
| response = MagicMock() | |
| response.__enter__.return_value = response | |
| legacy_key = "eyJ.test.legacy" | |
| with patch.dict( | |
| os.environ, | |
| { | |
| "SUPABASE_URL": "https://demo.supabase.co", | |
| "SUPABASE_SERVICE_ROLE_KEY": legacy_key, | |
| }, | |
| clear=True, | |
| ), patch.object(logger, "urlopen", return_value=response) as send: | |
| self.assertEqual(logger.upload_submission_to_supabase(self.record), (True, "")) | |
| headers = {key.lower(): value for key, value in send.call_args.args[0].header_items()} | |
| self.assertEqual(headers["authorization"], f"Bearer {legacy_key}") | |
| def test_http_errors_do_not_include_secret(self): | |
| error = HTTPError("https://demo.supabase.co", 401, "unauthorized", {}, None) | |
| with patch.dict( | |
| os.environ, | |
| { | |
| "SUPABASE_URL": "https://demo.supabase.co", | |
| "SUPABASE_SECRET_KEY": "sb_secret_private", | |
| }, | |
| clear=True, | |
| ), patch.object(logger, "urlopen", side_effect=error): | |
| ok, reason = logger.upload_submission_to_supabase(self.record) | |
| self.assertFalse(ok) | |
| self.assertIn("401/403", reason) | |
| self.assertNotIn("sb_secret_private", reason) | |
| if __name__ == "__main__": | |
| unittest.main() | |