|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
1 | 3 | """Unit tests for ``git_pw/utils.py``."""
|
2 | 4 |
|
3 | 5 | import subprocess
|
| 6 | +import textwrap |
4 | 7 | import os
|
5 | 8 |
|
6 | 9 | import mock
|
@@ -78,3 +81,59 @@ def test_echo_via_pager_env_default(mock_inner, mock_tabulate, mock_config):
|
78 | 81 | mock_config.assert_called_once_with('core.parser')
|
79 | 82 | mock_tabulate.assert_called_once_with('test', ('foo',), None)
|
80 | 83 | mock_inner.assert_called_once_with('less', mock_tabulate.return_value)
|
| 84 | + |
| 85 | + |
| 86 | +def _test_tabulate(fmt): |
| 87 | + output = [(b'foo', 'bar', u'baz', '😀', None)] |
| 88 | + headers = ('col1', 'colb', 'colIII', 'colX', 'colY') |
| 89 | + |
| 90 | + result = utils._tabulate(output, headers, fmt) |
| 91 | + |
| 92 | + return output, headers, result |
| 93 | + |
| 94 | + |
| 95 | +@mock.patch.object(utils, 'tabulate') |
| 96 | +def test_tabulate_table(mock_tabulate): |
| 97 | + output, headers, result = _test_tabulate('table') |
| 98 | + |
| 99 | + mock_tabulate.assert_called_once_with(output, headers, tablefmt='psql') |
| 100 | + assert result == mock_tabulate.return_value |
| 101 | + |
| 102 | + |
| 103 | +@mock.patch.object(utils, 'tabulate') |
| 104 | +def test_tabulate_simple(mock_tabulate): |
| 105 | + output, headers, result = _test_tabulate('simple') |
| 106 | + |
| 107 | + mock_tabulate.assert_called_once_with(output, headers, tablefmt='simple') |
| 108 | + assert result == mock_tabulate.return_value |
| 109 | + |
| 110 | + |
| 111 | +@mock.patch.object(utils, 'tabulate') |
| 112 | +def test_tabulate_csv(mock_tabulate): |
| 113 | + output, headers, result = _test_tabulate('csv') |
| 114 | + |
| 115 | + mock_tabulate.assert_not_called() |
| 116 | + assert result == textwrap.dedent("""\ |
| 117 | + "col1","colb","colIII","colX","colY" |
| 118 | + "foo","bar","baz","😀","" |
| 119 | + """) |
| 120 | + |
| 121 | + |
| 122 | +@mock.patch.object(utils, 'git_config', return_value='simple') |
| 123 | +@mock.patch.object(utils, 'tabulate') |
| 124 | +def test_tabulate_git_config(mock_tabulate, mock_git_config): |
| 125 | + output, headers, result = _test_tabulate(None) |
| 126 | + |
| 127 | + mock_git_config.assert_called_once_with('pw.format') |
| 128 | + mock_tabulate.assert_called_once_with(output, headers, tablefmt='simple') |
| 129 | + assert result == mock_tabulate.return_value |
| 130 | + |
| 131 | + |
| 132 | +@mock.patch.object(utils, 'git_config', return_value='') |
| 133 | +@mock.patch.object(utils, 'tabulate') |
| 134 | +def test_tabulate_default(mock_tabulate, mock_git_config): |
| 135 | + output, headers, result = _test_tabulate(None) |
| 136 | + |
| 137 | + mock_git_config.assert_called_once_with('pw.format') |
| 138 | + mock_tabulate.assert_called_once_with(output, headers, tablefmt='psql') |
| 139 | + assert result == mock_tabulate.return_value |
0 commit comments