2011-05-10 19:01:58 +04:00
|
|
|
"""Tests for the resource module"""
|
|
|
|
from django.test import TestCase
|
2011-05-13 12:59:36 +04:00
|
|
|
from djangorestframework.resources import _object_to_data
|
2011-05-10 19:01:58 +04:00
|
|
|
|
|
|
|
import datetime
|
|
|
|
import decimal
|
|
|
|
|
|
|
|
class TestObjectToData(TestCase):
|
|
|
|
"""Tests for the _object_to_data function"""
|
|
|
|
|
|
|
|
def test_decimal(self):
|
|
|
|
"""Decimals need to be converted to a string representation."""
|
|
|
|
self.assertEquals(_object_to_data(decimal.Decimal('1.5')), '1.5')
|
2011-05-13 20:19:12 +04:00
|
|
|
|
2011-05-10 19:01:58 +04:00
|
|
|
def test_function(self):
|
|
|
|
"""Functions with no arguments should be called."""
|
|
|
|
def foo():
|
|
|
|
return 1
|
|
|
|
self.assertEquals(_object_to_data(foo), 1)
|
|
|
|
|
|
|
|
def test_method(self):
|
|
|
|
"""Methods with only a ``self`` argument should be called."""
|
|
|
|
class Foo(object):
|
|
|
|
def foo(self):
|
|
|
|
return 1
|
|
|
|
self.assertEquals(_object_to_data(Foo().foo), 1)
|
|
|
|
|
|
|
|
def test_datetime(self):
|
|
|
|
"""datetime objects are left as-is."""
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
self.assertEquals(_object_to_data(now), now)
|