Skip to content

Commit 0953487

Browse files
authored
Merge pull request #2 from benchling/unit-tests
Add unit tests, linting, and CI checks for chem-sync-local-flask
2 parents 5901177 + 35352fa commit 0953487

28 files changed

Lines changed: 2036 additions & 10 deletions
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Chem Sync Local Flask App Tests
2+
3+
# Taken from https://github.com/actions/starter-workflows/blob/main/ci/python-app.yml
4+
5+
on:
6+
push:
7+
branches: [ main ]
8+
pull_request:
9+
types: [opened, edited, synchronize]
10+
11+
permissions:
12+
contents: read
13+
14+
jobs:
15+
test:
16+
17+
runs-on: ubuntu-20.04
18+
name: Quality Checks + Tests
19+
defaults:
20+
run:
21+
working-directory: ./examples/chem-sync-local-flask
22+
23+
steps:
24+
- uses: actions/checkout@v3
25+
- uses: actions/setup-python@v4
26+
with:
27+
python-version: "3.11"
28+
- name: Install requirements
29+
run: pip install -r requirements.txt
30+
- name: Install dev requirements
31+
run: pip install -r dev_requirements.txt
32+
- name: Lint
33+
run: ruff check .
34+
- name: Mypy
35+
run: mypy .
36+
- name: Run Tests
37+
run: pytest tests/unit

examples/chem-sync-local-flask/local_app/benchling_app/handler.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Any
22

3+
from benchling_sdk.apps.status.errors import AppUserFacingError
34
from benchling_sdk.models.webhooks.v0 import (
45
CanvasInitializeWebhookV0,
56
CanvasInteractionWebhookV0,
@@ -25,11 +26,17 @@ def handle_webhook(webhook_dict: dict[str, Any]) -> None:
2526
# Could also choose to route on webhook.message.type
2627
# Note: if your manifest specifies more than one item in `features`,
2728
# then `webhook.message.feature_id` may also need to be part of your routing logic
28-
if isinstance(webhook.message, CanvasInitializeWebhookV0):
29-
render_search_canvas(app, webhook.message)
30-
elif isinstance(webhook.message, CanvasInteractionWebhookV0):
31-
route_interaction_webhook(app, webhook.message)
32-
else:
33-
# Should only happen if the app's manifest requests webhooks that aren't handled in its code paths
34-
raise UnsupportedWebhookError(f"Received an unsupported webhook type: {webhook}")
35-
logger.debug("Successfully completed request for webhook: %s", webhook_dict)
29+
try:
30+
if isinstance(webhook.message, CanvasInitializeWebhookV0):
31+
render_search_canvas(app, webhook.message)
32+
elif isinstance(webhook.message, CanvasInteractionWebhookV0):
33+
route_interaction_webhook(app, webhook.message)
34+
else:
35+
# Should only happen if the app's manifest requests webhooks that aren't handled in its code paths
36+
raise UnsupportedWebhookError(f"Received an unsupported webhook type: {webhook}")
37+
logger.debug("Successfully completed request for webhook: %s", webhook_dict)
38+
# We want errors shown to the user to end control flow, but we don't want them to propagate
39+
# and show as errors in our logs.
40+
# For this example, Flask error handler won't intercept this since we're within a thread
41+
except AppUserFacingError as e:
42+
logger.debug("Exiting with client error: %s", e)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
flask~=3.0.0
22
# TODO Upgrade to stable SDK version once changes are released
33
# Cryptography extra needed for webhook verification
4-
benchling-sdk[cryptography]==1.10.0a9
4+
benchling-sdk[cryptography]==1.10.0a11

examples/chem-sync-local-flask/ruff.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ target-version = "py311"
44
line-length = 110
55

66
select = ["ALL"]
7-
ignore = ["D100", "D101", "D103", "D104", "EM101", "EM102", "S101", "TRY003"]
7+
ignore = ["D100", "D101", "D103", "D104", "EM101", "EM102", "S101", "TRY003"]
8+
9+
[per-file-ignores]
10+
"**/tests/*" = ["ANN001", "ANN101", "D102", "PLR2004"]

examples/chem-sync-local-flask/tests/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)