Skip to content

Commit

Permalink
fix(fal_client): handle missing metrics (#253)
Browse files Browse the repository at this point in the history
* fix(fal_client): handle missing metrics

* chore: enable gha tests

* don't forget pillow

* add FAL_KEY_PROD
  • Loading branch information
efiop authored Jun 28, 2024
1 parent f697a66 commit 09d9032
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 1 deletion.
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

0 comments on commit 09d9032

Please sign in to comment.