Commits

Anonymous committed e34bbc9

YamlResponder is now implemented, and has tests.

Comments (0)

Files changed (3)

generic/rest_views.py

 class SerializingResponder(BaseResponder):
     serializer = "YourSerializerHere"
     mimetype = "Serializer's MimeType"
+    ensure_ascii = False
     def render_list(self, request, object_list, paginator, page_obj):
-        data = serialize(self.serializer, object_list, ensure_ascii=False)
+        if self.ensure_ascii:
+            data = serialize(self.serializer, object_list, ensure_ascii=False)
+        else:
+            data = serialize(self.serializer, object_list)
         response = HttpResponse(data, mimetype=self.mimetype)
         return response
 
         """
         Renders single model objects to HttpResponse.
         """
-        data = serialize(self.serializer, [obj], ensure_ascii=False)
+        if self.ensure_ascii:
+            data = serialize(self.serializer, [obj], ensure_ascii=False)
+        else:
+            data = serialize(self.serializer, [obj])
         response = HttpResponse(data, mimetype=self.mimetype)
         return response
 
 class JsonResponder(SerializingResponder):
     serializer = 'json'
     mimetype = 'application/json'
+    ensure_ascii = True
 
 
 class XmlResponder(SerializingResponder):

tests/test_modelview/tests.py

 from django.test import TestCase
 from models import Article
 from django.utils import simplejson
+from xml.dom.minidom import parseString
         
 class ArticleTest(TestCase):
     def test_model(self):
         self.assertEqual(data[0]['fields']['body'], a.body)
 
     def test_json_responder_list(self):
-        pass
+        response = self.client.get('/articles/json/')
+        self.assertEqual(response.status_code, 200)
+        data = simplejson.loads(response.content)
+        self.assertEqual(Article.objects.count(), len(data))
+
 
     def test_xml_responder_detail(self):
-        from xml.dom.minidom import parseString
         a = Article.objects.all()[0]
         response = self.client.get('/articles/%s/xml/' % a.slug)
         self.assertEqual(response.status_code, 200)
-
         dom = parseString(response.content)
         obj = dom.childNodes[0].childNodes[0]
         self.assertEqual(obj.attributes['pk'].value, str(a.pk))
         for node in obj.childNodes:
             field = node.attributes['name'].value
             value = node.childNodes[0].nodeValue
-            # ignoring date, because checking it was failing on
-            # 'correct' but different results like:
-            # AssertionError: '2008-06-19 02:54:12.951959' != u'2008-06-19 02:54:12'
+            """
+            Ignoring date, because checking it was failing on
+            'correct' but different results like:
+            AssertionError: '2008-06-19 02:54:12.951959' != u'2008-06-19 02:54:12'
+            """
             if field != 'date':
                 self.assertEqual(str(getattr(a,field)),value)
 
 
     def test_xml_responder_list(self):
-        pass
+        response = self.client.get('/articles/xml/')
+        self.assertEqual(response.status_code, 200)
+        dom = parseString(response.content)
+        self.assertEqual(Article.objects.count(), len(dom.childNodes[0].childNodes))
 
     def test_yaml_responder_detail(self):
-        # require PyYaml library to use.
         try:
             import yaml
+            a = Article.objects.all()[0]
+            response = self.client.get(u'/articles/%s/yaml/' % a.slug)
+            self.assertEqual(response.status_code, 200)
+            data = yaml.load(response.content)[0]
+            self.assertEqual(a.slug, data['fields']['slug'])
+            self.assertEqual(a.body, data['fields']['body'])
+            
+
         except ImportError:
             pass
 
-
-    def test_yaml_responder_detail(self):
-        # Requires PyYaml library to use.
+    def test_yaml_responder_list(self):
         try:
             import yaml
+            response = self.client.get(u'/articles/yaml/')
+            self.assertEqual(response.status_code, 200)
+            data = yaml.load(response.content)
+            self.assertEqual(len(data), Article.objects.count())
         except ImportError:
             pass
-
-
-
 from test_modelview.models import Article
 from django_modelview.generic.rest_views import *
 
-responders = (HtmlResponder, JsonResponder, XmlResponder, AtomResponder, RssResponder)
+responders = (HtmlResponder, JsonResponder, XmlResponder, 
+              YamlResponder, AtomResponder, RssResponder)
 articles = ModelView(Article.objects.all(),
                      responders=responders,
                      methods=('GET',))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.