summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Uriarte <victor.m.uriarte@intel.com>2016-06-19 09:13:36 -0700
committerVictor Uriarte <victor.m.uriarte@intel.com>2016-06-20 07:40:20 -0700
commitc9e8230502fd2c72833a2ea4d2c6bac9234e580a (patch)
tree3def49609e6821ef25ffef38741e7b134de31ccd
parent64b9d3537783dbf761b421644ba07078a80b5640 (diff)
downloadsqlparse-c9e8230502fd2c72833a2ea4d2c6bac9234e580a.tar.gz
Replace tests/utils with conftest.py
http://stackoverflow.com/questions/34466027/what-is-the-use-of-conftest-py
-rw-r--r--tests/conftest.py41
-rw-r--r--tests/test_cli.py11
-rw-r--r--tests/test_regressions.py8
-rw-r--r--tests/test_split.py15
-rw-r--r--tests/utils.py15
5 files changed, 55 insertions, 35 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
new file mode 100644
index 0000000..d5621eb
--- /dev/null
+++ b/tests/conftest.py
@@ -0,0 +1,41 @@
+# -*- coding: utf-8 -*-
+
+"""Helpers for testing."""
+
+import io
+import os
+
+import pytest
+
+DIR_PATH = os.path.dirname(__file__)
+FILES_DIR = os.path.join(DIR_PATH, 'files')
+
+
+@pytest.fixture()
+def filepath():
+ """Returns full file path for test files."""
+
+ def make_filepath(filename):
+ # http://stackoverflow.com/questions/18011902/parameter-to-a-fixture
+ # Alternate solution is to use paramtrization `inderect=True`
+ # http://stackoverflow.com/a/33879151
+ # Syntax is noisy and requires specific variable names
+ return os.path.join(FILES_DIR, filename)
+
+ return make_filepath
+
+
+@pytest.fixture()
+def load_file(filepath):
+ """Opens filename with encoding and return its contents."""
+
+ def make_load_file(filename, encoding='utf-8'):
+ # http://stackoverflow.com/questions/18011902/parameter-to-a-fixture
+ # Alternate solution is to use paramtrization `inderect=True`
+ # http://stackoverflow.com/a/33879151
+ # Syntax is noisy and requires specific variable names
+ # And seems to be limited to only 1 argument.
+ with io.open(filepath(filename), encoding=encoding) as f:
+ return f.read()
+
+ return make_load_file
diff --git a/tests/test_cli.py b/tests/test_cli.py
index 6b66e5f..51ee61b 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -1,15 +1,11 @@
# -*- coding: utf-8 -*-
-import os
import subprocess
import sys
import pytest
import sqlparse
-from tests.utils import FILES_DIR
-
-path = os.path.join(FILES_DIR, 'function.sql')
def test_cli_main_empty():
@@ -30,12 +26,13 @@ def test_main_help():
assert exinfo.value.code == 0
-def test_valid_args():
+def test_valid_args(filepath):
# test doesn't abort
+ path = filepath('function.sql')
assert sqlparse.cli.main([path, '-r']) is not None
def test_script():
# Call with the --help option as a basic sanity check.
- cmdl = "{0:s} -m sqlparse.cli --help".format(sys.executable)
- assert subprocess.call(cmdl.split()) == 0
+ cmd = "{0:s} -m sqlparse.cli --help".format(sys.executable)
+ assert subprocess.call(cmd.split()) == 0
diff --git a/tests/test_regressions.py b/tests/test_regressions.py
index ee2ece7..10fd4a1 100644
--- a/tests/test_regressions.py
+++ b/tests/test_regressions.py
@@ -1,12 +1,10 @@
# -*- coding: utf-8 -*-
-import sys
-
import pytest # noqa
import sqlparse
from sqlparse import sql, tokens as T
-from tests.utils import load_file
+from sqlparse.compat import PY2
class RegressionTests(object):
@@ -171,7 +169,7 @@ def test_parse_sql_with_binary():
sql = "select * from foo where bar = '{0}'".format(digest)
formatted = sqlparse.format(sql, reindent=True)
tformatted = "select *\nfrom foo\nwhere bar = '{0}'".format(digest)
- if sys.version_info < (3,):
+ if PY2:
tformatted = tformatted.decode('unicode-escape')
assert formatted == tformatted
@@ -186,7 +184,7 @@ def test_dont_alias_keywords():
assert p.tokens[2].ttype is T.Keyword
-def test_format_accepts_encoding(): # issue20
+def test_format_accepts_encoding(load_file): # issue20
sql = load_file('test_cp1251.sql', 'cp1251')
formatted = sqlparse.format(sql, reindent=True, encoding='cp1251')
tformatted = u'insert into foo\nvalues (1); -- Песня про надежду\n'
diff --git a/tests/test_split.py b/tests/test_split.py
index 72f9682..5968806 100644
--- a/tests/test_split.py
+++ b/tests/test_split.py
@@ -8,7 +8,6 @@ import pytest # noqa
import sqlparse
from sqlparse.compat import StringIO, text_type
-from tests.utils import load_file
class SQLSplitTest(object):
@@ -28,31 +27,31 @@ class SQLSplitTest(object):
stmts = sqlparse.parse(r"select '\\'; select '\''; select '\\\'';")
assert len(stmts) == 3
- def test_create_function(self):
+ def test_create_function(self, load_file):
sql = load_file('function.sql')
stmts = sqlparse.parse(sql)
assert len(stmts) == 1
assert str(stmts[0]) == sql
- def test_create_function_psql(self):
+ def test_create_function_psql(self, load_file):
sql = load_file('function_psql.sql')
stmts = sqlparse.parse(sql)
assert len(stmts) == 1
assert str(stmts[0]) == sql
- def test_create_function_psql3(self):
+ def test_create_function_psql3(self, load_file):
sql = load_file('function_psql3.sql')
stmts = sqlparse.parse(sql)
assert len(stmts) == 1
assert str(stmts[0]) == sql
- def test_create_function_psql2(self):
+ def test_create_function_psql2(self, load_file):
sql = load_file('function_psql2.sql')
stmts = sqlparse.parse(sql)
assert len(stmts) == 1
assert str(stmts[0]) == sql
- def test_dashcomments(self):
+ def test_dashcomments(self, load_file):
sql = load_file('dashcomment.sql')
stmts = sqlparse.parse(sql)
assert len(stmts) == 3
@@ -68,13 +67,13 @@ class SQLSplitTest(object):
stmts = sqlparse.parse('select foo; -- comment')
assert len(stmts) == 1
- def test_begintag(self):
+ def test_begintag(self, load_file):
sql = load_file('begintag.sql')
stmts = sqlparse.parse(sql)
assert len(stmts) == 3
assert ''.join(str(q) for q in stmts) == sql
- def test_begintag_2(self):
+ def test_begintag_2(self, load_file):
sql = load_file('begintag_2.sql')
stmts = sqlparse.parse(sql)
assert len(stmts) == 1
diff --git a/tests/utils.py b/tests/utils.py
deleted file mode 100644
index 63e7e7b..0000000
--- a/tests/utils.py
+++ /dev/null
@@ -1,15 +0,0 @@
-# -*- coding: utf-8 -*-
-
-"""Helpers for testing."""
-
-import io
-import os
-
-DIR_PATH = os.path.dirname(__file__)
-FILES_DIR = os.path.join(DIR_PATH, 'files')
-
-
-def load_file(filename, encoding='utf-8'):
- """Opens filename with encoding and return its contents."""
- with io.open(os.path.join(FILES_DIR, filename), encoding=encoding) as f:
- return f.read()