|
| 1 | +"""Tests for synchronous ValidKit client. |
| 2 | +
|
| 3 | +Mirrors test_client.py patterns — mocks AsyncValidKit._request to |
| 4 | +verify the sync wrapper delegates correctly without making HTTP calls. |
| 5 | +""" |
| 6 | + |
| 7 | +import asyncio |
| 8 | +import pytest |
| 9 | +from unittest.mock import AsyncMock, patch |
| 10 | +from datetime import datetime |
| 11 | + |
| 12 | +from validkit import ValidKit |
| 13 | +from validkit.client import AsyncValidKit |
| 14 | +from validkit.config import ValidKitConfig |
| 15 | +from validkit.models import ( |
| 16 | + CompactResult, |
| 17 | + EmailVerificationResult, |
| 18 | + BatchJob, |
| 19 | + BatchVerificationResult, |
| 20 | + BatchJobStatus, |
| 21 | + ResponseFormat, |
| 22 | +) |
| 23 | +from validkit.exceptions import BatchSizeError |
| 24 | + |
| 25 | + |
| 26 | +class TestSyncClientInit: |
| 27 | + def test_requires_api_key(self): |
| 28 | + with pytest.raises(ValueError, match="API key"): |
| 29 | + ValidKit() |
| 30 | + |
| 31 | + def test_accepts_api_key_string(self, mock_request): |
| 32 | + client = ValidKit(api_key="vk_test_123") |
| 33 | + assert client._async_client.config.api_key == "vk_test_123" |
| 34 | + client.close() |
| 35 | + |
| 36 | + def test_accepts_config_object(self, mock_request): |
| 37 | + config = ValidKitConfig(api_key="vk_test_456", timeout=60) |
| 38 | + client = ValidKit(config=config) |
| 39 | + assert client._async_client.config.timeout == 60 |
| 40 | + client.close() |
| 41 | + |
| 42 | + |
| 43 | +class TestSyncVerify: |
| 44 | + def test_returns_compact_result(self, sync_client, mock_request): |
| 45 | + mock_request.return_value = {"result": {"v": True, "d": False}} |
| 46 | + result = sync_client.verify("user@test.com") |
| 47 | + assert isinstance(result, CompactResult) |
| 48 | + assert result.v is True |
| 49 | + |
| 50 | + def test_calls_correct_endpoint(self, sync_client, mock_request): |
| 51 | + mock_request.return_value = {"result": {"v": True}} |
| 52 | + sync_client.verify("test@example.com") |
| 53 | + mock_request.assert_called_once() |
| 54 | + args = mock_request.call_args |
| 55 | + assert args[0][0] == "POST" |
| 56 | + assert args[0][1] == "verify" |
| 57 | + |
| 58 | + def test_full_format_with_compact_disabled(self, mock_request): |
| 59 | + config = ValidKitConfig(api_key="test", compact_format=False) |
| 60 | + client = ValidKit(config=config) |
| 61 | + mock_request.return_value = { |
| 62 | + "success": True, |
| 63 | + "email": "t@t.com", |
| 64 | + "valid": True, |
| 65 | + } |
| 66 | + result = client.verify("t@t.com") |
| 67 | + assert isinstance(result, EmailVerificationResult) |
| 68 | + client.close() |
| 69 | + |
| 70 | + |
| 71 | +class TestSyncVerifyBatch: |
| 72 | + def test_calls_agent_bulk_endpoint(self, sync_client, mock_request): |
| 73 | + mock_request.return_value = { |
| 74 | + "results": {"a@b.com": {"v": True}}, |
| 75 | + } |
| 76 | + sync_client.verify_batch(["a@b.com"]) |
| 77 | + args = mock_request.call_args |
| 78 | + assert args[0][0] == "POST" |
| 79 | + assert args[0][1] == "verify/bulk/agent" |
| 80 | + |
| 81 | + def test_rejects_oversized_batch(self, sync_client, mock_request): |
| 82 | + emails = [f"user{i}@test.com" for i in range(10001)] |
| 83 | + with pytest.raises(BatchSizeError): |
| 84 | + sync_client.verify_batch(emails) |
| 85 | + |
| 86 | + def test_rejects_async_progress_callback(self, sync_client, mock_request): |
| 87 | + async def bad_callback(processed, total): |
| 88 | + pass |
| 89 | + |
| 90 | + with pytest.raises(TypeError, match="plain function"): |
| 91 | + sync_client.verify_batch(["a@b.com"], progress_callback=bad_callback) |
| 92 | + |
| 93 | + def test_sync_progress_callback(self, sync_client, mock_request): |
| 94 | + mock_request.return_value = { |
| 95 | + "results": {"a@b.com": {"v": True}}, |
| 96 | + } |
| 97 | + called = [] |
| 98 | + |
| 99 | + def callback(processed, total): |
| 100 | + called.append((processed, total)) |
| 101 | + |
| 102 | + sync_client.verify_batch(["a@b.com"], progress_callback=callback) |
| 103 | + assert len(called) == 1 |
| 104 | + assert called[0] == (1, 1) |
| 105 | + |
| 106 | + |
| 107 | +class TestSyncBatchLifecycle: |
| 108 | + def _batch_job_response(self, status="processing"): |
| 109 | + now = datetime.now().isoformat() |
| 110 | + return { |
| 111 | + "id": "batch-abc", |
| 112 | + "status": status, |
| 113 | + "total_emails": 100, |
| 114 | + "processed": 50, |
| 115 | + "created_at": now, |
| 116 | + "updated_at": now, |
| 117 | + } |
| 118 | + |
| 119 | + def test_get_batch_status(self, sync_client, mock_request): |
| 120 | + mock_request.return_value = self._batch_job_response("processing") |
| 121 | + result = sync_client.get_batch_status("batch-abc") |
| 122 | + assert isinstance(result, BatchJob) |
| 123 | + args = mock_request.call_args |
| 124 | + assert args[0][0] == "GET" |
| 125 | + assert args[0][1] == "batch/batch-abc" |
| 126 | + |
| 127 | + def test_get_batch_results(self, sync_client, mock_request): |
| 128 | + mock_request.return_value = { |
| 129 | + "success": True, |
| 130 | + "total": 1, |
| 131 | + "valid": 1, |
| 132 | + "invalid": 0, |
| 133 | + "results": {"a@b.com": {"v": True}}, |
| 134 | + } |
| 135 | + result = sync_client.get_batch_results("batch-abc") |
| 136 | + assert isinstance(result, BatchVerificationResult) |
| 137 | + args = mock_request.call_args |
| 138 | + assert args[0][0] == "GET" |
| 139 | + assert args[0][1] == "batch/batch-abc/results" |
| 140 | + |
| 141 | + def test_cancel_batch_uses_delete(self, sync_client, mock_request): |
| 142 | + """Regression: cancel_batch must use DELETE (#2316).""" |
| 143 | + mock_request.return_value = self._batch_job_response("cancelled") |
| 144 | + sync_client.cancel_batch("batch-abc") |
| 145 | + args = mock_request.call_args |
| 146 | + assert args[0][0] == "DELETE" |
| 147 | + |
| 148 | + |
| 149 | +class TestSyncContextManager: |
| 150 | + def test_with_statement(self, mock_request): |
| 151 | + mock_request.return_value = {"result": {"v": True}} |
| 152 | + with ValidKit(api_key="vk_test_123") as client: |
| 153 | + result = client.verify("test@example.com") |
| 154 | + assert result.v is True |
| 155 | + # After exiting, the runner thread should be stopped |
| 156 | + |
| 157 | + def test_close_is_idempotent(self, mock_request): |
| 158 | + client = ValidKit(api_key="vk_test_123") |
| 159 | + client.close() |
| 160 | + # Second close should not raise |
| 161 | + client.close() |
| 162 | + |
| 163 | + |
| 164 | +class TestNestedEventLoop: |
| 165 | + def test_works_inside_existing_event_loop(self, mock_request): |
| 166 | + """ValidKit must work when an event loop is already running. |
| 167 | +
|
| 168 | + This is the critical test for Jupyter / Django compatibility. |
| 169 | + We simulate an outer loop by running the test body inside one. |
| 170 | + """ |
| 171 | + mock_request.return_value = {"result": {"v": True}} |
| 172 | + |
| 173 | + async def _inner(): |
| 174 | + # An event loop is now running on THIS thread. |
| 175 | + client = ValidKit(api_key="vk_test_123") |
| 176 | + result = client.verify("test@example.com") |
| 177 | + assert result.v is True |
| 178 | + client.close() |
| 179 | + |
| 180 | + asyncio.run(_inner()) |
0 commit comments