Skip to content

Commit 80cf8f4

Browse files
committed
Increase coverage
-Use encoder.encode instead of json.dumps. encoder.default can't be used to test floats
1 parent 5777d25 commit 80cf8f4

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

tests/test_encoders.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import json
1+
# -*- coding: utf-8 -*-
2+
23
from datetime import date, datetime, timedelta
34
from decimal import Decimal
45
from uuid import uuid4
56

67
import pytest
78
from django.test import TestCase
9+
from django.utils import six
810
from django.utils.timezone import utc
911

1012
from rest_framework.compat import coreapi
@@ -100,4 +102,21 @@ def test_encode_float(self):
100102
"""
101103

102104
f = [3.141592653, float('inf'), float('-inf'), float('nan')]
103-
assert json.dumps(f, cls=JSONEncoder) == '[3.141592653, "Infinity", "-Infinity", "NaN"]'
105+
assert self.encoder.encode(f) == '[3.141592653, "Infinity", "-Infinity", "NaN"]'
106+
107+
encoder = JSONEncoder(allow_nan=False)
108+
try:
109+
encoder.encode(f)
110+
except ValueError:
111+
pass
112+
else:
113+
assert False
114+
115+
def test_encode_string(self):
116+
"""
117+
Tests encoding string
118+
"""
119+
120+
if six.PY2:
121+
encoder2 = JSONEncoder(encoding='latin_1', check_circular=False)
122+
assert encoder2.encode(['foo☺']) == '["foo\\u00e2\\u0098\\u00ba"]'

0 commit comments

Comments
 (0)