-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtest_utils.py
166 lines (119 loc) · 5.18 KB
/
test_utils.py
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
# -*- coding: utf-8 -*-
"""Unit tests for ``git_pw/utils.py``."""
import subprocess
import textwrap
import os
import yaml
from unittest import mock
from git_pw import utils
@mock.patch.object(utils.subprocess, 'check_output', return_value=b' bar ')
def test_git_config(mock_subprocess):
value = utils.git_config('foo')
assert value == 'bar'
mock_subprocess.assert_called_once_with(['git', 'config', 'foo'])
@mock.patch.object(
utils.subprocess, 'check_output', return_value=b'\xf0\x9f\xa4\xb7'
)
def test_git_config_unicode(mock_subprocess):
value = utils.git_config('foo')
assert value == u'\U0001f937'
mock_subprocess.assert_called_once_with(['git', 'config', 'foo'])
@mock.patch.object(
utils.subprocess,
'check_output',
side_effect=subprocess.CalledProcessError(1, 'xyz', '123'),
)
def test_git_config_error(mock_subprocess):
value = utils.git_config('foo')
assert value == ''
@mock.patch.object(utils, 'git_config', return_value='bar')
@mock.patch.object(utils, '_tabulate')
@mock.patch.object(utils, '_echo_via_pager')
@mock.patch.dict(os.environ, {'GIT_PAGER': 'foo', 'PAGER': 'baz'})
def test_echo_via_pager_env_GIT_PAGER(mock_inner, mock_tabulate, mock_config):
utils.echo_via_pager('test', ('foo',), None)
mock_config.assert_not_called()
mock_tabulate.assert_called_once_with('test', ('foo',), None)
mock_inner.assert_called_once_with('foo', mock_tabulate.return_value)
@mock.patch.object(utils, 'git_config', return_value='bar')
@mock.patch.object(utils, '_tabulate')
@mock.patch.object(utils, '_echo_via_pager')
@mock.patch.dict(os.environ, {'PAGER': 'baz'})
def test_echo_via_pager_config(mock_inner, mock_tabulate, mock_config):
utils.echo_via_pager('test', ('foo',), None)
mock_config.assert_called_once_with('core.pager')
mock_tabulate.assert_called_once_with('test', ('foo',), None)
mock_inner.assert_called_once_with('bar', mock_tabulate.return_value)
@mock.patch.object(utils, 'git_config', return_value=None)
@mock.patch.object(utils, '_tabulate')
@mock.patch.object(utils, '_echo_via_pager')
@mock.patch.dict(os.environ, {'PAGER': 'baz'})
def test_echo_via_pager_env_PAGER(mock_inner, mock_tabulate, mock_config):
utils.echo_via_pager('test', ('foo',), None)
mock_config.assert_called_once_with('core.pager')
mock_tabulate.assert_called_once_with('test', ('foo',), None)
mock_inner.assert_called_once_with('baz', mock_tabulate.return_value)
@mock.patch.object(utils, 'git_config', return_value=None)
@mock.patch.object(utils, '_tabulate')
@mock.patch.object(utils, '_echo_via_pager')
@mock.patch.dict(os.environ, {'PAGER': ''})
def test_echo_via_pager_env_default(mock_inner, mock_tabulate, mock_config):
utils.echo_via_pager('test', ('foo',), None)
mock_config.assert_called_once_with('core.pager')
mock_tabulate.assert_called_once_with('test', ('foo',), None)
mock_inner.assert_called_once_with('less', mock_tabulate.return_value)
def _test_tabulate(fmt):
output = [(b'foo', 'bar', u'baz', '😀', None, 1)]
headers = ('col1', 'colb', 'colIII', 'colX', 'colY', 'colZ')
result = utils._tabulate(output, headers, fmt)
return output, headers, result
@mock.patch.object(utils, 'tabulate')
def test_tabulate_table(mock_tabulate):
output, headers, result = _test_tabulate('table')
mock_tabulate.assert_called_once_with(output, headers, tablefmt='psql')
assert result == mock_tabulate.return_value
@mock.patch.object(utils, 'tabulate')
def test_tabulate_simple(mock_tabulate):
output, headers, result = _test_tabulate('simple')
mock_tabulate.assert_called_once_with(output, headers, tablefmt='simple')
assert result == mock_tabulate.return_value
@mock.patch.object(utils, 'tabulate')
def test_tabulate_csv(mock_tabulate):
output, headers, result = _test_tabulate('csv')
mock_tabulate.assert_not_called()
assert result == textwrap.dedent(
"""\
"col1","colb","colIII","colX","colY","colZ"
"foo","bar","baz","😀","","1"
"""
)
@mock.patch.object(yaml, 'dump')
def test_tabulate_yaml(mock_dump):
output, headers, result = _test_tabulate('yaml')
mock_dump.assert_called_once_with(
[
{
'col1': b'foo',
'colb': 'bar',
'coliii': u'baz',
'colx': '😀',
'coly': None,
'colz': 1,
}
],
default_flow_style=False,
)
@mock.patch.object(utils, 'git_config', return_value='simple')
@mock.patch.object(utils, 'tabulate')
def test_tabulate_git_config(mock_tabulate, mock_git_config):
output, headers, result = _test_tabulate(None)
mock_git_config.assert_called_once_with('pw.format')
mock_tabulate.assert_called_once_with(output, headers, tablefmt='simple')
assert result == mock_tabulate.return_value
@mock.patch.object(utils, 'git_config', return_value='')
@mock.patch.object(utils, 'tabulate')
def test_tabulate_default(mock_tabulate, mock_git_config):
output, headers, result = _test_tabulate(None)
mock_git_config.assert_called_once_with('pw.format')
mock_tabulate.assert_called_once_with(output, headers, tablefmt='psql')
assert result == mock_tabulate.return_value