Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log graphql errors while testing #5046

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/tests/benchmark/test_graphql_query.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os

import pytest
from graphql import graphql

from infrahub.core import registry
from infrahub.core.branch import Branch
Expand All @@ -20,6 +19,7 @@
from infrahub.core.utils import delete_all_nodes
from infrahub.database import InfrahubDatabase
from infrahub.graphql.initialization import prepare_graphql_params
from tests.helpers.graphql import graphql
from tests.test_data.dataset04 import load_data

NBR_WARMUP = int(os.getenv("INFRAHUB_BENCHMARK_NBR_WARMUP", "5"))
Expand Down
52 changes: 50 additions & 2 deletions backend/tests/helpers/graphql.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,66 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, Callable

from graphql import ExecutionResult, graphql
from graphql import (
ExecutionContext,
ExecutionResult,
GraphQLFieldResolver,
GraphQLSchema,
GraphQLTypeResolver,
Middleware,
Source,
)
from graphql import graphql as graphql_core

from infrahub.core.branch import Branch
from infrahub.graphql.initialization import prepare_graphql_params
from infrahub.log import get_logger
from infrahub.services import InfrahubServices, services

if TYPE_CHECKING:
from infrahub.auth import AccountSession
from infrahub.database import InfrahubDatabase


async def graphql(
schema: GraphQLSchema,
source: str | Source,
root_value: Any = None,
context_value: Any = None,
variable_values: dict[str, Any] | None = None,
operation_name: str | None = None,
field_resolver: GraphQLFieldResolver | None = None,
type_resolver: GraphQLTypeResolver | None = None,
middleware: Middleware | None = None,
execution_context_class: type[ExecutionContext] | None = None,
is_awaitable: Callable[[Any], bool] | None = None,
) -> ExecutionResult:
"""
Call `graphql` from graphql core package, and log potential errors to have full stack trace.
"""

result = await graphql_core(
schema=schema,
source=source,
root_value=root_value,
context_value=context_value,
variable_values=variable_values,
operation_name=operation_name,
field_resolver=field_resolver,
type_resolver=type_resolver,
middleware=middleware,
execution_context_class=execution_context_class,
is_awaitable=is_awaitable,
)
if result.errors is not None:
log = get_logger()
for error in result.errors:
if error.original_error:
log.error("Unhandled exception occurred in resolvers", exc_info=error.original_error)
return result


async def graphql_mutation(
query: str,
db: InfrahubDatabase,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
from typing import TYPE_CHECKING

import pytest
from graphql import graphql

from infrahub.core import registry
from infrahub.core.initialization import create_branch
from infrahub.core.manager import NodeManager
from infrahub.core.node import Node
from infrahub.graphql.initialization import prepare_graphql_params
from tests.helpers.graphql import graphql

from .base import TestIpamReconcileBase

Expand Down
2 changes: 1 addition & 1 deletion backend/tests/integration/ipam/test_ipam_utilization.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
from typing import TYPE_CHECKING

import pytest
from graphql import graphql

from infrahub.core import registry
from infrahub.core.initialization import create_branch, create_ipam_namespace, get_default_ipnamespace
from infrahub.core.ipam.utilization import PrefixUtilizationGetter
from infrahub.core.manager import NodeManager
from infrahub.core.node import Node
from infrahub.graphql.initialization import prepare_graphql_params
from tests.helpers.graphql import graphql
from tests.helpers.test_app import TestInfrahubApp

if TYPE_CHECKING:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
from graphql import graphql
from infrahub_sdk.client import InfrahubClient

from infrahub.core import registry
Expand All @@ -9,6 +8,7 @@
from infrahub.core.schema.node_schema import NodeSchema
from infrahub.database import InfrahubDatabase
from infrahub.graphql.initialization import prepare_graphql_params
from tests.helpers.graphql import graphql
from tests.helpers.schema import load_schema
from tests.helpers.test_app import TestInfrahubApp

Expand Down
4 changes: 1 addition & 3 deletions backend/tests/unit/graphql/mutations/test_branch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from graphql import graphql

from infrahub.core import registry
from infrahub.core.branch import Branch
from infrahub.core.initialization import create_branch
Expand All @@ -9,7 +7,7 @@
from infrahub.services import InfrahubServices, services
from infrahub.services.adapters.workflow.local import WorkflowLocalExecution
from tests.adapters.message_bus import BusRecorder
from tests.helpers.graphql import graphql_mutation
from tests.helpers.graphql import graphql, graphql_mutation
from tests.helpers.test_app import TestInfrahubApp
from tests.helpers.utils import init_global_service

Expand Down
3 changes: 1 addition & 2 deletions backend/tests/unit/graphql/mutations/test_ipam.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import ipaddress

from graphql import graphql

from infrahub.core import registry
from infrahub.core.branch import Branch
from infrahub.core.constants import InfrahubKind
from infrahub.core.node import Node
from infrahub.core.schema.schema_branch import SchemaBranch
from infrahub.database import InfrahubDatabase
from infrahub.graphql.initialization import prepare_graphql_params
from tests.helpers.graphql import graphql

CREATE_IPPREFIX = """
mutation CreatePrefix($prefix: String!) {
Expand Down
4 changes: 1 addition & 3 deletions backend/tests/unit/graphql/mutations/test_proposed_change.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from uuid import uuid4

from graphql import graphql

from infrahub.auth import AccountSession
from infrahub.core.branch import Branch
from infrahub.core.constants import CheckType, InfrahubKind
Expand All @@ -15,7 +13,7 @@
from infrahub.services import InfrahubServices
from infrahub.services.adapters.workflow.local import WorkflowLocalExecution
from tests.adapters.message_bus import BusRecorder
from tests.helpers.graphql import graphql_mutation
from tests.helpers.graphql import graphql, graphql_mutation
from tests.helpers.utils import init_global_service

CREATE_PROPOSED_CHANGE = """
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
from graphql import graphql

from infrahub.core import registry
from infrahub.core.branch import Branch
Expand All @@ -11,6 +10,7 @@
from infrahub.core.schema.schema_branch import SchemaBranch
from infrahub.database import InfrahubDatabase
from infrahub.graphql.initialization import prepare_graphql_params
from tests.helpers.graphql import graphql
from tests.helpers.schema import TICKET, load_schema


Expand Down
2 changes: 1 addition & 1 deletion backend/tests/unit/graphql/mutations/test_schema.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import pytest
from graphql import graphql

from infrahub.core.node import Node
from infrahub.database import InfrahubDatabase
from infrahub.exceptions import ValidationError
from infrahub.graphql.initialization import prepare_graphql_params
from infrahub.graphql.mutations.schema import validate_kind, validate_kind_dropdown, validate_kind_enum
from tests.helpers.graphql import graphql


async def test_delete_last_dropdown_option(db: InfrahubDatabase, default_branch, choices_schema):
Expand Down
3 changes: 1 addition & 2 deletions backend/tests/unit/graphql/mutations/test_task.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from graphql import graphql

from infrahub.core.node import Node
from infrahub.database import InfrahubDatabase
from infrahub.graphql.initialization import prepare_graphql_params
from tests.helpers.graphql import graphql

CREATE_TASK = """
mutation CreateTask(
Expand Down
3 changes: 1 addition & 2 deletions backend/tests/unit/graphql/mutations/test_update_generic.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from graphql import graphql

from infrahub.core.branch import Branch
from infrahub.core.node import Node
from infrahub.database import InfrahubDatabase
from infrahub.graphql.initialization import prepare_graphql_params
from tests.helpers.graphql import graphql


async def test_display_label_generic(db: InfrahubDatabase, animal_person_schema, branch: Branch):
Expand Down
3 changes: 1 addition & 2 deletions backend/tests/unit/graphql/profiles/test_mutation_create.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from graphql import graphql

from infrahub.core.manager import NodeManager
from infrahub.database import InfrahubDatabase
from infrahub.graphql.initialization import prepare_graphql_params
from tests.helpers.graphql import graphql


async def test_create_profile(db: InfrahubDatabase, default_branch, car_person_schema):
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/unit/graphql/profiles/test_query.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
from graphql import graphql

from infrahub.core import registry
from infrahub.core.branch import Branch
Expand All @@ -10,6 +9,7 @@
from infrahub.core.schema.generic_schema import GenericSchema
from infrahub.database import InfrahubDatabase
from infrahub.graphql.initialization import prepare_graphql_params
from tests.helpers.graphql import graphql


@pytest.fixture
Expand Down
3 changes: 1 addition & 2 deletions backend/tests/unit/graphql/queries/test_branch.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import operator

from graphql import graphql

from infrahub.core.branch import Branch
from infrahub.database import InfrahubDatabase
from infrahub.graphql.initialization import prepare_graphql_params
from infrahub.services import services
from tests.helpers.graphql import graphql
from tests.helpers.test_app import TestInfrahubApp


Expand Down
2 changes: 1 addition & 1 deletion backend/tests/unit/graphql/queries/test_ipam.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
from graphql import graphql

from infrahub.core import registry
from infrahub.core.branch import Branch
Expand All @@ -8,6 +7,7 @@
from infrahub.core.schema.schema_branch import SchemaBranch
from infrahub.database import InfrahubDatabase
from infrahub.graphql.initialization import prepare_graphql_params
from tests.helpers.graphql import graphql


@pytest.fixture
Expand Down
3 changes: 1 addition & 2 deletions backend/tests/unit/graphql/queries/test_list_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from typing import TYPE_CHECKING
from uuid import uuid4

from graphql import graphql

from infrahub.auth import AccountSession, AuthType
from infrahub.core.account import ObjectPermission
from infrahub.core.constants import InfrahubKind, PermissionAction
Expand All @@ -14,6 +12,7 @@
from infrahub.graphql.initialization import prepare_graphql_params
from infrahub.permissions.constants import BranchRelativePermissionDecision, PermissionDecisionFlag
from infrahub.permissions.local_backend import LocalPermissionBackend
from tests.helpers.graphql import graphql

if TYPE_CHECKING:
from infrahub.core.branch import Branch
Expand Down
3 changes: 1 addition & 2 deletions backend/tests/unit/graphql/queries/test_relationship.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from graphql import graphql

from infrahub.core.branch import Branch
from infrahub.database import InfrahubDatabase
from infrahub.graphql.initialization import prepare_graphql_params
from tests.helpers.graphql import graphql


async def test_relationship(
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/unit/graphql/queries/test_resource_pool.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
from graphql import graphql

from infrahub.core import registry
from infrahub.core.branch import Branch
Expand All @@ -14,6 +13,7 @@
from infrahub.core.schema.schema_branch import SchemaBranch
from infrahub.database import InfrahubDatabase
from infrahub.graphql.initialization import prepare_graphql_params
from tests.helpers.graphql import graphql
from tests.helpers.schema import TICKET, load_schema


Expand Down
2 changes: 1 addition & 1 deletion backend/tests/unit/graphql/queries/test_search.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import pytest
from graphql import graphql

from infrahub.core.branch import Branch
from infrahub.core.constants import InfrahubKind
from infrahub.core.node import Node
from infrahub.database import InfrahubDatabase
from infrahub.graphql.initialization import prepare_graphql_params
from infrahub.graphql.queries.search import _collapse_ipv6
from tests.helpers.graphql import graphql

SEARCH_QUERY = """
query ($search: String!) {
Expand Down
3 changes: 2 additions & 1 deletion backend/tests/unit/graphql/queries/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from uuid import uuid4

import pytest
from graphql import ExecutionResult, graphql
from graphql import ExecutionResult
from infrahub_sdk.graphql import Query
from prefect.artifacts import ArtifactRequest
from prefect.client.orchestration import PrefectClient, get_client
Expand All @@ -16,6 +16,7 @@
from infrahub.graphql.initialization import prepare_graphql_params
from infrahub.tasks.dummy import dummy_flow, dummy_flow_broken
from infrahub.workflows.constants import TAG_NAMESPACE, WorkflowTag
from tests.helpers.graphql import graphql

CREATE_TASK = """
mutation CreateTask(
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/unit/graphql/test_core_account.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import bcrypt
from graphql import graphql

from infrahub.auth import AccountSession, AuthType
from infrahub.core import registry
Expand All @@ -10,6 +9,7 @@
from infrahub.database import InfrahubDatabase
from infrahub.graphql.initialization import prepare_graphql_params
from infrahub.permissions.local_backend import LocalPermissionBackend
from tests.helpers.graphql import graphql


async def test_everyone_can_update_password(db: InfrahubDatabase, default_branch: Branch, first_account):
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/unit/graphql/test_diff_tree_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from unittest.mock import AsyncMock

import pytest
from graphql import graphql

from infrahub.core.branch import Branch
from infrahub.core.diff.coordinator import DiffCoordinator
Expand All @@ -20,6 +19,7 @@
from infrahub.dependencies.registry import get_component_registry
from infrahub.graphql.enums import ConflictSelection as GraphQLConfictSelection
from infrahub.graphql.initialization import prepare_graphql_params
from tests.helpers.graphql import graphql

ADDED_ACTION = "ADDED"
UPDATED_ACTION = "UPDATED"
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/unit/graphql/test_graphql_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import pytest
from deepdiff import DeepDiff
from graphql import graphql

from infrahub import __version__, config
from infrahub.core import registry
Expand All @@ -15,6 +14,7 @@
from infrahub.core.timestamp import Timestamp
from infrahub.database import InfrahubDatabase
from infrahub.graphql.initialization import prepare_graphql_params
from tests.helpers.graphql import graphql


async def test_info_query(db: InfrahubDatabase, default_branch: Branch, criticality_schema: NodeSchema):
Expand Down
Loading
Loading