summaryrefslogtreecommitdiffstats
path: root/tests/tabular_output/test_delimited_output_adapter.py
blob: 86a622e7adb34d3a0c2e29939c240d875390ebe7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-
"""Test the delimited output adapter."""

from __future__ import unicode_literals
from textwrap import dedent

import pytest

from cli_helpers.tabular_output import delimited_output_adapter


def test_csv_wrapper():
    """Test the delimited output adapter."""
    # Test comma-delimited output.
    data = [["abc", "1"], ["d", "456"]]
    headers = ["letters", "number"]
    output = delimited_output_adapter.adapter(iter(data), headers, dialect="unix")
    assert "\n".join(output) == dedent(
        '''\
        "letters","number"\n\
        "abc","1"\n\
        "d","456"'''
    )

    # Test tab-delimited output.
    data = [["abc", "1"], ["d", "456"]]
    headers = ["letters", "number"]
    output = delimited_output_adapter.adapter(
        iter(data), headers, table_format="csv-tab", dialect="unix"
    )
    assert "\n".join(output) == dedent(
        '''\
        "letters"\t"number"\n\
        "abc"\t"1"\n\
        "d"\t"456"'''
    )

    with pytest.raises(ValueError):
        output = delimited_output_adapter.adapter(
            iter(data), headers, table_format="foobar"
        )
        list(output)


def test_unicode_with_csv():
    """Test that the csv wrapper can handle non-ascii characters."""
    data = [["观音", "1"], ["Ποσειδῶν", "456"]]
    headers = ["letters", "number"]
    output = delimited_output_adapter.adapter(data, headers)
    assert "\n".join(output) == dedent(
        """\
        letters,number\n\
        观音,1\n\
        Ποσειδῶν,456"""
    )