Skip to content

Commit 67295cd

Browse files
committed
refactor: with sonnet
1 parent 7593487 commit 67295cd

File tree

2 files changed

+73
-24
lines changed

2 files changed

+73
-24
lines changed

src/cat_ai/runner.py

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,59 @@
11
import os
2+
from typing import Callable, List, Optional
23

34
from .reporter import Reporter
4-
from typing import Callable, Any
55

66

77
class Runner:
8-
def __init__(self, test_function: Callable[..., Any], reporter: Reporter) -> None:
8+
"""Executes test functions and collects results using a reporter."""
9+
10+
def __init__(self, test_function: Callable[..., bool], reporter: Reporter) -> None:
11+
"""
12+
Initialize the Runner with a test function and reporter.
13+
14+
Args:
15+
test_function: Function to execute during test runs
16+
reporter: Reporter instance to track and report test results
17+
"""
918
self.reporter = reporter
1019
self.test_function = test_function
1120

1221
@staticmethod
13-
def sample_size(default_size: int = 1) -> int:
22+
def get_sample_size(default_size: int = 1) -> int:
23+
"""
24+
Get sample size from environment variable or use default.
25+
26+
Args:
27+
default_size: Default sample size if not specified in environment
28+
29+
Returns:
30+
Number of test runs to perform
31+
"""
1432
return int(os.getenv("CAT_AI_SAMPLE_SIZE", str(default_size)))
1533

16-
def run_once(self, run_number: int = 0) -> Any:
34+
def run_once(self, run_number: int = 0) -> bool:
35+
"""
36+
Execute the test function once.
37+
38+
Args:
39+
run_number: Current run index for reporting
40+
41+
Returns:
42+
Result from the test function
43+
"""
1744
self.reporter.run_number = run_number
18-
result = self.test_function(reporter=self.reporter)
19-
return result
20-
21-
def run_loop(self, tries: int = sample_size()) -> list[Any]:
22-
results = []
23-
for x in range(0, tries):
24-
results.append(self.run_once(x))
25-
return results
45+
return self.test_function(reporter=self.reporter)
46+
47+
def run(self, sample_size: Optional[int] = None) -> List[bool]:
48+
"""
49+
Execute the test function multiple times based on sample size.
50+
51+
Args:
52+
sample_size: Number of times to run the test, defaults to
53+
value from get_sample_size() if None
54+
55+
Returns:
56+
List of results from all test runs
57+
"""
58+
runs = sample_size or self.get_sample_size()
59+
return [self.run_once(i) for i in range(runs)]

tests/cat_ai/test_runner.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import pytest
2-
from src.cat_ai.runner import Runner
31
from src.cat_ai.reporter import Reporter
2+
from src.cat_ai.runner import Runner
43

54

65
# Dummy test function that will be passed to Runner
@@ -12,11 +11,11 @@ def dummy_test_function(reporter: Reporter):
1211
def test_runner_sample_size(monkeypatch):
1312
# Set an environment variable to test
1413
monkeypatch.setenv("CAT_AI_SAMPLE_SIZE", "5")
15-
assert Runner.sample_size() == 5
14+
assert Runner.get_sample_size() == 5
1615

1716
# Test default size
1817
monkeypatch.delenv("CAT_AI_SAMPLE_SIZE", raising=False)
19-
assert Runner.sample_size(default_size=3) == 3
18+
assert Runner.get_sample_size(default_size=3) == 3
2019

2120

2221
def test_run_once():
@@ -32,22 +31,38 @@ def test_run_once():
3231
assert reporter.run_number == 1
3332

3433

35-
def test_run_loop(monkeypatch):
36-
# Set the environment variable for a controlled loop
34+
def test_run():
35+
# Create a Reporter with necessary arguments
36+
reporter = Reporter(test_name="test_run", output_dir="/tmp")
37+
38+
# Initialize Runner with dummy test function and Reporter
39+
runner = Runner(test_function=dummy_test_function, reporter=reporter)
40+
41+
# Test with explicit sample size parameter
42+
results = runner.run(sample_size=3)
43+
assert len(results) == 3
44+
assert all(res.startswith("Running test with run number ") for res in results)
45+
expected_results = [
46+
"Running test with run number 0",
47+
"Running test with run number 1",
48+
"Running test with run number 2",
49+
]
50+
assert results == expected_results
51+
52+
53+
def test_run_with_env_variable(monkeypatch):
54+
# Set the environment variable for a controlled test
3755
monkeypatch.setenv("CAT_AI_SAMPLE_SIZE", "3")
3856

3957
# Create a Reporter with necessary arguments
40-
reporter = Reporter(test_name="test_run_loop", output_dir="/tmp")
58+
reporter = Reporter(test_name="test_run_with_env", output_dir="/tmp")
4159

4260
# Initialize Runner with dummy test function and Reporter
4361
runner = Runner(test_function=dummy_test_function, reporter=reporter)
4462

45-
# Test run_loop
46-
tries = Runner.sample_size()
47-
48-
results = runner.run_loop(tries=tries)
63+
# Test without explicit sample size (should use environment variable)
64+
results = runner.run()
4965
assert len(results) == 3
50-
assert all(res.startswith("Running test with run number ") for res in results)
5166
expected_results = [
5267
"Running test with run number 0",
5368
"Running test with run number 1",

0 commit comments

Comments
 (0)