# 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
        sender=WithId('id'), results=[5], context={'3rd': 'context'})
    assert len(report.data) == 1
    assert (7, {'2nd': 'context'}) == get_value_and_context(report, 'id')


def test_has_copy_of_the_context():
    report = WorstReport()
    sent_context = {'sent': 'context'}
    results_read.send(
        sender=WithId('foo'), results=[4], context=sent_context)
    assert len(report.data) == 1
    r_val, r_context = get_value_and_context(report, 'foo')
    assert r_context == {'sent': 'context'}
    assert id(r_context) != id(sent_context)
    sent_context['another'] = 'entry'
    assert r_context == {'sent': 'context'}


def test_handles_multiple_sender_ids_as_separate_items():
    report = WorstReport()
    results_read.send(
        sender=WithId('id one'), results=['a'], context={'context': 'one'})
    results_read.send(
        sender=WithId('id two'), results=['z'], context={'context': 'two'})
    assert len(report.data) == 2
    assert ('a', {'context': 'one'}) == get_value_and_context(report, 'id one')
    assert ('z', {'context': 'two'}) == get_value_and_context(report, 'id two')


def test_result_repr_is_human_readable():

# 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

        sender=WithId('id'), results=[5], context={'3rd': 'context'})
    assert len(report.data) == 1
    assert (7, {'2nd': 'context'}) == get_value_and_context(report, 'id')


def test_has_copy_of_the_context():
    report = WorstReport()
    sent_context = {'sent': 'context'}
    results_read.send(
        sender=WithId('foo'), results=[4], context=sent_context)
    assert len(report.data) == 1
    r_val, r_context = get_value_and_context(report, 'foo')
    assert r_context == {'sent': 'context'}
    assert id(r_context) != id(sent_context)
    sent_context['another'] = 'entry'
    assert r_context == {'sent': 'context'}


def test_handles_multiple_sender_ids_as_separate_items():
    report = WorstReport()
    results_read.send(
        sender=WithId('id one'), results=['a'], context={'context': 'one'})
    results_read.send(
        sender=WithId('id two'), results=['z'], context={'context': 'two'})
    assert len(report.data) == 2
    assert ('a', {'context': 'one'}) == get_value_and_context(report, 'id one')
    assert ('z', {'context': 'two'}) == get_value_and_context(report, 'id two')


def test_result_repr_is_human_readable():

    result = Result(value='val', context={'foo': 'bar', 'baz': 4})