2017-02-15 09:14:49 +03:00
|
|
|
from datetime import date, datetime, timedelta
|
2016-10-05 11:54:59 +03:00
|
|
|
from decimal import Decimal
|
|
|
|
from uuid import uuid4
|
|
|
|
|
2017-01-08 19:10:47 +03:00
|
|
|
import pytest
|
2016-10-05 11:54:59 +03:00
|
|
|
from django.test import TestCase
|
2017-02-15 09:14:49 +03:00
|
|
|
from django.utils.timezone import utc
|
2016-10-05 11:54:59 +03:00
|
|
|
|
2017-01-07 01:32:13 +03:00
|
|
|
from rest_framework.compat import coreapi
|
2016-10-05 11:54:59 +03:00
|
|
|
from rest_framework.utils.encoders import JSONEncoder
|
|
|
|
|
|
|
|
|
2017-02-15 09:14:49 +03:00
|
|
|
class MockList(object):
|
|
|
|
def tolist(self):
|
|
|
|
return [1, 2, 3]
|
|
|
|
|
|
|
|
|
2016-10-05 11:54:59 +03:00
|
|
|
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)
|
2017-02-15 09:14:49 +03:00
|
|
|
assert self.encoder.default(d) == float(d)
|
2016-10-05 11:54:59 +03:00
|
|
|
|
|
|
|
def test_encode_datetime(self):
|
|
|
|
"""
|
|
|
|
Tests encoding a datetime object
|
|
|
|
"""
|
|
|
|
current_time = datetime.now()
|
2016-11-30 13:45:48 +03:00
|
|
|
assert self.encoder.default(current_time) == current_time.isoformat()
|
2017-02-15 09:14:49 +03:00
|
|
|
current_time_utc = current_time.replace(tzinfo=utc)
|
|
|
|
assert self.encoder.default(current_time_utc) == current_time.isoformat() + 'Z'
|
2016-10-05 11:54:59 +03:00
|
|
|
|
|
|
|
def test_encode_time(self):
|
|
|
|
"""
|
|
|
|
Tests encoding a timezone
|
|
|
|
"""
|
|
|
|
current_time = datetime.now().time()
|
2017-09-22 13:03:05 +03:00
|
|
|
assert self.encoder.default(current_time) == current_time.isoformat()
|
2016-10-05 11:54:59 +03:00
|
|
|
|
|
|
|
def test_encode_time_tz(self):
|
|
|
|
"""
|
|
|
|
Tests encoding a timezone aware timestamp
|
|
|
|
"""
|
|
|
|
current_time = datetime.now().time()
|
2017-02-15 09:14:49 +03:00
|
|
|
current_time = current_time.replace(tzinfo=utc)
|
2017-01-08 19:10:47 +03:00
|
|
|
with pytest.raises(ValueError):
|
2016-10-05 11:54:59 +03:00
|
|
|
self.encoder.default(current_time)
|
|
|
|
|
|
|
|
def test_encode_date(self):
|
|
|
|
"""
|
|
|
|
Tests encoding a date object
|
|
|
|
"""
|
|
|
|
current_date = date.today()
|
2016-11-30 13:45:48 +03:00
|
|
|
assert self.encoder.default(current_date) == current_date.isoformat()
|
2016-10-05 11:54:59 +03:00
|
|
|
|
|
|
|
def test_encode_timedelta(self):
|
|
|
|
"""
|
|
|
|
Tests encoding a timedelta object
|
|
|
|
"""
|
|
|
|
delta = timedelta(hours=1)
|
2016-11-30 13:45:48 +03:00
|
|
|
assert self.encoder.default(delta) == str(delta.total_seconds())
|
2016-10-05 11:54:59 +03:00
|
|
|
|
|
|
|
def test_encode_uuid(self):
|
|
|
|
"""
|
|
|
|
Tests encoding a UUID object
|
|
|
|
"""
|
|
|
|
unique_id = uuid4()
|
2016-11-30 13:45:48 +03:00
|
|
|
assert self.encoder.default(unique_id) == str(unique_id)
|
2017-01-07 01:32:13 +03:00
|
|
|
|
2018-06-22 11:16:57 +03:00
|
|
|
@pytest.mark.skipif(not coreapi, reason='coreapi is not installed')
|
2017-01-07 01:32:13 +03:00
|
|
|
def test_encode_coreapi_raises_error(self):
|
|
|
|
"""
|
|
|
|
Tests encoding a coreapi objects raises proper error
|
|
|
|
"""
|
2017-01-08 19:10:47 +03:00
|
|
|
with pytest.raises(RuntimeError):
|
2017-01-07 01:32:13 +03:00
|
|
|
self.encoder.default(coreapi.Document())
|
|
|
|
|
2017-01-08 19:10:47 +03:00
|
|
|
with pytest.raises(RuntimeError):
|
2017-01-07 01:32:13 +03:00
|
|
|
self.encoder.default(coreapi.Error())
|
2017-02-15 09:14:49 +03:00
|
|
|
|
|
|
|
def test_encode_object_with_tolist(self):
|
|
|
|
"""
|
|
|
|
Tests encoding a object with tolist method
|
|
|
|
"""
|
|
|
|
foo = MockList()
|
|
|
|
assert self.encoder.default(foo) == [1, 2, 3]
|