|
| 1 | +# PostHog Python SDK Test Adapter |
| 2 | + |
| 3 | +This adapter wraps the posthog-python SDK for compliance testing with the [PostHog SDK Test Harness](https://github.com/PostHog/posthog-sdk-test-harness). |
| 4 | + |
| 5 | +## What is This? |
| 6 | + |
| 7 | +This is a simple Flask app that: |
| 8 | +1. Wraps the posthog-python SDK |
| 9 | +2. Exposes a REST API for the test harness to control |
| 10 | +3. Tracks internal SDK state for test assertions |
| 11 | + |
| 12 | +## Running Tests |
| 13 | + |
| 14 | +Tests run automatically in CI via GitHub Actions. See the test harness repo for details. |
| 15 | + |
| 16 | +### Locally with Docker Compose |
| 17 | + |
| 18 | +```bash |
| 19 | +# From the posthog-python/sdk_compliance_adapter directory |
| 20 | +docker-compose up --build --abort-on-container-exit |
| 21 | +``` |
| 22 | + |
| 23 | +This will: |
| 24 | +1. Build the Python SDK adapter |
| 25 | +2. Pull the test harness image |
| 26 | +3. Run all compliance tests |
| 27 | +4. Show results |
| 28 | + |
| 29 | +### Manually with Docker |
| 30 | + |
| 31 | +```bash |
| 32 | +# Create network |
| 33 | +docker network create test-network |
| 34 | + |
| 35 | +# Build and run adapter |
| 36 | +docker build -f sdk_compliance_adapter/Dockerfile -t posthog-python-adapter . |
| 37 | +docker run -d --name sdk-adapter --network test-network -p 8080:8080 posthog-python-adapter |
| 38 | + |
| 39 | +# Run test harness |
| 40 | +docker run --rm \ |
| 41 | + --name test-harness \ |
| 42 | + --network test-network \ |
| 43 | + ghcr.io/posthog/sdk-test-harness:latest \ |
| 44 | + run --adapter-url http://sdk-adapter:8080 --mock-url http://test-harness:8081 |
| 45 | + |
| 46 | +# Cleanup |
| 47 | +docker stop sdk-adapter && docker rm sdk-adapter |
| 48 | +docker network rm test-network |
| 49 | +``` |
| 50 | + |
| 51 | +## Adapter Implementation |
| 52 | + |
| 53 | +See [adapter.py](adapter.py) for the implementation. |
| 54 | + |
| 55 | +The adapter implements the standard SDK adapter interface defined in the [test harness CONTRACT](https://github.com/PostHog/posthog-sdk-test-harness/blob/main/CONTRACT.yaml): |
| 56 | + |
| 57 | +- `GET /health` - Return SDK information |
| 58 | +- `POST /init` - Initialize SDK with config |
| 59 | +- `POST /capture` - Capture an event |
| 60 | +- `POST /flush` - Flush pending events |
| 61 | +- `GET /state` - Return internal state |
| 62 | +- `POST /reset` - Reset SDK state |
| 63 | + |
| 64 | +### Key Implementation Details |
| 65 | + |
| 66 | +**Request Tracking**: The adapter monkey-patches `batch_post` to track all HTTP requests made by the SDK, including retries. |
| 67 | + |
| 68 | +**State Management**: Thread-safe state tracking for events captured vs sent, retry attempts, and errors. |
| 69 | + |
| 70 | +**UUID Tracking**: Extracts and tracks UUIDs from batches to verify deduplication. |
| 71 | + |
| 72 | +## Documentation |
| 73 | + |
| 74 | +For complete documentation on the test harness and how to implement adapters, see: |
| 75 | +- [PostHog SDK Test Harness](https://github.com/PostHog/posthog-sdk-test-harness) |
| 76 | +- [Adapter Implementation Guide](https://github.com/PostHog/posthog-sdk-test-harness/blob/main/ADAPTER_GUIDE.md) |
0 commit comments