11# coding=utf-8
22from collections import namedtuple
33
4- from pytest_django_queries .entry import Entry
4+ from pytest_django_queries .entry import Entry # noqa
55from pytest_django_queries .filters import format_underscore_name_to_human
66
77_ROW_FIELD = namedtuple ("_RowField" , ("comp_field" , "align_char" , "length_field" ))
88_ROW_FIELDS = (
99 _ROW_FIELD ("test_name" , "<" , "test_name" ),
1010 _ROW_FIELD ("left_count" , ">" , "query_count" ),
1111 _ROW_FIELD ("right_count" , ">" , "query_count" ),
12+ _ROW_FIELD ("duplicate_count" , ">" , "duplicate_count" ),
1213)
1314_ROW_PREFIX = " "
1415_NA_CHAR = "-"
@@ -18,7 +19,7 @@ def entry_row(entry_comp, lengths):
1819 cols = []
1920
2021 for field , align , length_key in _ROW_FIELDS :
21- fmt = "{cmp.%s: %s{lengths[%s]}} " % (field , align , length_key )
22+ fmt = "{cmp.%s: %s%d} " % (field , align , lengths [ length_key ] )
2223 cols .append (fmt .format (cmp = entry_comp , lengths = lengths ))
2324
2425 return "%(diff_char)s %(results)s" % (
@@ -99,6 +100,14 @@ def left_count(self):
99100 def right_count (self ):
100101 return str (self .right .query_count ) if self .right else _NA_CHAR
101102
103+ @property
104+ def duplicate_count (self ):
105+ if self .right :
106+ return self .right .duplicate_count
107+ elif self .left :
108+ return self .left .duplicate_count
109+ return _NA_CHAR
110+
102111 @property
103112 def diff (self ):
104113 return self ._diff_from_newest ()
@@ -123,31 +132,18 @@ def __init__(self, entries_left, entries_right):
123132
124133 self ._mapping = {}
125134 self ._generate_mapping ()
126- self .longest_props = self ._get_longest_per_prop ({ "query_count" , "test_name" } )
135+ self .longest_props = self ._get_longest_per_prop ()
127136 self .header_rows = get_header_row (lengths = self .longest_props )
128137
129- def _get_longest_per_prop (self , props ):
130- """
131- :param props:
132- :type props: set
133- :return:
134- """
135-
136- longest = {prop : 0 for prop in props }
137- entries = (
138- self .entries_left
139- + self .entries_right
140- + [field for field , _ , _ in _ROW_FIELDS ]
141- )
138+ def _get_longest_per_prop (self ):
139+ longest = {field .length_field : len (field .comp_field ) for field in _ROW_FIELDS }
140+ entries = self .entries_left + self .entries_right
142141
143142 for entry in entries :
144- for prop in props :
145- if isinstance (entry , Entry ):
146- current_length = len (str (getattr (entry , prop , None )))
147- else :
148- current_length = len (entry )
149- if current_length > longest [prop ]:
150- longest [prop ] = current_length
143+ for field in _ROW_FIELDS :
144+ current_length = len (str (getattr (entry , field .comp_field , None )))
145+ if current_length > longest [field .length_field ]:
146+ longest [field .length_field ] = current_length
151147
152148 return longest
153149
0 commit comments