예제 #1
0
    def test_json_error_response_1(self):
        class TestForm(forms.Form):
            foo = forms.IntegerField(required=True)

            def clean(self):
                raise forms.ValidationError(u"Test")

        form = TestForm({"foo": "a"})
        self.assertFalse(form.is_valid())

        view = SuperView()
        data = view.render_json_error(form.errors)
        data = json.loads(data.content)

        self.assertIn("errors", data)
        self.assertIn("global", data["errors"])
        self.assertEqual(len(data["errors"]["global"]), 1)

        data = view.render_json_error(form.errors, aditional=["test2"], context={"ff": 1})
        data = json.loads(data.content)

        self.assertIn("errors", data)
        self.assertIn("global", data["errors"])
        self.assertEqual(len(data["errors"]["global"]), 2)
        self.assertIn("ff", data)
        self.assertIn("test2", data["errors"]["global"])
예제 #2
0
    def test_json_error_response_1(self):
        class TestForm(forms.Form):
            foo = forms.IntegerField(required=True)

            def clean(self):
                raise forms.ValidationError(u"Test")

        form = TestForm({'foo': 'a'})
        self.assertFalse(form.is_valid())

        view = SuperView()
        data = view.render_json_error(form.errors)
        data = json.loads(data.content)

        self.assertIn('errors', data)
        self.assertIn('global', data['errors'])
        self.assertEqual(len(data['errors']['global']), 1)

        data = view.render_json_error(form.errors,
                                      aditional=['test2'],
                                      context={'ff': 1})
        data = json.loads(data.content)

        self.assertIn('errors', data)
        self.assertIn('global', data['errors'])
        self.assertEqual(len(data['errors']['global']), 2)
        self.assertIn('ff', data)
        self.assertIn('test2', data['errors']['global'])
예제 #3
0
    def test_json_error_response_3(self):
        class TestForm(forms.Form):
            foo = forms.IntegerField(required=True, label=u"Fòó")

        form = TestForm({"foo": "a"})
        view = SuperView()
        data = view.render_json_error(form.errors, form=form)
        data = json.loads(data.content)

        self.assertIn("errors", data)
        self.assertIn("global", data["errors"])
        self.assertIn("fields", data["errors"])
예제 #4
0
    def test_json_error_response_3(self):
        class TestForm(forms.Form):
            foo = forms.IntegerField(required=True, label=u"Fòó")

        form = TestForm({'foo': 'a'})
        view = SuperView()
        data = view.render_json_error(form.errors, form=form)
        data = json.loads(data.content)

        self.assertIn('errors', data)
        self.assertIn('global', data['errors'])
        self.assertIn('fields', data['errors'])
예제 #5
0
    def test_json_tz(self):
        view = SuperView()
        context_0 = {"date": parse_datetime("2012-02-21 10:28:45").replace(tzinfo=utc)}

        with override(pytz.timezone("Australia/Sydney")):
            response = view.render_json(context_0)
            jdata = json.loads(response.content)
            self.assertEqual(jdata["date"], "2012-02-21T21:28:45+11:00")

        with override(pytz.timezone("Europe/Madrid")):
            response = view.render_json(context_0)
            jdata = json.loads(response.content)
            self.assertEqual(jdata["date"], "2012-02-21T11:28:45+01:00")
예제 #6
0
    def test_json_response(self):
        view = SuperView()

        data = view.render_json({"foo": 1})
        data = json.loads(data.content)

        self.assertIn("foo", data)
        self.assertIn("success", data)

        self.assertTrue(data["success"])

        data = view.render_json({"foo": 1}, ok=False)
        data = json.loads(data.content)

        self.assertFalse(data["success"])
예제 #7
0
    def test_json_response(self):
        view = SuperView()

        data = view.render_json({'foo': 1})
        data = json.loads(data.content)

        self.assertIn('foo', data)
        self.assertIn('success', data)

        self.assertTrue(data['success'])

        data = view.render_json({'foo': 1}, ok=False)
        data = json.loads(data.content)

        self.assertFalse(data['success'])
예제 #8
0
    def test_json_tz(self):
        view = SuperView()
        context_0 = {
            'date': parse_datetime("2012-02-21 10:28:45").replace(tzinfo=utc)
        }

        with override(pytz.timezone("Australia/Sydney")):
            response = view.render_json(context_0)
            jdata = json.loads(response.content)
            self.assertEqual(jdata['date'], "2012-02-21T21:28:45+11:00")

        with override(pytz.timezone("Europe/Madrid")):
            response = view.render_json(context_0)
            jdata = json.loads(response.content)
            self.assertEqual(jdata['date'], "2012-02-21T11:28:45+01:00")
예제 #9
0
    def test_json_error_response_4(self):
        class TestModel(models.Model):
            foo = models.IntegerField(_(u"Fóò"))

        class TestForm(forms.ModelForm):
            class Meta:
                model = TestModel

        form = TestForm({"foo": "a"})
        view = SuperView()
        data = view.render_json_error(form.errors, form=form)
        data = json.loads(data.content)

        print data

        self.assertIn("errors", data)
        self.assertIn("global", data["errors"])
        self.assertIn("fields", data["errors"])
예제 #10
0
    def test_json_error_response_2(self):
        class TestForm(forms.Form):
            foo = forms.IntegerField(required=True, label="Jojoo")

            def clean(self):
                raise forms.ValidationError(u"Test")

        form = TestForm({"foo": "a"})
        self.assertFalse(form.is_valid())

        view = SuperView()
        data = view.render_json_error(form.errors, form=form)
        data = json.loads(data.content)

        self.assertIn("errors", data)
        self.assertIn("global", data["errors"])
        self.assertEqual(len(data["errors"]["global"]), 1)
        self.assertIn("fields", data["errors"])
예제 #11
0
    def test_json_error_response_4(self):
        class TestModel(models.Model):
            foo = models.IntegerField(_(u"Fóò"))

        class TestForm(forms.ModelForm):
            class Meta:
                model = TestModel

        form = TestForm({'foo': 'a'})
        view = SuperView()
        data = view.render_json_error(form.errors, form=form)
        data = json.loads(data.content)

        print data

        self.assertIn('errors', data)
        self.assertIn('global', data['errors'])
        self.assertIn('fields', data['errors'])
예제 #12
0
    def test_json_error_response_2(self):
        class TestForm(forms.Form):
            foo = forms.IntegerField(required=True, label="Jojoo")

            def clean(self):
                raise forms.ValidationError(u"Test")

        form = TestForm({'foo': 'a'})
        self.assertFalse(form.is_valid())

        view = SuperView()
        data = view.render_json_error(form.errors, form=form)
        data = json.loads(data.content)

        self.assertIn('errors', data)
        self.assertIn('global', data['errors'])
        self.assertEqual(len(data['errors']['global']), 1)
        self.assertIn('fields', data['errors'])
예제 #13
0
    def test_json_error_response_0(self):
        class TestForm(forms.Form):
            foo = forms.IntegerField(required=True)

        form = TestForm({"foo": "a"})
        self.assertFalse(form.is_valid())

        view = SuperView()
        data = view.render_json_error(form.errors)
        data = json.loads(data.content)

        self.assertIn("errors", data)
        self.assertIn("success", data)

        self.assertFalse(data["success"])
        self.assertTrue(isinstance(data["errors"], dict))

        self.assertIn("form", data["errors"])
        self.assertIn("global", data["errors"])

        self.assertTrue(isinstance(data["errors"]["global"], list))
        self.assertTrue(isinstance(data["errors"]["form"], dict))

        self.assertIn("foo", data["errors"]["form"])
예제 #14
0
    def test_json_error_response_0(self):
        class TestForm(forms.Form):
            foo = forms.IntegerField(required=True)

        form = TestForm({'foo': 'a'})
        self.assertFalse(form.is_valid())

        view = SuperView()
        data = view.render_json_error(form.errors)
        data = json.loads(data.content)

        self.assertIn('errors', data)
        self.assertIn('success', data)

        self.assertFalse(data['success'])
        self.assertTrue(isinstance(data['errors'], dict))

        self.assertIn('form', data['errors'])
        self.assertIn('global', data['errors'])

        self.assertTrue(isinstance(data['errors']['global'], list))
        self.assertTrue(isinstance(data['errors']['form'], dict))

        self.assertIn('foo', data['errors']['form'])