@inject decorator or inject_all = True for AsyncBase, refs #878

This commit is contained in:
Simon Willison 2021-11-16 14:01:16 -08:00
commit 22f41f7983
2 changed files with 22 additions and 2 deletions

View file

@ -1,5 +1,5 @@
import asyncio
from datasette.utils.asyncdi import AsyncBase
from datasette.utils.asyncdi import AsyncBase, inject
import pytest
from random import random
@ -8,15 +8,22 @@ class Simple(AsyncBase):
def __init__(self):
self.log = []
@inject
async def two(self):
self.log.append("two")
@inject
async def one(self, two):
self.log.append("one")
return self.log
async def not_inject(self, one, two):
return one + two
class Complex(AsyncBase):
inject_all = True
def __init__(self):
self.log = []
@ -40,6 +47,8 @@ class Complex(AsyncBase):
class WithParameters(AsyncBase):
inject_all = True
async def go(self, calc1, calc2, param1):
return param1 + calc1 + calc2
@ -53,6 +62,7 @@ class WithParameters(AsyncBase):
@pytest.mark.asyncio
async def test_simple():
assert await Simple().one() == ["two", "one"]
assert await Simple().not_inject(6, 7) == 13
@pytest.mark.asyncio