This repository was archived by the owner on Mar 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathtest_commandline.py
More file actions
147 lines (107 loc) · 3.93 KB
/
test_commandline.py
File metadata and controls
147 lines (107 loc) · 3.93 KB
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
from click.testing import CliRunner
from coreapi import __version__ as version
from coreapi import Document, Link
from coreapi.commandline import client, coerce_key_types
from coreapi.transports import HTTPTransport
import pytest
import os
import shutil
import tempfile
mock_response = None
def set_response(doc):
global mock_response
mock_response = doc
@pytest.fixture(scope="function")
def cli(request):
"""
A fixture returning a runner for the command line client.
"""
config_dir = tempfile.mkdtemp()
os.environ['COREAPI_CONFIG_DIR'] = config_dir
saved = HTTPTransport.transition
def transition(*args, **kwargs):
return mock_response
def finalize():
shutil.rmtree(config_dir)
HTTPTransport.transition = saved
def _cli(*args):
return runner.invoke(client, args)
runner = CliRunner()
request.addfinalizer(finalize)
HTTPTransport.transition = transition
return _cli
# Integration tests
def test_no_command(cli):
result = cli()
assert result.output.startswith('Usage:')
def test_version_option(cli):
result = cli('--version')
assert result.output == 'coreapi version %s\n' % version
def test_cli_get(cli):
set_response(Document('http://example.com', 'Example'))
result = cli('get', 'http://mock')
assert result.output == '<Example "http://example.com">\n'
result = cli('show')
assert result.output == '<Example "http://example.com">\n'
def test_cli_clear(cli):
set_response(Document('http://example.com', 'Example'))
result = cli('get', 'http://mock')
cli('clear')
result = cli('show')
assert result.output == 'No current document. Use `coreapi get` to fetch a document first.\n'
assert result.exit_code == 1
def test_cli_reload(cli):
result = cli('reload')
assert result.output == 'No current document. Use `coreapi get` to fetch a document first.\n'
assert result.exit_code == 1
set_response(Document('http://example.com', 'Example'))
result = cli('get', 'http://mock')
set_response(Document('http://example.com', 'New'))
cli('reload')
result = cli('show')
assert result.output == '<New "http://example.com">\n'
# History
def test_cli_history(cli):
set_response(Document('http://1.com'))
result = cli('get', 'http://mock')
set_response(Document('http://2.com'))
result = cli('get', 'http://mock')
result = cli('history', 'show')
assert result.output == (
'History\n'
'[*] <Document "http://2.com">\n'
'[ ] <Document "http://1.com">\n'
)
set_response(Document('http://1.com'))
result = cli('history', 'back')
result = cli('history', 'show')
assert result.output == (
'History\n'
'[ ] <Document "http://2.com">\n'
'[*] <Document "http://1.com">\n'
)
result = cli('show')
assert result.output == '<Document "http://1.com">\n'
set_response(Document('http://2.com'))
result = cli('history', 'forward')
result = cli('history', 'show')
assert result.output == (
'History\n'
'[*] <Document "http://2.com">\n'
'[ ] <Document "http://1.com">\n'
)
result = cli('show')
assert result.output == '<Document "http://2.com">\n'
# Test dotted path notation maps to list of keys correctly.
def test_dotted_path_notation():
doc = Document(content={'rows': [Document(content={'edit': Link()})]})
keys = coerce_key_types(doc, ['rows', 0, 'edit'])
assert keys == ['rows', 0, 'edit']
def test_dotted_path_notation_with_invalid_array_lookup():
doc = Document(content={'rows': [Document(content={'edit': Link()})]})
keys = coerce_key_types(doc, ['rows', 'zero', 'edit'])
assert keys == ['rows', 'zero', 'edit']
def test_dotted_path_notation_with_invalid_key():
doc = Document(content={'rows': [Document(content={'edit': Link()})]})
keys = coerce_key_types(doc, ['dummy', '0', 'edit'])
assert keys == ['dummy', '0', 'edit']