Skip to content

Commit ceb42ad

Browse files
committed
Refactoring to increase coverage
``` abramowi at Marcs-MacBook-Pro-3 in ~/Code/OpenSource/python-firefly (main●) $ uv run pytest --cov=firefly --cov-report=term-missing --pdb ============================================================= test session starts ============================================================= platform darwin -- Python 3.10.16, pytest-8.3.5, pluggy-1.5.0 rootdir: /Users/abramowi/Code/OpenSource/python-firefly configfile: pyproject.toml plugins: cov-6.1.1 collected 26 items tests/test_cli.py ........ [ 30%] tests/test_firefly_client.py ...... [ 53%] tests/test_ims_auth.py ...... [ 76%] tests/test_models.py ...... [100%] =============================================================== tests coverage ================================================================ ______________________________________________ coverage: platform darwin, python 3.10.16-final-0 ______________________________________________ Name Stmts Miss Cover Missing ----------------------------------------------------- firefly/__init__.py 2 0 100% firefly/cli.py 92 1 99% 176 firefly/client.py 41 0 100% firefly/exceptions.py 4 0 100% firefly/ims_auth.py 25 0 100% firefly/models.py 22 0 100% ----------------------------------------------------- TOTAL 186 1 99% ============================================================= 26 passed in 0.31s ============================================================== ```
1 parent 14c853b commit ceb42ad

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

firefly/cli.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,7 @@ def capture_request(*args, **kwargs):
134134
nonlocal response_obj, status_code, num_bytes
135135
response_obj = resp
136136
status_code = resp.status_code
137-
try:
138-
num_bytes = len(resp.content)
139-
except Exception:
140-
num_bytes = None
137+
num_bytes = len(resp.content)
141138
return resp
142139

143140
_requests.request = capture_request
@@ -148,17 +145,10 @@ def capture_request(*args, **kwargs):
148145
_requests.request = orig_request
149146

150147
if verbose:
151-
if response_obj is not None:
152-
typer.secho(
153-
f"Received HTTP {status_code} response ({num_bytes} bytes) from {image_api_url}.",
154-
fg=typer.colors.YELLOW, err=True
155-
)
156-
else:
157-
raw_json = json.dumps(response.json())
158-
typer.secho(
159-
f"Received HTTP 200 response ({len(raw_json.encode('utf-8'))} bytes) from {image_api_url}.",
160-
fg=typer.colors.YELLOW, err=True
161-
)
148+
typer.secho(
149+
f"Received HTTP {status_code} response ({num_bytes} bytes) from {image_api_url}.",
150+
fg=typer.colors.YELLOW, err=True
151+
)
162152

163153
# Output formatting
164154
if format == "json":
@@ -171,10 +161,10 @@ def capture_request(*args, **kwargs):
171161
download_image(image_url)
172162
if show_images:
173163
try:
164+
imgcat_cmd = f"imgcat --url '{image_url}'"
174165
if image_url == mock_image:
175-
subprocess.run(["imgcat tests/images/cat-coding.png"], shell=True, check=True)
176-
else:
177-
subprocess.run([f"imgcat --url '{image_url}'"], shell=True, check=True)
166+
imgcat_cmd = "imgcat tests/images/cat-coding.png"
167+
subprocess.run([imgcat_cmd], shell=True, check=True)
178168
except Exception as e:
179169
typer.secho(f"[warn] Could not display image in terminal using imgcat: {e}", fg=typer.colors.YELLOW, err=True)
180170

tests/test_cli.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,22 @@ def test_generate_show_images(mock_run):
6868
assert mock_run.called
6969
assert "Generated image URL:" in result.output
7070

71+
@mock.patch("subprocess.run", side_effect=FileNotFoundError("imgcat not found"))
72+
def test_generate_show_images_imgcat_missing(mock_run):
73+
result = runner.invoke(
74+
app,
75+
[
76+
"image", "generate",
77+
"--client-id", "dummy_id",
78+
"--client-secret", "dummy_secret",
79+
"--prompt", "a cat coding",
80+
"--use-mocks",
81+
"--show-images"
82+
]
83+
)
84+
assert result.exit_code == 0
85+
assert "[warn] Could not display image in terminal using imgcat" in result.output
86+
7187
@pytest.mark.parametrize("missing,expected", [
7288
("--client-id", "client_id must be provided"),
7389
("--client-secret", "client_secret must be provided"),

0 commit comments

Comments
 (0)