def test_format_item_no_special_formatting(self): formatter = GraphQLResultFormatter(expand_history=False) formatter._flatten_edge = MagicMock() result = list(formatter.format_item('item')) formatter._flatten_edge.assert_called_with('item') self.assertEqual([formatter._flatten_edge.return_value], result)
def test_format_item_with_unfold(self): formatter = GraphQLResultFormatter(unfold=True) formatter._box_item = MagicMock() formatter._box_item.return_value = ['a', 'b', 'c'] formatter._flatten_edge = lambda x: 'flattened_' + x result = list(formatter.format_item('item')) self.assertEqual(['flattened_a', 'flattened_b', 'flattened_c'], result)
def test_format_item_with_sorter(self): formatter = GraphQLResultFormatter(sort=True) formatter._box_item = MagicMock(return_value=['a', 'b', 'c']) formatter.sorter = MagicMock() formatter.sorter.sort_items = lambda x: x[1] formatter._flatten_edge = lambda x: 'flattened_' + x result = next(formatter.format_item('item')) self.assertEqual('flattened_b', result)
def test_expand_history(self, mock_convert_to_history_rows): expected_result = [i for i in range(5)] mock_convert_to_history_rows.return_value = expected_result formatter = GraphQLResultFormatter() formatter._flatten_edge = MagicMock() result = list(formatter._expand_history('edge')) formatter._flatten_edge.assert_called_with('edge') mock_convert_to_history_rows.assert_called_with(formatter._flatten_edge.return_value) self.assertEqual(expected_result, result)
def test_flatten_edge(self): formatter = GraphQLResultFormatter() edge = { 'node': { 'reference': { 'edges': [ { 'node': { 'value': 'value' } } ] } } } nested_edge = { "node": { "value": "value", "reference": { "edges": [ { "node": { "value": "value", "nested_reference": { "edges": [ { "node": { "nested_value": "value" } } ] } } } ] }, "empty_reference": { "edges": [ { "node": { "empty_nested_reference": { "edges": [] } } } ] } } } expected_result = {'reference': [{'value': 'value'}]} result = formatter._flatten_edge(edge) assert (expected_result == result) expected_result = { 'value': 'value', 'reference': [{ 'value': 'value', 'nested_reference': [{'nested_value': 'value'}] }], 'nested_reference': [{'nested_value': 'value'}], 'empty_reference': [{'empty_nested_reference': []}], 'empty_nested_reference': [], } result = formatter._flatten_edge(nested_edge) assert (expected_result == result)
def test_format_item_with_row_formatter(self): row_formatter = lambda x: 'formatted_row(' + x + ')' formatter = GraphQLResultFormatter(row_formatter=row_formatter) formatter._flatten_edge = lambda x: 'flattened(' + x + ')' self.assertEqual(['flattened(formatted_row(a))'], list(formatter.format_item('a')))