# Path: django_performance_testing/core.py
# class NameValueResult(object):
#     def __init__(self, name, value):
#         self.name = name
#         self.value = value
# 
#     def _to_cmp_val(self, other):
#         if type(other) == type(self):
#             return other.value
#         try:
#             # other is numeric
#             # need it e.g.: to support _pytest.python.ApproxNonIterable
#             x = bool(self.value == other)  # noqa: F841
#             return other
#         except TypeError:
#             raise NotImplementedError()
# 
#     def __lt__(self, other):
#         return self.value < self._to_cmp_val(other)
# 
#     def __eq__(self, other):
#         return self.value == self._to_cmp_val(other)
# 
#     def __str__(self):
#         return str(self.value)
# 
# Path: django_performance_testing/reports.py
# class WorstReport(object):
# 
#     def __init__(self):
#         results_read.connect(self.handle_results_collected)
#         self.data = {}
# 
#     def handle_results_collected(self, signal, sender, results, context, **kw):
#         name_value_pairs = list(map(self.to_name_value_pair, results))
#         self.ensure_unique_names(name_value_pairs)
# 
#         def get_data(*parts):
#             d = self.data
#             for p in parts:
#                 d.setdefault(p, {})
#                 d = d[p]
#             return d
# 
#         def handle_result(name, result):
#             d = get_data(sender.id_, sender.type_name)
#             current = d.get(name, None)
#             if current is None or current.value < result:
#                 # TODO: once serialization, no need to deepcopy
#                 d[name] = Result(value=result, context=copy.deepcopy(context))
# 
#         for name, result in name_value_pairs:
#             handle_result(name, result)
# 
#     def to_name_value_pair(self, value):
#         return (
#             getattr(value, 'name', ''),
#             getattr(value, 'value', value)
#         )
# 
#     def ensure_unique_names(self, name_value_pairs):
#         dupes = set()
#         last = None
#         for (name, value) in sorted(name_value_pairs):
#             if name == last:
#                 dupes.add(name)
#             last = name
#         if dupes:
#             dupes_as_str = ', '.join(map(repr, dupes))
#             raise TypeError(
#                 'Duplicate result name(s): {}'.format(dupes_as_str))
# 
#     def render(self, stream):
#         if not self.data:
#             return
#         stream.write('\nWorst Performing Items\n\n')
#         self.render_dict(stream=stream, d=self.data, indent=0, underline='=')
# 
#     def render_dict(self, stream, d, indent, underline=None):
#         prefix = ' '*indent
# 
#         for k in sorted(d.keys()):
#             v = d[k]
#             val, nextdict = self._to_val_nextdict(v)
# 
#             if val:
#                 val = ' {}'.format(val)
#             else:
#                 val = ''
#             line = '{}{}:{}'.format(prefix, k, val)
#             stream.write('{}\n'.format(line))
#             if underline:
#                 stream.write('{}\n'.format(underline*len(line)))
#             if nextdict:
#                 self.render_dict(stream=stream, d=nextdict, indent=indent + 2)
# 
#     def _to_val_nextdict(self, v):
#         if isinstance(v, Result):
#             return (v.value, v.context)
#         elif isinstance(v, dict):
#             return (None, v)
#         else:
#             return (v, None)
# 
#     def rendered(self):
#         out = six.StringIO()
#         self.render(out)
#         return out.getvalue()
# 
# class Result(object):
#     def __init__(self, value, context):
#         self.value = value
#         self.context = context
# 
#     def __repr__(self):
#         return '{} {}'.format(self.value, pprint.pformat(self.context))
# 
# Path: django_performance_testing/signals.py



import pytest
from django.utils import six
from django_performance_testing.core import NameValueResult
from django_performance_testing.reports import WorstReport, Result
from django_performance_testing.signals import results_read
from testapp.test_helpers import WithId, FakeSender
    lines = [
        '{0: \'\',',
        ' 1: \'{}\','.format('a'*10),
        ' 2: \'{}\','.format('a'*20),
        ' 3: \'{}\'}}'.format('a'*30),
    ]
    assert '(1, 2) {}'.format('\n'.join(lines)) == repr(result)


expected_report_data = """
Worst Performing Items

id 1:
=====
  querycount:
    two: 2
      test: some.app.tests.TestCase.test_foo
id 2:
=====
  querycount:
    nine: 9
      foo: bar
""".strip()


def test_report_printed_includes_all_needed_data():
    report = WorstReport()
    report.handle_results_collected(
        signal=None, sender=FakeSender('id 2', 'querycount'),
        results=[

# Path: django_performance_testing/core.py
# class NameValueResult(object):
#     def __init__(self, name, value):
#         self.name = name
#         self.value = value
# 
#     def _to_cmp_val(self, other):
#         if type(other) == type(self):
#             return other.value
#         try:
#             # other is numeric
#             # need it e.g.: to support _pytest.python.ApproxNonIterable
#             x = bool(self.value == other)  # noqa: F841
#             return other
#         except TypeError:
#             raise NotImplementedError()
# 
#     def __lt__(self, other):
#         return self.value < self._to_cmp_val(other)
# 
#     def __eq__(self, other):
#         return self.value == self._to_cmp_val(other)
# 
#     def __str__(self):
#         return str(self.value)
# 
# Path: django_performance_testing/reports.py
# class WorstReport(object):
# 
#     def __init__(self):
#         results_read.connect(self.handle_results_collected)
#         self.data = {}
# 
#     def handle_results_collected(self, signal, sender, results, context, **kw):
#         name_value_pairs = list(map(self.to_name_value_pair, results))
#         self.ensure_unique_names(name_value_pairs)
# 
#         def get_data(*parts):
#             d = self.data
#             for p in parts:
#                 d.setdefault(p, {})
#                 d = d[p]
#             return d
# 
#         def handle_result(name, result):
#             d = get_data(sender.id_, sender.type_name)
#             current = d.get(name, None)
#             if current is None or current.value < result:
#                 # TODO: once serialization, no need to deepcopy
#                 d[name] = Result(value=result, context=copy.deepcopy(context))
# 
#         for name, result in name_value_pairs:
#             handle_result(name, result)
# 
#     def to_name_value_pair(self, value):
#         return (
#             getattr(value, 'name', ''),
#             getattr(value, 'value', value)
#         )
# 
#     def ensure_unique_names(self, name_value_pairs):
#         dupes = set()
#         last = None
#         for (name, value) in sorted(name_value_pairs):
#             if name == last:
#                 dupes.add(name)
#             last = name
#         if dupes:
#             dupes_as_str = ', '.join(map(repr, dupes))
#             raise TypeError(
#                 'Duplicate result name(s): {}'.format(dupes_as_str))
# 
#     def render(self, stream):
#         if not self.data:
#             return
#         stream.write('\nWorst Performing Items\n\n')
#         self.render_dict(stream=stream, d=self.data, indent=0, underline='=')
# 
#     def render_dict(self, stream, d, indent, underline=None):
#         prefix = ' '*indent
# 
#         for k in sorted(d.keys()):
#             v = d[k]
#             val, nextdict = self._to_val_nextdict(v)
# 
#             if val:
#                 val = ' {}'.format(val)
#             else:
#                 val = ''
#             line = '{}{}:{}'.format(prefix, k, val)
#             stream.write('{}\n'.format(line))
#             if underline:
#                 stream.write('{}\n'.format(underline*len(line)))
#             if nextdict:
#                 self.render_dict(stream=stream, d=nextdict, indent=indent + 2)
# 
#     def _to_val_nextdict(self, v):
#         if isinstance(v, Result):
#             return (v.value, v.context)
#         elif isinstance(v, dict):
#             return (None, v)
#         else:
#             return (v, None)
# 
#     def rendered(self):
#         out = six.StringIO()
#         self.render(out)
#         return out.getvalue()
# 
# class Result(object):
#     def __init__(self, value, context):
#         self.value = value
#         self.context = context
# 
#     def __repr__(self):
#         return '{} {}'.format(self.value, pprint.pformat(self.context))
# 
# Path: django_performance_testing/signals.py



# Path: tests/testapp/tests/test_worst_report.py
import pytest
from django.utils import six
from django_performance_testing.core import NameValueResult
from django_performance_testing.reports import WorstReport, Result
from django_performance_testing.signals import results_read
from testapp.test_helpers import WithId, FakeSender

    lines = [
        '{0: \'\',',
        ' 1: \'{}\','.format('a'*10),
        ' 2: \'{}\','.format('a'*20),
        ' 3: \'{}\'}}'.format('a'*30),
    ]
    assert '(1, 2) {}'.format('\n'.join(lines)) == repr(result)


expected_report_data = """
Worst Performing Items

id 1:
=====
  querycount:
    two: 2
      test: some.app.tests.TestCase.test_foo
id 2:
=====
  querycount:
    nine: 9
      foo: bar
""".strip()


def test_report_printed_includes_all_needed_data():
    report = WorstReport()
    report.handle_results_collected(
        signal=None, sender=FakeSender('id 2', 'querycount'),
        results=[

            NameValueResult(name='nine', value=9)], context={'foo': 'bar'})