-
Notifications
You must be signed in to change notification settings - Fork 27
/
tests.py
69 lines (51 loc) · 1.77 KB
/
tests.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
from unittest import TestCase
from io import BytesIO
from django.conf import settings
import ujson
settings.configure()
from drf_ujson.renderers import UJSONRenderer
from drf_ujson.parsers import UJSONParser
class UJSONRendererTests(TestCase):
def setUp(self):
self.renderer = UJSONRenderer()
self.data = {
'a': [1, 2, 3],
'b': True,
'c': 1.23,
'd': 'test',
'e': {'foo': 'bar'},
}
def test_basic_data_structures_rendered_correctly(self):
rendered = self.renderer.render(self.data)
reloaded = ujson.loads(rendered)
self.assertEqual(reloaded, self.data)
def test_renderer_works_correctly_when_media_type_and_context_provided(self):
rendered = self.renderer.render(
data=self.data,
media_type='application/json',
renderer_context={},
)
reloaded = ujson.loads(rendered)
self.assertEqual(reloaded, self.data)
class UJSONParserTests(TestCase):
def setUp(self):
self.parser = UJSONParser()
self.data = {
'a': [1, 2, 3],
'b': True,
'c': 1.23,
'd': 'test',
'e': {'foo': 'bar'},
}
def test_basic_data_structures_parsed_correctly(self):
dumped = ujson.dumps(self.data)
parsed = self.parser.parse(BytesIO(dumped.encode('utf-8')))
self.assertEqual(parsed, self.data)
def test_parser_works_correctly_when_media_type_and_context_provided(self):
dumped = ujson.dumps(self.data)
parsed = self.parser.parse(
stream=BytesIO(dumped.encode('utf-8')),
media_type='application/json',
parser_context={},
)
self.assertEqual(parsed, self.data)