Skip to content

Commit

Permalink
Reduced code logic redundancy in pathlib.Path test
Browse files Browse the repository at this point in the history
  • Loading branch information
clintval committed Mar 21, 2018
1 parent 279b4d7 commit 1a33339
Showing 1 changed file with 5 additions and 25 deletions.
30 changes: 5 additions & 25 deletions smart_open/tests/test_smart_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
# This code is distributed under the terms and conditions
# from the MIT License (MIT).

import importlib
import unittest
import logging
import tempfile
import os
import pkgutil
import sys
import hashlib

Expand Down Expand Up @@ -193,35 +191,17 @@ def test_open_with_keywords_explicit_r(self):
actual = fin.read()
self.assertEqual(expected, actual)

@unittest.skipIf(
(pkgutil.find_loader('pathlib') is None and
pkgutil.find_loader('pathlib2') is None),
@unittest.skipUnless(
smart_open_lib.PATHLIB_SUPPORT,
"do not test pathlib support if pathlib or backport are not available")
def test_open_and_read_pathlib_path(self):
"""If ``pathlib.Path`` is available we should be able to open and read."""
fpath = os.path.join(CURR_DIR, 'test_data/cp852.tsv.txt')

# Import ``pathlib`` if the builtin ``pathlib`` or the backport
# ``pathlib2`` are available. The builtin ``pathlib`` will be imported
# with higher precedence.
for pathlib_module in ('pathlib', 'pathlib2'):
try:
pathlib = importlib.import_module(pathlib_module)
break
# Unit test will skip if either module is unavailable so it's safe
# to assume we can import _at least_ one working ``pathlib``.
except ImportError:
pass
from smart_open.smart_open_lib.pathlib import Path

# builtin open() supports pathlib.Path in python>=3.6 only
path_open = pathlib.Path(fpath) if sys.version_info >= (3, 6) else fpath
path_smart_open = pathlib.Path(fpath)
fpath = os.path.join(CURR_DIR, 'test_data/cp852.tsv.txt')

with open(path_open, 'rb') as fin:
with open(Path(fpath), 'rb') as fin:
expected = fin.read().decode('cp852')
with smart_open.smart_open(path_smart_open, encoding='cp852') as fin:
actual = fin.read()
self.assertEqual(expected, actual)

@mock_s3
def test_read_never_returns_none(self):
Expand Down

0 comments on commit 1a33339

Please sign in to comment.