Skip to content

Commit

Permalink
feat: ModelSerializerTestCase
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Jan 30, 2024
1 parent c022576 commit 0bd252c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions codeforlife/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
"""

from .cron import CronTestCase
from .model_serializer import ModelSerializerTestCase
from .model_view_set import ModelViewSetTestCase
63 changes: 63 additions & 0 deletions codeforlife/tests/model_serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""
© Ocado Group
Created on 30/01/2024 at 18:06:31(+00:00).
Base test case for all model serializers.
"""

import typing as t

from django.db.models import Model
from django.test import TestCase
from rest_framework.serializers import ValidationError

from ..serializers import ModelSerializer

AnyModel = t.TypeVar("AnyModel", bound=Model)


class ModelSerializerTestCase(TestCase, t.Generic[AnyModel]):
"""Base for all model serializer test cases."""

model_serializer_class: t.Type[ModelSerializer[AnyModel]]

@classmethod
def get_model_class(cls) -> t.Type[AnyModel]:
"""Get the model view set's class.
Returns:
The model view set's class.
"""

# pylint: disable-next=no-member
return t.get_args(cls.__orig_bases__[0])[ # type: ignore[attr-defined]
0
]

def assert_raises_validation_error(self, code: str, *args, **kwargs):
"""Assert code block raises a validation error.
Args:
code: The validation code to assert.
Returns:
The assert-raises context which will auto-assert the code.
"""

context = self.assertRaises(ValidationError, *args, **kwargs)

class ContextWrapper:
"""Wrap context to assert code exit."""

def __init__(self, context):
self.context = context

def __enter__(self, *args, **kwargs):
return self.context.__enter__(*args, **kwargs)

def __exit__(self, *args, **kwargs):
value = self.context.__exit__(*args, **kwargs)
assert self.context.exception.detail[0].code == code
return value

return ContextWrapper(context)

0 comments on commit 0bd252c

Please sign in to comment.