Generalize entry point tests
authorKarl O. Pinc <kop@karlpinc.com>
Mon, 31 Aug 2020 03:44:06 +0000 (22:44 -0500)
committerKarl O. Pinc <kop@karlpinc.com>
Mon, 31 Aug 2020 03:44:06 +0000 (22:44 -0500)
src/pgwui_testing/testing.py
tests/test_testing.py

index 13010c771c74d8b66c1c0de87ab817eda12291b9..92f8c3d538962fb9a79e241a7e01007fce790dba 100644 (file)
@@ -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
index a27e3978f33afad35defe61c7adc287818610fff..500a54bd642fc9890cb47e02710cef9bb0a7fb3c 100644 (file)
@@ -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
         '''
     )