예제 #1
0
    def test_out_table_no_query_yes_transformer_order(self):
        output_producer = OutputProducer(cli_ctx=self.mock_ctx)
        obj = {
            'name': 'qwerty',
            'val': '0b1f6472qwerty',
            'active': True,
            'sub': '0b1f6472'
        }

        def transformer(r):
            return OrderedDict([('Name', r['name']), ('Val', r['val']),
                                ('Active', r['active']), ('Sub', r['sub'])])

        result_item = CommandResultItem(obj,
                                        table_transformer=transformer,
                                        is_query_active=False)
        output_producer.out(result_item,
                            formatter=format_table,
                            out_file=self.io)
        # Should be table transformer order
        self.assertEqual(
            normalize_newlines(self.io.getvalue()),
            normalize_newlines("""Name    Val             Active    Sub
------  --------------  --------  --------
qwerty  0b1f6472qwerty  True      0b1f6472
"""))
예제 #2
0
    def test_out_json_byte_empty(self):
        output_producer = OutputProducer(formatter=format_json, file=self.io)
        output_producer.out(CommandResultItem({'active': True, 'contents': b''}))
        self.assertEqual(normalize_newlines(self.io.getvalue()), normalize_newlines(
            """{
  "active": true,
  "contents": ""
}
"""))
예제 #3
0
    def test_out_json_from_ordered_dict(self):
        # The JSON output when the input is OrderedDict should be serialized to JSON
        output_producer = OutputProducer(formatter=format_json, file=self.io)
        output_producer.out(CommandResultItem(OrderedDict({'active': True, 'id': '0b1f6472'})))
        self.assertEqual(normalize_newlines(self.io.getvalue()), normalize_newlines(
            """{
  "active": true,
  "id": "0b1f6472"
}
"""))
예제 #4
0
    def test_out_table_list_of_lists(self):
        output_producer = OutputProducer(formatter=format_table, file=self.io)
        obj = [['a', 'b'], ['c', 'd']]
        output_producer.out(CommandResultItem(obj))
        self.assertEqual(normalize_newlines(self.io.getvalue()), normalize_newlines(
            """Column1    Column2
---------  ---------
a          b
c          d
"""))
예제 #5
0
    def test_out_table_no_query_no_transformer_order(self):
        output_producer = OutputProducer(formatter=format_table, file=self.io)
        obj = {'name': 'qwerty', 'val': '0b1f6472qwerty', 'active': True, 'sub': '0b1f6472'}
        result_item = CommandResultItem(obj, table_transformer=None, is_query_active=False)
        output_producer.out(result_item)
        # Should be alphabetical order as no table transformer and query is not active.
        self.assertEqual(normalize_newlines(self.io.getvalue()), normalize_newlines(
            """Active    Name    Sub       Val
--------  ------  --------  --------------
True      qwerty  0b1f6472  0b1f6472qwerty
"""))
예제 #6
0
    def test_out_table_list_of_lists(self):
        output_producer = OutputProducer(formatter=format_table, file=self.io)
        obj = [['a', 'b'], ['c', 'd']]
        output_producer.out(CommandResultItem(obj))
        self.assertEqual(
            normalize_newlines(self.io.getvalue()),
            normalize_newlines("""Column1    Column2
---------  ---------
a          b
c          d
"""))
예제 #7
0
    def test_out_table(self):
        output_producer = OutputProducer(formatter=format_table, file=self.io)
        obj = OrderedDict()
        obj['active'] = True
        obj['val'] = '0b1f6472'
        output_producer.out(CommandResultItem(obj))
        self.assertEqual(normalize_newlines(self.io.getvalue()), normalize_newlines(
            """Active    Val
--------  --------
True      0b1f6472
"""))
예제 #8
0
    def test_out_table_no_query_yes_jmespath_table_transformer(self):
        output_producer = OutputProducer(formatter=format_table, file=self.io)
        obj = {'name': 'qwerty', 'val': '0b1f6472qwerty', 'active': True, 'sub': '0b1f6472'}

        result_item = CommandResultItem(obj, table_transformer='{Name:name, Val:val, Active:active}', is_query_active=False)
        output_producer.out(result_item)
        # Should be table transformer order
        self.assertEqual(normalize_newlines(self.io.getvalue()), normalize_newlines(
            """Name    Val             Active
------  --------------  --------
qwerty  0b1f6472qwerty  True
"""))
예제 #9
0
    def test_out_table(self):
        output_producer = OutputProducer(formatter=format_table, file=self.io)
        obj = OrderedDict()
        obj['active'] = True
        obj['val'] = '0b1f6472'
        output_producer.out(CommandResultItem(obj))
        self.assertEqual(
            normalize_newlines(self.io.getvalue()),
            normalize_newlines("""Active    Val
--------  --------
True      0b1f6472
"""))
예제 #10
0
    def test_out_table_with_number(self):
        output_producer = OutputProducer(cli_ctx=self.mock_ctx)
        obj = OrderedDict()
        obj['Sku'] = '6.10'
        output_producer.out(CommandResultItem(obj),
                            formatter=format_table,
                            out_file=self.io)
        self.assertEqual(normalize_newlines(self.io.getvalue()),
                         normalize_newlines("""Sku
-----
6.10
"""))
예제 #11
0
    def test_out_table_complex_obj(self):
        output_producer = OutputProducer(formatter=format_table, file=self.io)
        obj = OrderedDict()
        obj['name'] = 'qwerty'
        obj['val'] = '0b1f6472qwerty'
        obj['sub'] = {'1'}
        result_item = CommandResultItem(obj)
        output_producer.out(result_item)
        self.assertEqual(normalize_newlines(self.io.getvalue()), normalize_newlines(
            """Name    Val
------  --------------
qwerty  0b1f6472qwerty
"""))
예제 #12
0
    def test_out_yaml_non_ASCII(self):
        output_producer = OutputProducer(cli_ctx=self.mock_ctx)
        output_producer.out(CommandResultItem({
            'active': True,
            'contents': 'こんにちは'
        }),
                            formatter=format_yaml,
                            out_file=self.io)
        self.assertEqual(
            normalize_newlines(self.io.getvalue()),
            normalize_newlines("""active: true
contents: こんにちは
"""))
예제 #13
0
    def test_out_yaml_byte(self):
        output_producer = OutputProducer(cli_ctx=self.mock_ctx)
        output_producer.out(CommandResultItem({
            'active': True,
            'contents': b'0b1f6472'
        }),
                            formatter=format_yaml,
                            out_file=self.io)
        self.assertEqual(
            normalize_newlines(self.io.getvalue()),
            normalize_newlines("""active: true
contents: !!binary |
  MGIxZjY0NzI=
"""))
예제 #14
0
    def test_out_table_complex_obj(self):
        output_producer = OutputProducer(formatter=format_table, file=self.io)
        obj = OrderedDict()
        obj['name'] = 'qwerty'
        obj['val'] = '0b1f6472qwerty'
        obj['sub'] = {'1'}
        result_item = CommandResultItem(obj)
        output_producer.out(result_item)
        self.assertEqual(
            normalize_newlines(self.io.getvalue()),
            normalize_newlines("""Name    Val
------  --------------
qwerty  0b1f6472qwerty
"""))
예제 #15
0
    def test_out_json_byte_empty(self):
        output_producer = OutputProducer(formatter=format_json, file=self.io)
        output_producer.out(
            CommandResultItem({
                'active': True,
                'contents': b''
            }))
        self.assertEqual(
            normalize_newlines(self.io.getvalue()),
            normalize_newlines("""{
  "active": true,
  "contents": ""
}
"""))
예제 #16
0
    def test_out_table(self):
        output_producer = OutputProducer(cli_ctx=self.mock_ctx)
        obj = OrderedDict()
        obj['active'] = True
        obj['val'] = '0b1f6472'
        obj['lun'] = 0
        output_producer.out(CommandResultItem(obj),
                            formatter=format_table,
                            out_file=self.io)
        self.assertEqual(
            normalize_newlines(self.io.getvalue()),
            normalize_newlines("""Active    Lun    Val
--------  -----  --------
True      0      0b1f6472
"""))
예제 #17
0
    def test_out_json_byte(self):
        output_producer = OutputProducer(cli_ctx=self.mock_ctx)
        output_producer.out(CommandResultItem({
            'active': True,
            'contents': b'0b1f6472'
        }),
                            formatter=format_json,
                            out_file=self.io)
        self.assertEqual(
            normalize_newlines(self.io.getvalue()),
            normalize_newlines("""{
  "active": true,
  "contents": "0b1f6472"
}
"""))
예제 #18
0
    def test_out_json_from_ordered_dict(self):
        # The JSON output when the input is OrderedDict should be serialized to JSON
        output_producer = OutputProducer(formatter=format_json, file=self.io)
        output_producer.out(
            CommandResultItem(OrderedDict({
                'active': True,
                'id': '0b1f6472'
            })))
        self.assertEqual(
            normalize_newlines(self.io.getvalue()),
            normalize_newlines("""{
  "active": true,
  "id": "0b1f6472"
}
"""))
예제 #19
0
    def test_out_yaml_valid(self):
        """
        Test Dict serialized to YAML
        """
        output_producer = OutputProducer(cli_ctx=self.mock_ctx)
        output_producer.out(CommandResultItem({
            'active': True,
            'id': '0b1f6472'
        }),
                            formatter=format_yaml,
                            out_file=self.io)
        self.assertEqual(normalize_newlines(self.io.getvalue()),
                         normalize_newlines("""active: true
id: 0b1f6472
"""))
예제 #20
0
    def test_out_table_no_query_yes_transformer_order(self):
        output_producer = OutputProducer(formatter=format_table, file=self.io)
        obj = {'name': 'qwerty', 'val': '0b1f6472qwerty', 'active': True, 'sub': '0b1f6472'}

        def transformer(r):
            return OrderedDict([('Name', r['name']), ('Val', r['val']),
                                ('Active', r['active']), ('Sub', r['sub'])])

        result_item = CommandResultItem(obj, table_transformer=transformer, is_query_active=False)
        output_producer.out(result_item)
        # Should be table transformer order
        self.assertEqual(normalize_newlines(self.io.getvalue()), normalize_newlines(
            """Name    Val             Active    Sub
------  --------------  --------  --------
qwerty  0b1f6472qwerty  True      0b1f6472
"""))
예제 #21
0
    def test_out_json_valid(self):
        """
        The JSON output when the input is a dict should be the dict serialized to JSON
        """
        output_producer = OutputProducer(cli_ctx=self.mock_ctx)
        output_producer.out(CommandResultItem({
            'active': True,
            'id': '0b1f6472'
        }),
                            formatter=format_json,
                            out_file=self.io)
        self.assertEqual(
            normalize_newlines(self.io.getvalue()),
            normalize_newlines("""{
  "active": true,
  "id": "0b1f6472"
}
"""))
예제 #22
0
    def test_out_table_no_query_no_transformer_order(self):
        output_producer = OutputProducer(formatter=format_table, file=self.io)
        obj = {
            'name': 'qwerty',
            'val': '0b1f6472qwerty',
            'active': True,
            'sub': '0b1f6472'
        }
        result_item = CommandResultItem(obj,
                                        table_transformer=None,
                                        is_query_active=False)
        output_producer.out(result_item)
        # Should be alphabetical order as no table transformer and query is not active.
        self.assertEqual(
            normalize_newlines(self.io.getvalue()),
            normalize_newlines("""Active    Name    Sub       Val
--------  ------  --------  --------------
True      qwerty  0b1f6472  0b1f6472qwerty
"""))
예제 #23
0
    def test_out_table_no_query_yes_jmespath_table_transformer(self):
        output_producer = OutputProducer(formatter=format_table, file=self.io)
        obj = {
            'name': 'qwerty',
            'val': '0b1f6472qwerty',
            'active': True,
            'sub': '0b1f6472'
        }

        result_item = CommandResultItem(
            obj,
            table_transformer='{Name:name, Val:val, Active:active}',
            is_query_active=False)
        output_producer.out(result_item)
        # Should be table transformer order
        self.assertEqual(
            normalize_newlines(self.io.getvalue()),
            normalize_newlines("""Name    Val             Active
------  --------------  --------
qwerty  0b1f6472qwerty  True
"""))
예제 #24
0
    def test_out_table(self):
        output_producer = OutputProducer(cli_ctx=self.mock_ctx)

        obj1 = OrderedDict()
        obj1['active'] = True
        obj1['val'] = '0b1f6472'
        obj1['lun'] = 0

        obj2 = OrderedDict()
        obj2['active'] = False
        obj2['val'] = '0b1f6485'
        obj2['lun'] = 0

        output_producer.out(CommandResultItem([obj1, obj2]),
                            formatter=format_table,
                            out_file=self.io)
        self.assertEqual(
            normalize_newlines(self.io.getvalue()),
            normalize_newlines("""Active    Val    Lun
--------  --------  -----
True      0b1f6472  0
False     0b1f6485  0
"""))