-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New unit tests which improve coverage
- Loading branch information
Showing
9 changed files
with
215 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
from django.forms import fields | ||
from django.test import TestCase | ||
from django_api_forms import Form | ||
|
||
|
||
class FormIteratorTests(TestCase): | ||
def test_iter_fields(self): | ||
class TestForm(Form): | ||
field1 = fields.CharField(label='field1') | ||
field2 = fields.IntegerField(label='field2') | ||
|
||
form = TestForm() | ||
field_labels = [field.label for field in form] | ||
|
||
# Check that fields can be iterated over and contain the expected field names | ||
self.assertEqual(sorted(field_labels), ['field1', 'field2']) | ||
|
||
def test_getitem_field(self): | ||
class TestForm(Form): | ||
field1 = fields.CharField() | ||
field2 = fields.IntegerField() | ||
|
||
form = TestForm() | ||
|
||
field1 = form['field1'] | ||
self.assertIsInstance(field1, fields.CharField) | ||
|
||
field2 = form['field2'] | ||
self.assertIsInstance(field2, fields.IntegerField) | ||
|
||
with self.assertRaises(KeyError): | ||
form['nonexistent_field'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,12 @@ | |
from typing import Optional | ||
|
||
import msgpack | ||
from django.core.exceptions import ValidationError | ||
from django.forms import fields | ||
from django.test import TestCase | ||
from django.test.client import RequestFactory | ||
from django_api_forms import Form, BooleanField | ||
from django_api_forms.exceptions import UnsupportedMediaType | ||
from tests.testapp.forms import BandForm | ||
from tests.testapp.models import Band | ||
|
||
|
||
|
@@ -90,13 +90,6 @@ class FunnyForm(Form): | |
def _normalize_url(cls, url: str) -> Optional[str]: | ||
if not url: | ||
return None | ||
if url.startswith('http://'): | ||
url = url.replace('http://', '') | ||
|
||
if not url.startswith('https://'): | ||
url = f"https://{url}" | ||
|
||
return url | ||
|
||
def clean_url(self): | ||
return self._normalize_url(self.cleaned_data['url']) | ||
|
@@ -134,13 +127,6 @@ class Meta: | |
def _normalize_url(cls, url: str) -> Optional[str]: | ||
if not url: | ||
return None | ||
if url.startswith('http://'): | ||
url = url.replace('http://', '') | ||
|
||
if not url.startswith('https://'): | ||
url = f"https://{url}" | ||
|
||
return url | ||
|
||
def clean_url(self): | ||
return self._normalize_url(self.cleaned_data['url']) | ||
|
@@ -181,21 +167,6 @@ class Meta: | |
formed = fields.IntegerField() | ||
has_award = BooleanField() | ||
|
||
@classmethod | ||
def _normalize_url(cls, url: str) -> Optional[str]: | ||
if not url: | ||
return None | ||
if url.startswith('http://'): | ||
url = url.replace('http://', '') | ||
|
||
if not url.startswith('https://'): | ||
url = f"https://{url}" | ||
|
||
return url | ||
|
||
def clean_url(self): | ||
return self._normalize_url(self.cleaned_data['url']) | ||
|
||
request_factory = RequestFactory() | ||
request = request_factory.post( | ||
'/test/', | ||
|
@@ -265,13 +236,6 @@ class FunnyForm(Form): | |
def _normalize_url(cls, url: str) -> Optional[str]: | ||
if not url: | ||
return None | ||
if url.startswith('http://'): | ||
url = url.replace('http://', '') | ||
|
||
if not url.startswith('https://'): | ||
url = f"https://{url}" | ||
|
||
return url | ||
|
||
def clean_url(self): | ||
return self._normalize_url(self.cleaned_data['url']) | ||
|
@@ -285,8 +249,6 @@ def clean(self): | |
if 'param1' in self.extras and 'param2' in self.extras: | ||
self.extras['param2'] = 'param4' | ||
return self.cleaned_data | ||
else: | ||
raise ValidationError("Missing params!", code='missing-params') | ||
|
||
request_factory = RequestFactory() | ||
request = request_factory.post( | ||
|
@@ -305,3 +267,37 @@ def clean(self): | |
self.assertTrue(len(form.cleaned_data.keys()) == 3) | ||
self.assertIsNone(form.cleaned_data['url']) | ||
self.assertEqual(form.extras, valid_test_extras) | ||
|
||
def test_exclude_field(self): | ||
# Create form from request | ||
request_factory = RequestFactory() | ||
request = request_factory.post( | ||
'/test/', | ||
data={ | ||
'emails': {'Joy Division': '[email protected]'}, | ||
'name': 'Queen', | ||
'formed': '1870', | ||
'has_award': False, | ||
}, | ||
content_type='application/json' | ||
) | ||
form = BandForm.create_from_request(request) | ||
self.assertTrue(form.is_valid()) | ||
|
||
# Populate form | ||
band = Band() | ||
form.populate(band, exclude=['emails']) | ||
|
||
self.assertEqual(band.name, form.cleaned_data['name']) | ||
self.assertEqual(band.formed, 2000) | ||
self.assertEqual(band.has_award, True) | ||
|
||
def test_empty_request_body(self): | ||
# Create form from request | ||
request_factory = RequestFactory() | ||
request = request_factory.get( | ||
'/test/', | ||
content_type='application/json' | ||
) | ||
form = BandForm.create_from_request(request) | ||
self.assertFalse(form.is_valid()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters