-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from jackson541/feat/use-serializer-context
Use serializer context in virtual model
- Loading branch information
Showing
6 changed files
with
97 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
45 changes: 45 additions & 0 deletions
45
tests/virtual_model_serializers/test_virtual_model_serializers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from unittest.mock import MagicMock | ||
|
||
from django.test import TestCase | ||
|
||
from rest_framework import serializers | ||
|
||
from model_bakery import baker | ||
|
||
import django_virtual_models as v | ||
|
||
from ..virtual_models.models import Course, User | ||
|
||
|
||
class VirtualModelSerializerTest(TestCase): | ||
def test_pass_serializer_context_to_virtual_model(self): | ||
class MockedVirtualCourse(v.VirtualModel): | ||
something = v.Annotation( | ||
lambda qs, user, **kwargs: qs.annotate_something(user=user, **kwargs) | ||
) | ||
|
||
class Meta: | ||
model = Course | ||
deferred_fields = ["name", "description", "something"] | ||
|
||
class CourseSerializer(v.VirtualModelSerializer): | ||
something = serializers.CharField() | ||
|
||
class Meta: | ||
model = Course | ||
virtual_model = MockedVirtualCourse | ||
fields = ["name", "description", "something"] | ||
|
||
user = baker.make(User) | ||
user.is_anonymous = False | ||
request = MagicMock() | ||
request.method = "GET" | ||
request.user = user | ||
serializer_context = {"request": request, "value": 12345} | ||
mock_qs = MagicMock() | ||
virtual_course_serializer = CourseSerializer(instance=None, context=serializer_context) | ||
virtual_course_serializer.get_optimized_queryset(mock_qs) | ||
|
||
mock_qs.annotate_something.assert_called_once_with( | ||
user=user, serializer_context=serializer_context | ||
) |