# Path: slo_generator/utils.py
# def get_backend_cls(backend):
#     """Get backend class.
# 
#     Args:
#         backend (str): Exporter type.
# 
#     Returns:
#         class: Backend class.
#     """
#     expected_type = "Backend"
#     return import_cls(backend, expected_type)
# 
# def get_exporter_cls(exporter):
#     """Get exporter class.
# 
#     Args:
#         exporter (str): Backend type.
# 
#     Returns:
#         class: Exporter class.
#     """
#     expected_type = "Exporter"
#     return import_cls(exporter, expected_type)
# 
# def get_human_time(timestamp, timezone=None):
#     """Get human-readable timestamp from UNIX UTC timestamp.
# 
#     Args:
#         timestamp (int): UNIX UTC timestamp.
#         timezone (optional): Explicit timezone (e.g: "America/Chicago").
# 
#     Returns:
#         str: Formatted human-readable date in ISO format (UTC), with
#              time zone added.
# 
#     Example:
#         >>> get_human_time(1565092435, timezone='Europe/Paris')
#         >>> 2019-08-06T11:53:55.000000+02:00
#         which corresponds to the UTC time appended the timezone format
#         to help with display and retrieval of the date localized.
#     """
#     if timezone is not None:  # get timezone from arg
#         to_zone = tz.gettz(timezone)
#     else:  # auto-detect locale
#         to_zone = tz.tzlocal()
#     dt_utc = datetime.utcfromtimestamp(timestamp)
#     dt_tz = dt_utc.replace(tzinfo=to_zone)
#     timeformat = '%Y-%m-%dT%H:%M:%S.%f%z'
#     date_str = datetime.strftime(dt_tz, timeformat)
#     core_str = date_str[:-2]
#     tz_str = date_str[-2:]
#     date_str = f'{core_str}:{tz_str}'
#     return date_str
# 
# def import_dynamic(package, name, prefix="class"):
#     """Import class or method dynamically from package and name.
# 
#     Args:
#         package: Where the method or class is located in the import path.
#         name: Name of method or class.
# 
#     Returns:
#         obj: Imported class or method object.
#     """
#     try:
#         return getattr(importlib.import_module(package), name)
#     except Exception as exception:  # pylint: disable=W0703
#         dep = package.split('.')[-1]
#         warnings.warn(
#             f'{prefix} "{package}.{name}" not found.\nPlease ensure that:\n'
#             f'1. Package and class name are valid.\n'
#             f'2. Extra dependency {dep} is installed. If not, install it '
#             f'locally with "pip install slo-generator[{dep}]" or remotely '
#             f'by adding "slo-generator[{dep}]" to your requirements.txt.',
#             ImportWarning)
#         if DEBUG:
#             LOGGER.debug(exception, exc_info=True)
#         return None


import unittest
from slo_generator.utils import (get_backend_cls, get_exporter_cls,
                                 get_human_time, import_dynamic)
    def test_get_backend_cls(self):
        res1 = get_backend_cls("CloudMonitoring")
        res2 = get_backend_cls("Prometheus")
        self.assertEqual(res1.__name__, "CloudMonitoringBackend")
        self.assertEqual(res1.__module__,
                         "slo_generator.backends.cloud_monitoring")
        self.assertEqual(res2.__name__, "PrometheusBackend")
        self.assertEqual(res2.__module__, "slo_generator.backends.prometheus")
        with self.assertWarns(ImportWarning):
            get_backend_cls("UndefinedBackend")

    def test_get_backend_dynamic_cls(self):
        res1 = get_backend_cls("pathlib.Path")
        self.assertEqual(res1.__name__, "Path")
        self.assertEqual(res1.__module__, "pathlib")
        with self.assertWarns(ImportWarning):
            get_exporter_cls("foo.bar.DoesNotExist")

    def test_get_exporter_cls(self):
        res1 = get_exporter_cls("CloudMonitoring")
        res2 = get_exporter_cls("Pubsub")
        res3 = get_exporter_cls("Bigquery")
        self.assertEqual(res1.__name__, "CloudMonitoringExporter")
        self.assertEqual(res1.__module__,
                         "slo_generator.exporters.cloud_monitoring")
        self.assertEqual(res2.__name__, "PubsubExporter")
        self.assertEqual(res2.__module__, "slo_generator.exporters.pubsub")
        self.assertEqual(res3.__name__, "BigqueryExporter")
        self.assertEqual(res3.__module__, "slo_generator.exporters.bigquery")
        with self.assertWarns(ImportWarning):

# Path: slo_generator/utils.py
# def get_backend_cls(backend):
#     """Get backend class.
# 
#     Args:
#         backend (str): Exporter type.
# 
#     Returns:
#         class: Backend class.
#     """
#     expected_type = "Backend"
#     return import_cls(backend, expected_type)
# 
# def get_exporter_cls(exporter):
#     """Get exporter class.
# 
#     Args:
#         exporter (str): Backend type.
# 
#     Returns:
#         class: Exporter class.
#     """
#     expected_type = "Exporter"
#     return import_cls(exporter, expected_type)
# 
# def get_human_time(timestamp, timezone=None):
#     """Get human-readable timestamp from UNIX UTC timestamp.
# 
#     Args:
#         timestamp (int): UNIX UTC timestamp.
#         timezone (optional): Explicit timezone (e.g: "America/Chicago").
# 
#     Returns:
#         str: Formatted human-readable date in ISO format (UTC), with
#              time zone added.
# 
#     Example:
#         >>> get_human_time(1565092435, timezone='Europe/Paris')
#         >>> 2019-08-06T11:53:55.000000+02:00
#         which corresponds to the UTC time appended the timezone format
#         to help with display and retrieval of the date localized.
#     """
#     if timezone is not None:  # get timezone from arg
#         to_zone = tz.gettz(timezone)
#     else:  # auto-detect locale
#         to_zone = tz.tzlocal()
#     dt_utc = datetime.utcfromtimestamp(timestamp)
#     dt_tz = dt_utc.replace(tzinfo=to_zone)
#     timeformat = '%Y-%m-%dT%H:%M:%S.%f%z'
#     date_str = datetime.strftime(dt_tz, timeformat)
#     core_str = date_str[:-2]
#     tz_str = date_str[-2:]
#     date_str = f'{core_str}:{tz_str}'
#     return date_str
# 
# def import_dynamic(package, name, prefix="class"):
#     """Import class or method dynamically from package and name.
# 
#     Args:
#         package: Where the method or class is located in the import path.
#         name: Name of method or class.
# 
#     Returns:
#         obj: Imported class or method object.
#     """
#     try:
#         return getattr(importlib.import_module(package), name)
#     except Exception as exception:  # pylint: disable=W0703
#         dep = package.split('.')[-1]
#         warnings.warn(
#             f'{prefix} "{package}.{name}" not found.\nPlease ensure that:\n'
#             f'1. Package and class name are valid.\n'
#             f'2. Extra dependency {dep} is installed. If not, install it '
#             f'locally with "pip install slo-generator[{dep}]" or remotely '
#             f'by adding "slo-generator[{dep}]" to your requirements.txt.',
#             ImportWarning)
#         if DEBUG:
#             LOGGER.debug(exception, exc_info=True)
#         return None


# Path: tests/unit/test_utils.py
import unittest
from slo_generator.utils import (get_backend_cls, get_exporter_cls,
                                 get_human_time, import_dynamic)

    def test_get_backend_cls(self):
        res1 = get_backend_cls("CloudMonitoring")
        res2 = get_backend_cls("Prometheus")
        self.assertEqual(res1.__name__, "CloudMonitoringBackend")
        self.assertEqual(res1.__module__,
                         "slo_generator.backends.cloud_monitoring")
        self.assertEqual(res2.__name__, "PrometheusBackend")
        self.assertEqual(res2.__module__, "slo_generator.backends.prometheus")
        with self.assertWarns(ImportWarning):
            get_backend_cls("UndefinedBackend")

    def test_get_backend_dynamic_cls(self):
        res1 = get_backend_cls("pathlib.Path")
        self.assertEqual(res1.__name__, "Path")
        self.assertEqual(res1.__module__, "pathlib")
        with self.assertWarns(ImportWarning):
            get_exporter_cls("foo.bar.DoesNotExist")

    def test_get_exporter_cls(self):
        res1 = get_exporter_cls("CloudMonitoring")
        res2 = get_exporter_cls("Pubsub")
        res3 = get_exporter_cls("Bigquery")
        self.assertEqual(res1.__name__, "CloudMonitoringExporter")
        self.assertEqual(res1.__module__,
                         "slo_generator.exporters.cloud_monitoring")
        self.assertEqual(res2.__name__, "PubsubExporter")
        self.assertEqual(res2.__module__, "slo_generator.exporters.pubsub")
        self.assertEqual(res3.__name__, "BigqueryExporter")
        self.assertEqual(res3.__module__, "slo_generator.exporters.bigquery")
        with self.assertWarns(ImportWarning):

            get_exporter_cls("UndefinedExporter")