/
test_utils.py
73 lines (57 loc) · 2.11 KB
/
test_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from __future__ import absolute_import, print_function, unicode_literals
import unittest
from rest_framework import fields
from drf_braces.utils import (
find_class_args,
find_function_args,
get_attr_from_base_classes,
get_class_name_with_new_suffix,
)
class TestUtils(unittest.TestCase):
def test_get_class_name_with_new_suffix(self):
new_name = get_class_name_with_new_suffix(
klass=fields.IntegerField,
existing_suffix='Field',
new_suffix='StrawberryFields'
)
self.assertEqual(new_name, 'IntegerStrawberryFields')
new_name = get_class_name_with_new_suffix(
klass=fields.IntegerField,
existing_suffix='straws',
new_suffix='Blueberries'
)
self.assertEqual(new_name, 'IntegerFieldBlueberries')
def test_get_attr_from_base_classes(self):
Parent = type(str('Parent'), (), {'fields': 'pancakes'})
self.assertEqual(
get_attr_from_base_classes((Parent,), [], 'fields'), 'pancakes'
)
self.assertEqual(
get_attr_from_base_classes(
(Parent,), {'fields': 'mushrooms'}, 'fields'
),
'mushrooms'
)
self.assertEqual(
get_attr_from_base_classes((Parent,), [], '', default='maple_syrup'),
'maple_syrup'
)
with self.assertRaises(AttributeError):
get_attr_from_base_classes(
(Parent,), {'fields': 'mushrooms'}, 'catchmeifyoucan'
)
def test_find_function_args(self):
def foo(a, b, c):
pass
self.assertListEqual(find_function_args(foo), ['a', 'b', 'c'])
def test_find_function_args_invalid(self):
self.assertListEqual(find_function_args(None), [])
def test_find_class_args(self):
class Bar(object):
def __init__(self, a, b):
pass
class Foo(Bar):
def __init__(self, c, d):
super(Foo, self).__init__(None, None)
pass
self.assertSetEqual(set(find_class_args(Foo)), {'a', 'b', 'c', 'd'})