feat: add AppHandler methods, get_distribution() and get_version()
This commit is contained in:
parent
0a46dddf3f
commit
2a21e70ff1
2 changed files with 167 additions and 0 deletions
|
@ -2,6 +2,7 @@
|
|||
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
import warnings
|
||||
from unittest import TestCase
|
||||
|
@ -116,6 +117,88 @@ class TestAppHandler(TestCase):
|
|||
def test_get_title(self):
|
||||
self.assertEqual(self.app.get_title(), 'WuttJamaican')
|
||||
|
||||
def test_get_distribution(self):
|
||||
|
||||
# default should always be WuttJamaican (right..?)
|
||||
dist = self.app.get_distribution()
|
||||
self.assertEqual(dist, 'WuttJamaican')
|
||||
|
||||
# also works with "non-native" objects
|
||||
from config import Configuration
|
||||
config = Configuration({})
|
||||
dist = self.app.get_distribution(config)
|
||||
self.assertEqual(dist, 'python-configuration')
|
||||
|
||||
# can override dist via config
|
||||
self.config.setdefault('wuttatest.app_dist', 'importlib_metadata')
|
||||
dist = self.app.get_distribution()
|
||||
self.assertEqual(dist, 'importlib_metadata')
|
||||
|
||||
# but the provided object takes precedence
|
||||
dist = self.app.get_distribution(config)
|
||||
self.assertEqual(dist, 'python-configuration')
|
||||
|
||||
def test_get_distribution_pre_python_3_10(self):
|
||||
|
||||
# the goal here is to get coverage for code which would only
|
||||
# run on python 3,9 and older, but we only need that coverage
|
||||
# if we are currently testing python 3.10+
|
||||
if sys.version_info.major == 3 and sys.version_info.minor < 10:
|
||||
pytest.skip("this test is not relevant before python 3.10")
|
||||
|
||||
importlib_metadata = MagicMock()
|
||||
importlib_metadata.packages_distributions = MagicMock(
|
||||
return_value={
|
||||
'wuttjamaican': ['WuttJamaican'],
|
||||
'config': ['python-configuration'],
|
||||
})
|
||||
|
||||
orig_import = __import__
|
||||
|
||||
def mock_import(name, *args, **kwargs):
|
||||
if name == 'importlib.metadata':
|
||||
raise ImportError
|
||||
if name == 'importlib_metadata':
|
||||
return importlib_metadata
|
||||
return orig_import(name, *args, **kwargs)
|
||||
|
||||
with patch('builtins.__import__', side_effect=mock_import):
|
||||
|
||||
# default should always be WuttJamaican (right..?)
|
||||
dist = self.app.get_distribution()
|
||||
self.assertEqual(dist, 'WuttJamaican')
|
||||
|
||||
# also works with "non-native" objects
|
||||
from config import Configuration
|
||||
config = Configuration({})
|
||||
dist = self.app.get_distribution(config)
|
||||
self.assertEqual(dist, 'python-configuration')
|
||||
|
||||
# hacky sort of test, just in case we can't deduce the
|
||||
# package dist based on the obj - easy enough since we
|
||||
# have limited the packages_distributions() above
|
||||
dist = self.app.get_distribution(42)
|
||||
self.assertIsNone(dist)
|
||||
|
||||
# can override dist via config
|
||||
self.config.setdefault('wuttatest.app_dist', 'importlib_metadata')
|
||||
dist = self.app.get_distribution()
|
||||
self.assertEqual(dist, 'importlib_metadata')
|
||||
|
||||
# but the provided object takes precedence
|
||||
dist = self.app.get_distribution(config)
|
||||
self.assertEqual(dist, 'python-configuration')
|
||||
|
||||
# hacky test again, this time config override should win
|
||||
dist = self.app.get_distribution(42)
|
||||
self.assertEqual(dist, 'importlib_metadata')
|
||||
|
||||
def test_get_version(self):
|
||||
from importlib.metadata import version
|
||||
|
||||
# default should always be for WuttJamaican (right..?)
|
||||
self.assertEqual(self.app.get_version(), version('WuttJamaican'))
|
||||
|
||||
def test_make_title(self):
|
||||
text = self.app.make_title('foo_bar')
|
||||
self.assertEqual(text, "Foo Bar")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue