Re: assert_match_hash(self, reference, sample)

2010-04-19 Thread Phlip
>         for key, value in reference.items():
>             if value == sample.get(key, value or True):

   if value == sample.get(key, not(value)):

D'oh! C-:

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



assert_match_hash(self, reference, sample)

2010-04-19 Thread Phlip
Djangoists:

Because I'm a perfectionist with deadlines, I write scads of developer
tests.

But they too frequently find me comparing two hashes for equality. The
goal of any assertion should be a pristine diagnostic that tells
everything it knows, neatly formatted. So assert_equal() fails on that
last count - neat formatting - hence you should not dump two big
hashes ('dict's) into it. You can never tell which items differ, and
which are just noise.

So I wrote an assertion that only prints out the differing items in
its diagnostic:

def assert_match_hash(self, reference, sample, diagnostic=''):
if reference == sample:  return
reference = reference.copy()
sample = sample.copy()
from pprint import pformat

for key, value in reference.items():
if value == sample.get(key, value or True):
reference.pop(key)
sample.pop(key)

diagnostic = ( 'hashes should not differ by these items:' +
   '\n%s\n!=\n%s\n%s' %
  ( pformat(reference),
pformat(sample),
diagnostic or '' ) )
diagnostic = diagnostic.strip()
self.assert_equal( reference, sample, diagnostic )

Can anyone improve it? Did I overlook any dict manipulations that
could simplify it? And if two hash values are themselves hashes, it
could recurse, right?

& Happy April 20th, y'all! Pic not related.

--
  Phlip
  http://penbird.deviantart.com/art/YARBK2-Charlie-Brown-160323823

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.