Skip to content

Commit

Permalink
Refs #34822 -- Added tests for serializing decorated functions in mig…
Browse files Browse the repository at this point in the history
…rations.

Functions decorated with a decorator that is properly wrapped, e.g. by
using `@functools.wraps`, are already supported.
  • Loading branch information
ngnpope authored and nessita committed Sep 13, 2023
1 parent 0e540fc commit c131949
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
7 changes: 6 additions & 1 deletion docs/topics/migrations.txt
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,12 @@ Django can serialize the following:
- ``LazyObject`` instances which wrap a serializable value.
- Enumeration types (e.g. ``TextChoices`` or ``IntegerChoices``) instances.
- Any Django field
- Any function or method reference (e.g. ``datetime.datetime.today``) (must be in module's top-level scope)
- Any function or method reference (e.g. ``datetime.datetime.today``) (must be
in module's top-level scope)

- Functions may be decorated if wrapped properly, i.e. using
:func:`functools.wraps`

- Unbound methods used from within the class body
- Any class reference (must be in module's top-level scope)
- Anything with a custom ``deconstruct()`` method (:ref:`see below <custom-deconstruct-method>`)
Expand Down
16 changes: 16 additions & 0 deletions tests/migrations/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@ class IntFlagEnum(enum.IntFlag):
B = 2


def decorator(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
return f(*args, **kwargs)

return wrapper


@decorator
def function_with_decorator():
pass


class OperationWriterTests(SimpleTestCase):
def test_empty_signature(self):
operation = custom_migration_operations.operations.TestOperation()
Expand Down Expand Up @@ -566,6 +579,9 @@ def test_serialize_functions(self):
self.assertEqual(string, "models.SET(42)")
self.serialize_round_trip(models.SET(42))

def test_serialize_decorated_functions(self):
self.assertSerializedEqual(function_with_decorator)

def test_serialize_datetime(self):
self.assertSerializedEqual(datetime.datetime.now())
self.assertSerializedEqual(datetime.datetime.now)
Expand Down

0 comments on commit c131949

Please sign in to comment.