class TestPydanticView(View): breadcrumbs_resources: str = injected(FakePydanticAdapter, attr='name') callable: FakePydanticCallableAdapter injected_resource_title: str = injected(Resource, attr='title') viewservice_config: ViewServiceConfig name: str = 'One Pydantic View'
class BreadcrumbsAdapter: resources: Dict[str, SampleResource] = injected(Request, attr='resources') resourceid: str = injected(Resource, attr='id') def __call__(self): resources = [] targetid = self.resourceid while targetid is not None: resource = self.resources[targetid] resources.insert(0, resource) targetid = resource.parentid return Breadcrumbs(resources=resources)
def test_other_data(): # Ensure the field can have other stuff in it i = injected(BogusResource, attr='title', metadata=dict(a=1), init=False) assert BogusResource == i.metadata['injected']['type_'] assert 'title' == i.metadata['injected']['attr'] assert 1 is i.metadata['a'] assert False is i.init
class ArticleView: breadcrumbs: Breadcrumbs resource_title: str = injected(Resource, attr='title') name: str = 'Article View' @property def breadcrumb_titles(self): return ' >> '.join([r.title for r in self.breadcrumbs.resources])
class ArticleView: resources: Dict[str, SampleResource] = injected(Request, attr='resources') resourceid: str = injected(Resource, attr='id') resource_title: str = injected(Resource, attr='title') name: str = 'Article View' @property def breadcrumb_titles(self): return ' >> '.join([r.title for r in self.breadcrumbs]) @property def breadcrumbs(self) -> List[SampleResource]: resources = [] targetid = self.resourceid while targetid is not None: resource = self.resources[targetid] resources.insert(0, resource) targetid = resource.parentid return resources
def test_invalid_both_attr_and_key(): with pytest.raises(InjectedArgumentException) as exc: injected(BogusResource, attr='title', key='name') msg = 'Cannot supply both attr and key arguments to inject' assert msg == str(exc.value)
def test_default(): i = injected(BogusResource) assert BogusResource == i.metadata['injected']['type_'] assert True is i.metadata['injected']['call']
def test_call(): i = injected(BogusResource, call=False) assert BogusResource == i.metadata['injected']['type_'] assert False is i.metadata['injected']['call']
def test_key(): i = injected(BogusResource, key='title') assert BogusResource == i.metadata['injected']['type_'] assert 'title' == i.metadata['injected']['key']
class InjectedAthlete: shoe_size: int = injected(CallableShoe)
class ArticleView: resource_title: str = injected(Resource, attr='title') name: str = 'Article View'
class TestNocallAdapterView(View): nocall: FakeNocallAdapter = injected(FakeNocallMarker, call=False) name: str = 'Use a Nocall Adapter View'
class TestInjectedResourceIdAdapterView(View): breadcrumbs_resources: FakeBreadcrumbsResources viewservice_config: ViewServiceConfig adapter_flag: str = injected(FakeBreadcrumbsResources, attr='injected_flag') name: str = 'Use a ResourceId Injected Adapter'
class ResourceView: resource_title: str = injected(Resource, attr='title') name: str = 'Resource View'
class InjectedAthlete: shoe_size: int = injected(Jersey, attr='size')
def test_attr(): i = injected(BogusResource, attr='title') assert BogusResource == i.metadata['injected']['type_'] assert 'title' == i.metadata['injected']['attr']
class InjectedAthlete: shoe_size: int = injected(Shoe, attr='size')