Skip to content

Commit

Permalink
Autotest the primer recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
lslunis committed Sep 27, 2022
1 parent 39c3b4a commit 5ecb9e2
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 26 deletions.
12 changes: 10 additions & 2 deletions ice/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys

from abc import abstractmethod
from collections.abc import Awaitable
from collections.abc import Callable
from functools import wraps
from inspect import iscoroutinefunction
Expand Down Expand Up @@ -90,11 +91,15 @@ def __str__(self) -> str:
return self.__class__.__name__


FunctionBasedRecipe = Callable[..., Awaitable]


class RecipeHelper:
def __init__(self):
self._mode: Mode | None = "machine"
self.all_recipes: list[FunctionBasedRecipe] = []

def main(self, main):
def main(self, main: FunctionBasedRecipe):
if not iscoroutinefunction(main):
raise TypeError("recipe.main must be given an async function")

Expand All @@ -104,6 +109,9 @@ def main(self, main):
if getattr(value, "__module__", None) == main.__module__:
g[name] = trace(value)

traced_main = trace(main)
self.all_recipes.append(traced_main)

if main.__module__ != "__main__":
return

Expand All @@ -113,7 +121,7 @@ def main(self, main):
@wraps(main)
async def hidden_wrapper(*args, **kwargs):
try:
result = await trace(main)(*args, **kwargs)
result = await traced_main(*args, **kwargs)
except NameError:
print_exc()
print(
Expand Down
42 changes: 42 additions & 0 deletions tests/test_primer_recipes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from importlib import import_module
from inspect import signature
from pathlib import Path

import pytest

from faker import Faker

from ice.paper import Paper
from ice.recipe import FunctionBasedRecipe
from ice.recipe import recipe

Faker.seed(0)
fake = Faker()


root_dir = Path(__file__).parent.parent

primer_recipes_dir = root_dir / "ice" / "recipes" / "primer"
for path in primer_recipes_dir.glob("**/*.py"):
relative_path = path.relative_to(primer_recipes_dir)
module_name = ".".join(relative_path.parts[:-1] + (relative_path.stem,))
import_module(f"ice.recipes.primer.{module_name}")

paper = Paper.load(Path(root_dir / "papers" / "keenan-2018-tiny.txt"))


@pytest.mark.parametrize("main", recipe.all_recipes)
@pytest.mark.anyio
async def test_all_primer_recipes(main: FunctionBasedRecipe):
kwargs = {}
for p in signature(main).parameters.values():
if p.default is not p.empty:
value = p.default
elif issubclass(p.annotation, str):
value = fake.sentence()
elif issubclass(p.annotation, Paper):
value = paper
else:
raise ValueError(f"Cannot handle parameter {p}")
kwargs[p.name] = value
await main(**kwargs)
8 changes: 0 additions & 8 deletions tests/test_recipes/test_primer/test_hello.py

This file was deleted.

8 changes: 0 additions & 8 deletions tests/test_recipes/test_primer/test_qa.py

This file was deleted.

8 changes: 0 additions & 8 deletions tests/test_recipes/test_primer/test_qa_simple.py

This file was deleted.

0 comments on commit 5ecb9e2

Please sign in to comment.