|
| 1 | +# |
| 2 | +# Copyright (c) 2025, salesforce.com, inc. |
| 3 | +# All rights reserved. |
| 4 | +# SPDX-License-Identifier: BSD-3-Clause |
| 5 | +# For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | +# |
| 7 | + |
| 8 | +import unittest |
| 9 | +from unittest.mock import patch, Mock |
| 10 | +from django_declarative_apis.events import _import_hook, emit_events |
| 11 | +from django.test import override_settings |
| 12 | + |
| 13 | + |
| 14 | +def test_function(): |
| 15 | + pass |
| 16 | + |
| 17 | + |
| 18 | +class EmitEventsTest(unittest.TestCase): |
| 19 | + @override_settings(DDA_EVENT_HOOK="tests.test_events.test_function") |
| 20 | + def test_import_hook(self): |
| 21 | + hook_path = "tests.test_events.test_function" |
| 22 | + hook_function = _import_hook(hook_path) |
| 23 | + self.assertTrue(callable(hook_function)) |
| 24 | + self.assertEqual(hook_function.__name__, "test_function") |
| 25 | + |
| 26 | + def test_invalid_hook_path(self): |
| 27 | + with self.assertRaises(ValueError) as context: |
| 28 | + _import_hook("invalid_path") |
| 29 | + self.assertEqual( |
| 30 | + str(context.exception), |
| 31 | + "not enough values to unpack (expected 2, got 1)", |
| 32 | + ) |
| 33 | + |
| 34 | + def test_nonexistent_function(self): |
| 35 | + with self.assertRaises(AttributeError) as context: |
| 36 | + _import_hook("tests.test_events.no_function") |
| 37 | + self.assertIn( |
| 38 | + "module 'tests.test_events' has no attribute 'no_function'", |
| 39 | + str(context.exception), |
| 40 | + ) |
| 41 | + |
| 42 | + @patch("django_declarative_apis.events._import_hook") |
| 43 | + @patch("django_declarative_apis.events.logger") |
| 44 | + def test_emit_events_with_hook(self, mock_logger, mock_import_hook): |
| 45 | + event_type, payload = "test_event", {"key": "value"} |
| 46 | + mock_hook_function = Mock() |
| 47 | + mock_import_hook.return_value = mock_hook_function |
| 48 | + with patch( |
| 49 | + "django_declarative_apis.events.HOOK", "tests.test_events.test_function" |
| 50 | + ): |
| 51 | + emit_events(event_type, payload) |
| 52 | + mock_import_hook.assert_called_once_with("tests.test_events.test_function") |
| 53 | + mock_hook_function.assert_called_once_with(event_type, payload) |
| 54 | + mock_logger.info.assert_called_once_with( |
| 55 | + "Event emitted via custom hook: test_event" |
| 56 | + ) |
| 57 | + |
| 58 | + @patch("django_declarative_apis.events._import_hook") |
| 59 | + @patch("django_declarative_apis.events.logger") |
| 60 | + @override_settings(DDA_EVENT_HOOK="tests.test_events.test_function") |
| 61 | + def test_emit_events_with_exception_calling_hook( |
| 62 | + self, mock_logger, mock_import_hook |
| 63 | + ): |
| 64 | + event_type, payload = "test_event", {"key": "value"} |
| 65 | + mock_import_hook.return_value.side_effect = Exception("Simulated Exception") |
| 66 | + with patch( |
| 67 | + "django_declarative_apis.events.HOOK", "tests.test_events.test_function" |
| 68 | + ): |
| 69 | + emit_events(event_type, payload) |
| 70 | + mock_logger.error.assert_called_once_with( |
| 71 | + "Error in custom hook for events: Simulated Exception", exc_info=True |
| 72 | + ) |
0 commit comments