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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
# -*- coding: utf-8 -*-
"""Test the generic output formatter interface."""
from __future__ import unicode_literals
from decimal import Decimal
from textwrap import dedent
import pytest
from cli_helpers.tabular_output import format_output, TabularOutputFormatter
from cli_helpers.compat import binary_type, text_type
from cli_helpers.utils import strip_ansi
def test_tabular_output_formatter():
"""Test the TabularOutputFormatter class."""
headers = ["text", "numeric"]
data = [
["abc", Decimal(1)],
["defg", Decimal("11.1")],
["hi", Decimal("1.1")],
["Pablo\rß\n", 0],
]
expected = dedent(
"""\
+-------+---------+
| text | numeric |
+-------+---------+
| abc | 1 |
| defg | 11.1 |
| hi | 1.1 |
| Pablo | 0 |
| ß | |
+-------+---------+"""
)
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")
)
def test_tabular_output_escaped():
"""Test the ascii_escaped output format."""
headers = ["text", "numeric"]
data = [
["abc", Decimal(1)],
["defg", Decimal("11.1")],
["hi", Decimal("1.1")],
["Pablo\rß\n", 0],
]
expected = dedent(
"""\
+------------+---------+
| text | numeric |
+------------+---------+
| abc | 1 |
| defg | 11.1 |
| hi | 1.1 |
| Pablo\\rß\\n | 0 |
+------------+---------+"""
)
print(expected)
print(
"\n".join(
TabularOutputFormatter().format_output(
iter(data), headers, format_name="ascii_escaped"
)
)
)
assert expected == "\n".join(
TabularOutputFormatter().format_output(
iter(data), headers, format_name="ascii_escaped"
)
)
def test_tabular_output_mysql():
"""Test the mysql output format."""
headers = ["text", "numeric"]
data = [
["abc", Decimal(1)],
["defg", Decimal("11.1")],
["hi", Decimal("1.1")],
["Pablo\rß\n", 0],
]
expected = dedent(
"""\
+-------+---------+
| text | numeric |
+-------+---------+
| abc | 1 |
| defg | 11.1 |
| hi | 1.1 |
| Pablo | 0 |
| ß | |
+-------+---------+"""
)
print(expected)
print(
"\n".join(
TabularOutputFormatter().format_output(
iter(data), headers, format_name="mysql"
)
)
)
assert expected == "\n".join(
TabularOutputFormatter().format_output(iter(data), headers, format_name="mysql")
)
def test_tabular_output_mysql_unicode():
"""Test the mysql_unicode output format."""
headers = ["text", "numeric"]
data = [
["abc", Decimal(1)],
["defg", Decimal("11.1")],
["hi", Decimal("1.1")],
["Pablo\rß\n", 0],
]
expected = dedent(
"""\
┌───────┬─────────┐
│ text │ numeric │
├───────┼─────────┤
│ abc │ 1 │
│ defg │ 11.1 │
│ hi │ 1.1 │
│ Pablo │ 0 │
│ ß │ │
└───────┴─────────┘"""
)
print(expected)
print(
"\n".join(
TabularOutputFormatter().format_output(
iter(data), headers, format_name="mysql_unicode"
)
)
)
assert expected == "\n".join(
TabularOutputFormatter().format_output(
iter(data), headers, format_name="mysql_unicode"
)
)
def test_tabular_format_output_wrapper():
"""Test the format_output wrapper."""
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")
)
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":
row[i] = "{}, world".format(value)
yield row
return hello_world_data(data), headers
data = [["foo", None], ["hello!", "hello"]]
headers = "ab"
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",
)
)
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"
with pytest.raises(ValueError):
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():
"""Test that TabularOutputFormatter rejects unknown formats."""
formatter = TabularOutputFormatter()
with pytest.raises(ValueError):
formatter.format_name = "foobar"
with pytest.raises(ValueError):
formatter.format_output((), (), format_name="foobar")
def test_supported_json_formats():
"""Test that the JSONl formats are known."""
formatter = TabularOutputFormatter()
assert "jsonl" in formatter.supported_formats
assert "jsonl_escaped" in formatter.supported_formats
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"]
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]
assert list(unstyled) == stripped_styled
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),
)
for value, data_type in tests:
assert data_type is formatter._get_type(value)
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")
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,),
)
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(
" "
)
)
for format_name in formatter.supported_formats:
formatter.format_name = format_name
try:
formatted = next(formatter.format_output(zip(loremipsum), ["lorem"]))
except TypeError:
assert False, "{0} doesn't return iterable".format(format_name)
@pytest.mark.parametrize(
"extra_kwargs",
[
{},
{"style": "default"},
{"style": "colorful"},
],
)
def test_all_text_type(extra_kwargs):
"""Test the TabularOutputFormatter class."""
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, **extra_kwargs
):
assert isinstance(row, text_type), "not unicode for {}".format(format_name)
|