Exemple #1
0
    def test_deregister_post_import_hook_after_register_multiple_all(self):
        """
        Test that multiple import hooks can be deregistered.
        """
        test_hook = mock.MagicMock()
        test_hook2 = mock.MagicMock()
        register_post_import_hook('tests.utils.test_module', test_hook)
        register_post_import_hook('tests.utils.test_module', test_hook2)

        outcome = deregister_post_import_hook('tests.utils.test_module', test_hook)
        self.assertTrue(outcome)
        outcome = deregister_post_import_hook('tests.utils.test_module', test_hook2)
        self.assertTrue(outcome)
        import tests.utils.test_module  # noqa
        self.assertEqual(test_hook.call_count, 0, 'hook has been deregistered and should be removed')
        self.assertEqual(test_hook2.call_count, 0, 'hook has been deregistered and should be removed')
Exemple #2
0
    def test_deregister_post_import_hook_no_register(self):
        """
        Test that deregistering import hooks that do not exist is a no-op.
        """
        def hook():
            return

        outcome = deregister_post_import_hook('tests.test_module', hook)
        self.assertFalse(outcome)
        import tests.test_module  # noqa
Exemple #3
0
 def test_deregister_post_import_hook_after_register(self):
     """
     Test that import hooks can be deregistered after being registered.
     """
     test_hook = mock.MagicMock()
     register_post_import_hook('tests.utils.test_module', test_hook)
     outcome = deregister_post_import_hook('tests.utils.test_module', test_hook)
     self.assertTrue(outcome)
     import tests.utils.test_module  # noqa
     self.assertEqual(test_hook.call_count, 0, 'hook has been deregistered and should have been removed')
Exemple #4
0
    def test_deregister_post_import_hook_after_import(self):
        """
        Test that import hooks can be deregistered after being registered.
        """
        test_hook = mock.MagicMock()
        register_post_import_hook('tests.utils.test_module', test_hook)

        import tests.utils.test_module
        test_hook.assert_called_once()
        outcome = deregister_post_import_hook('tests.utils.test_module', test_hook)
        self.assertTrue(outcome)
        reload_module(tests.utils.test_module)
        self.assertEqual(test_hook.call_count, 1, 'hook should only be called once')
Exemple #5
0
    def test_deregister_post_import_hook_after_register_multiple(self):
        """
        Test that only the specified import hook can be deregistered after being registered.
        """
        # Enforce a spec so that hasattr doesn't vacuously return True.
        test_hook = mock.MagicMock(spec=[])
        test_hook2 = mock.MagicMock(spec=[])
        register_post_import_hook('tests.utils.test_module', test_hook)
        register_post_import_hook('tests.utils.test_module', test_hook2)

        outcome = deregister_post_import_hook('tests.utils.test_module', test_hook)
        self.assertTrue(outcome)
        import tests.utils.test_module  # noqa
        self.assertEqual(test_hook.call_count, 0, 'hook has been deregistered and should be removed')
        self.assertEqual(test_hook2.call_count, 1, 'hook should have been called')