Python, PyTest & SQL Foundations for AI QE Β· Day 4 Β· 20 min
AI REST Contracts & SSE Streaming
Python, PyTest & SQL Foundations for AI QE β Day 4
Bridge Postman-style API testing into automated PyTest suites using a local http.server mock AI gateway to verify JSON schema contracts, HTTP 429 Retry-After backoff, timeout guardrails, and SSE chunk reassembly.
π¦Sample Codebase & Working Solution (Day 4)Available Now
View on GitHubThe official reference codebase for Day 4 is hosted in the repository. Clone the repository and navigate to day-4-ai-rest-streaming-api/test_day4_api_streaming.py to run the working solution.
Git Clone β Working Solution
git clone https://github.com/777abhi/pytest-sql-ai-qe-roadmap.git
cd pytest-sql-ai-qe-roadmap/day-4-ai-rest-streaming-api/test_day4_api_streaming.pyπProduction Context & Enterprise Implementation
π‘ Why We Are Doing This: Manual Postman checks cannot reliably validate transient HTTP 429 rate-limit bursts, upstream timeouts, or incremental Server-Sent Events (SSE) token streams on every commit.
π’ Real-World Production Usecase: Automated API resilience and streaming contract verification for customer-facing AI chat gateways.
βοΈ How Implemented in Production: MockAIServer runs a local ThreadingHTTPServer with fault injection; AIServiceClient validates JSON payloads, honors Retry-After headers, enforces timeouts, and reassembles SSE data frames until [DONE].
Phase 1
Run Local REST Contract, Fault Injection & SSE Tests
Step 1.1: Execute Day 4 API & Streaming Suite
Terminal β Run Day 4
npm run test:day4Step 1.2: Verify HTTP 429 Retry-After Recovery
day-4-ai-rest-streaming-api/test_day4_api_streaming.py
mock_ai_server.set_rate_limit_failures(2)
payload, attempts = ai_client.chat_completion(
"I want to open a billing dispute for an unrecognized charge."
)
assert attempts == 3Day 4 Key Learnings
- Validated REST JSON response schemas including choices, finish_reason, and token usage arithmetic.
- Tested deterministic HTTP 429 Retry-After backoff recovery and strict client timeout enforcement.
- Verified Server-Sent Events (SSE) chunk ordering, reassembly, and [DONE] sentinel termination.
π§ Knowledge Check
When testing an AI Server-Sent Events (SSE) streaming endpoint in PyTest, what must the test assert beyond receiving a 200 OK status?