Skip to content

Commit

Permalink
Factor out common tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brettcannon committed Oct 27, 2017
1 parent a9bafd0 commit 47aaaa0
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 143 deletions.
56 changes: 6 additions & 50 deletions importlib_resources/tests/test_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import unittest

import importlib_resources as resources
from importlib_resources.tests import data
from . import data
from . import util


ZIP_DATA_PATH = None # type: Optional[pathlib.Path]
Expand All @@ -29,56 +30,11 @@ def tearDownModule():
pass


class CommonTests(unittest.TestCase):

def test_package_name(self):
# Passing in the package name should succeed.
with resources.open(data.__name__, 'utf-8.file') as file:
pass # No error.

def test_package_object(self):
# Passing in the package itself should succeed.
with resources.open(data, 'utf-8.file') as file:
pass # No error.

def test_string_path(self):
path = 'utf-8.file'
# Passing in a string for the path should succeed.
with resources.open(data, path) as file:
pass # No error.

@unittest.skipIf(sys.version_info < (3, 6), 'requires os.PathLike support')
def test_pathlib_path(self):
# Passing in a pathlib.PurePath object for the path should succeed.
path = pathlib.PurePath('utf-8.file')
with resources.open(data, path) as file:
pass # No error.

def test_absolute_path(self):
# An absolute path is a ValueError.
path = pathlib.Path(__file__)
full_path = path.parent/'utf-8.file'
with self.assertRaises(ValueError):
with resources.open(data, str(full_path)) as file:
pass

def test_relative_path(self):
# A reative path is a ValueError.
with self.assertRaises(ValueError):
with resources.open(data, '../data/utf-8.file') as file:
pass
class CommonTests(util.CommonTests, unittest.TestCase):

def test_importing_module_as_side_effect(self):
# The anchor package can already be imported.
del sys.modules[data.__name__]
with resources.open(data.__name__, 'utf-8.file') as file:
pass # No Errors.

def test_non_package(self):
# The anchor package cannot be a module.
with self.assertRaises(TypeError):
with resources.open(__spec__.name, 'utf-8.file') as file:
pass
def execute(self, package, path):
with resources.open(package, path):
pass


class OpenTests:
Expand Down
57 changes: 6 additions & 51 deletions importlib_resources/tests/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,15 @@
import unittest

import importlib_resources as resources
from importlib_resources.tests import data
from . import data
from . import util


class CommonTests(unittest.TestCase):
class CommonTests(util.CommonTests, unittest.TestCase):

def test_package_name(self):
# Passing in the package name should succeed.
with resources.path(data.__name__, 'utf-8.file') as path:
pass # No error.

def test_package_object(self):
# Passing in the package itself should succeed.
with resources.path(data, 'utf-8.file') as path:
pass # No error.

def test_string_path(self):
path = 'utf-8.file'
# Passing in a string for the path should succeed.
with resources.path(data, path) as path:
pass # No error.

@unittest.skipIf(sys.version_info < (3, 6), 'requires os.PathLike support')
def test_pathlib_path(self):
# Passing in a pathlib.PurePath object for the path should succeed.
path = pathlib.PurePath('utf-8.file')
with resources.path(data, path) as path:
pass # No error.

# Don't fail if run under e.g. pytest.
def test_absolute_path(self):
# An absolute path is a ValueError.
path = pathlib.Path(__file__)
full_path = path.parent/'utf-8.file'
with self.assertRaises(ValueError):
with resources.path(data, str(full_path)) as path:
pass

def test_relative_path(self):
# A reative path is a ValueError.
with self.assertRaises(ValueError):
with resources.path(data, '../data/utf-8.file') as path:
pass

def test_importing_module_as_side_effect(self):
# The anchor package can already be imported.
del sys.modules[data.__name__]
with resources.path(data.__name__, 'utf-8.file') as path:
pass # No Errors.

def test_non_package(self):
# The anchor package cannot be a module.
with self.assertRaises(TypeError):
with resources.path(__spec__.name, 'utf-8.file') as path:
pass
def execute(self, package, path):
with resources.path(package, path):
pass


class PathTests(unittest.TestCase):
Expand Down
47 changes: 5 additions & 42 deletions importlib_resources/tests/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,14 @@
import unittest

import importlib_resources as resources
from importlib_resources.tests import data
from . import data
from . import util


class CommonTests(unittest.TestCase):
class CommonTests(util.CommonTests, unittest.TestCase):

def test_package_name(self):
# Passing in the package name should succeed.
resources.read(data.__name__, 'utf-8.file')

def test_package_object(self):
# Passing in the package itself should succeed.
resources.read(data, 'utf-8.file')

def test_string_path(self):
path = 'utf-8.file'
# Passing in a string for the path should succeed.
resources.read(data, path)

@unittest.skipIf(sys.version_info < (3, 6), 'requires os.PathLike support')
def test_pathlib_path(self):
# Passing in a pathlib.PurePath object for the path should succeed.
path = pathlib.PurePath('utf-8.file')
resources.read(data, path)

def test_absolute_path(self):
# An absolute path is a ValueError.
path = pathlib.Path(__file__)
full_path = path.parent/'utf-8.file'
with self.assertRaises(ValueError):
resources.read(data, str(full_path))

def test_relative_path(self):
# A reative path is a ValueError.
with self.assertRaises(ValueError):
resources.read(data, '../data/utf-8.file')

def test_importing_module_as_side_effect(self):
# The anchor package can already be imported.
del sys.modules[data.__name__]
resources.read(data.__name__, 'utf-8.file')

def test_non_package(self):
# The anchor package cannot be a module.
with self.assertRaises(TypeError):
resources.read(__spec__.name, 'utf-8.file')
def execute(self, package, path):
resources.read(package, path)


class ReadTests(unittest.TestCase):
Expand Down
54 changes: 54 additions & 0 deletions importlib_resources/tests/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import abc
import pathlib
import sys
import unittest

from . import data


class CommonTests(abc.ABC):

@abc.abstractmethod
def execute(self, package, path):
raise NotImplementedError

def test_package_name(self):
# Passing in the package name should succeed.
self.execute(data.__name__, 'utf-8.file')

def test_package_object(self):
# Passing in the package itself should succeed.
self.execute(data, 'utf-8.file')

def test_string_path(self):
# Passing in a string for the path should succeed.
path = 'utf-8.file'
self.execute(data, path)

@unittest.skipIf(sys.version_info < (3, 6), 'requires os.PathLike support')
def test_pathlib_path(self):
# Passing in a pathlib.PurePath object for the path should succeed.
path = pathlib.PurePath('utf-8.file')
self.execute(data, path)

def test_absolute_path(self):
# An absolute path is a ValueError.
path = pathlib.Path(__file__)
full_path = path.parent/'utf-8.file'
with self.assertRaises(ValueError):
self.execute(data, full_path)

def test_relative_path(self):
# A reative path is a ValueError.
with self.assertRaises(ValueError):
self.execute(data, '../data/utf-8.file')

def test_importing_module_as_side_effect(self):
# The anchor package can already be imported.
del sys.modules[data.__name__]
self.execute(data.__name__, 'utf-8.file')

def test_non_package(self):
# The anchor package cannot be a module.
with self.assertRaises(TypeError):
self.execute(__spec__.name, 'utf-8.file')

0 comments on commit 47aaaa0

Please sign in to comment.