summaryrefslogtreecommitdiff
path: root/tests/test_cli.py
diff options
context:
space:
mode:
authorVictor Uriarte <vmuriart@gmail.com>2017-01-22 23:55:25 -0700
committerGitHub <noreply@github.com>2017-01-22 23:55:25 -0700
commit35c6db5d6572479aced1b113763190b8ae85d78a (patch)
tree86a3ae298553e857ef0e0c7ac75ba95318600eca /tests/test_cli.py
parentc92e281c19f43bbcb945b5a8c43211263ee25386 (diff)
parente312917f58542b4c202cb5970f72f49e1e8599f9 (diff)
downloadsqlparse-35c6db5d6572479aced1b113763190b8ae85d78a.tar.gz
Merge pull request #317 from twang2218/cli-add-encoding
Add --encoding option in CLI
Diffstat (limited to 'tests/test_cli.py')
-rw-r--r--tests/test_cli.py68
1 files changed, 68 insertions, 0 deletions
diff --git a/tests/test_cli.py b/tests/test_cli.py
index 77a764e..c1a5a75 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -73,3 +73,71 @@ def test_script():
# Call with the --help option as a basic sanity check.
cmd = "{0:s} -m sqlparse.cli --help".format(sys.executable)
assert subprocess.call(cmd.split()) == 0
+
+
+def test_encoding_utf8_stdout(filepath, load_file, capfd):
+ path = filepath('encoding_utf8.sql')
+ expected = load_file('encoding_utf8.sql', 'utf-8')
+ sys.stdout.encoding = 'utf-8'
+ sqlparse.cli.main([path])
+ out, _ = capfd.readouterr()
+ assert out == expected
+
+
+def test_encoding_utf8_output_file(filepath, load_file, tmpdir):
+ in_path = filepath('encoding_utf8.sql')
+ expected = load_file('encoding_utf8.sql', 'utf-8')
+ out_path = tmpdir.dirname + '/encoding_utf8.out.sql'
+ sqlparse.cli.main([in_path, '-o', out_path])
+ out = load_file(out_path, 'utf-8')
+ assert out == expected
+
+
+def test_encoding_gbk_stdout(filepath, load_file, capfd):
+ path = filepath('encoding_gbk.sql')
+ expected = load_file('encoding_gbk.sql', 'gbk')
+ sys.stdout.encoding = 'gbk'
+ sqlparse.cli.main([path, '--encoding', 'gbk'])
+ out, _ = capfd.readouterr()
+ assert out == expected
+
+
+def test_encoding_gbk_output_file(filepath, load_file, tmpdir):
+ in_path = filepath('encoding_gbk.sql')
+ expected = load_file('encoding_gbk.sql', 'gbk')
+ out_path = tmpdir.dirname + '/encoding_gbk.out.sql'
+ sqlparse.cli.main([in_path, '--encoding', 'gbk', '-o', out_path])
+ out = load_file(out_path, 'gbk')
+ assert out == expected
+
+
+def test_encoding_stdin_utf8(filepath, load_file, capfd):
+ path = filepath('encoding_utf8.sql')
+ expected = load_file('encoding_utf8.sql', 'utf-8')
+ old_stdin = sys.stdin
+ sys.stdin = open(path, 'r')
+ sys.stdout.encoding = 'utf-8'
+ sqlparse.cli.main(['-'])
+ sys.stdin = old_stdin
+ out, _ = capfd.readouterr()
+ assert out == expected
+
+
+def test_encoding_stdin_gbk(filepath, load_file, capfd):
+ path = filepath('encoding_gbk.sql')
+ expected = load_file('encoding_gbk.sql', 'gbk')
+ old_stdin = sys.stdin
+ sys.stdin = open(path, 'r')
+ sys.stdout.encoding = 'gbk'
+ sqlparse.cli.main(['-', '--encoding', 'gbk'])
+ sys.stdin = old_stdin
+ out, _ = capfd.readouterr()
+ assert out == expected
+
+
+def test_encoding(filepath, capsys):
+ path = filepath('test_cp1251.sql')
+ expected = u'insert into foo values (1); -- Песня про надежду\n'
+ sqlparse.cli.main([path, '--encoding=cp1251'])
+ out, _ = capsys.readouterr()
+ assert out == expected