Add tests for opentracing
+from unittest.mock import ANY
+
import pytest
+from opentracing.ext import tags
from ariadne import graphql
-from ariadne.contrib.tracing.opentracing import OpenTracingExtension
+from ariadne.contrib.tracing.opentracing import (
+ OpenTracingExtension,
+ opentracing_extension,
+)
+
+
+@pytest.fixture
+def global_tracer_mock(mocker):
+ return mocker.patch("ariadne.contrib.tracing.opentracing.global_tracer")
+
+
+@pytest.fixture
+def active_span_mock(global_tracer_mock):
+ return global_tracer_mock.return_value.start_active_span.return_value
@pytest.mark.asyncio
async def test_opentracing_extension_causes_no_errors_in_query_execution(schema):
_, result = await graphql(
schema,
- {"query": '{ status hello(name:"bob") }'},
+ {"query": '{ status hello(name: "Bob") }'},
extensions=[OpenTracingExtension],
)
- assert result == {"data": {"hello": "Hello, bob!", "status": True}}
+ assert result == {"data": {"hello": "Hello, Bob!", "status": True}}
+
+
+@pytest.mark.asyncio
+async def test_opentracing_extension_uses_global_tracer(schema, global_tracer_mock):
+ await graphql(
+ schema,
+ {"query": '{ status hello(name: "Bob") }'},
+ extensions=[OpenTracingExtension],
+ )
+ global_tracer_mock.assert_called_once()
+
+
+@pytest.mark.asyncio
+async def test_opentracing_extension_creates_span_for_query_root(
+ schema, global_tracer_mock
+):
+ await graphql(schema, {"query": "{ status }"}, extensions=[OpenTracingExtension])
+ global_tracer_mock.return_value.start_active_span.assert_any_call("GraphQL Query")
+
+
+@pytest.mark.asyncio
+async def test_opentracing_extension_creates_span_for_field(schema, global_tracer_mock):
+ await graphql(schema, {"query": "{ status }"}, extensions=[OpenTracingExtension])
+ global_tracer_mock.return_value.start_active_span.assert_any_call("status")
+
+
+@pytest.mark.asyncio
+async def test_opentracing_extension_sets_graphql_component_tag_on_root_span(
+ schema, active_span_mock
+):
+ await graphql(
+ schema,
+ {"query": '{ status hello(name: "Bob") }'},
+ extensions=[OpenTracingExtension],
+ )
+ active_span_mock.span.set_tag.assert_called_once_with(tags.COMPONENT, "graphql")
+
+
+@pytest.mark.asyncio
+async def test_opentracing_extension_calls_custom_arg_filter(schema, mocker):
+ arg_filter = mocker.Mock(return_value={})
+ await graphql(
+ schema,
+ {"query": '{ hello(name: "Bob") }'},
+ extensions=[opentracing_extension(arg_filter=arg_filter)],
+ )
+ arg_filter.assert_called_once_with({"name": "Bob"}, ANY)
