-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
Copy pathtest_encoders.py
147 lines (122 loc) · 4.35 KB
/
test_encoders.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import ipaddress
from datetime import date, datetime, timedelta, timezone
from decimal import Decimal
from uuid import uuid4
import pytest
from django.test import TestCase
from rest_framework.compat import coreapi
from rest_framework.utils.encoders import JSONEncoder
from rest_framework.utils.serializer_helpers import ReturnList
utc = timezone.utc
class MockList:
def tolist(self):
return [1, 2, 3]
class JSONEncoderTests(TestCase):
"""
Tests the JSONEncoder method
"""
def setUp(self):
self.encoder = JSONEncoder()
def test_encode_decimal(self):
"""
Tests encoding a decimal
"""
d = Decimal(3.14)
assert self.encoder.default(d) == float(d)
def test_encode_datetime(self):
"""
Tests encoding a datetime object
"""
current_time = datetime.now()
assert self.encoder.default(current_time) == current_time.isoformat()
current_time_utc = current_time.replace(tzinfo=utc)
assert self.encoder.default(current_time_utc) == current_time.isoformat() + 'Z'
def test_encode_time(self):
"""
Tests encoding a timezone
"""
current_time = datetime.now().time()
assert self.encoder.default(current_time) == current_time.isoformat()
def test_encode_time_tz(self):
"""
Tests encoding a timezone aware timestamp
"""
current_time = datetime.now().time()
current_time = current_time.replace(tzinfo=utc)
with pytest.raises(ValueError):
self.encoder.default(current_time)
def test_encode_date(self):
"""
Tests encoding a date object
"""
current_date = date.today()
assert self.encoder.default(current_date) == current_date.isoformat()
def test_encode_timedelta(self):
"""
Tests encoding a timedelta object
"""
delta = timedelta(hours=1)
assert self.encoder.default(delta) == str(delta.total_seconds())
def test_encode_uuid(self):
"""
Tests encoding a UUID object
"""
unique_id = uuid4()
assert self.encoder.default(unique_id) == str(unique_id)
def test_encode_ipaddress_ipv4address(self):
"""
Tests encoding ipaddress IPv4Address object
"""
obj = ipaddress.IPv4Address("192.168.1.1")
assert self.encoder.default(obj) == str(obj)
def test_encode_ipaddress_ipv6address(self):
"""
Tests encoding ipaddress IPv6Address object
"""
obj = ipaddress.IPv6Address("2001:0db8:85a3:0000:0000:8a2e:0370:7334")
assert self.encoder.default(obj) == str(obj)
def test_encode_ipaddress_ipv4network(self):
"""
Tests encoding ipaddress IPv4Network object
"""
obj = ipaddress.IPv4Network("192.0.2.8/29")
assert self.encoder.default(obj) == str(obj)
def test_encode_ipaddress_ipv6network(self):
"""
Tests encoding ipaddress IPv4Network object
"""
obj = ipaddress.IPv6Network("2001:4860:0000::0000/32")
assert self.encoder.default(obj) == str(obj)
def test_encode_ipaddress_ipv4interface(self):
"""
Tests encoding ipaddress IPv4Interface object
"""
obj = ipaddress.IPv4Interface("192.0.2.8/29")
assert self.encoder.default(obj) == str(obj)
def test_encode_ipaddress_ipv6interface(self):
"""
Tests encoding ipaddress IPv4Network object
"""
obj = ipaddress.IPv6Interface("2001:4860:4860::8888/32")
assert self.encoder.default(obj) == str(obj)
@pytest.mark.skipif(not coreapi, reason='coreapi is not installed')
def test_encode_coreapi_raises_error(self):
"""
Tests encoding a coreapi objects raises proper error
"""
with pytest.raises(RuntimeError):
self.encoder.default(coreapi.Document())
with pytest.raises(RuntimeError):
self.encoder.default(coreapi.Error())
def test_encode_object_with_tolist(self):
"""
Tests encoding a object with tolist method
"""
foo = MockList()
assert self.encoder.default(foo) == [1, 2, 3]
def test_encode_empty_returnlist(self):
"""
Tests encoding an empty ReturnList
"""
foo = ReturnList(serializer=None)
assert self.encoder.default(foo) == []