Exemplo n.º 1
0
def test_injected_field(monkeypatch, container):
    # Using the injected field

    monkeypatch.setattr(container, 'get', DummyInjectedField.d_get)
    inj = Injector(DummyInjectedField)
    result: DummyInjectedField = inj(container)
    assert 'source' == result.target.name
Exemplo n.º 2
0
def test_injected_context(container, dummy_customer):
    # Return the context from the container.context via injection

    container.context = dummy_customer
    inj = Injector(DummyInjected)
    result: DummyInjected = inj(container)
    assert 'dummy_customer' == getattr(result.context, 'name')
Exemplo n.º 3
0
def test_injected_singleton(monkeypatch, container):
    # Using the injected field to find a singleton

    monkeypatch.setattr(container, 'get', DummyInjectedSingleton.d_get)
    inj = Injector(DummyInjectedSingleton)
    result: DummyInjectedSingleton = inj(container)
    assert 'source' == result.target.name
Exemplo n.º 4
0
def test_injected_field_with_attr(monkeypatch, container):
    # Using the injected field

    monkeypatch.setattr(container, 'get', DummyIFWA.d_get)
    inj = Injector(DummyIFWA)
    result: DummyIFWA = inj(container)
    assert 'source' == result.target
Exemplo n.º 5
0
def test_singleton(monkeypatch, container):
    # A singleton is also registered, use it

    monkeypatch.setattr(container, 'get', DummySingleton.d_get)
    inj = Injector(DummySingleton)
    result: DummySingleton = inj(container)
    assert 'singleton' == result.singleton.name
Exemplo n.º 6
0
def test_basic_dataclass(monkeypatch, container):
    # Put something in the container

    monkeypatch.setattr(container, 'get', DummyBasicDataclass.d_get)
    inj = Injector(DummyBasicDataclass)
    result = inj(container)
    assert 'source' == result.target.name
Exemplo n.º 7
0
def test_init_false_missing_postinit(container):
    # A field has init=False but the dataclass is missing a __post_init__

    inj = Injector(DummyNotInit)
    with pytest.raises(LookupError) as exc:
        inj(container)
    expected = 'Field "name" has init=False but no __post_init__'
    assert expected == str(exc.value)
Exemplo n.º 8
0
def test_injected_field_missing_attr(monkeypatch, container):
    # Using the injected field

    monkeypatch.setattr(container, 'get', DummyIFMA.d_get)
    inj = Injector(DummyIFMA)
    with pytest.raises(AttributeError) as exc:
        inj(container)
    assert 'XXX' in str(exc.value)
Exemplo n.º 9
0
def test_no_registered_or_manual_context(container):
    # No context assigned to container
    @dataclass
    class Dummy:
        context: Context

    inj = Injector(Dummy)
    result: Dummy = inj(container)
    assert None is result.context
Exemplo n.º 10
0
def test_no_registrations(monkeypatch, container):
    # Nothing in registry

    monkeypatch.setattr(container, 'get', DummyNoRegistrations.d_get)
    inj = Injector(DummyNoRegistrations)
    with pytest.raises(LookupError) as exc:
        inj(container)
    msg = 'Injector failed for target on DummyNoRegistrations'
    assert msg == str(exc.value)
Exemplo n.º 11
0
def test_fail_primitive_value_no_default(monkeypatch, container):
    # Type is str but you don't put primitive values in registry

    monkeypatch.setattr(container, 'get', DummyFailPVND.d_get)
    inj = Injector(DummyFailPVND)
    with pytest.raises(LookupError) as exc:
        inj(container)
    msg = 'No default value on field target'
    assert msg == str(exc.value)
Exemplo n.º 12
0
def test_nothing_needed(container):
    # Our dataclass doesn't want anything

    @dataclass
    class Dummy:
        pass

    inj = Injector(Dummy)
    result: Dummy = inj(container)
    assert Dummy == result.__class__
Exemplo n.º 13
0
def test_primitive_value_with_default(container):
    # Type is str but you don't put primitive values in registry

    @dataclass
    class Dummy:
        target: str = 'Dummy Target'

    inj = Injector(Dummy)
    result: Dummy = inj(container)
    assert 'Dummy Target' == result.target
Exemplo n.º 14
0
def test_injected_customer_from_injector(container, dummy_customer):
    # The best spelling for getting the context as the correct type

    @dataclass
    class Dummy:
        context: DummyCustomer = injected(Context)

    container.context = dummy_customer
    inj = Injector(Dummy)
    result: Dummy = inj(container)
    assert 'dummy_customer' == getattr(result.context, 'name')
Exemplo n.º 15
0
def test_injected_customer_from_container_attr(container, dummy_customer):
    # Slightly-better spelling for getting context, but as correct type

    @dataclass
    class Dummy:
        context: DummyCustomer = injected(ServiceContainer, attr='context')

    container.context = dummy_customer
    inj = Injector(Dummy)
    result: Dummy = inj(container)
    assert 'dummy_customer' == getattr(result.context, 'name')
Exemplo n.º 16
0
def test_container_post_init(monkeypatch, container, dummy_customer):
    # Use dataclass post init to get something out of the
    # container

    @dataclass
    class Dummy:
        container: ServiceContainer
        url: Optional[str] = None

        def __post_init__(self):
            self.url = self.container.get(Url)

    def d_get(key):
        if key is Url:
            return Url()
        raise TypeError()

    monkeypatch.setattr(container, 'get', d_get)
    container.context = dummy_customer
    inj = Injector(Dummy)
    result: Dummy = inj(container)
    assert container == result.container
Exemplo n.º 17
0
def test_container(container):
    # Target wants to grab the container itself

    inj = Injector(DummyContainer)
    result: DummyContainer = inj(container)
    assert container == result.container
Exemplo n.º 18
0
def test_init_false(container):
    # Use __post_init__ to initialize a field

    inj = Injector(DummyInit)
    result: DummyInit = inj(container)
    assert 'initialized' == result.name