Skip to content
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

fix(fal_client): handle missing metrics #253

Merged
merged 4 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/tests-fal-client.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Run fal-client tests

on:
push:
branches:
- main
pull_request:
workflow_dispatch:

jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-python@v4
with:
python-version: "3.12"
- name: Install dependencies
run: |
pip install --upgrade pip wheel
pip install -e 'projects/fal_client[test]'
- name: Run tests
env:
FAL_KEY: ${{ secrets.FAL_KEY_PROD }}
run: |
pytest projects/fal_client/tests
1 change: 1 addition & 0 deletions projects/fal_client/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies = [
test = [
"pytest",
"pytest-asyncio",
"pillow",
]
dev = [
"fal_client[test]",
Expand Down
4 changes: 3 additions & 1 deletion projects/fal_client/src/fal_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ def _parse_status(self, data: AnyJSON) -> Status:
elif data["status"] == "IN_PROGRESS":
return InProgress(logs=data["logs"])
elif data["status"] == "COMPLETED":
return Completed(logs=data["logs"], metrics=data["metrics"])
# NOTE: legacy apps might not return metrics
metrics = data.get("metrics", {})
return Completed(logs=data["logs"], metrics=metrics)
else:
raise ValueError(f"Unknown status: {data['status']}")

Expand Down
Empty file.
Empty file.
43 changes: 43 additions & 0 deletions projects/fal_client/tests/unit/test_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import pytest

from fal_client.client import Queued, InProgress, Completed, _BaseRequestHandle


@pytest.mark.parametrize(
"data, result, raised",
[
(
{"status": "IN_QUEUE", "queue_position": 123},
Queued(position=123),
False,
),
(
{"status": "IN_PROGRESS", "logs": [{"msg": "foo"}, {"msg": "bar"}]},
InProgress(logs=[{"msg": "foo"}, {"msg": "bar"}]),
False,
),
(
{"status": "COMPLETED", "logs": [{"msg": "foo"}, {"msg": "bar"}]},
Completed(logs=[{"msg": "foo"}, {"msg": "bar"}], metrics={}),
False,
),
(
{"status": "COMPLETED", "logs": [{"msg": "foo"}, {"msg": "bar"}], "metrics": {"m1": "v1", "m2": "v2"}},
Completed(logs=[{"msg": "foo"}, {"msg": "bar"}], metrics={"m1": "v1", "m2": "v2"}),
False,
),
(
{"status": "FOO"},
ValueError,
True,
)
]
)
def test_parse_status(data, result, raised):
handle = _BaseRequestHandle("foo", "bar", "baz", "qux")

if raised:
with pytest.raises(result):
handle._parse_status(data)
else:
assert handle._parse_status(data) == result