Permalink
Browse files

Fix decimal serialize

  • Loading branch information...
1 parent e6c73e5 commit 34b84c5ad33477edd101d9c3136c0d96988c24ab @klen committed Oct 3, 2012
Showing with 49 additions and 24 deletions.
  1. +3 −3 adrest/utils/serializer.py
  2. +46 −21 tests/main/tests.py
@@ -38,15 +38,15 @@ def to_simple(self, value, **options):
if isinstance(value, basestring):
return smart_unicode(value)
+ if isinstance(value, Decimal):
+ return float(str(value))
+
if isinstance(value, numbers.Number):
return value
if isinstance(value, (datetime, date, time)):
return self.to_simple_datetime(value)
- if isinstance(value, Decimal):
- return str(value)
-
if isinstance(value, dict):
return dict((k, self.to_simple(v, **options)) for k, v in value.iteritems())
View
@@ -1,5 +1,6 @@
import random
import re
+from decimal import Decimal
from django.contrib.auth.models import User
from django.core import mail
@@ -31,10 +32,14 @@ def get(self, request):
content = self.emit('test')
response = HttpResponse(content)
return response
+
test = Test()
response = test.dispatch(request)
self.assertContains(response, 'test')
+ content = emitter.JSONSerializer().serialize([1, Decimal('3.4')])
+ self.assertEqual(content, '[1, 3.4]')
+
class MetaTest(TestCase):
@@ -53,7 +58,8 @@ def test_meta(self):
self.assertEqual(AuthorResource.meta.emitters_types, [
emitter.JSONEmitter.media_type,
])
- self.assertEqual(AuthorResource.meta.default_emitter, emitter.JSONEmitter)
+ self.assertEqual(
+ AuthorResource.meta.default_emitter, emitter.JSONEmitter)
self.assertEqual(AuthorResource.meta.parsers_dict, {
parser.FormParser.media_type: parser.FormParser,
parser.XMLParser.media_type: parser.XMLParser,
@@ -64,7 +70,8 @@ def test_meta(self):
def test_meta_parents(self):
self.assertEqual(AuthorResource.meta.parents, [])
self.assertEqual(BookPrefixResource.meta.parents, [AuthorResource])
- self.assertEqual(ArticleResource.meta.parents, [AuthorResource, BookPrefixResource])
+ self.assertEqual(ArticleResource.meta.parents, [
+ AuthorResource, BookPrefixResource])
def test_meta_name(self):
self.assertEqual(AuthorResource.meta.name, 'author')
@@ -75,12 +82,15 @@ def test_meta_url_name(self):
self.assertEqual(AuthorResource.meta.url_name, 'author')
self.assertEqual(BookResource.meta.url_name, 'author-book')
self.assertEqual(BookPrefixResource.meta.url_name, 'author-test-book')
- self.assertEqual(ArticleResource.meta.url_name, 'author-test-book-article')
- self.assertEqual(SomeOtherResource.meta.url_name, 'author-device-someother')
+ self.assertEqual(
+ ArticleResource.meta.url_name, 'author-test-book-article')
+ self.assertEqual(
+ SomeOtherResource.meta.url_name, 'author-device-someother')
def test_meta_url_regex(self):
self.assertEqual(AuthorResource.meta.url_regex, '^owner/$')
- self.assertEqual(BookPrefixResource.meta.url_regex, 'owner/test/book/(?:(?P<book>[^/]+)/)?')
+ self.assertEqual(BookPrefixResource.meta.url_regex,
+ 'owner/test/book/(?:(?P<book>[^/]+)/)?')
self.assertEqual(ArticleResource.meta.url_regex, 'owner/book/(?P<book>[^/]+)/article/(?:(?P<article>[^/]+)/)?')
self.assertEqual(SomeOtherResource.meta.url_regex, 'owner/device/(?P<device>[^/]+)/someother/(?:(?P<someother>[^/]+)/)?')
@@ -135,8 +145,9 @@ def test_owners_checking(self):
))
self.assertContains(response, 'false', status_code=401)
- response = self.get_resource('author-test-book-article', key=self.author.user.accesskey_set.get(),
- book=self.book.pk, data=dict(author=self.author.pk))
+ response = self.get_resource(
+ 'author-test-book-article', key=self.author.user.accesskey_set.get(),
+ book=self.book.pk, data=dict(author=self.author.pk))
self.assertContains(response, 'true')
def test_access_logging(self):
@@ -151,7 +162,8 @@ def test_access_logging(self):
self.assertEquals(response['Content-Type'], 'text/csv')
access = Access.objects.filter(uri=response.request['PATH_INFO'])
self.assertEquals(access.count(), 1)
- self.assertEquals(access.get().response, "Invalid response content encoding")
+ self.assertEquals(
+ access.get().response, "Invalid response content encoding")
def test_options(self):
self.assertTrue('OPTIONS' in ArticleResource.allowed_methods)
@@ -175,7 +187,8 @@ def setUp(self):
self.author = Author.objects.create(name='author%s' % i, user=user)
for i in range(148):
- Book.objects.create(author=self.author, title="book%s" % i, status=random.choice((1, 2, 3)), price=432)
+ Book.objects.create(author=self.author, title="book%s" %
+ i, status=random.choice((1, 2, 3)), price=432)
def test_patch(self):
response = self.patch_resource('author')
@@ -215,15 +228,17 @@ def test_book(self):
response = self.get_resource('author-test-book', data=dict(
author=self.author.pk
))
- self.assertContains(response, 'count="%s"' % Book.objects.filter(author=self.author).count())
+ self.assertContains(response, 'count="%s"' %
+ Book.objects.filter(author=self.author).count())
self.assertContains(response, '<name>%s</name>' % self.author.name)
self.assertContains(response, '<book_price>432</book_price>')
response = self.get_resource('author-test-book', data=dict(
title__startswith="book1",
title__megadeath=12,
))
- self.assertContains(response, 'count="%s"' % Book.objects.filter(title__startswith='book1').count())
+ self.assertContains(response, 'count="%s"' % Book.objects.filter(
+ title__startswith='book1').count())
response = self.post_resource('author-test-book', data=dict(
title="new book",
@@ -252,14 +267,16 @@ def test_filter(self):
response = self.client.get(uri, data=dict(
author=self.author.pk,
status=[1, 2, 3]))
- self.assertContains(response, 'count="%s"' % Book.objects.all().count())
+ self.assertContains(
+ response, 'count="%s"' % Book.objects.all().count())
response = self.client.get(uri, data=dict(
author=self.author.pk,
status=[1, 3]))
self.assertNotContains(response, '<status>2</status>')
- response = self.client.get(uri + "?title=book2&title=book3&author=%s" % self.author.pk)
+ response = self.client.get(
+ uri + "?title=book2&title=book3&author=%s" % self.author.pk)
self.assertContains(response, 'count="2"')
def test_not_filter(self):
@@ -281,7 +298,8 @@ def test_not_filter(self):
self.assertContains(response, '<name>exclude_author</name>')
for i in xrange(5):
- self.assertContains(response, '<title>book_for_exclude%s</title>' % i)
+ self.assertContains(
+ response, '<title>book_for_exclude%s</title>' % i)
response = self.client.get(uri, data=dict(
author__not=self.author.pk,
@@ -298,19 +316,23 @@ def test_not_filter(self):
def test_custom(self):
uri = self.reverse('book')
response = self.client.get(uri)
- self.assertContains(response, 'count="%s"' % Book.objects.all().count())
+ self.assertContains(
+ response, 'count="%s"' % Book.objects.all().count())
book = Book.objects.create(author=self.author, title="book", status=1)
response = self.client.get(uri)
- self.assertContains(response, 'count="%s"' % Book.objects.all().count())
+ self.assertContains(
+ response, 'count="%s"' % Book.objects.all().count())
- uri = self.reverse('author-test-book-article', book=book.pk) + "?author=" + str(self.author.pk)
+ uri = self.reverse('author-test-book-article',
+ book=book.pk) + "?author=" + str(self.author.pk)
response = self.client.delete(uri,
HTTP_AUTHORIZATION=self.author.user.accesskey_set.get().key)
self.assertContains(response, 'Some error', status_code=500)
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[-1].subject, '[Django] ADREST API Error (500): /1.0.0/owner/book/%s/article/' % Book.objects.all().count())
- response = self.client.put(uri, HTTP_AUTHORIZATION=self.author.user.accesskey_set.get().key)
+ response = self.client.put(
+ uri, HTTP_AUTHORIZATION=self.author.user.accesskey_set.get().key)
self.assertContains(response, 'Assertion error', status_code=400)
self.assertEqual(len(mail.outbox), 2)
self.assertEqual(mail.outbox[-1].subject, '[Django] ADREST API Error (400): /1.0.0/owner/book/%s/article/' % Book.objects.all().count())
@@ -340,10 +362,12 @@ def test_books(self):
links = link_re.findall(response['Link'])
- self.assertEquals(links[0][0], '%s?page=3&author=5' % self.reverse('author-test-book'))
+ self.assertEquals(links[0][0], '%s?page=3&author=5' %
+ self.reverse('author-test-book'))
self.assertEquals(links[0][1], 'next')
- self.assertEquals(links[1][0], '%s?author=5' % self.reverse('author-test-book'))
+ self.assertEquals(
+ links[1][0], '%s?author=5' % self.reverse('author-test-book'))
self.assertEquals(links[1][1], 'previous')
def test_bson(self):
@@ -357,7 +381,8 @@ def test_bson(self):
bson = BSON.encode(dict(counter=4))
uri = self.reverse('bson')
- response = self.client.post(uri, data=bson, content_type='application/bson')
+ response = self.client.post(
+ uri, data=bson, content_type='application/bson')
test = BSON(response.content).decode()
self.assertEqual(test['counter'], 5)

0 comments on commit 34b84c5

Please sign in to comment.