test\unit\test_helper.py
-- -- --- --- --- --- --- --- ------- ------- ------- | from io import BytesIO, StringIO, TextIOWrapper
-- -- --- --- --- --- --- --- ------- ------- ------- | from unittest import mock
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- | import tabulate
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- | from wily.defaults import DEFAULT_GRID_STYLE
-- -- --- --- --- --- --- --- ------- ------- ------- | from wily.helper import get_maxcolwidth, get_style
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- | SHORT_DATA = [list("abcdefgh"), list("abcdefgh")]
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- | MEDIUM_DATA = [["medium_data"] * 2, ["medium_data"] * 2]
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- | LONG_DATA = [["long_data"] * 8, ["long_data"] * 8]
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- | HUGE_DATA = [["huge_data"] * 18, ["huge_data"] * 18]
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- | LONG_LINE_MEDIUM_DATA = [
-- -- --- --- --- --- --- --- ------- ------- ------- | ["long_line_for_some_medium_data"] * 2,
-- -- --- --- --- --- --- --- ------- ------- ------- | ["long_line_for_some_medium_data"] * 2,
-- -- --- --- --- --- --- --- ------- ------- ------- | ]
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def test_get_maxcolwidth_no_wrap():
-- -- 001 002 001 002 003 003 0004.75 0002.38 0000.50 | result = get_maxcolwidth([], False)
-- -- 001 002 001 002 003 003 0004.75 0002.38 0000.50 | assert result is None
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- 02 003 005 003 006 008 009 0027.00 0048.60 0001.80 | def test_get_maxcolwidth_wrap_short():
-- 02 003 005 003 006 008 009 0027.00 0048.60 0001.80 | for width in range(35, 100):
-- 02 003 005 003 006 008 009 0027.00 0048.60 0001.80 | mock_get_terminal_size = mock.Mock(return_value=(width, 24))
-- 02 003 005 003 006 008 009 0027.00 0048.60 0001.80 | mock_shutil = mock.Mock(get_terminal_size=mock_get_terminal_size)
-- 02 003 005 003 006 008 009 0027.00 0048.60 0001.80 |
-- 02 003 005 003 006 008 009 0027.00 0048.60 0001.80 | with mock.patch("wily.helper.shutil", mock_shutil):
-- 02 003 005 003 006 008 009 0027.00 0048.60 0001.80 | result = get_maxcolwidth(SHORT_DATA[0], True)
-- 02 003 005 003 006 008 009 0027.00 0048.60 0001.80 | as_table = tabulate.tabulate(
-- 02 003 005 003 006 008 009 0027.00 0048.60 0001.80 | tabular_data=SHORT_DATA,
-- 02 003 005 003 006 008 009 0027.00 0048.60 0001.80 | tablefmt="grid",
-- 02 003 005 003 006 008 009 0027.00 0048.60 0001.80 | maxcolwidths=result,
-- 02 003 005 003 006 008 009 0027.00 0048.60 0001.80 | maxheadercolwidths=result,
-- 02 003 005 003 006 008 009 0027.00 0048.60 0001.80 | )
-- 02 003 005 003 006 008 009 0027.00 0048.60 0001.80 |
-- 02 003 005 003 006 008 009 0027.00 0048.60 0001.80 | line = as_table.splitlines()[0]
-- -- 003 005 003 006 008 009 0027.00 0048.60 0001.80 | assert len(line) < width
-- -- 003 005 003 006 008 009 0027.00 0048.60 0001.80 | assert len(line) >= width / 3
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | def test_get_maxcolwidth_wrap_medium():
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | for width in range(35, 100):
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | mock_get_terminal_size = mock.Mock(return_value=(width, 24))
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | mock_shutil = mock.Mock(get_terminal_size=mock_get_terminal_size)
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 |
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | with mock.patch("wily.helper.shutil", mock_shutil):
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | result = get_maxcolwidth(MEDIUM_DATA[0], True)
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | as_table = tabulate.tabulate(
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | tabular_data=MEDIUM_DATA,
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | tablefmt="grid",
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | maxcolwidths=result,
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | maxheadercolwidths=result,
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | )
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 |
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | line = as_table.splitlines()[0]
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | print(line)
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | print(width, len(line))
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | assert len(line) < width
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | if width < 85:
-- -- 003 006 004 008 009 012 0038.04 0076.08 0002.00 | assert len(line) >= width / 3
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | def test_get_maxcolwidth_wrap_long_line_medium():
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | for width in range(35, 100):
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | mock_get_terminal_size = mock.Mock(return_value=(width, 24))
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | mock_shutil = mock.Mock(get_terminal_size=mock_get_terminal_size)
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 |
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | with mock.patch("wily.helper.shutil", mock_shutil):
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | result = get_maxcolwidth(LONG_LINE_MEDIUM_DATA[0], True)
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | as_table = tabulate.tabulate(
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | tabular_data=LONG_LINE_MEDIUM_DATA,
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | tablefmt="grid",
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | maxcolwidths=result,
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | maxheadercolwidths=result,
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | )
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 |
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | line = as_table.splitlines()[0]
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | print(line)
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | print(width, len(line))
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | assert len(line) < width
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | if width < 85:
-- -- 003 006 004 008 009 012 0038.04 0076.08 0002.00 | assert len(line) >= width / 3
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | def test_get_maxcolwidth_wrap_long():
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | for width in range(35, 290):
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | mock_get_terminal_size = mock.Mock(return_value=(width, 24))
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | mock_shutil = mock.Mock(get_terminal_size=mock_get_terminal_size)
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 |
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | with mock.patch("wily.helper.shutil", mock_shutil):
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | result = get_maxcolwidth(LONG_DATA[0], True)
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | as_table = tabulate.tabulate(
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | tabular_data=LONG_DATA,
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | tablefmt="grid",
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | maxcolwidths=result,
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | maxheadercolwidths=result,
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | )
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 |
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | line = as_table.splitlines()[0]
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | assert len(line) < width
-- 03 003 006 004 008 009 012 0038.04 0076.08 0002.00 | if width < 290:
-- -- 003 006 004 008 009 012 0038.04 0076.08 0002.00 | assert len(line) >= width / 3
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- 03 003 009 006 012 012 018 0064.53 0129.06 0002.00 | def test_get_maxcolwidth_wrap_huge():
-- 03 003 009 006 012 012 018 0064.53 0129.06 0002.00 | for width in range(75, 450):
-- 03 003 009 006 012 012 018 0064.53 0129.06 0002.00 | mock_get_terminal_size = mock.Mock(return_value=(width, 24))
-- 03 003 009 006 012 012 018 0064.53 0129.06 0002.00 | mock_shutil = mock.Mock(get_terminal_size=mock_get_terminal_size)
-- 03 003 009 006 012 012 018 0064.53 0129.06 0002.00 |
-- 03 003 009 006 012 012 018 0064.53 0129.06 0002.00 | with mock.patch("wily.helper.shutil", mock_shutil):
-- 03 003 009 006 012 012 018 0064.53 0129.06 0002.00 | result = get_maxcolwidth(HUGE_DATA[0], True)
-- 03 003 009 006 012 012 018 0064.53 0129.06 0002.00 | as_table = tabulate.tabulate(
-- 03 003 009 006 012 012 018 0064.53 0129.06 0002.00 | tabular_data=HUGE_DATA,
-- 03 003 009 006 012 012 018 0064.53 0129.06 0002.00 | tablefmt="grid",
-- 03 003 009 006 012 012 018 0064.53 0129.06 0002.00 | maxcolwidths=result,
-- 03 003 009 006 012 012 018 0064.53 0129.06 0002.00 | maxheadercolwidths=result,
-- 03 003 009 006 012 012 018 0064.53 0129.06 0002.00 | )
-- 03 003 009 006 012 012 018 0064.53 0129.06 0002.00 |
-- 03 003 009 006 012 012 018 0064.53 0129.06 0002.00 | line = as_table.splitlines()[0]
-- 03 003 009 006 012 012 018 0064.53 0129.06 0002.00 | assert len(line) < width
-- 03 003 009 006 012 012 018 0064.53 0129.06 0002.00 | if width < 220:
-- -- 003 009 006 012 012 018 0064.53 0129.06 0002.00 | assert len(line) >= width / 3
-- -- 003 009 006 012 012 018 0064.53 0129.06 0002.00 | else:
-- -- 003 009 006 012 012 018 0064.53 0129.06 0002.00 | assert len(line) >= width / 4
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def test_get_style():
-- -- 001 002 001 002 003 003 0004.75 0002.38 0000.50 | output = TextIOWrapper(BytesIO(), encoding="utf-8")
-- -- 001 002 001 002 003 003 0004.75 0002.38 0000.50 | with mock.patch("sys.stdout", output):
-- -- 001 002 001 002 003 003 0004.75 0002.38 0000.50 | style = get_style()
-- -- 001 002 001 002 003 003 0004.75 0002.38 0000.50 | assert style == DEFAULT_GRID_STYLE
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def test_get_style_charmap():
-- -- 001 002 001 002 003 003 0004.75 0002.38 0000.50 | output = TextIOWrapper(BytesIO(), encoding="charmap")
-- -- 001 002 001 002 003 003 0004.75 0002.38 0000.50 | with mock.patch("sys.stdout", output):
-- -- 001 002 001 002 003 003 0004.75 0002.38 0000.50 | style = get_style()
-- -- 001 002 001 002 003 003 0004.75 0002.38 0000.50 | assert style == "grid"
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def test_get_style_charmap_not_default_grid_style():
-- -- 001 002 001 002 003 003 0004.75 0002.38 0000.50 | output = TextIOWrapper(BytesIO(), encoding="charmap")
-- -- 001 002 001 002 003 003 0004.75 0002.38 0000.50 | with mock.patch("sys.stdout", output):
-- -- 001 002 001 002 003 003 0004.75 0002.38 0000.50 | style = get_style("something_else")
-- -- 001 002 001 002 003 003 0004.75 0002.38 0000.50 | assert style == "something_else"
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def test_get_style_none():
-- -- 001 002 001 002 003 003 0004.75 0002.38 0000.50 | output = StringIO() # output.encoding is None
-- -- --- --- --- --- --- --- ------- ------- ------- | with mock.patch("sys.stdout", output):
-- -- --- --- --- --- --- --- ------- ------- ------- | style = get_style()
-- -- --- --- --- --- --- --- ------- ------- ------- | assert style == "fancy_grid"