import os import sys from unittest.mock import MagicMock, patch # Set dummy env vars BEFORE importing app to avoid Supabase init errors os.environ["SUPABASE_URL"] = "https://example.supabase.co" os.environ["SUPABASE_KEY"] = "eyJh...dummy...key" try: from fastapi.testclient import TestClient from app import app client = TestClient(app) def test_limit_parameter(): print("Testing limit parameter logic...") # We need to mock the supabase client usage in src.services # The chain is: supabase.table().select().eq().order().limit().execute() with patch("src.services.supabase") as mock_supabase: # Construct the mock chain mock_table = mock_supabase.table.return_value mock_select = mock_table.select.return_value mock_eq = mock_select.eq.return_value mock_order = mock_eq.order.return_value mock_limit = mock_order.limit # This is the function we want to check mock_execute = mock_limit.return_value.execute # Mock return data to be empty so we don't have to mock decryption mock_execute.return_value.data = [] # 1. Call with default limit (should be 100) print(" Case 1: Default limit") res = client.post("/retrieve", json={"group_name": "test", "password": "pass"}) assert res.status_code == 200, f"Status code failed: {res.text}" # Verify limit(100) was called (from default in model) # wait, model default is 100, so services receives 100. mock_limit.assert_called_with(100) print(" PASS: limit(100) called by default.") # 2. Call with specific limit print(" Case 2: Custom limit = 42") res = client.post("/retrieve", json={"group_name": "test", "password": "pass", "limit": 42}) assert res.status_code == 200 mock_limit.assert_called_with(42) print(" PASS: limit(42) called correctly.") print("\nSUCCESS: Verification Passed.") if __name__ == "__main__": test_limit_parameter() except Exception as e: print(f"\nCRITICAL ERROR: {e}") sys.exit(1)