-
Notifications
You must be signed in to change notification settings - Fork 16
feat: pact contract for get job by id and post job #106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
duvanmoionq
wants to merge
8
commits into
main
Choose a base branch
from
pact/contract
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
eda1b3c
feat: pact contract for get job by id and post job
duvanmoionq 61b0d32
chore: fix contract test error
duvanmoionq c2347e3
fix: lint issue
duvanmoionq 9f4c423
chore: new ci publish logic for http pact contracts
duvanmoionq a1b7c59
chore: improve the comments in the new consumer contract
duvanmoionq 0ecf47f
Merge branch 'main' into pact/contract
antalszava b177f9f
chore: split consumer by endpoint
duvanmoionq a123fac
chore: remove comments
duvanmoionq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| name: Pact — publish consumer contracts | ||
| on: | ||
| workflow_dispatch: {} | ||
| push: | ||
| branches: [main] | ||
| pull_request: | ||
| branches: [main] | ||
| paths: | ||
| - "ionq_core/**" | ||
| - "tests/pact/**" | ||
| - ".github/workflows/pact-publish.yml" | ||
|
|
||
| concurrency: | ||
| group: ${{ github.workflow }}-${{ github.ref }} | ||
| cancel-in-progress: true | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| env: | ||
| UV_FROZEN: true | ||
|
|
||
| jobs: | ||
| publish-pacts: | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 10 | ||
| steps: | ||
| - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 | ||
| with: | ||
| persist-credentials: false | ||
| - uses: ./.github/actions/setup-uv | ||
| with: | ||
| enable-cache: ${{ github.event_name == 'push' }} | ||
| - run: uv sync | ||
| # --no-cov: the pact test alone cannot meet the repo's 100% coverage gate | ||
| # (addopts sets --cov-fail-under=100), same as ci.yml's non-3.11 matrix legs. | ||
| - name: Generate consumer pact | ||
| run: uv run pytest tests/pact --no-cov | ||
| - name: Publish pact to PactFlow | ||
| uses: pactflow/actions/publish-pact-files@3074b72c0df6af10087afccfa6f91e2308dc151c # v2.0.0 | ||
| with: | ||
| pactfiles: pacts | ||
| broker_url: ${{ vars.PACT_BROKER_BASE_URL }} | ||
| token: ${{ secrets.PACT_BROKER_TOKEN }} | ||
| # Explicit so a PR's detached-HEAD checkout doesn't misreport them. | ||
| branch: ${{ github.head_ref || github.ref_name }} | ||
| version: ${{ github.event.pull_request.head.sha || github.sha }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,3 +13,6 @@ docs/ | |
| .env | ||
| .idea/ | ||
| *.orig | ||
|
|
||
| # Pact contract files (generated by consumer tests; published to the broker) | ||
| pacts/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| """Pact HTTP contract: ionq-core-python -> cloud-job-manager-http (POST /v0.4/jobs). | ||
| """ | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| from pact import Pact, match | ||
|
|
||
| from ionq_core.api.default import create_job | ||
| from ionq_core.client import AuthenticatedClient | ||
| from ionq_core.models import CircuitJobCreationPayload, JobCreationResponse | ||
|
|
||
| PACT_DIR = Path(__file__).resolve().parents[3] / "pacts" | ||
|
|
||
| CREATE_PAYLOAD = CircuitJobCreationPayload.from_dict( | ||
| { | ||
| "type": "ionq.circuit.v1", | ||
| "backend": "simulator", | ||
| "name": "pact test job", | ||
| "shots": 100, | ||
| "input": { | ||
| "gateset": "qis", | ||
| "qubits": 2, | ||
| "circuit": [ | ||
| {"gate": "h", "target": 0}, | ||
| {"gate": "cnot", "target": 0, "control": 1}, | ||
| ], | ||
| }, | ||
| "settings": {"error_mitigation": {"debiasing": False}}, | ||
| "noise": {"model": "ideal"}, | ||
| } | ||
| ) | ||
|
|
||
| # Only the fields the SDK reads off the create response. | ||
| CREATE_RESPONSE_BODY = { | ||
| "id": match.uuid("0198097d-3888-72ad-a9dd-9ace842cf182"), | ||
| "status": "submitted", | ||
| "session_id": None, | ||
| } | ||
|
|
||
|
|
||
| def _client(mock_url: str) -> AuthenticatedClient: | ||
| return AuthenticatedClient( | ||
| base_url=f"{mock_url}/v0.4", | ||
| token="pact-test-key", | ||
| prefix="apiKey", | ||
| auth_header_name="Authorization", | ||
| ) | ||
|
|
||
|
|
||
| def test_jobs_create_circuit_api_contract() -> None: | ||
| pact = Pact("ionq-core-python", "cloud-job-manager-http").with_specification("V3") | ||
| ( | ||
| pact.upon_receiving("a request to create a circuit job") | ||
| .given("a valid project and org region exist") | ||
| .with_request("POST", "/v0.4/jobs") | ||
| .with_body(CREATE_PAYLOAD.to_dict(), content_type="application/json") | ||
| .will_respond_with(201) | ||
| .with_body(CREATE_RESPONSE_BODY, content_type="application/json") | ||
| ) | ||
| with pact.serve() as srv, _client(srv.url) as client: | ||
| created = create_job.sync(client=client, body=CREATE_PAYLOAD) | ||
| assert isinstance(created, JobCreationResponse) | ||
| assert created.status == "submitted" | ||
| assert created.session_id is None | ||
|
|
||
| PACT_DIR.mkdir(exist_ok=True) | ||
| pact.write_file(PACT_DIR, overwrite=False) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| """Pact HTTP contract: ionq-core-python -> cloud-job-manager-http (GET /v0.4/jobs/{id}). | ||
| """ | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| from pact import Pact, match | ||
|
|
||
| from ionq_core.api.default import get_job | ||
| from ionq_core.client import AuthenticatedClient | ||
| from ionq_core.models import SingleCircuitJob | ||
|
|
||
| PACT_DIR = Path(__file__).resolve().parents[3] / "pacts" | ||
|
|
||
| ISO_TIMESTAMP = r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z$" | ||
|
|
||
| JOB_ID = "0198097d-3888-72ad-a9dd-9ace842cf181" | ||
|
|
||
| GET_RESPONSE_BODY = { | ||
| "id": JOB_ID, | ||
| "status": "completed", | ||
| "type": "ionq.circuit.v1", | ||
| "backend": match.string("simulator"), | ||
| "dry_run": match.boolean(False), | ||
| "submitter_id": match.string("66841feb7addb210ae6214c2"), | ||
| "project_id": match.uuid("31a4dc21-9b52-4a51-89db-c62f1f77f356"), | ||
| "parent_job_id": None, | ||
| "session_id": None, | ||
| "metadata": None, | ||
| "name": match.string("pact test job"), | ||
| "submitted_at": match.regex("2026-09-09T12:00:00.000Z", regex=ISO_TIMESTAMP), | ||
| "started_at": None, | ||
| "completed_at": None, | ||
| "predicted_wait_time_ms": None, | ||
| "predicted_execution_duration_ms": None, | ||
| "execution_duration_ms": match.integer(0), | ||
| "failure": None, | ||
| "output": match.like({}), | ||
| "settings": match.like({}), | ||
| "stats": match.like({}), | ||
| "results": { | ||
| "ionq.result.probabilities.json.v1": match.like( | ||
| { | ||
| "id": match.string("probabilities"), | ||
| "format": match.string("ionq.result.probabilities.json.v1"), | ||
| "media_type": match.string("application/json"), | ||
| } | ||
| ) | ||
| }, | ||
| "child_job_ids": None, | ||
| } | ||
|
|
||
|
|
||
| def _client(mock_url: str) -> AuthenticatedClient: | ||
| return AuthenticatedClient( | ||
| base_url=f"{mock_url}/v0.4", | ||
| token="pact-test-key", | ||
| prefix="apiKey", | ||
| auth_header_name="Authorization", | ||
| ) | ||
|
|
||
|
|
||
| def test_jobs_get_job_api_contract() -> None: | ||
| pact = Pact("ionq-core-python", "cloud-job-manager-http").with_specification("V3") | ||
| ( | ||
| pact.upon_receiving("a request to get a completed job") | ||
| .given("a completed circuit job with results exists", id=JOB_ID) | ||
| .with_request("GET", f"/v0.4/jobs/{JOB_ID}") | ||
| .will_respond_with(200) | ||
| .with_body(GET_RESPONSE_BODY, content_type="application/json") | ||
| ) | ||
| with pact.serve() as srv, _client(srv.url) as client: | ||
| job = get_job.sync(uuid=JOB_ID, client=client) | ||
| assert isinstance(job, SingleCircuitJob) | ||
| assert job.id == JOB_ID | ||
| assert job.status == "completed" | ||
| assert job.results is not None | ||
|
|
||
| PACT_DIR.mkdir(exist_ok=True) | ||
| pact.write_file(PACT_DIR, overwrite=False) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.