Merging upstream version 2.2.0.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
ab1302c465
commit
95bca6b33d
42 changed files with 1085 additions and 840 deletions
|
@ -14,14 +14,15 @@ from cli_helpers.utils import strip_ansi
|
|||
|
||||
def test_tabular_output_formatter():
|
||||
"""Test the TabularOutputFormatter class."""
|
||||
headers = ['text', 'numeric']
|
||||
headers = ["text", "numeric"]
|
||||
data = [
|
||||
["abc", Decimal(1)],
|
||||
["defg", Decimal("11.1")],
|
||||
["hi", Decimal("1.1")],
|
||||
["Pablo\rß\n", 0],
|
||||
]
|
||||
expected = dedent("""\
|
||||
expected = dedent(
|
||||
"""\
|
||||
+------------+---------+
|
||||
| text | numeric |
|
||||
+------------+---------+
|
||||
|
@ -33,66 +34,99 @@ def test_tabular_output_formatter():
|
|||
)
|
||||
|
||||
print(expected)
|
||||
print("\n".join(TabularOutputFormatter().format_output(
|
||||
iter(data), headers, format_name='ascii')))
|
||||
assert expected == "\n".join(TabularOutputFormatter().format_output(
|
||||
iter(data), headers, format_name='ascii'))
|
||||
print(
|
||||
"\n".join(
|
||||
TabularOutputFormatter().format_output(
|
||||
iter(data), headers, format_name="ascii"
|
||||
)
|
||||
)
|
||||
)
|
||||
assert expected == "\n".join(
|
||||
TabularOutputFormatter().format_output(iter(data), headers, format_name="ascii")
|
||||
)
|
||||
|
||||
|
||||
def test_tabular_format_output_wrapper():
|
||||
"""Test the format_output wrapper."""
|
||||
data = [['1', None], ['2', 'Sam'],
|
||||
['3', 'Joe']]
|
||||
headers = ['id', 'name']
|
||||
expected = dedent('''\
|
||||
data = [["1", None], ["2", "Sam"], ["3", "Joe"]]
|
||||
headers = ["id", "name"]
|
||||
expected = dedent(
|
||||
"""\
|
||||
+----+------+
|
||||
| id | name |
|
||||
+----+------+
|
||||
| 1 | N/A |
|
||||
| 2 | Sam |
|
||||
| 3 | Joe |
|
||||
+----+------+''')
|
||||
+----+------+"""
|
||||
)
|
||||
|
||||
assert expected == "\n".join(format_output(iter(data), headers, format_name='ascii',
|
||||
missing_value='N/A'))
|
||||
assert expected == "\n".join(
|
||||
format_output(iter(data), headers, format_name="ascii", missing_value="N/A")
|
||||
)
|
||||
|
||||
|
||||
def test_additional_preprocessors():
|
||||
"""Test that additional preprocessors are run."""
|
||||
|
||||
def hello_world(data, headers, **_):
|
||||
def hello_world_data(data):
|
||||
for row in data:
|
||||
for i, value in enumerate(row):
|
||||
if value == 'hello':
|
||||
if value == "hello":
|
||||
row[i] = "{}, world".format(value)
|
||||
yield row
|
||||
|
||||
return hello_world_data(data), headers
|
||||
|
||||
data = [['foo', None], ['hello!', 'hello']]
|
||||
headers = 'ab'
|
||||
data = [["foo", None], ["hello!", "hello"]]
|
||||
headers = "ab"
|
||||
|
||||
expected = dedent('''\
|
||||
expected = dedent(
|
||||
"""\
|
||||
+--------+--------------+
|
||||
| a | b |
|
||||
+--------+--------------+
|
||||
| foo | hello |
|
||||
| hello! | hello, world |
|
||||
+--------+--------------+''')
|
||||
+--------+--------------+"""
|
||||
)
|
||||
|
||||
assert expected == "\n".join(TabularOutputFormatter().format_output(
|
||||
iter(data), headers, format_name='ascii', preprocessors=(hello_world,),
|
||||
missing_value='hello'))
|
||||
assert expected == "\n".join(
|
||||
TabularOutputFormatter().format_output(
|
||||
iter(data),
|
||||
headers,
|
||||
format_name="ascii",
|
||||
preprocessors=(hello_world,),
|
||||
missing_value="hello",
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def test_format_name_attribute():
|
||||
"""Test the the format_name attribute be set and retrieved."""
|
||||
formatter = TabularOutputFormatter(format_name='plain')
|
||||
assert formatter.format_name == 'plain'
|
||||
formatter.format_name = 'simple'
|
||||
assert formatter.format_name == 'simple'
|
||||
formatter = TabularOutputFormatter(format_name="plain")
|
||||
assert formatter.format_name == "plain"
|
||||
formatter.format_name = "simple"
|
||||
assert formatter.format_name == "simple"
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
formatter.format_name = 'foobar'
|
||||
formatter.format_name = "foobar"
|
||||
|
||||
|
||||
def test_headless_tabulate_format():
|
||||
"""Test that a headless formatter doesn't display headers"""
|
||||
formatter = TabularOutputFormatter(format_name="minimal")
|
||||
headers = ["text", "numeric"]
|
||||
data = [["a"], ["b"], ["c"]]
|
||||
expected = "a\nb\nc"
|
||||
assert expected == "\n".join(
|
||||
TabularOutputFormatter().format_output(
|
||||
iter(data),
|
||||
headers,
|
||||
format_name="minimal",
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def test_unsupported_format():
|
||||
|
@ -100,23 +134,27 @@ def test_unsupported_format():
|
|||
formatter = TabularOutputFormatter()
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
formatter.format_name = 'foobar'
|
||||
formatter.format_name = "foobar"
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
formatter.format_output((), (), format_name='foobar')
|
||||
formatter.format_output((), (), format_name="foobar")
|
||||
|
||||
|
||||
def test_tabulate_ansi_escape_in_default_value():
|
||||
"""Test that ANSI escape codes work with tabulate."""
|
||||
|
||||
data = [['1', None], ['2', 'Sam'],
|
||||
['3', 'Joe']]
|
||||
headers = ['id', 'name']
|
||||
data = [["1", None], ["2", "Sam"], ["3", "Joe"]]
|
||||
headers = ["id", "name"]
|
||||
|
||||
styled = format_output(iter(data), headers, format_name='psql',
|
||||
missing_value='\x1b[38;5;10mNULL\x1b[39m')
|
||||
unstyled = format_output(iter(data), headers, format_name='psql',
|
||||
missing_value='NULL')
|
||||
styled = format_output(
|
||||
iter(data),
|
||||
headers,
|
||||
format_name="psql",
|
||||
missing_value="\x1b[38;5;10mNULL\x1b[39m",
|
||||
)
|
||||
unstyled = format_output(
|
||||
iter(data), headers, format_name="psql", missing_value="NULL"
|
||||
)
|
||||
|
||||
stripped_styled = [strip_ansi(s) for s in styled]
|
||||
|
||||
|
@ -127,8 +165,14 @@ def test_get_type():
|
|||
"""Test that _get_type returns the expected type."""
|
||||
formatter = TabularOutputFormatter()
|
||||
|
||||
tests = ((1, int), (2.0, float), (b'binary', binary_type),
|
||||
('text', text_type), (None, type(None)), ((), text_type))
|
||||
tests = (
|
||||
(1, int),
|
||||
(2.0, float),
|
||||
(b"binary", binary_type),
|
||||
("text", text_type),
|
||||
(None, type(None)),
|
||||
((), text_type),
|
||||
)
|
||||
|
||||
for value, data_type in tests:
|
||||
assert data_type is formatter._get_type(value)
|
||||
|
@ -138,36 +182,45 @@ def test_provide_column_types():
|
|||
"""Test that provided column types are passed to preprocessors."""
|
||||
expected_column_types = (bool, float)
|
||||
data = ((1, 1.0), (0, 2))
|
||||
headers = ('a', 'b')
|
||||
headers = ("a", "b")
|
||||
|
||||
def preprocessor(data, headers, column_types=(), **_):
|
||||
assert expected_column_types == column_types
|
||||
return data, headers
|
||||
|
||||
format_output(data, headers, 'csv',
|
||||
column_types=expected_column_types,
|
||||
preprocessors=(preprocessor,))
|
||||
format_output(
|
||||
data,
|
||||
headers,
|
||||
"csv",
|
||||
column_types=expected_column_types,
|
||||
preprocessors=(preprocessor,),
|
||||
)
|
||||
|
||||
|
||||
def test_enforce_iterable():
|
||||
"""Test that all output formatters accept iterable"""
|
||||
formatter = TabularOutputFormatter()
|
||||
loremipsum = 'lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod'.split(' ')
|
||||
loremipsum = (
|
||||
"lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod".split(
|
||||
" "
|
||||
)
|
||||
)
|
||||
|
||||
for format_name in formatter.supported_formats:
|
||||
formatter.format_name = format_name
|
||||
try:
|
||||
formatted = next(formatter.format_output(
|
||||
zip(loremipsum), ['lorem']))
|
||||
formatted = next(formatter.format_output(zip(loremipsum), ["lorem"]))
|
||||
except TypeError:
|
||||
assert False, "{0} doesn't return iterable".format(format_name)
|
||||
|
||||
|
||||
def test_all_text_type():
|
||||
"""Test the TabularOutputFormatter class."""
|
||||
data = [[1, u"", None, Decimal(2)]]
|
||||
headers = ['col1', 'col2', 'col3', 'col4']
|
||||
data = [[1, "", None, Decimal(2)]]
|
||||
headers = ["col1", "col2", "col3", "col4"]
|
||||
output_formatter = TabularOutputFormatter()
|
||||
for format_name in output_formatter.supported_formats:
|
||||
for row in output_formatter.format_output(iter(data), headers, format_name=format_name):
|
||||
for row in output_formatter.format_output(
|
||||
iter(data), headers, format_name=format_name
|
||||
):
|
||||
assert isinstance(row, text_type), "not unicode for {}".format(format_name)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue