Skip to content

Commit 78fb204

Browse files
authored
Merge pull request andialbrecht#263 from vmuriart/clean-tests
Clean-up tests. Fully migrate to Py.test
2 parents c56652e + 85349e6 commit 78fb204

10 files changed

Lines changed: 1367 additions & 1361 deletions

File tree

sqlparse/cli.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ def create_parser():
123123

124124
def _error(msg):
125125
"""Print msg and optionally exit with return code exit_."""
126-
sys.stderr.write('[ERROR] %s\n' % msg)
126+
sys.stderr.write('[ERROR] {0}\n'.format(msg))
127+
return 1
127128

128129

129130
def main(args=None):
@@ -137,24 +138,21 @@ def main(args=None):
137138
# TODO: Needs to deal with encoding
138139
data = ''.join(open(args.filename).readlines())
139140
except IOError as e:
140-
_error('Failed to read %s: %s' % (args.filename, e))
141-
return 1
141+
return _error('Failed to read {0}: {1}'.format(args.filename, e))
142142

143143
if args.outfile:
144144
try:
145145
stream = open(args.outfile, 'w')
146146
except IOError as e:
147-
_error('Failed to open %s: %s' % (args.outfile, e))
148-
return 1
147+
return _error('Failed to open {0}: {1}'.format(args.outfile, e))
149148
else:
150149
stream = sys.stdout
151150

152151
formatter_opts = vars(args)
153152
try:
154153
formatter_opts = sqlparse.formatter.validate_options(formatter_opts)
155154
except SQLParseError as e:
156-
_error('Invalid options: %s' % e)
157-
return 1
155+
return _error('Invalid options: {0}'.format(e))
158156

159157
s = sqlparse.format(data, **formatter_opts)
160158
if PY2:

tests/conftest.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""Helpers for testing."""
4+
5+
import io
6+
import os
7+
8+
import pytest
9+
10+
DIR_PATH = os.path.dirname(__file__)
11+
FILES_DIR = os.path.join(DIR_PATH, 'files')
12+
13+
14+
@pytest.fixture()
15+
def filepath():
16+
"""Returns full file path for test files."""
17+
18+
def make_filepath(filename):
19+
# http://stackoverflow.com/questions/18011902/parameter-to-a-fixture
20+
# Alternate solution is to use paramtrization `inderect=True`
21+
# http://stackoverflow.com/a/33879151
22+
# Syntax is noisy and requires specific variable names
23+
return os.path.join(FILES_DIR, filename)
24+
25+
return make_filepath
26+
27+
28+
@pytest.fixture()
29+
def load_file(filepath):
30+
"""Opens filename with encoding and return its contents."""
31+
32+
def make_load_file(filename, encoding='utf-8'):
33+
# http://stackoverflow.com/questions/18011902/parameter-to-a-fixture
34+
# Alternate solution is to use paramtrization `inderect=True`
35+
# http://stackoverflow.com/a/33879151
36+
# Syntax is noisy and requires specific variable names
37+
# And seems to be limited to only 1 argument.
38+
with io.open(filepath(filename), encoding=encoding) as f:
39+
return f.read()
40+
41+
return make_load_file

tests/test_cli.py

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
# -*- coding: utf-8 -*-
22

3-
import os
43
import subprocess
54
import sys
65

76
import pytest
87

98
import sqlparse
10-
import sqlparse.__main__
11-
from tests.utils import FILES_DIR
12-
13-
path = os.path.join(FILES_DIR, 'function.sql')
149

1510

1611
def test_cli_main_empty():
@@ -31,12 +26,50 @@ def test_main_help():
3126
assert exinfo.value.code == 0
3227

3328

34-
def test_valid_args():
29+
def test_valid_args(filepath):
3530
# test doesn't abort
31+
path = filepath('function.sql')
3632
assert sqlparse.cli.main([path, '-r']) is not None
3733

3834

35+
def test_invalid_choise(filepath):
36+
path = filepath('function.sql')
37+
with pytest.raises(SystemExit):
38+
sqlparse.cli.main([path, '-l', 'spanish'])
39+
40+
41+
def test_invalid_args(filepath, capsys):
42+
path = filepath('function.sql')
43+
sqlparse.cli.main([path, '-r', '--indent_width', '0'])
44+
_, err = capsys.readouterr()
45+
assert err == ("[ERROR] Invalid options: indent_width requires "
46+
"a positive integer\n")
47+
48+
49+
def test_invalid_infile(filepath, capsys):
50+
path = filepath('missing.sql')
51+
sqlparse.cli.main([path, '-r'])
52+
_, err = capsys.readouterr()
53+
assert err[:22] == "[ERROR] Failed to read"
54+
55+
56+
def test_invalid_outfile(filepath, capsys):
57+
path = filepath('function.sql')
58+
outpath = filepath('/missing/function.sql')
59+
sqlparse.cli.main([path, '-r', '-o', outpath])
60+
_, err = capsys.readouterr()
61+
assert err[:22] == "[ERROR] Failed to open"
62+
63+
64+
def test_stdout(filepath, load_file, capsys):
65+
path = filepath('begintag.sql')
66+
expected = load_file('begintag.sql')
67+
sqlparse.cli.main([path])
68+
out, _ = capsys.readouterr()
69+
assert out == expected
70+
71+
3972
def test_script():
4073
# Call with the --help option as a basic sanity check.
41-
cmdl = "{0:s} -m sqlparse.cli --help".format(sys.executable)
42-
assert subprocess.call(cmdl.split()) == 0
74+
cmd = "{0:s} -m sqlparse.cli --help".format(sys.executable)
75+
assert subprocess.call(cmd.split()) == 0

0 commit comments

Comments
 (0)