summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth M Morton <seth.m.morton@gmail.com>2015-06-24 22:35:01 -0700
committerSeth M Morton <seth.m.morton@gmail.com>2015-06-24 22:35:01 -0700
commitd8aca1dd56638b2bfff0b70232c6801cf8b4b471 (patch)
treed3f5caa1efd8084d9816f7c14de4782179cdb256
parent37d46df8349abc660600899b3bfdaafd5b6e9ca0 (diff)
downloadnatsort-d8aca1dd56638b2bfff0b70232c6801cf8b4b471.tar.gz
Consolidated compat functions in testing.
All the try: excepts: and if: else: statements in the testing code that is related to handling different python versions or different installed modules has been relegated to a compat folder that provide an easy and uniform interface.
-rw-r--r--natsort/compat/locale.py16
-rw-r--r--setup.cfg1
-rw-r--r--test_natsort/compat/hypothesis.py (renamed from test_natsort/compat/py26.py)17
-rw-r--r--test_natsort/compat/locale.py50
-rw-r--r--test_natsort/compat/mock.py12
-rw-r--r--test_natsort/test_fake_fastnumbers.py2
-rw-r--r--test_natsort/test_locale_help.py52
-rw-r--r--test_natsort/test_main.py9
-rw-r--r--test_natsort/test_natsort.py2
-rw-r--r--test_natsort/test_utils.py46
10 files changed, 97 insertions, 110 deletions
diff --git a/natsort/compat/locale.py b/natsort/compat/locale.py
deleted file mode 100644
index 12b3789..0000000
--- a/natsort/compat/locale.py
+++ /dev/null
@@ -1,16 +0,0 @@
-from __future__ import absolute_import
-
-import locale
-
-
-def load_locale(x):
- try:
- locale.setlocale(locale.LC_ALL, str('{}.ISO8859-1'.format(x)))
- except:
- locale.setlocale(locale.LC_ALL, str('{}.UTF-8'.format(x)))
-
-try:
- load_locale('de_DE')
- has_locale_de_DE = True
-except locale.Error:
- has_locale_de_DE = False
diff --git a/setup.cfg b/setup.cfg
index 369e3cd..2bae2d6 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -11,6 +11,7 @@ flakes-ignore =
docs/source/conf.py ALL
test_natsort/test_natsort.py UnusedImport RedefinedWhileUnused
test_natsort/test_locale_help.py UnusedImport RedefinedWhileUnused
+ test_natsort/compat/* UnusedImport
pep8ignore =
natsort/ns_enum.py E126 E241 E123
diff --git a/test_natsort/compat/py26.py b/test_natsort/compat/hypothesis.py
index 15991e1..ade7277 100644
--- a/test_natsort/compat/py26.py
+++ b/test_natsort/compat/hypothesis.py
@@ -1,8 +1,12 @@
+# -*- coding: utf-8 -*-
+from __future__ import (
+ print_function,
+ division,
+ unicode_literals,
+ absolute_import
+)
import sys
-try:
- import unittest.mock as mock
-except ImportError:
- import mock
+import compat.mock
major_minor = sys.version_info[:2]
@@ -15,8 +19,9 @@ if major_minor != (2, 6):
integers_from,
sampled_from,
)
-# Otherwise mock these imports.
+# Otherwise mock these imports, because hypothesis
+# is incompatible with python 2.6.
else:
example = integers_in_range = integers_from = \
- sampled_from = assume = given = mock.MagicMock()
+ sampled_from = assume = given = compat.mock.MagicMock()
use_hypothesis = False
diff --git a/test_natsort/compat/locale.py b/test_natsort/compat/locale.py
new file mode 100644
index 0000000..245782a
--- /dev/null
+++ b/test_natsort/compat/locale.py
@@ -0,0 +1,50 @@
+# -*- coding: utf-8 -*-
+from __future__ import (
+ print_function,
+ division,
+ unicode_literals,
+ absolute_import
+)
+
+# Std. lib imports.
+import locale
+
+# Local imports
+from natsort.locale_help import use_pyicu
+from natsort.compat.py23 import py23_str
+
+
+def load_locale(x):
+ """ Convenience to load a locale, trying ISO8859-1 first."""
+ try:
+ locale.setlocale(locale.LC_ALL, str('{}.ISO8859-1'.format(x)))
+ except:
+ locale.setlocale(locale.LC_ALL, str('{}.UTF-8'.format(x)))
+
+# Check if de_DE is installed.
+try:
+ load_locale('de_DE')
+ has_locale_de_DE = True
+except locale.Error:
+ has_locale_de_DE = False
+
+# Make a function that will return the appropriate
+# strxfrm for the current locale.
+if use_pyicu:
+ from natsort.locale_help import get_pyicu_transform
+ from locale import getlocale
+
+ def get_strxfrm():
+ return get_pyicu_transform(getlocale())
+else:
+ from natsort.locale_help import strxfrm
+
+ def get_strxfrm():
+ return strxfrm
+
+# Depending on the python version, use lower or casefold
+# to make a string lowercase.
+try:
+ low = py23_str.casefold
+except AttributeError:
+ low = py23_str.lower
diff --git a/test_natsort/compat/mock.py b/test_natsort/compat/mock.py
new file mode 100644
index 0000000..9ddb302
--- /dev/null
+++ b/test_natsort/compat/mock.py
@@ -0,0 +1,12 @@
+# -*- coding: utf-8 -*-
+from __future__ import (
+ print_function,
+ division,
+ unicode_literals,
+ absolute_import
+)
+# Load mock functions from the right place.
+try:
+ from unittest.mock import MagicMock, patch, call
+except ImportError:
+ from mock import MagicMock, patch, call
diff --git a/test_natsort/test_fake_fastnumbers.py b/test_natsort/test_fake_fastnumbers.py
index 2951759..a7450ff 100644
--- a/test_natsort/test_fake_fastnumbers.py
+++ b/test_natsort/test_fake_fastnumbers.py
@@ -14,7 +14,7 @@ from natsort.fake_fastnumbers import (
isfloat,
isint,
)
-from compat.py26 import (
+from compat.hypothesis import (
assume,
given,
use_hypothesis,
diff --git a/test_natsort/test_locale_help.py b/test_natsort/test_locale_help.py
index 0e4af91..370edaa 100644
--- a/test_natsort/test_locale_help.py
+++ b/test_natsort/test_locale_help.py
@@ -10,25 +10,23 @@ from math import isnan
from itertools import chain
from natsort.compat.py23 import py23_str
from natsort.fake_fastnumbers import fast_float, isfloat
-from natsort.compat.locale import load_locale, has_locale_de_DE
from natsort.locale_help import (
grouper,
locale_convert,
use_pyicu,
)
-from compat.py26 import (
+from compat.locale import (
+ load_locale,
+ has_locale_de_DE,
+ get_strxfrm,
+ low,
+)
+from compat.hypothesis import (
assume,
given,
use_hypothesis,
)
-if use_pyicu:
- from natsort.locale_help import get_pyicu_transform
- from locale import getlocale
- strxfrm = get_pyicu_transform(getlocale())
-else:
- from natsort.locale_help import strxfrm
-
# Each test has an "example" version for demonstrative purposes,
# and a test that uses the hypothesis module.
@@ -43,10 +41,6 @@ def test_grouper_returns_letters_with_lowercase_transform_of_letter_example():
@given(py23_str)
def test_grouper_returns_letters_with_lowercase_transform_of_letter(x):
assume(type(fast_float(x)) is not float)
- try:
- low = py23_str.casefold
- except AttributeError:
- low = py23_str.lower
assert grouper(x, (fast_float, isfloat)) == ''.join(chain.from_iterable([low(y), y] for y in x))
@@ -78,12 +72,7 @@ def test_locale_convert_transforms_float_string_to_float(x):
def test_locale_convert_transforms_nonfloat_string_to_strxfrm_string_example():
load_locale('en_US')
- if use_pyicu:
- from natsort.locale_help import get_pyicu_transform
- from locale import getlocale
- strxfrm = get_pyicu_transform(getlocale())
- else:
- from natsort.locale_help import strxfrm
+ strxfrm = get_strxfrm()
assert locale_convert('45,8', (fast_float, isfloat), False) == strxfrm('45,8')
assert locale_convert('hello', (fast_float, isfloat), False) == strxfrm('hello')
locale.setlocale(locale.LC_NUMERIC, str(''))
@@ -94,24 +83,14 @@ def test_locale_convert_transforms_nonfloat_string_to_strxfrm_string_example():
def test_locale_convert_transforms_nonfloat_string_to_strxfrm_string(x):
assume(type(fast_float(x)) is not float)
load_locale('en_US')
- if use_pyicu:
- from natsort.locale_help import get_pyicu_transform
- from locale import getlocale
- strxfrm = get_pyicu_transform(getlocale())
- else:
- from natsort.locale_help import strxfrm
+ strxfrm = get_strxfrm()
assert locale_convert(x, (fast_float, isfloat), False) == strxfrm(x)
locale.setlocale(locale.LC_NUMERIC, str(''))
def test_locale_convert_with_groupletters_transforms_nonfloat_string_to_strxfrm_string_with_grouped_letters_example():
load_locale('en_US')
- if use_pyicu:
- from natsort.locale_help import get_pyicu_transform
- from locale import getlocale
- strxfrm = get_pyicu_transform(getlocale())
- else:
- from natsort.locale_help import strxfrm
+ strxfrm = get_strxfrm()
assert locale_convert('hello', (fast_float, isfloat), True) == strxfrm('hheelllloo')
assert locale_convert('45,8', (fast_float, isfloat), True) == strxfrm('4455,,88')
locale.setlocale(locale.LC_NUMERIC, str(''))
@@ -122,16 +101,7 @@ def test_locale_convert_with_groupletters_transforms_nonfloat_string_to_strxfrm_
def test_locale_convert_with_groupletters_transforms_nonfloat_string_to_strxfrm_string_with_grouped_letters(x):
assume(type(fast_float(x)) is not float)
load_locale('en_US')
- if use_pyicu:
- from natsort.locale_help import get_pyicu_transform
- from locale import getlocale
- strxfrm = get_pyicu_transform(getlocale())
- else:
- from natsort.locale_help import strxfrm
- try:
- low = py23_str.casefold
- except AttributeError:
- low = py23_str.lower
+ strxfrm = get_strxfrm()
assert locale_convert(x, (fast_float, isfloat), True) == strxfrm(''.join(chain.from_iterable([low(y), y] for y in x)))
locale.setlocale(locale.LC_NUMERIC, str(''))
diff --git a/test_natsort/test_main.py b/test_natsort/test_main.py
index 10c142b..4751c4a 100644
--- a/test_natsort/test_main.py
+++ b/test_natsort/test_main.py
@@ -7,7 +7,8 @@ import pytest
import re
import sys
from pytest import raises
-from compat.py26 import (
+from compat.mock import patch, call
+from compat.hypothesis import (
assume,
given,
integers_from,
@@ -15,12 +16,6 @@ from compat.py26 import (
sampled_from,
use_hypothesis,
)
-
-try:
- from unittest.mock import patch, call
-except ImportError:
- from mock import patch, call
-
from natsort.__main__ import (
main,
range_check,
diff --git a/test_natsort/test_natsort.py b/test_natsort/test_natsort.py
index edd664a..eac8c39 100644
--- a/test_natsort/test_natsort.py
+++ b/test_natsort/test_natsort.py
@@ -27,7 +27,7 @@ from natsort import (
as_ascii,
as_utf8,
)
-from natsort.compat.locale import load_locale, has_locale_de_DE
+from compat.locale import load_locale, has_locale_de_DE
from natsort.utils import _natsort_key
diff --git a/test_natsort/test_utils.py b/test_natsort/test_utils.py
index a4cacdd..d89d4fe 100644
--- a/test_natsort/test_utils.py
+++ b/test_natsort/test_utils.py
@@ -39,9 +39,12 @@ from slow_splitters import (
float_splitter,
sep_inserter,
)
-
-
-from compat.py26 import (
+from compat.locale import (
+ load_locale,
+ get_strxfrm,
+ low,
+)
+from compat.hypothesis import (
assume,
given,
example,
@@ -65,13 +68,6 @@ if sys.version[0] == '3':
ichain = chain.from_iterable
-def load_locale(x):
- try:
- locale.setlocale(locale.LC_ALL, str('{}.ISO8859-1'.format(x)))
- except:
- locale.setlocale(locale.LC_ALL, str('{}.UTF-8'.format(x)))
-
-
def test_do_decoding_decodes_bytes_string_to_unicode():
assert type(_do_decoding(b'bytes', 'ascii')) is py23_str
assert _do_decoding(b'bytes', 'ascii') == 'bytes'
@@ -359,10 +355,6 @@ def test_number_extracter_doubles_letters_with_lowercase_version_with_grouplette
def test_number_extracter_doubles_letters_with_lowercase_version_with_groupletters_for_float(x):
assume(len(x) <= 10)
assume(not any(type(y) == float and isnan(y) for y in x))
- try:
- low = py23_str.casefold
- except AttributeError:
- low = py23_str.lower
s = ''.join(repr(y) if type(y) in (float, long, int) else y for y in x)
t = float_splitter(s, True, True, False, '')
t = [''.join([low(z) + z for z in y]) if type(y) != float else y for y in t]
@@ -377,10 +369,6 @@ def test_number_extracter_doubles_letters_with_lowercase_version_with_grouplette
@given([float, py23_str, int])
def test_number_extracter_doubles_letters_with_lowercase_version_with_groupletters_for_int(x):
assume(len(x) <= 10)
- try:
- low = py23_str.casefold
- except AttributeError:
- low = py23_str.lower
s = ''.join(repr(y) if type(y) in (float, long, int) else y for y in x)
t = int_splitter(s, False, False, '')
t = [''.join([low(z) + z for z in y]) if type(y) not in (int, long) else y for y in t]
@@ -389,12 +377,7 @@ def test_number_extracter_doubles_letters_with_lowercase_version_with_grouplette
def test_number_extracter_extracts_numbers_and_strxfrms_strings_with_use_locale_example():
load_locale('en_US')
- if use_pyicu:
- from natsort.locale_help import get_pyicu_transform
- from locale import getlocale
- strxfrm = get_pyicu_transform(getlocale())
- else:
- from natsort.locale_help import strxfrm
+ strxfrm = get_strxfrm()
assert _number_extracter('A5+5.034E-1', _int_nosign_re, *int_nosafe_locale_nogroup) == [strxfrm('A'), 5, strxfrm('+'), 5, strxfrm('.'), 34, strxfrm('E-'), 1]
locale.setlocale(locale.LC_NUMERIC, str(''))
@@ -413,12 +396,7 @@ def test_number_extracter_extracts_numbers_and_strxfrms_strings_with_use_locale(
def test_number_extracter_extracts_numbers_and_strxfrms_letter_doubled_strings_with_use_locale_and_groupletters_example():
load_locale('en_US')
- if use_pyicu:
- from natsort.locale_help import get_pyicu_transform
- from locale import getlocale
- strxfrm = get_pyicu_transform(getlocale())
- else:
- from natsort.locale_help import strxfrm
+ strxfrm = get_strxfrm()
assert _number_extracter('A5+5.034E-1', _int_nosign_re, *int_nosafe_locale_group) == [strxfrm('aA'), 5, strxfrm('++'), 5, strxfrm('..'), 34, strxfrm('eE--'), 1]
locale.setlocale(locale.LC_NUMERIC, str(''))
@@ -626,10 +604,6 @@ def test__natsort_key_with_LOWERCASEFIRST_inverts_text_case(x):
@pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater')
@given([float, py23_str, int])
def test__natsort_key_with_GROUPLETTERS_doubles_text_with_lowercase_letter_first(x):
- try:
- low = py23_str.casefold
- except AttributeError:
- low = py23_str.lower
assume(len(x) <= 10)
assume(not any(type(y) == float and isnan(y) for y in x))
s = ''.join(ichain([repr(y)] if type(y) in (float, long, int) else [low(y), y] for y in x))
@@ -641,10 +615,6 @@ def test__natsort_key_with_GROUPLETTERS_doubles_text_with_lowercase_letter_first
@pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater')
@given([float, py23_str, int])
def test__natsort_key_with_GROUPLETTERS_and_LOWERCASEFIRST_inverts_text_first_then_doubles_letters_with_lowercase_letter_first(x):
- try:
- low = py23_str.casefold
- except AttributeError:
- low = py23_str.lower
assume(len(x) <= 10)
assume(not any(type(y) == float and isnan(y) for y in x))
s = ''.join(ichain([repr(y)] if type(y) in (float, long, int) else [low(y), y] for y in x))