Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
trees = self.converter.dict_to_trees({
'foo': [
'bar',
'bar.baz',
'bar.qux',
],
})
self.assertEqual(len(trees), 1)
type_node = trees[0]
self.assertIsInstance(type_node, TypeNode)
self.assertEqual(type_node.type, 'foo')
self.assertEqual(len(type_node.expansions), 1)
bar_expansion_node = type_node.expansions[0]
self.assertIsInstance(bar_expansion_node, ExpansionNode)
self.assertEqual(bar_expansion_node.type, 'bar')
self.assertEqual(bar_expansion_node.name, 'bar')
self.assertEqual(bar_expansion_node.source_field, 'bar_id')
self.assertEqual(bar_expansion_node.destination_field, 'bar')
self.assertEqual(bar_expansion_node.service, 'bar')
self.assertEqual(bar_expansion_node.action, 'get_bar')
self.assertEqual(bar_expansion_node.request_field, 'id')
self.assertEqual(bar_expansion_node.response_field, 'bar')
self.assertEqual(len(bar_expansion_node.expansions), 2)
baz_expansion_node, qux_expansion_node = sorted(bar_expansion_node.expansions, key=lambda x: x.type)
self.assertIsInstance(baz_expansion_node, ExpansionNode)
self.assertEqual(baz_expansion_node.type, 'baz')
self.assertEqual(baz_expansion_node.name, 'baz')
self.assertEqual(baz_expansion_node.source_field, 'baz_id')
self.assertEqual(baz_expansion_node.destination_field, 'baz')
def test_to_dict(self):
bar_expansion_node = ExpansionNode(
node_type='bar',
name='bar',
source_field='bar_id',
destination_field='bar',
service='bar',
action='get_bar',
request_field='id',
response_field='bar',
)
baz_expansion_node = ExpansionNode(
node_type='baz',
name='baz',
source_field='baz_id',
destination_field='baz',
service='baz',
action='get_baz',
def setUp(self):
self.expansion_node = ExpansionNode(
node_type='foo',
name='foo',
source_field='foo_id',
destination_field='foo',
service='foo',
action='get_foo',
request_field='id',
response_field='foo',
)
def test_to_strings_with_expansions(self):
bar_expansion_node = ExpansionNode(
node_type='bar',
name='bar',
source_field='bar_id',
destination_field='bar',
service='bar',
action='get_bar',
request_field='id',
response_field='bar',
)
baz_expansion_node = ExpansionNode(
node_type='baz',
name='baz',
source_field='baz_id',
destination_field='baz',
service='baz',
action='get_baz',
request_field='id',
response_field='baz',
)
self.expansion_node.add_expansion(bar_expansion_node)
self.expansion_node.add_expansion(baz_expansion_node)
self.assertEqual(
set(self.expansion_node.to_strings()),
{'foo.baz', 'foo.bar'},
)
def test_to_strings_with_expansions(self):
bar_expansion_node = ExpansionNode(
node_type='bar',
name='bar',
source_field='bar_id',
destination_field='bar',
service='bar',
action='get_bar',
request_field='id',
response_field='bar',
)
baz_expansion_node = ExpansionNode(
node_type='baz',
name='baz',
source_field='baz_id',
destination_field='baz',
service='baz',
action='get_baz',
service='bar',
action='get_bar',
request_field='id',
response_field='bar',
)
baz_expansion_node = ExpansionNode(
node_type='baz',
name='baz',
source_field='baz_id',
destination_field='baz',
service='baz',
action='get_baz',
request_field='id',
response_field='baz',
)
qux_expansion_node = ExpansionNode(
node_type='qux',
name='qux',
source_field='qux_id',
destination_field='qux',
service='qux',
action='get_qux',
request_field='id',
response_field='qux',
)
bar_expansion_node.add_expansion(baz_expansion_node)
bar_expansion_node.add_expansion(qux_expansion_node)
self.type_node.add_expansion(bar_expansion_node)
type_node_dict = self.type_node.to_dict()
self.assertTrue('foo' in type_node_dict)
def test_get_expansion(self):
expansion_node = ExpansionNode(
node_type='bar',
name='bar',
source_field='bar_id',
destination_field='bar',
service='bar',
action='get_bar',
request_field='id',
response_field='bar',
)
self.type_node.add_expansion(expansion_node)
self.assertEqual(
self.type_node.get_expansion(expansion_node.name),
expansion_node,
)
def test_cannot_add_same_expansion_twice(self):
expansion_node = ExpansionNode(
node_type='bar',
name='bar',
source_field='bar_id',
destination_field='bar',
service='bar',
action='get_bar',
request_field='id',
response_field='bar',
)
self.type_node.add_expansion(expansion_node)
self.type_node.add_expansion(expansion_node)
self.assertEqual(len(self.type_node.expansions), 1)
self.assertEqual(
self.type_node.expansions[0],
expansion_node,
def test_trees_to_dict(self):
foo_tree_node = TypeNode(node_type='foo')
bar_expansion_node = ExpansionNode(
node_type='bar',
name='bar',
source_field='bar_id',
destination_field='bar',
service='bar',
action='get_bar',
request_field='id',
response_field='bar',
)
foo_tree_node.add_expansion(bar_expansion_node)
baz_tree_node = TypeNode(node_type='baz')
qux_expansion_node = ExpansionNode(
node_type='qux',
name='qux',
source_field='qux_id',
for expansion_string in expansion_list:
expansion_node = type_node
for expansion_name in expansion_string.split('.'):
child_expansion_node = expansion_node.get_expansion(expansion_name)
if not child_expansion_node:
type_expansion = self.type_expansions[expansion_node.type][expansion_name]
type_route = self.type_routes[cast(six.text_type, type_expansion['route'])]
if type_expansion['destination_field'] == type_expansion['source_field']:
raise ValueError(
'Expansion configuration destination_field error: '
'destination_field can not have the same name as the source_field: '
'{}'.format(type_expansion['source_field'])
)
child_expansion_node = ExpansionNode(
node_type=cast(six.text_type, type_expansion['type']),
name=expansion_name,
source_field=cast(six.text_type, type_expansion['source_field']),
destination_field=cast(six.text_type, type_expansion['destination_field']),
service=type_route['service'],
action=type_route['action'],
request_field=type_route['request_field'],
response_field=type_route['response_field'],
raise_action_errors=cast(bool, type_expansion.get('raise_action_errors', False)),
)
expansion_node.add_expansion(child_expansion_node)
expansion_node = child_expansion_node
trees.append(type_node)