From 948106ddfeb676db47ae06910473013e63fe4426 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Sun, 30 Aug 2020 22:44:06 -0500 Subject: [PATCH] Generalize entry point tests --- src/pgwui_testing/testing.py | 25 +++++++++--- tests/test_testing.py | 79 ++++++++++++++++++++++++------------ 2 files changed, 73 insertions(+), 31 deletions(-) diff --git a/src/pgwui_testing/testing.py b/src/pgwui_testing/testing.py index 13010c7..92f8c3d 100644 --- a/src/pgwui_testing/testing.py +++ b/src/pgwui_testing/testing.py @@ -48,16 +48,31 @@ def pyramid_request_config(): tearDown() +def pgwui_entry_point(ep_name): + '''Test that the supplied pgwui component is a entry point with the + given name + ''' + def run(module): + return (module in + [entry_point.module_name for entry_point in + pkg_resources.iter_entry_points(ep_name)]) + + return run + + @fixture def pgwui_component_entry_point(): '''Test that the supplied pgwui component is a pgui.components entry point ''' - def run(component): - return (component in - [entry_point.resolve().__name__ for entry_point in - pkg_resources.iter_entry_points('pgwui.components')]) + return pgwui_entry_point('pgwui.components') - return run + +@fixture +def pgwui_check_settings_entry_point(): + '''Test that the supplied pgwui component is a pgwui.check_settings + entry point + ''' + return pgwui_entry_point('pgwui.check_settings') # Mock support diff --git a/tests/test_testing.py b/tests/test_testing.py index a27e397..500a54b 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -58,48 +58,75 @@ def test_pyramid_request_config(pyramid_request_config): assert get_current_request() is not None -def test_pgwui_component_entry_point(testdir): - '''Test the pgwui_component_entry_point fixture +# pgwui_entry_point() + +def test_pgwui_entry_point_there(monkeypatch): + # True when the component is a pgwui.components entry point + test_name = 'pgwui_example' + + class MockEntryPoint(): + def __init__(self, module_name): + self.module_name = module_name + + monkeypatch.setattr( + testing.pkg_resources, 'iter_entry_points', + lambda *args: [MockEntryPoint(test_name)]) + + assert testing.pgwui_entry_point(test_name)(test_name) is True + + +def test_pgwui_entry_point_not_there(monkeypatch): + # False when the component is not pgwui.components entry point + monkeypatch.setattr( + testing.pkg_resources, 'iter_entry_points', + lambda *args: []) + + assert testing.pgwui_entry_point('foo')('foo') is False + + +# pgwui_component_entry_point() +# pgwui_check_settings_entry_point() + +def test_pgwui_testing_fixtures(testdir): + '''Test the fixtures supplied by the testing module ''' testdir.makepyfile( ''' import pgwui_testing.testing as testing import pytest + from unittest import mock - # pgwui_component_entry_point - def test_pgwui_component_entry_point_there( - monkeypatch, pgwui_component_entry_point): - # True when the component is a pgwui.components entry point - test_name = 'pgwui_example' + @pytest.fixture + def mock_pgwui_entry_point(monkeypatch): + mocked = mock.Mock( + spec=getattr(testing, 'pgwui_entry_point'), + name='pgwui_entry_point') + monkeypatch.setattr(testing, 'pgwui_entry_point', mocked) + return mocked + + + # pgwui_component_entry_point - class MockEntryPoint(): - def __init__(self, name): - self.name = name + def test_pgwui_component_entry_point( + mock_pgwui_entry_point, pgwui_component_entry_point): + #Calls pgwui_entry_point - def resolve(self): - class MockModule(): - def __init__(self, name): - self.__name__ = name + pgwui_component_entry_point('pgwui_example') + mock_pgwui_entry_point.assert_called_once() - return MockModule(self.name) - monkeypatch.setattr( - testing.pkg_resources, 'iter_entry_points', - lambda *args: [MockEntryPoint(test_name)]) + # pgwui_check_settings_entry_point - assert pgwui_component_entry_point(test_name) is True + def test_pgwui_check_settings_entry_point( + mock_pgwui_entry_point, pgwui_check_settings_entry_point): + #Calls pgwui_entry_point + pgwui_check_settings_entry_point('pgwui_example') + mock_pgwui_entry_point.assert_called_once() - def test_pgwui_component_entry_point_not_there( - monkeypatch, pgwui_component_entry_point): - # False when the component is not pgwui.components entry point - monkeypatch.setattr( - testing.pkg_resources, 'iter_entry_points', - lambda *args: []) - assert pgwui_component_entry_point('foo') is False ''' ) -- 2.34.1