daily pastebin goal
14%
SHARE
TWEET

Untitled

a guest Jun 19th, 2018 140 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. """
  2. Run it with:
  3.     pytest dicts.py --tb=short
  4. """
  5. import dictdiffer
  6. import json
  7. import pytest
  8.  
  9.  
  10. @pytest.fixture
  11. def dicts_are_same(pytestconfig):
  12.     verbose = pytestconfig.getoption('verbose')
  13.  
  14.     def check_objects(d1, d2, verbose=1):
  15.         if d1 == d2:
  16.             return True
  17.         else:
  18.             diff_chunks = dictdiffer.diff(d1, d2)
  19.             diff = '\n'.join([
  20.                 ' => %s: at key %r values are different | Left: %r | Right: %r' % (
  21.                     action.upper(), path, values[0], values[1]
  22.                 )
  23.                 for action, path, values in diff_chunks
  24.             ])
  25.             sep = '\n' + ('=' * 80) + '\n'
  26.             msg_lines = [
  27.                 'Provided items are NOT the same.',
  28.                 'Left:',
  29.                 as_json(d1),
  30.                 sep,
  31.                 'Right:',
  32.                 as_json(d2),
  33.                 sep,
  34.                 'Diff:',
  35.                 diff,
  36.             ]
  37.             pytest.fail('\n\n'.join(msg_lines))
  38.  
  39.     def as_json(d):
  40.         return json.dumps(d, sort_keys=True, indent=2)
  41.  
  42.     return check_objects
  43.  
  44.  
  45. def test_compare_dicts(dicts_are_same):
  46.     result = {
  47.         "name": {
  48.             "first": "juvena",
  49.             "last": "rodrigues",
  50.             "title": "miss"
  51.         },
  52.         "nat": "BR",
  53.         "phone": "(75) 9072-2545",
  54.         "email": "juvena.rodrigues@example.com",
  55.         "gender": "female",
  56.         "location": {
  57.             "city": "arapiraca",
  58.             "postcode": 27146,
  59.             "state": "minas gerais",
  60.             "street": "4903 rua bela vista ",
  61.         }
  62.     }
  63.  
  64.     expected_data = {
  65.         "name": {
  66.             "first": "Juvena",
  67.             "last": "Rodrigues",
  68.             "title": "miss"
  69.         },
  70.         "nat": "BR",
  71.         "phone": "(75) 9072-2545",
  72.         "email": "juvenarodrigues@example.com",
  73.         "gender": "female",
  74.         "location": {
  75.             "city": "Arapiraca",
  76.             "postcode": 21746,
  77.             "state": "minas gerais",
  78.             "street": "4903 rua bela vista ",
  79.         }
  80.     }
  81.  
  82.     assert dicts_are_same(result, expected_data)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top