Skip to content

Commit

Permalink
Update canonicalRepr to take entity type into consideration.
Browse files Browse the repository at this point in the history
  • Loading branch information
gkrimer committed May 6, 2016
1 parent e3271a1 commit a4bd683
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
14 changes: 13 additions & 1 deletion common/common_tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import unittest
import itertools

from common.test import BaseFlaskTestCase


class IntegrationTest(unittest.TestCase):

Expand All @@ -14,4 +16,14 @@ def test_imports(self):
assert 'text_messaging' in itertools.chain.from_iterable(submodules) # Sanity check

for file_finder, name, _ in submodules:
file_finder.find_module(name).load_module(name)
file_finder.find_module(name).load_module(name)

def test_canonical_repr(self):
x1 = {'type': 'x', 'id': 1, 'attributes': {'name': 'John'}, 'relationships': {}}
x2 = {'type': 'x', 'id': 2, 'attributes': {'name': 'Neil'}, 'relationships': {}}
x3 = {'type': 'x', 'id': 3, 'attributes': {'name': 'Doug'}, 'relationships': {}}
y1 = {'type': 'y', 'id': 1, 'attributes': {'name': 'Mike'}}
z1 = {'type': 'z', 'id': 1, 'attributes': {'name': 'Frank', 'a': 1, 'b': 2}, 'relationships': {}}

t = BaseFlaskTestCase()
assert t.canonicalRepr({'data': x2, 'included': [z1, x1, y1, x3,]}) == {'data': x2, 'included': [x1, y1, z1, x3]}
7 changes: 3 additions & 4 deletions common/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,11 @@ def assertEntitiesContain(self, actual_entities, expected_entities):
if not set(actual.items()).issuperset(set(expected.items())):
self.fail('Actual does not have everything expected: {actual}, {expected}'.format(actual=actual,
expected=expected))

def canonicalRepr(self, payload):
"""
Canonicalize a payload intended to be consumed by Ember's Rest Adapter by sorting the lists. This enables
payloads to be compared with == and leverages pytest's magic introspection.
Canonicalize a JSON payload intended to be consumed by Ember Data's Rest or JSONAPI adaptors by sorting
top-level lists. This enables comparison of responses with == and leverages pytest's magic introspection.
"""

return {key: sorted(value, key=itemgetter('id')) if isinstance(value, list) else value
return {key: sorted(value, key=lambda x: (x['id'], x.get('type'))) if isinstance(value, list) else value
for key, value in payload.items()}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup

setup(name='common',
version='0.1.7',
version='0.1.8',
author='Unascribed',
author_email='[email protected]',
description='Code intended to be used across Polymath Ventures repositories.',
Expand Down

0 comments on commit a4bd683

Please sign in to comment.