Beispiel #1
0
    def _resolve_single_arg(self, arg: Callable,
                            cache: FixtureCache) -> Union[Any, Fixture]:
        """
        Get the fixture return value

        If the fixture has been cached, return the value from the cache.
        Otherwise, call the fixture function and return the value.
        """

        if not hasattr(arg, "ward_meta"):
            return arg

        fixture = Fixture(arg)
        if cache.contains(fixture, fixture.scope,
                          self.scope_key_from(fixture.scope)):
            return cache.get(fixture.key, fixture.scope,
                             self.scope_key_from(fixture.scope))

        has_deps = len(fixture.deps()) > 0
        is_generator = fixture.is_generator_fixture
        if not has_deps:
            try:
                if is_generator:
                    fixture.gen = arg()
                    fixture.resolved_val = next(fixture.gen)
                else:
                    fixture.resolved_val = arg()
            except Exception as e:
                raise FixtureError(
                    f"Unable to resolve fixture '{fixture.name}'") from e
            scope_key = self.scope_key_from(fixture.scope)
            cache.cache_fixture(fixture, scope_key)
            return fixture

        children_defaults = self._get_default_args(func=arg)
        children_resolved = {}
        for name, child_fixture in children_defaults.items():
            child_resolved = self._resolve_single_arg(child_fixture, cache)
            children_resolved[name] = child_resolved

        try:
            args_to_inject = self._unpack_resolved(children_resolved)
            if is_generator:
                fixture.gen = arg(**args_to_inject)
                fixture.resolved_val = next(fixture.gen)
            else:
                fixture.resolved_val = arg(**args_to_inject)
        except Exception as e:
            raise FixtureError(
                f"Unable to resolve fixture '{fixture.name}'") from e
        scope_key = self.scope_key_from(fixture.scope)
        cache.cache_fixture(fixture, scope_key)
        return fixture
Beispiel #2
0
def _(f=exception_raising_fixture):
    cache = FixtureCache()
    cache.cache_fixture(f, "test_id")

    assert cache.get(f.key, Scope.Test, "test_id") == f
Beispiel #3
0
def _(f=exception_raising_fixture):
    cache = FixtureCache()
    cache.cache_fixture(f, "test_id")

    expect(cache.get(f.key, Scope.Test, "test_id")).equals(f)