# Path: django_spanner/_opentelemetry_tracing.py
#     HAS_OPENTELEMETRY_INSTALLED = True
#     HAS_OPENTELEMETRY_INSTALLED = False
# def trace_call(name, connection, extra_attributes=None):
# 
# Path: tests/_helpers.py
# class OpenTelemetryBase(unittest.TestCase):
#     @classmethod
#     def setUpClass(cls):
#         if HAS_OPENTELEMETRY_INSTALLED:
#             use_test_ot_exporter()
#             cls.ot_exporter = get_test_ot_exporter()
# 
#     def tearDown(self):
#         if HAS_OPENTELEMETRY_INSTALLED:
#             self.ot_exporter.clear()
# 
#     def assertNoSpans(self):
#         if HAS_OPENTELEMETRY_INSTALLED:
#             span_list = self.ot_exporter.get_finished_spans()
#             self.assertEqual(len(span_list), 0)
# 
#     def assertSpanAttributes(
#         self, name, status=StatusCode.OK, attributes=None, span=None
#     ):
#         if HAS_OPENTELEMETRY_INSTALLED:
#             if not span:
#                 span_list = self.ot_exporter.get_finished_spans()
#                 self.assertEqual(len(span_list), 1)
#                 span = span_list[0]
# 
#             self.assertEqual(span.name, name)
#             self.assertEqual(span.status.status_code, status)
#             self.assertEqual(dict(span.attributes), attributes)
# 
# HAS_OPENTELEMETRY_INSTALLED = True


import importlib
import mock
import unittest
import sys
import os
    import grpc
    from opentelemetry import trace as trace_api
    from opentelemetry.trace.status import StatusCode
from google.api_core.exceptions import GoogleAPICallError
from django_spanner import _opentelemetry_tracing
from tests._helpers import OpenTelemetryBase, HAS_OPENTELEMETRY_INSTALLED
    from django_spanner.base import DatabaseWrapper
                    from google.api_core.exceptions import InvalidArgument
DATABASE_ID = "database_id"
OPTIONS = {"option": "dummy"}


def _make_rpc_error(error_cls, trailing_metadata=None):

    grpc_error = mock.create_autospec(grpc.Call, instance=True)
    grpc_error.trailing_metadata.return_value = trailing_metadata
    return error_cls("error", errors=(grpc_error,))


def _make_connection():

    settings_dict = {
        "PROJECT": PROJECT,
        "INSTANCE": INSTANCE_ID,
        "NAME": DATABASE_ID,
        "OPTIONS": OPTIONS,
    }
    return DatabaseWrapper(settings_dict)


# Skip all of these tests if we don't have OpenTelemetry
if HAS_OPENTELEMETRY_INSTALLED:

    class TestNoTracing(unittest.TestCase):
        def setUp(self):
            self._temp_opentelemetry = sys.modules["opentelemetry"]

            sys.modules["opentelemetry"] = None

# Path: django_spanner/_opentelemetry_tracing.py
#     HAS_OPENTELEMETRY_INSTALLED = True
#     HAS_OPENTELEMETRY_INSTALLED = False
# def trace_call(name, connection, extra_attributes=None):
# 
# Path: tests/_helpers.py
# class OpenTelemetryBase(unittest.TestCase):
#     @classmethod
#     def setUpClass(cls):
#         if HAS_OPENTELEMETRY_INSTALLED:
#             use_test_ot_exporter()
#             cls.ot_exporter = get_test_ot_exporter()
# 
#     def tearDown(self):
#         if HAS_OPENTELEMETRY_INSTALLED:
#             self.ot_exporter.clear()
# 
#     def assertNoSpans(self):
#         if HAS_OPENTELEMETRY_INSTALLED:
#             span_list = self.ot_exporter.get_finished_spans()
#             self.assertEqual(len(span_list), 0)
# 
#     def assertSpanAttributes(
#         self, name, status=StatusCode.OK, attributes=None, span=None
#     ):
#         if HAS_OPENTELEMETRY_INSTALLED:
#             if not span:
#                 span_list = self.ot_exporter.get_finished_spans()
#                 self.assertEqual(len(span_list), 1)
#                 span = span_list[0]
# 
#             self.assertEqual(span.name, name)
#             self.assertEqual(span.status.status_code, status)
#             self.assertEqual(dict(span.attributes), attributes)
# 
# HAS_OPENTELEMETRY_INSTALLED = True


# Path: tests/unit/django_spanner/test__opentelemetry_tracing.py
import importlib
import mock
import unittest
import sys
import os
    import grpc
    from opentelemetry import trace as trace_api
    from opentelemetry.trace.status import StatusCode
from google.api_core.exceptions import GoogleAPICallError
from django_spanner import _opentelemetry_tracing
from tests._helpers import OpenTelemetryBase, HAS_OPENTELEMETRY_INSTALLED
    from django_spanner.base import DatabaseWrapper
                    from google.api_core.exceptions import InvalidArgument

DATABASE_ID = "database_id"
OPTIONS = {"option": "dummy"}


def _make_rpc_error(error_cls, trailing_metadata=None):

    grpc_error = mock.create_autospec(grpc.Call, instance=True)
    grpc_error.trailing_metadata.return_value = trailing_metadata
    return error_cls("error", errors=(grpc_error,))


def _make_connection():

    settings_dict = {
        "PROJECT": PROJECT,
        "INSTANCE": INSTANCE_ID,
        "NAME": DATABASE_ID,
        "OPTIONS": OPTIONS,
    }
    return DatabaseWrapper(settings_dict)


# Skip all of these tests if we don't have OpenTelemetry
if HAS_OPENTELEMETRY_INSTALLED:

    class TestNoTracing(unittest.TestCase):
        def setUp(self):
            self._temp_opentelemetry = sys.modules["opentelemetry"]

            sys.modules["opentelemetry"] = None

            importlib.reload(_opentelemetry_tracing)