Skip to content

Commit ddc3d1d

Browse files
committed
Refactor test_cli.py for improved readability and maintainability
The response token size for tests in test_cli.py has been extracted to a constant, TEST_RESPONSE_TOKEN_SIZE, to avoid repetition and improve readability. This change enhances the maintainability of the code by centralizing the control of this value.
1 parent e627014 commit ddc3d1d

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

tests/test_cli.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
from pathlib import Path
88
import json, os, pytest
99

10+
# smaller than the default so tests go a little faster
11+
TEST_RESPONSE_TOKEN_SIZE = 200
12+
1013

1114
@pytest.mark.skipif(not os.getenv("OPENAI_API_KEY"), reason="Skipping live tests without an API key.")
1215
def test_alignment(cli_runner):
@@ -19,12 +22,11 @@ def test_commit(cli_runner, temp_git_repo):
1922
with cli_runner.isolated_filesystem():
2023
os.chdir(temp_git_repo.working_dir) # change to the temporary repo directory
2124

22-
response_token_size = 200 # smaller than the default so tests go a little faster
2325
# Scenario 1: Only unstaged changes
2426
create_and_write_file("test1.txt", "This is a test file.")
2527
repo = Repo(temp_git_repo.working_dir)
2628
repo.git.add("test1.txt") # stage the new file
27-
result = cli_runner.invoke(cli, ["commit", "-y", "-t", response_token_size, "test1.txt"])
29+
result = cli_runner.invoke(cli, ["commit", "-y", "-t", TEST_RESPONSE_TOKEN_SIZE, "test1.txt"])
2830
assert result.exit_code == 0
2931
assert "✅ 1 file(s) committed" in result.output
3032

@@ -33,12 +35,12 @@ def test_commit(cli_runner, temp_git_repo):
3335
repo = Repo(temp_git_repo.working_dir)
3436
repo.git.add("test2.txt") # stage the new file
3537
create_and_write_file("test3.txt", "This is yet another test file.") # unstaged file
36-
result = cli_runner.invoke(cli, ["commit", "-y", "-t", response_token_size])
38+
result = cli_runner.invoke(cli, ["commit", "-y", "-t", TEST_RESPONSE_TOKEN_SIZE])
3739
assert result.exit_code == 0
3840
assert "✅ 1 file(s) committed" in result.output
3941

4042
# Scenario 3: No changes at all
41-
result = cli_runner.invoke(cli, ["commit", "-y", "-t", response_token_size])
43+
result = cli_runner.invoke(cli, ["commit", "-y", "-t", TEST_RESPONSE_TOKEN_SIZE])
4244
assert result.exit_code == 0
4345
assert "No changes" in result.output
4446

@@ -102,7 +104,7 @@ def test_debug_failure(cli_runner):
102104

103105
@pytest.mark.skipif(not os.getenv("OPENAI_API_KEY"), reason="Skipping live tests without an API key.")
104106
def test_fun_fact(cli_runner):
105-
result = cli_runner.invoke(cli, ["fun-fact", "-t", "50"])
107+
result = cli_runner.invoke(cli, ["fun-fact", "-t", TEST_RESPONSE_TOKEN_SIZE])
106108
assert result.exit_code == 0, f"Output: {result.output}"
107109

108110

@@ -119,14 +121,16 @@ def test_review(cli_runner, temp_git_repo):
119121
repo.git.add("test.txt")
120122

121123
# Run the review command
122-
result = cli_runner.invoke(cli, ["review", "-t", "100", "test.txt"])
124+
result = cli_runner.invoke(cli, ["review", "-t", TEST_RESPONSE_TOKEN_SIZE, "test.txt"])
123125

124126
# Check that the review command ran successfully
125127
assert result.exit_code == 0
126128
assert len(result.output) > 20
127129

128130
# Again with json output
129-
result = cli_runner.invoke(cli, ["review", "-t", "100", "--output-format", "json", "test.txt"])
131+
result = cli_runner.invoke(
132+
cli, ["review", "-t", TEST_RESPONSE_TOKEN_SIZE, "--output-format", "json", "test.txt"]
133+
)
130134

131135
assert result.exit_code == 0
132136
# Check if it's valid json
@@ -141,7 +145,9 @@ def test_sidekick(cli_runner):
141145
mock_files = [".gitignore"]
142146

143147
# Invoke the sidekick command
144-
result = cli_runner.invoke(cli, ["sidekick", "-t", "100", "--request", mock_request] + mock_files)
148+
result = cli_runner.invoke(
149+
cli, ["sidekick", "-t", TEST_RESPONSE_TOKEN_SIZE, "--request", mock_request] + mock_files
150+
)
145151

146152
assert result.exit_code == 0, f"Output: {result.output}"
147153
assert "5" in result.output

0 commit comments

Comments
 (0)