-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from reworkd/monke
🚀 Field parameterization
- Loading branch information
Showing
13 changed files
with
980 additions
and
318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from typing import Dict | ||
from urllib.parse import urlparse | ||
|
||
from bananalyzer import Example | ||
from bananalyzer.data.schemas import Eval | ||
from bananalyzer.runner.runner import BananalyzerTest | ||
|
||
|
||
class PytestTestGenerator: | ||
def __init__(self) -> None: | ||
self._classnames: Dict[str, int] = {} | ||
|
||
def generate_test(self, example: Example) -> BananalyzerTest: | ||
return BananalyzerTest( | ||
code=f""" | ||
@pytest.mark.asyncio | ||
class {self._generate_class_name(example)}: | ||
@classmethod | ||
def setup_class(cls): | ||
cls.example = get_example_by_url("{example.url}") | ||
@pytest_asyncio.fixture(scope="class") | ||
async def result(self, context, agent): | ||
yield await agent.run(context, self.example) | ||
{"".join(self._generate_eval_test(eval_, i) for i, eval_ in enumerate(example.evals))} | ||
""", | ||
example=example, | ||
) | ||
|
||
def _generate_eval_test(self, eval_: Eval, i: int) -> str: | ||
if eval_.type == "json_match" and isinstance(eval_.expected, dict): | ||
return f""" | ||
@pytest.mark.parametrize("key", {list(eval_.expected.keys())}) | ||
async def test_match_field(self, key, result) -> None: | ||
assert self.example.evals[{i}].expected.get(key, None) == result.get(key, None) | ||
""" | ||
return f""" | ||
async def test_{eval_.type}(self, result) -> None: | ||
self.example.evals[{i}].eval_results(None, result) | ||
""" | ||
|
||
def _generate_class_name(self, example: Example) -> str: | ||
domain = urlparse(example.url).netloc | ||
domain = domain.replace(".", "_") | ||
if domain.startswith("www_"): | ||
domain = domain[4:] | ||
|
||
domain = "".join([part.capitalize() for part in domain.split("_")]) | ||
|
||
key = f"{example.type.capitalize()}{domain}" | ||
self._classnames[key] = self._classnames.get(key, -1) + 1 | ||
suffix = "" if not self._classnames[key] else f"{self._classnames[key] + 1}" | ||
return f"Test{key}{suffix}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.