|
| 1 | +from unittest.mock import AsyncMock, Mock, patch |
| 2 | + |
| 3 | +import pytest |
| 4 | +from pydantic import BaseModel |
| 5 | + |
| 6 | +from ninja_extra.controllers.model.service import ModelService, _async_django_support |
| 7 | +from ninja_extra.exceptions import NotFound |
| 8 | + |
| 9 | +from ..models import Event |
| 10 | + |
| 11 | + |
| 12 | +class EventTestSchema(BaseModel): |
| 13 | + title: str |
| 14 | + start_date: str |
| 15 | + end_date: str |
| 16 | + |
| 17 | + |
| 18 | +class TestAsyncDjangoSupport: |
| 19 | + """Test the _async_django_support decorator functionality""" |
| 20 | + |
| 21 | + def setup_method(self): |
| 22 | + """Set up test fixtures""" |
| 23 | + self.service = ModelService(Event) |
| 24 | + self.test_schema = EventTestSchema( |
| 25 | + title="Test Event", start_date="2020-01-01", end_date="2020-01-02" |
| 26 | + ) |
| 27 | + |
| 28 | + @pytest.mark.asyncio |
| 29 | + @patch( |
| 30 | + "ninja_extra.controllers.model.service.django_version_greater_than_4_2", False |
| 31 | + ) |
| 32 | + @patch("ninja_extra.controllers.model.service.sync_to_async") |
| 33 | + async def test_async_django_support_with_exception_in_sync_method( |
| 34 | + self, mock_sync_to_async |
| 35 | + ): |
| 36 | + """Test decorator behavior when sync method raises an exception in Django < 4.2""" |
| 37 | + |
| 38 | + # Mock sync method that raises an exception |
| 39 | + sync_method = Mock(side_effect=NotFound("Test error")) |
| 40 | + mock_async_wrapper = AsyncMock(side_effect=NotFound("Test error")) |
| 41 | + mock_sync_to_async.return_value = mock_async_wrapper |
| 42 | + |
| 43 | + # Add the sync method to the service instance |
| 44 | + self.service.test_sync_method = sync_method |
| 45 | + |
| 46 | + # Create a dummy async method |
| 47 | + async_method = AsyncMock() |
| 48 | + |
| 49 | + # Apply the decorator |
| 50 | + decorated_method = _async_django_support("test_sync_method")(async_method) |
| 51 | + |
| 52 | + # Verify the exception is propagated |
| 53 | + with pytest.raises(NotFound, match="Test error"): |
| 54 | + await decorated_method(self.service, "test_arg") |
| 55 | + |
| 56 | + @pytest.mark.asyncio |
| 57 | + @patch( |
| 58 | + "ninja_extra.controllers.model.service.django_version_greater_than_4_2", True |
| 59 | + ) |
| 60 | + async def test_async_django_support_with_exception_in_async_method(self): |
| 61 | + """Test decorator behavior when async method raises an exception in Django >= 4.2""" |
| 62 | + |
| 63 | + # Mock an async method that raises an exception |
| 64 | + original_async_method = AsyncMock(side_effect=NotFound("Async test error")) |
| 65 | + |
| 66 | + # Apply the decorator |
| 67 | + decorated_method = _async_django_support("sync_method_name")( |
| 68 | + original_async_method |
| 69 | + ) |
| 70 | + |
| 71 | + # Verify the exception is propagated |
| 72 | + with pytest.raises(NotFound, match="Async test error"): |
| 73 | + await decorated_method(self.service, "test_arg") |
| 74 | + |
| 75 | + @pytest.mark.asyncio |
| 76 | + @pytest.mark.django_db |
| 77 | + async def test_actual_model_service_methods_with_django_4_2_true(self): |
| 78 | + """Integration test with actual ModelService methods when Django >= 4.2""" |
| 79 | + |
| 80 | + with patch( |
| 81 | + "ninja_extra.controllers.model.service.django_version_greater_than_4_2", |
| 82 | + True, |
| 83 | + ): |
| 84 | + # Test create_async |
| 85 | + event = await self.service.create_async(self.test_schema) |
| 86 | + assert event.title == "Test Event" |
| 87 | + assert str(event.start_date) == "2020-01-01" |
| 88 | + |
| 89 | + # Test get_one_async |
| 90 | + retrieved_event = await self.service.get_one_async(event.pk) |
| 91 | + assert retrieved_event.id == event.id |
| 92 | + assert retrieved_event.title == "Test Event" |
| 93 | + |
| 94 | + # Test update_async |
| 95 | + update_schema = EventTestSchema( |
| 96 | + title="Updated Event", start_date="2020-01-01", end_date="2020-01-02" |
| 97 | + ) |
| 98 | + updated_event = await self.service.update_async(event, update_schema) |
| 99 | + assert updated_event.title == "Updated Event" |
| 100 | + |
| 101 | + # Test delete_async |
| 102 | + await self.service.delete_async(event) |
| 103 | + |
| 104 | + # Verify event is deleted |
| 105 | + with pytest.raises(NotFound): |
| 106 | + await self.service.get_one_async(event.pk) |
| 107 | + |
| 108 | + @pytest.mark.asyncio |
| 109 | + @pytest.mark.django_db |
| 110 | + async def test_actual_model_service_methods_with_django_4_2_false(self): |
| 111 | + """Integration test with actual ModelService methods when Django < 4.2""" |
| 112 | + |
| 113 | + with patch( |
| 114 | + "ninja_extra.controllers.model.service.django_version_greater_than_4_2", |
| 115 | + False, |
| 116 | + ): |
| 117 | + # Test create_async (should fall back to sync method) |
| 118 | + event = await self.service.create_async(self.test_schema) |
| 119 | + assert event.title == "Test Event" |
| 120 | + assert str(event.start_date) == "2020-01-01" |
| 121 | + |
| 122 | + # Test get_one_async (should fall back to sync method) |
| 123 | + retrieved_event = await self.service.get_one_async(event.pk) |
| 124 | + assert retrieved_event.id == event.id |
| 125 | + assert retrieved_event.title == "Test Event" |
| 126 | + |
| 127 | + # Test update_async (should fall back to sync method) |
| 128 | + update_schema = EventTestSchema( |
| 129 | + title="Updated Event Sync", |
| 130 | + start_date="2020-01-01", |
| 131 | + end_date="2020-01-02", |
| 132 | + ) |
| 133 | + updated_event = await self.service.update_async(event, update_schema) |
| 134 | + assert updated_event.title == "Updated Event Sync" |
| 135 | + |
| 136 | + # Test delete_async (should fall back to sync method) |
| 137 | + await self.service.delete_async(event) |
| 138 | + |
| 139 | + # Verify event is deleted |
| 140 | + with pytest.raises(NotFound): |
| 141 | + await self.service.get_one_async(event.pk) |
0 commit comments