mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-07 13:54:47 +03:00
Add is_simple_callable tests
This commit is contained in:
parent
a0a8b9890a
commit
adcf6536e7
|
@ -1,6 +1,7 @@
|
|||
import datetime
|
||||
import os
|
||||
import re
|
||||
import unittest
|
||||
import uuid
|
||||
from decimal import Decimal
|
||||
|
||||
|
@ -11,6 +12,67 @@ from django.utils import six, timezone
|
|||
|
||||
import rest_framework
|
||||
from rest_framework import serializers
|
||||
from rest_framework.fields import is_simple_callable
|
||||
|
||||
try:
|
||||
import typings
|
||||
except ImportError:
|
||||
typings = False
|
||||
|
||||
|
||||
# Tests for helper functions.
|
||||
# ---------------------------
|
||||
|
||||
class TestIsSimpleCallable:
|
||||
|
||||
def test_method(self):
|
||||
class Foo:
|
||||
@classmethod
|
||||
def classmethod(cls):
|
||||
pass
|
||||
|
||||
def valid(self):
|
||||
pass
|
||||
|
||||
def valid_kwargs(self, param='value'):
|
||||
pass
|
||||
|
||||
def invalid(self, param):
|
||||
pass
|
||||
|
||||
assert is_simple_callable(Foo.classmethod)
|
||||
|
||||
# unbound methods
|
||||
assert not is_simple_callable(Foo.valid)
|
||||
assert not is_simple_callable(Foo.valid_kwargs)
|
||||
assert not is_simple_callable(Foo.invalid)
|
||||
|
||||
# bound methods
|
||||
assert is_simple_callable(Foo().valid)
|
||||
assert is_simple_callable(Foo().valid_kwargs)
|
||||
assert not is_simple_callable(Foo().invalid)
|
||||
|
||||
def test_function(self):
|
||||
def simple():
|
||||
pass
|
||||
|
||||
def valid(param='value', param2='value'):
|
||||
pass
|
||||
|
||||
def invalid(param, param2='value'):
|
||||
pass
|
||||
|
||||
assert is_simple_callable(simple)
|
||||
assert is_simple_callable(valid)
|
||||
assert not is_simple_callable(invalid)
|
||||
|
||||
@unittest.skipUnless(typings, 'requires python 3.5')
|
||||
def test_type_annotation(self):
|
||||
# The annotation will otherwise raise a syntax error in python < 3.5
|
||||
exec("def valid(param: str='value'): pass", locals())
|
||||
valid = locals()['valid']
|
||||
|
||||
assert is_simple_callable(valid)
|
||||
|
||||
|
||||
# Tests for field keyword arguments and core functionality.
|
||||
|
|
Loading…
Reference in New Issue
Block a user