|
| 1 | +# coding=utf-8 |
| 2 | +from collections import namedtuple |
| 3 | +from pytest_django_queries.entry import Entry |
| 4 | +from pytest_django_queries.filters import format_underscore_name_to_human |
| 5 | + |
| 6 | +_ROW_FIELD = namedtuple('_RowField', ('comp_field', 'align_char', 'length_field')) |
| 7 | +_ROW_FIELDS = ( |
| 8 | + _ROW_FIELD('test_name', '<', 'test_name'), |
| 9 | + _ROW_FIELD('left_count', '>', 'query_count'), |
| 10 | + _ROW_FIELD('right_count', '>', 'query_count'), |
| 11 | +) |
| 12 | +_ROW_PREFIX = ' ' |
| 13 | +_NA_CHAR = '-' |
| 14 | + |
| 15 | + |
| 16 | +def entry_row(entry_comp, lengths): |
| 17 | + cols = [] |
| 18 | + |
| 19 | + for field, align, length_key in _ROW_FIELDS: |
| 20 | + fmt = '{cmp.%s: %s{lengths[%s]}}' % (field, align, length_key) |
| 21 | + cols.append(fmt.format(cmp=entry_comp, lengths=lengths)) |
| 22 | + |
| 23 | + return '%(diff_char)s %(results)s' % ({ |
| 24 | + 'diff_char': entry_comp.diff, |
| 25 | + 'results': '\t'.join(cols)}) |
| 26 | + |
| 27 | + |
| 28 | +def get_header_row(lengths): |
| 29 | + sep_row = [] |
| 30 | + head_row = [] |
| 31 | + |
| 32 | + for field, _, length_key in _ROW_FIELDS: |
| 33 | + length = lengths[length_key] |
| 34 | + sep_row.append('%s' % ('-' * length)) |
| 35 | + head_row.append('{field: <{length}}'.format( |
| 36 | + field=field.replace('_', ' '), length=length)) |
| 37 | + |
| 38 | + return '%(prefix)s%(head)s\n%(prefix)s%(sep)s' % ({ |
| 39 | + 'prefix': _ROW_PREFIX, |
| 40 | + 'head': '\t'.join(head_row), |
| 41 | + 'sep': '\t'.join(sep_row)}) |
| 42 | + |
| 43 | + |
| 44 | +class DiffChars(object): |
| 45 | + NEGATIVE = '-' |
| 46 | + NEUTRAL = ' ' |
| 47 | + POSITIVE = '+' |
| 48 | + |
| 49 | + @classmethod |
| 50 | + def convert(cls, diff): |
| 51 | + if diff < 0: |
| 52 | + return DiffChars.POSITIVE |
| 53 | + if diff > 0: |
| 54 | + return DiffChars.NEGATIVE |
| 55 | + return DiffChars.NEUTRAL |
| 56 | + |
| 57 | + |
| 58 | +class SingleEntryComparison(object): |
| 59 | + __slots__ = ["left", "right"] |
| 60 | + |
| 61 | + def __init__(self, left=None, right=None): |
| 62 | + """ |
| 63 | + :param left: Previous version. |
| 64 | + :type left: Entry |
| 65 | +
|
| 66 | + :param right: Newest version. |
| 67 | + :type right: Entry |
| 68 | + """ |
| 69 | + |
| 70 | + self.left = left |
| 71 | + self.right = right |
| 72 | + |
| 73 | + def _diff_from_newest(self): |
| 74 | + """ |
| 75 | + Returns the query count difference from the previous version. |
| 76 | + If there is no older version, we assume it's an "improvement" (positive output) |
| 77 | + If there is no new version, we assume it's not an improvement (negative output) |
| 78 | + """ |
| 79 | + if self.left is None: |
| 80 | + return DiffChars.POSITIVE |
| 81 | + if self.right is None: |
| 82 | + return DiffChars.NEGATIVE |
| 83 | + return DiffChars.convert(self.right.query_count - self.left.query_count) |
| 84 | + |
| 85 | + @property |
| 86 | + def test(self): |
| 87 | + return self.left or self.right |
| 88 | + |
| 89 | + @property |
| 90 | + def test_name(self): |
| 91 | + return format_underscore_name_to_human(self.test.test_name) |
| 92 | + |
| 93 | + @property |
| 94 | + def left_count(self): |
| 95 | + return str(self.left.query_count) if self.left else _NA_CHAR |
| 96 | + |
| 97 | + @property |
| 98 | + def right_count(self): |
| 99 | + return str(self.right.query_count) if self.right else _NA_CHAR |
| 100 | + |
| 101 | + @property |
| 102 | + def diff(self): |
| 103 | + return self._diff_from_newest() |
| 104 | + |
| 105 | + def to_string(self, lengths): |
| 106 | + return entry_row(self, lengths=lengths) |
| 107 | + |
| 108 | + |
| 109 | +class DiffGenerator(object): |
| 110 | + def __init__(self, entries_left, entries_right): |
| 111 | + """ |
| 112 | + Generates the diffs from two files. |
| 113 | +
|
| 114 | + :param entries_left: |
| 115 | + :type entries_left: List[Entry] |
| 116 | + :param entries_right: |
| 117 | + :type entries_right: List[Entry] |
| 118 | + """ |
| 119 | + |
| 120 | + self.entries_left = entries_left |
| 121 | + self.entries_right = entries_right |
| 122 | + |
| 123 | + self._mapping = {} |
| 124 | + self._generate_mapping() |
| 125 | + self.longest_props = self._get_longest_per_prop({'query_count', 'test_name'}) |
| 126 | + self.header_rows = get_header_row(lengths=self.longest_props) |
| 127 | + |
| 128 | + def _get_longest_per_prop(self, props): |
| 129 | + """ |
| 130 | + :param props: |
| 131 | + :type props: set |
| 132 | + :return: |
| 133 | + """ |
| 134 | + |
| 135 | + longest = {prop: 0 for prop in props} |
| 136 | + entries = ( |
| 137 | + self.entries_left + self.entries_right + [field for field, _, _ in _ROW_FIELDS] |
| 138 | + ) |
| 139 | + |
| 140 | + for entry in entries: |
| 141 | + for prop in props: |
| 142 | + if isinstance(entry, Entry): |
| 143 | + current_length = len(str(getattr(entry, prop, None))) |
| 144 | + else: |
| 145 | + current_length = len(entry) |
| 146 | + if current_length > longest[prop]: |
| 147 | + longest[prop] = current_length |
| 148 | + |
| 149 | + return longest |
| 150 | + |
| 151 | + def _map_side(self, entries, side_name): |
| 152 | + for entry in entries: |
| 153 | + module_map = self._mapping.setdefault(entry.module_name, {}) |
| 154 | + |
| 155 | + if entry.test_name not in module_map: |
| 156 | + module_map[entry.test_name] = SingleEntryComparison() |
| 157 | + |
| 158 | + setattr(module_map[entry.test_name], side_name, entry) |
| 159 | + |
| 160 | + def _generate_mapping(self): |
| 161 | + self._map_side(self.entries_left, 'left') |
| 162 | + self._map_side(self.entries_right, 'right') |
| 163 | + |
| 164 | + def _iter_module(self, module_entries): |
| 165 | + yield self.header_rows |
| 166 | + for _, test_comparison in sorted(module_entries.items()): # type: SingleEntryComparison |
| 167 | + yield test_comparison.to_string(lengths=self.longest_props) |
| 168 | + |
| 169 | + def _iter_modules(self): |
| 170 | + for module_name, module_entries in sorted(self._mapping.items()): |
| 171 | + yield ( |
| 172 | + format_underscore_name_to_human(module_name), |
| 173 | + self._iter_module(module_entries)) |
| 174 | + |
| 175 | + def __iter__(self): |
| 176 | + return self._iter_modules() |
0 commit comments