Skip to content

Add proofline.testing, a fake Proofline server for testing code that uses the SDK - #129

Open
amareshhebbar wants to merge 3 commits into
prooflinedev:mainfrom
amareshhebbar:feat/proofline-testing
Open

amareshhebbar wants to merge 3 commits into
prooflinedev:mainfrom
amareshhebbar:feat/proofline-testing

Conversation

@amareshhebbar

Copy link
Copy Markdown

Closes #128

What proofline.testing is

The problem. A Proofline customer writes code on top of the SDK. For example, a CI script that requests a review, waits for it, and fails the build on serious findings. To test that script today, they have three bad options:

  1. Call the real Proofline API in their tests. That is slow, needs a real token, uses real reviews, and breaks when the network is down.
  2. Mock the SDK with unittest.mock. That is fragile. The mock returns whatever they guessed, not what Proofline really returns, so the test passes while production fails.
  3. Inject a fake network layer. The SDK blocks this on purpose. It builds its own HTTP client with its own retry logic, and gives users no way to swap it.

The fix. The SDK already allows one door: base_url can be http://127.0.0.1. So this PR ships a small fake Proofline server inside the SDK. A test starts it, tells it what to say ("this review is queued, then running, then done with 2 findings"), and points the real client at it. The customer's code runs unchanged, with real HTTP, real retries and real parsing, but against a fake that answers in milliseconds.

Think of a flight simulator. The pilot uses the real controls, and only the world outside is fake.

What makes it trustworthy. The fake builds every answer through the SDK's own generated models, which come from Proofline's API contract. So whatever the fake serves, the real client can read, and a broken test setup fails right away instead of later. It also records every request, so a test can check things like "my script asked for a review of PR 7 exactly once".

The same idea is behind Stripe's stripe-mock, botocore's Stubber and moto for AWS.

Example

from proofline import ProoflineClient
from proofline.testing import FakeProofline, fake_finding

with FakeProofline() as fake:
    review_id = fake.add_review(pull_request=7, findings=[fake_finding(severity="SERIOUS")])
    with ProoflineClient(fake.token, base_url=fake.url) as client:
        outcome = client.wait_for_review(fake.organization_id, review_id)

assert outcome.status == "COMPLETED"

What it supports

  • Reviews: request_review, get_review, list_reviews, iter_reviews, list_pull_request_reviews, get_pull_request_review, stop_review and rerun_review.
  • States over time: add_review(states=["RUNNING", "COMPLETED"]) returns the next state on each read, so wait_for_review can be tested through to COMPLETED, INCOMPLETE, FAILED or STOPPED.
  • Errors: fail_next(429, retry_after="1") and similar return real problem responses. The client's own retry and error handling deal with them.
  • Anything else: route(method, path, handler) serves an endpoint the fake does not cover.
  • Request log: fake.requests holds every request, with its method, path, query, headers and body.

What it does not change

  • Nothing in proofline/_generated/, the transport or the client methods.
  • No new dependency. It uses only the standard library.
  • It works with both ProoflineClient and AsyncProoflineClient.

Tests

tests/test_testing.py has 46 tests that run the real sync and async clients against the fake, with no mocks apart from the clock, the same way tests/test_http.py does. They cover staged states, outcomes, paging, stop and rerun, retries, wrong tokens, unknown ids, custom routes and bad setups. They cover 98% of proofline/testing.py.

scripts/check.py passes in full, with 264 tests. The README has a new section, "Testing code that uses the SDK", whose example runs as written.

AI assistance

I used an AI assistant to study the codebase, design the fake, and draft the code, tests and this description. I checked the design against the SDK's polling, retry and outcome code myself, ran a demo of a real CI gate against the fake, ran every check above, and I can answer questions about any line.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add proofline.testing, a fake Proofline server for testing code that uses the SDK

1 participant