Test scenario: Generate basic SSE data chunk Strategy: Pure function test, verify output format and structure
()
| 9 | |
| 10 | |
| 11 | def test_generate_sse_chunk_basic(): |
| 12 | """ |
| 13 | Test scenario: Generate basic SSE data chunk |
| 14 | Strategy: Pure function test, verify output format and structure |
| 15 | """ |
| 16 | from api_utils.sse import generate_sse_chunk |
| 17 | |
| 18 | result = generate_sse_chunk(delta="Hello", req_id="req123", model="gemini-1.5-pro") |
| 19 | |
| 20 | # Verify SSE format |
| 21 | assert isinstance(result, str) |
| 22 | assert result.startswith("data: ") |
| 23 | assert result.endswith("\n\n") |
| 24 | |
| 25 | # Extract and parse JSON |
| 26 | json_part = result[6:-2] # Remove "data: " prefix and "\n\n" suffix |
| 27 | chunk_data = json.loads(json_part) |
| 28 | |
| 29 | # Verify structure |
| 30 | assert chunk_data["id"] == "chatcmpl-req123" |
| 31 | assert chunk_data["object"] == "chat.completion.chunk" |
| 32 | assert chunk_data["model"] == "gemini-1.5-pro" |
| 33 | assert "created" in chunk_data |
| 34 | assert isinstance(chunk_data["created"], int) |
| 35 | |
| 36 | # Verify choices |
| 37 | assert len(chunk_data["choices"]) == 1 |
| 38 | choice = chunk_data["choices"][0] |
| 39 | assert choice["index"] == 0 |
| 40 | assert choice["delta"]["content"] == "Hello" |
| 41 | assert choice["finish_reason"] is None |
| 42 | |
| 43 | |
| 44 | def test_generate_sse_chunk_empty_delta(): |
nothing calls this directly
no test coverage detected