|
| 1 | +""" |
| 2 | +Tests for the edxapp_wrapper module. |
| 3 | +
|
| 4 | +This module tests the wrapper that abstracts Open edX core imports, |
| 5 | +allowing the plugin to work with different Open edX versions. |
| 6 | +""" |
| 7 | + |
| 8 | +import sys |
| 9 | +from unittest.mock import MagicMock, patch |
| 10 | + |
| 11 | +from django.conf import settings |
| 12 | +from django.test import override_settings |
| 13 | + |
| 14 | +# Mock the openedx module before importing the wrapper |
| 15 | +# This must be done before importing the edxapp_wrapper modules to avoid import errors |
| 16 | +mock_content_libraries = MagicMock() |
| 17 | +mock_content_libraries.__name__ = 'openedx.core.djangoapps.content_libraries' |
| 18 | +sys.modules['openedx'] = MagicMock() |
| 19 | +sys.modules['openedx.core'] = MagicMock() |
| 20 | +sys.modules['openedx.core.djangoapps'] = MagicMock() |
| 21 | +sys.modules['openedx.core.djangoapps.content_libraries'] = mock_content_libraries |
| 22 | + |
| 23 | +# pylint: disable=wrong-import-position |
| 24 | +# These imports must come after mocking the openedx module |
| 25 | +from openedx_ai_extensions.edxapp_wrapper import content_libraries_module # noqa: E402 |
| 26 | +from openedx_ai_extensions.edxapp_wrapper.backends import content_libraries_module_t_v1 # noqa: E402 |
| 27 | +# pylint: enable=wrong-import-position |
| 28 | + |
| 29 | + |
| 30 | +class TestContentLibrariesModuleWrapper: |
| 31 | + """ |
| 32 | + Test the content_libraries_module wrapper function. |
| 33 | + """ |
| 34 | + |
| 35 | + def test_get_content_libraries_returns_module(self): |
| 36 | + """ |
| 37 | + Test that get_content_libraries() returns the content_libraries module. |
| 38 | +
|
| 39 | + This tests the backend abstraction layer that loads the appropriate |
| 40 | + backend based on Django settings. |
| 41 | + """ |
| 42 | + # Call the wrapper function |
| 43 | + result = content_libraries_module.get_content_libraries() |
| 44 | + |
| 45 | + # Verify it returns a module (should be the content_libraries module from the backend) |
| 46 | + assert result is not None |
| 47 | + |
| 48 | + @override_settings( |
| 49 | + CONTENT_LIBRARIES_MODULE_BACKEND="openedx_ai_extensions.edxapp_wrapper.backends.content_libraries_module_t_v1" |
| 50 | + ) |
| 51 | + def test_get_content_libraries_uses_settings_backend(self): |
| 52 | + """ |
| 53 | + Test that get_content_libraries() uses the backend specified in settings. |
| 54 | +
|
| 55 | + This verifies that the wrapper correctly reads the CONTENT_LIBRARIES_MODULE_BACKEND |
| 56 | + setting and imports the specified backend module. |
| 57 | + """ |
| 58 | + # Mock the import_module to verify it's called with the correct backend |
| 59 | + with patch('openedx_ai_extensions.edxapp_wrapper.content_libraries_module.import_module') as mock_import: |
| 60 | + mock_backend = MagicMock() |
| 61 | + mock_backend.get_content_libraries.return_value = MagicMock() |
| 62 | + mock_import.return_value = mock_backend |
| 63 | + |
| 64 | + # Call the wrapper function |
| 65 | + result = content_libraries_module.get_content_libraries() |
| 66 | + |
| 67 | + # Verify import_module was called with the backend from settings |
| 68 | + mock_import.assert_called_once_with(settings.CONTENT_LIBRARIES_MODULE_BACKEND) |
| 69 | + # Verify the backend's get_content_libraries was called |
| 70 | + mock_backend.get_content_libraries.assert_called_once() |
| 71 | + assert result is not None |
| 72 | + |
| 73 | + |
| 74 | +class TestContentLibrariesModuleBackend: |
| 75 | + """ |
| 76 | + Test the content_libraries_module_t_v1 backend. |
| 77 | + """ |
| 78 | + |
| 79 | + def test_backend_get_content_libraries(self): |
| 80 | + """ |
| 81 | + Test that the backend's get_content_libraries() returns the content_libraries module. |
| 82 | +
|
| 83 | + This tests the actual backend implementation that imports from openedx.core.djangoapps. |
| 84 | + The openedx module is mocked at the module level to avoid import errors. |
| 85 | + """ |
| 86 | + # Call the backend function (using the mocked openedx module) |
| 87 | + result = content_libraries_module_t_v1.get_content_libraries() |
| 88 | + |
| 89 | + # Verify it returns the content_libraries module |
| 90 | + # The function simply returns the imported module, so as long as it returns something |
| 91 | + # and doesn't raise an exception, it's working correctly |
| 92 | + assert result is not None |
0 commit comments