-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathtest_dumb.py
127 lines (92 loc) · 2.92 KB
/
test_dumb.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
""" Snippets to creatte other tests """
from __future__ import unicode_literals
from django.test import TestCase
from rest_framework import fields
from rest_framework_mongoengine.serializers import DocumentSerializer
from .models import DumbDocument
from .utils import FieldTest, dedent
class SomeField(fields.IntegerField):
pass
class TestSomeField(FieldTest, TestCase):
field = SomeField()
valid_inputs = {
123: 123,
"123": 123
}
invalid_inputs = {
'xxx': ""
}
outputs = {
123: 123
}
class TestMapping(TestCase):
""" Field mapping test
Test if model fields are recognized and created in DocumentSerializer
"""
def test_mapping(self):
class TestSerializer(DocumentSerializer):
class Meta:
model = DumbDocument
fields = '__all__'
expected = dedent("""
TestSerializer():
id = ObjectIdField(read_only=True)
name = CharField(required=False)
foo = IntegerField(required=False)
""")
# better output then self.assertEqual()
assert repr(TestSerializer()) == expected
class TestSerializer(DocumentSerializer):
class Meta:
model = DumbDocument
fields = '__all__'
class TestIntegration(TestCase):
""" Operational test
Test if all operations performed correctly
"""
def doCleanups(self):
DumbDocument.drop_collection()
def test_parsing(self):
input_data = {'name': "dumb", 'foo': "123"}
serializer = TestSerializer(data=input_data)
assert serializer.is_valid(), serializer.errors
expected = {'name': "dumb", 'foo': 123}
assert serializer.validated_data == expected
def test_retrive(self):
instance = DumbDocument.objects.create(name="dumb", foo=123)
serializer = TestSerializer(instance)
expected = {
'id': str(instance.id),
'name': "dumb",
'foo': 123,
}
assert serializer.data == expected
def test_create(self):
data = {
'foo': 123
}
serializer = TestSerializer(data=data)
assert serializer.is_valid(), serializer.errors
instance = serializer.save()
assert instance.foo == 123
expected = {
'id': str(instance.id),
'name': None,
'foo': 123,
}
assert serializer.data == expected
def test_update(self):
instance = DumbDocument.objects.create(foo=123)
data = {
'foo': 234
}
serializer = TestSerializer(instance, data=data)
assert serializer.is_valid(), serializer.errors
instance = serializer.save()
assert instance.foo == 234
expected = {
'id': str(instance.id),
'foo': 234,
'name': None
}
assert serializer.data == expected