Skip to content

Commit

Permalink
add tests for non-injectable items in OverrideRule
Browse files Browse the repository at this point in the history
  • Loading branch information
BurnzZ committed Mar 2, 2022
1 parent 17689b5 commit 7e00bf6
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions tests/test_injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,77 @@ def callback(response: DummyResponse, price_po: PricePO, rate_po: EurDollarRate)
assert item == {"price": 22, "currency": "€"}


class InjectableItem(Injectable):
is_injectable = True


class NonInjectableItem:
is_injectable = False


class TestInjectableProperties:
"""This looks at the consequences when an item/po does not inherit from
``web_poet.Injectable``.
"""

pattern = Patterns(["example.com"])

@staticmethod
def get_deps(overrides, callback, providers):
registry = OverridesRegistry(overrides)
injector = get_injector_for_testing(providers, overrides_registry=registry)
response = get_response_for_testing(callback)
kwargs = yield from injector.build_callback_dependencies(
response.request, response)
kwargs_types = {key: type(value) for key, value in kwargs.items()}
return kwargs_types

@inlineCallbacks
def test_non_injectable_source(self, providers):
"""This ensures that overriding an object from ``instead_of`` which is
not injectable still works.
"""
overrides = [
OverrideRule(self.pattern, use=InjectableItem, instead_of=NonInjectableItem)
]

def callback(response: DummyResponse, item: NonInjectableItem):
pass

kwargs_types = yield from self.get_deps(overrides, callback, providers)
assert kwargs_types == {"item": InjectableItem}

@inlineCallbacks
def test_non_injectable_replacement(self, providers):
"""By its nature, the replacement asked using the ``use`` param must
be an injectable so that the override component would work.
"""
overrides = [
OverrideRule(self.pattern, use=NonInjectableItem, instead_of=InjectableItem)
]

def callback(response: DummyResponse, item: InjectableItem):
pass

kwargs_types = yield from self.get_deps(overrides, callback, providers)
assert kwargs_types == {}

@inlineCallbacks
def test_non_injectable_source_and_replacement(self, providers):
"""Having both of ``instead_of`` and ``use`` not being Injectables won't
work at all.
"""
overrides = [
OverrideRule(self.pattern, use=NonInjectableItem, instead_of=NonInjectableItem)
]

def callback(response: DummyResponse, item: NonInjectableItem):
pass

kwargs_types = yield from self.get_deps(overrides, callback, providers)
assert kwargs_types == {}


def test_load_provider_classes():
provider_as_string = f"{ResponseDataProvider.__module__}.{ResponseDataProvider.__name__}"
injector = get_injector_for_testing({provider_as_string: 2, ResponseDataProvider: 1})
Expand Down

0 comments on commit 7e00bf6

Please sign in to comment.