Example #1
0
async def test_parser_node_nodefield___call___fe_is_excepting():
    from tartiflette.parser.nodes.field import NodeField
    from tests.unit.utils import AsyncMock

    raw = Exception("ninja")
    coerced = None

    class fex:
        async def __call__(self, *_, **__):
            return raw, coerced

    fe = fex()
    fe.schema_field = Mock()
    fe.cant_be_null = True

    nf = NodeField("B", None, fe, None, None, None, None)
    nf.children = [Mock()]
    nf._execute_children = AsyncMock()
    nf.parent = Mock()
    nf.parent.bubble_error = Mock()

    exectx = Mock()
    exectx.add_error = Mock()
    reqctx = Mock()

    prm = {}

    await nf(exectx, reqctx, parent_marshalled=prm)

    assert nf.marshalled == {}
    assert prm["B"] == coerced
    assert nf.parent.bubble_error.called
    assert exectx.add_error.called
Example #2
0
async def test_parser_node_nodefield___call___with_children():
    from tartiflette.parser.nodes.field import NodeField
    from tests.unit.utils import AsyncMock

    raw = Mock()
    coerced = Mock()

    class fex:
        async def __call__(self, *_, **__):
            return raw, coerced

    fe = fex()
    fe.schema_field = Mock()

    nf = NodeField("B", None, fe, None, None, None, None)
    nf.children = [Mock()]
    nf._execute_children = AsyncMock()

    exectx = Mock()
    reqctx = Mock()

    prm = {}

    await nf(exectx, reqctx, parent_marshalled=prm)

    assert nf.marshalled == {}
    assert prm["B"] == coerced
    assert nf._execute_children.called
    assert nf._execute_children.call_args == (
        (exectx, reqctx),
        {"result": raw, "coerced": coerced},
    )
Example #3
0
async def test_parser_node_nodefield___call___with_a_parent():
    from tartiflette.parser.nodes.field import NodeField

    raw = Mock()
    coerced = Mock()

    class fex:
        async def __call__(self, *_, **__):
            return raw, coerced

    fe = fex()
    fe.schema_field = Mock()

    nf = NodeField("B", None, fe, None, None, None, None)
    nf.children = None

    exectx = Mock()
    reqctx = Mock()

    prm = {}

    await nf(exectx, reqctx, parent_marshalled=prm)

    assert nf.marshalled == {}
    assert prm["B"] == coerced
Example #4
0
async def test_parser_node_nodefield__execute_children_a_list():
    from tartiflette.parser.nodes.field import NodeField
    from tests.unit.utils import AsyncMock

    exectx = Mock()
    reqctx = Mock()
    result = [Mock(), Mock()]
    coerce = [Mock(), Mock()]

    fe = Mock()
    fe.shall_produce_list = True

    child = AsyncMock()
    child.type_condition = None

    nf = NodeField("NtM", None, fe, None, None, None, None)

    nf.children = [child]

    await nf._execute_children(exectx, reqctx, result, coerce)

    assert child.called
    assert (
        (exectx, reqctx),
        {"parent_result": result[0], "parent_marshalled": coerce[0]},
    ) in child.call_args_list
    assert (
        (exectx, reqctx),
        {"parent_result": result[1], "parent_marshalled": coerce[1]},
    ) in child.call_args_list
Example #5
0
def test_parser_node_nodefield__get_coroutz_from_child_no_cond():
    from tartiflette.parser.nodes.field import NodeField

    nf = NodeField("NtM", None, None, None, None, None, None)

    child = Mock()
    child.type_condition = None

    exectx = Mock()
    reqctx = Mock()
    result = Mock()
    coerce = Mock()

    nf.children = [child, child, child]

    crtz = nf._get_coroutz_from_child(exectx, reqctx, result, coerce, None)

    assert len(crtz) == 3
    assert child.call_args_list == [
        (
            (exectx, reqctx),
            {"parent_result": result, "parent_marshalled": coerce},
        ),
        (
            (exectx, reqctx),
            {"parent_result": result, "parent_marshalled": coerce},
        ),
        (
            (exectx, reqctx),
            {"parent_result": result, "parent_marshalled": coerce},
        ),
    ]
Example #6
0
def test_parser_node_nodefield():
    from tartiflette.parser.nodes.field import NodeField

    nf = NodeField("Roberto", None, None, None, [], None, None)

    assert nf.name == "Roberto"
    assert nf.alias == "Roberto"

    nf = NodeField("Roberto", None, None, None, [], None, "James")

    assert nf.name == "Roberto"
    assert nf.alias == "James"
Example #7
0
def test_parser_node_nodefield_shall_produce_list(value):
    from tartiflette.parser.nodes.field import NodeField

    fe = Mock()
    fe.shall_produce_list = value

    nf = NodeField("Rb", None, fe, None, None, None, None)

    assert nf.shall_produce_list == value
Example #8
0
def test_parser_node_nodefield_contains_not_null(value):
    from tartiflette.parser.nodes.field import NodeField

    fe = Mock()
    fe.contains_not_null = value

    nf = NodeField("Rb", None, fe, None, None, None, None)

    assert nf.contains_not_null == value
Example #9
0
async def test_parser_node_nodefield__call__custom_exception():
    from tartiflette.parser.nodes.field import NodeField
    from tests.unit.utils import AsyncMock

    class CustomException(Exception):
        def coerce_value(self, *_args, path=None, locations=None, **_kwargs):
            return {"msg": "error", "type": "bad_request"}

    raw = CustomException("ninja")
    coerced = None

    class fex:
        async def __call__(self, *_, **__):
            return raw, coerced

    fe = fex()
    fe.schema_field = Mock()
    fe.cant_be_null = True

    nf = NodeField("B", None, fe, None, None, None, None)
    nf.children = [Mock()]
    nf._execute_children = AsyncMock()
    nf.parent = Mock()
    nf.parent.bubble_error = Mock()

    exectx = ExecutionContext()
    reqctx = Mock()

    prm = {}

    assert not bool(exectx.errors)

    await nf(exectx, reqctx, parent_marshalled=prm)

    assert bool(exectx.errors)

    assert exectx.errors[0] is raw
    assert exectx.errors[0].coerce_value() == {
        "msg": "error",
        "type": "bad_request",
    }
Example #10
0
async def test_parser_node_nodefield__call__exception():
    from tartiflette.parser.nodes.field import NodeField
    from tests.unit.utils import AsyncMock

    raw = Exception("ninja")
    coerced = None

    class fex:
        async def __call__(self, *_, **__):
            return raw, coerced

    fe = fex()
    fe.schema_field = Mock()
    fe.cant_be_null = True

    nf = NodeField("B", None, fe, None, None, None, None)
    nf.children = [Mock()]
    nf._execute_children = AsyncMock()
    nf.parent = Mock()
    nf.parent.bubble_error = Mock()

    exectx = ExecutionContext()
    reqctx = Mock()

    prm = {}

    assert not bool(exectx.errors)

    await nf(exectx, reqctx, parent_marshalled=prm)

    assert bool(exectx.errors)

    assert exectx.errors[0] is not raw
    assert isinstance(exectx.errors[0], GraphQLError)
    assert exectx.errors[0].coerce_value() == {
        "message": "ninja",
        "path": None,
        "locations": [],
    }
Example #11
0
    def _on_field_in(
        self,
        element: _VisitorElementField,
        *_args,
        type_cond_depth: int = -1,
        directives: List[Dict[str, Any]] = None,
        **_kwargs,
    ) -> None:
        # pylint: disable=too-many-locals
        type_cond = self._internal_ctx.compute_type_cond(type_cond_depth)
        parent_type = self._get_parent_type(self._internal_ctx.node)

        try:
            field = self.schema.get_field_by_name(
                str(parent_type) + "." + element.name)
        except UnknownSchemaFieldResolver as e:
            try:
                if type_cond is None:
                    raise
                field = self.schema.get_field_by_name(
                    str(type_cond) + "." + element.name)
            except UnknownSchemaFieldResolver as e:
                e.path = self._internal_ctx.field_path[:] + [element.name]
                e.locations = [element.get_location()]
                self._add_exception(e)
                return

        if field.is_leaf and element.get_selection_set_size() > 0:
            self._add_exception(
                NotAnObjectType(
                    message=
                    f"field < {field.name} > is a leaf and thus can't have a selection set",
                    path=self._internal_ctx.field_path[:] + [element.name],
                    locations=[element.get_location()],
                ))
            return

        if not field.is_leaf and element.get_selection_set_size() < 1:
            self._add_exception(
                NotALeafType(
                    message=
                    f"field < {field.name} > is not a leaf and thus must have a selection set",
                    path=self._internal_ctx.field_path[:] + [element.name],
                    locations=[element.get_location()],
                ))
            return

        self._internal_ctx.move_in_field(element, field)

        node = NodeField(
            element.name,
            self.schema,
            field.resolver,
            element.get_location(),
            self._internal_ctx.field_path[:],
            type_cond,
            element.get_alias(),
            subscribe=field.subscribe,
        )

        if self._internal_ctx.inline_fragment_info:
            for (directive
                 ) in self._internal_ctx.inline_fragment_info.directives:
                node.add_directive(directive)

        if directives:
            for directive in directives:
                node.add_directive(directive)

        node.set_parent(self._internal_ctx.node)
        if self._internal_ctx.node:
            self._internal_ctx.node.add_child(node)

        self._internal_ctx.node = node

        if self._internal_ctx.depth == 1:
            self.operations[self._internal_ctx.operation.name].children.append(
                node)
Example #12
0
def test_parser_node_nodefield_bubble_error():
    from tartiflette.parser.nodes.field import NodeField

    fe = Mock()
    fe.cant_be_null = False

    nf = NodeField("Rb", None, fe, None, None, None, None)

    nf.marshalled = {}
    nf.parent = None

    nf.bubble_error()

    assert nf.marshalled is None

    nf.marshalled = {}
    nf.parent = Mock()
    nf.parent.marshalled = {"Rb": "Lol"}
    nf.parent.bubble_error = Mock()

    nf.bubble_error()

    assert "Rb" in nf.parent.marshalled
    assert nf.parent.marshalled["Rb"] is None

    fe.cant_be_null = True

    nf.bubble_error()

    assert nf.parent.bubble_error.called

    nf.parent = None
    nf.marshalled = {}

    nf.bubble_error()

    assert nf.marshalled is None