# Path: grafana_dashboards/components/base.py
# class JsonListGenerator(JsonGenerator):
#     def __init__(self, data, registry, item_base_class):
#         super(JsonListGenerator, self).__init__(data, registry)
#         self.component_item_types = [get_component_type(clazz) for clazz in _get_subclasses(item_base_class)]
# 
#     def gen_json_from_data(self, data, context):
#         super(JsonListGenerator, self).gen_json_from_data(data, context)
#         result_list = []
#         for items in data:
#             self.gen_item_json(items, result_list)
#         return result_list
# 
#     def gen_item_json(self, items, result_list):
#         if isinstance(items, basestring):
#             # this is component without context
#             result_list += self.registry.get_component(type(self), items).gen_json()
#         else:
#             self._gen_item_json_with_context(items, result_list)
# 
#     def _gen_item_json_with_context(self, items, result_list):
#         # TODO add check for dictionary
#         for (item_type, item_data) in items.items():
#             if item_type not in self.component_item_types:
#                 # this is named component with context
#                 for context in Context.create_context(item_data, get_placeholders(item_type)):
#                     result_list += self.registry.get_component(type(self), item_type).gen_json(context)
#             else:
#                 # this is inplace defined component
#                 item = self.registry.create_component(item_type, {item_type: item_data}).gen_json()
#                 if isinstance(item, list):
#                     result_list += item
#                 else:
#                     result_list.append(item)
# 
# Path: grafana_dashboards/errors.py
# class UnregisteredComponentError(DashboardBuilderException):
#     pass


import inspect
import json
import os
import mock as mock
import yaml
import grafana_dashboards.common
from grafana_dashboards.components import *  # NOQA
from grafana_dashboards.components.base import JsonListGenerator
from grafana_dashboards.errors import UnregisteredComponentError
def pytest_generate_tests(metafunc):
    fixtures = []
    ids = []
    for (component, test, config, output) in load_test_fixtures():
        fixtures.append((component, config, output))
        ids.append('%s;%s' % (grafana_dashboards.common.get_component_type(component), test))
    metafunc.parametrize('component,config,expected', fixtures, ids=ids)


def load_test_fixtures():
    for component in base.get_generators():  # NOQA
        component_type = grafana_dashboards.common.get_component_type(component)
        dirname = os.path.join(os.path.dirname(os.path.abspath(__file__)), component_type)
        if not os.path.isdir(dirname):
            continue
        for f in os.listdir(dirname):
            if not f.endswith('.yaml'):
                continue
            filename = f[:-5]
            with open(os.path.join(dirname, '%s.yaml' % filename), 'r') as fp:
                config = yaml.load(fp, Loader=yaml.FullLoader)
            with open(os.path.join(dirname, '%s.json' % filename), 'r') as fp:
                output = json.load(fp)
            yield component, filename, config, output


def test_component(component, config, expected):
    with mock.patch('grafana_dashboards.components.base.ComponentRegistry') as registry:
        def create_component(component_type, data):
            gen = mock.Mock()

# Path: grafana_dashboards/components/base.py
# class JsonListGenerator(JsonGenerator):
#     def __init__(self, data, registry, item_base_class):
#         super(JsonListGenerator, self).__init__(data, registry)
#         self.component_item_types = [get_component_type(clazz) for clazz in _get_subclasses(item_base_class)]
# 
#     def gen_json_from_data(self, data, context):
#         super(JsonListGenerator, self).gen_json_from_data(data, context)
#         result_list = []
#         for items in data:
#             self.gen_item_json(items, result_list)
#         return result_list
# 
#     def gen_item_json(self, items, result_list):
#         if isinstance(items, basestring):
#             # this is component without context
#             result_list += self.registry.get_component(type(self), items).gen_json()
#         else:
#             self._gen_item_json_with_context(items, result_list)
# 
#     def _gen_item_json_with_context(self, items, result_list):
#         # TODO add check for dictionary
#         for (item_type, item_data) in items.items():
#             if item_type not in self.component_item_types:
#                 # this is named component with context
#                 for context in Context.create_context(item_data, get_placeholders(item_type)):
#                     result_list += self.registry.get_component(type(self), item_type).gen_json(context)
#             else:
#                 # this is inplace defined component
#                 item = self.registry.create_component(item_type, {item_type: item_data}).gen_json()
#                 if isinstance(item, list):
#                     result_list += item
#                 else:
#                     result_list.append(item)
# 
# Path: grafana_dashboards/errors.py
# class UnregisteredComponentError(DashboardBuilderException):
#     pass


# Path: tests/grafana_dashboards/components/test_components.py
import inspect
import json
import os
import mock as mock
import yaml
import grafana_dashboards.common
from grafana_dashboards.components import *  # NOQA
from grafana_dashboards.components.base import JsonListGenerator
from grafana_dashboards.errors import UnregisteredComponentError

def pytest_generate_tests(metafunc):
    fixtures = []
    ids = []
    for (component, test, config, output) in load_test_fixtures():
        fixtures.append((component, config, output))
        ids.append('%s;%s' % (grafana_dashboards.common.get_component_type(component), test))
    metafunc.parametrize('component,config,expected', fixtures, ids=ids)


def load_test_fixtures():
    for component in base.get_generators():  # NOQA
        component_type = grafana_dashboards.common.get_component_type(component)
        dirname = os.path.join(os.path.dirname(os.path.abspath(__file__)), component_type)
        if not os.path.isdir(dirname):
            continue
        for f in os.listdir(dirname):
            if not f.endswith('.yaml'):
                continue
            filename = f[:-5]
            with open(os.path.join(dirname, '%s.yaml' % filename), 'r') as fp:
                config = yaml.load(fp, Loader=yaml.FullLoader)
            with open(os.path.join(dirname, '%s.json' % filename), 'r') as fp:
                output = json.load(fp)
            yield component, filename, config, output


def test_component(component, config, expected):
    with mock.patch('grafana_dashboards.components.base.ComponentRegistry') as registry:
        def create_component(component_type, data):
            gen = mock.Mock()

            if inspect.isclass(component_type) and issubclass(component_type, JsonListGenerator):