from __future__ import unicode_literals from rest_framework.compat import StringIO from django import forms from django.test import TestCase from rest_framework.parsers import FormParser from rest_framework.parsers import XMLParser import datetime class Form(forms.Form): field1 = forms.CharField(max_length=3) field2 = forms.CharField() class TestFormParser(TestCase): def setUp(self): self.string = "field1=abc&field2=defghijk" def test_parse(self): """ Make sure the `QueryDict` works OK """ parser = FormParser() stream = StringIO(self.string) data = parser.parse(stream) self.assertEqual(Form(data).is_valid(), True) class TestXMLParser(TestCase): def setUp(self): self._input = StringIO( '' '' '121.0' 'dasd' '' '2011-12-25 12:45:00' '' ) self._data = { 'field_a': 121, 'field_b': 'dasd', 'field_c': None, 'field_d': datetime.datetime(2011, 12, 25, 12, 45, 00) } self._complex_data_input = StringIO( '' '' '2011-12-25 12:45:00' '' '1first' '2second' '' 'name' '' ) self._complex_data = { "creation_date": datetime.datetime(2011, 12, 25, 12, 45, 00), "name": "name", "sub_data_list": [ { "sub_id": 1, "sub_name": "first" }, { "sub_id": 2, "sub_name": "second" } ] } def test_parse(self): parser = XMLParser() data = parser.parse(self._input) self.assertEqual(data, self._data) def test_complex_data_parse(self): parser = XMLParser() data = parser.parse(self._complex_data_input) self.assertEqual(data, self._complex_data)