From e6409bbc836cbe61be7f525900f5bc0616f5c225 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20Gro=C3=9F?= Date: Sat, 18 May 2013 15:48:36 +0200 Subject: [PATCH 1/4] Temp commit for branch change ;-) --- rest_framework/tests/fields.py | 61 ++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/rest_framework/tests/fields.py b/rest_framework/tests/fields.py index 6b1cdfc77..cb5b05dc7 100644 --- a/rest_framework/tests/fields.py +++ b/rest_framework/tests/fields.py @@ -10,6 +10,7 @@ from django.test import TestCase from django.core import validators from rest_framework import serializers from rest_framework.serializers import Serializer +from rest_framework.tests.models import RESTFrameworkModel class TimestampedModel(models.Model): @@ -685,3 +686,63 @@ class ChoiceFieldTests(TestCase): """ f = serializers.ChoiceField(required=False, choices=self.SAMPLE_CHOICES) self.assertEqual(f.choices, models.fields.BLANK_CHOICE_DASH + self.SAMPLE_CHOICES) + + +class SlugFieldTests(TestCase): + """ + Tests for SlugField attribute values + """ + + def test_default_value(self): + class SlugFieldModel(RESTFrameworkModel): + slug_field = models.SlugField(blank=True) + + class SlugFieldSerializer(serializers.ModelSerializer): + class Meta: + model = SlugFieldModel + + serializer = SlugFieldSerializer(data={}) + self.assertEqual(serializer.is_valid(), True) + self.assertEqual(getattr(serializer.fields['slug_field'], 'max_length'), 50) + + def test_given_value(self): + class SlugFieldModel(RESTFrameworkModel): + slug_field = models.SlugField(max_length=84, blank=True) + + class SlugFieldSerializer(serializers.ModelSerializer): + class Meta: + model = SlugFieldModel + + serializer = SlugFieldSerializer(data={}) + self.assertEqual(serializer.is_valid(), True) + self.assertEqual(getattr(serializer.fields['slug_field'], 'max_length'), 84) + + +class URLFieldTests(TestCase): + """ + Tests for URLField attribute values + """ + + def test_default_value(self): + class URLFieldModel(RESTFrameworkModel): + url_field = models.URLField(blank=True) + + class URLFieldSerializer(serializers.ModelSerializer): + class Meta: + model = URLFieldModel + + serializer = URLFieldSerializer(data={}) + self.assertEqual(serializer.is_valid(), True) + self.assertEqual(getattr(serializer.fields['url_field'], 'max_length'), 200) + + def test_given_value(self): + class URLFieldModel(RESTFrameworkModel): + url_field = models.URLField(max_length=128, blank=True) + + class URLFieldSerializer(serializers.ModelSerializer): + class Meta: + model = URLFieldModel + + serializer = URLFieldSerializer(data={}) + self.assertEqual(serializer.is_valid(), True) + self.assertEqual(getattr(serializer.fields['url_field'], 'max_length'), 128) \ No newline at end of file From 9f9cb97d6538e320a0740749b200d418d9d52040 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20Gro=C3=9F?= Date: Sat, 18 May 2013 16:24:54 +0200 Subject: [PATCH 2/4] Add TestCases for default field values --- rest_framework/fields.py | 2 - rest_framework/tests/fields.py | 101 ++++++++++++++++++++++++++------- 2 files changed, 82 insertions(+), 21 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 491aa7eda..fc14184cc 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -383,7 +383,6 @@ class URLField(CharField): type_name = 'URLField' def __init__(self, **kwargs): - kwargs['max_length'] = kwargs.get('max_length', 200) kwargs['validators'] = [validators.URLValidator()] super(URLField, self).__init__(**kwargs) @@ -392,7 +391,6 @@ class SlugField(CharField): type_name = 'SlugField' def __init__(self, *args, **kwargs): - kwargs['max_length'] = kwargs.get('max_length', 50) super(SlugField, self).__init__(*args, **kwargs) diff --git a/rest_framework/tests/fields.py b/rest_framework/tests/fields.py index cb5b05dc7..d82f6b235 100644 --- a/rest_framework/tests/fields.py +++ b/rest_framework/tests/fields.py @@ -688,61 +688,124 @@ class ChoiceFieldTests(TestCase): self.assertEqual(f.choices, models.fields.BLANK_CHOICE_DASH + self.SAMPLE_CHOICES) +class EmailFieldTests(TestCase): + """ + Tests for EmailField attribute values + """ + + class EmailFieldModel(RESTFrameworkModel): + email_field = models.EmailField(blank=True) + + class EmailFieldWithGivenMaxLengthModel(RESTFrameworkModel): + email_field = models.EmailField(max_length=150, blank=True) + + def test_default_model_value(self): + class EmailFieldSerializer(serializers.ModelSerializer): + class Meta: + model = self.EmailFieldModel + + serializer = EmailFieldSerializer(data={}) + self.assertEqual(serializer.is_valid(), True) + self.assertEqual(getattr(serializer.fields['email_field'], 'max_length'), 75) + + def test_given_model_value(self): + class EmailFieldSerializer(serializers.ModelSerializer): + class Meta: + model = self.EmailFieldWithGivenMaxLengthModel + + serializer = EmailFieldSerializer(data={}) + self.assertEqual(serializer.is_valid(), True) + self.assertEqual(getattr(serializer.fields['email_field'], 'max_length'), 150) + + def test_given_serializer_value(self): + class EmailFieldSerializer(serializers.ModelSerializer): + email_field = serializers.EmailField(source='email_field', max_length=20, required=False) + + class Meta: + model = self.EmailFieldModel + + serializer = EmailFieldSerializer(data={}) + self.assertEqual(serializer.is_valid(), True) + self.assertEqual(getattr(serializer.fields['email_field'], 'max_length'), 20) + + class SlugFieldTests(TestCase): """ Tests for SlugField attribute values """ - def test_default_value(self): - class SlugFieldModel(RESTFrameworkModel): - slug_field = models.SlugField(blank=True) + class SlugFieldModel(RESTFrameworkModel): + slug_field = models.SlugField(blank=True) + class SlugFieldWithGivenMaxLengthModel(RESTFrameworkModel): + slug_field = models.SlugField(max_length=84, blank=True) + + def test_default_model_value(self): class SlugFieldSerializer(serializers.ModelSerializer): class Meta: - model = SlugFieldModel + model = self.SlugFieldModel serializer = SlugFieldSerializer(data={}) self.assertEqual(serializer.is_valid(), True) self.assertEqual(getattr(serializer.fields['slug_field'], 'max_length'), 50) - def test_given_value(self): - class SlugFieldModel(RESTFrameworkModel): - slug_field = models.SlugField(max_length=84, blank=True) - + def test_given_model_value(self): class SlugFieldSerializer(serializers.ModelSerializer): class Meta: - model = SlugFieldModel + model = self.SlugFieldWithGivenMaxLengthModel serializer = SlugFieldSerializer(data={}) self.assertEqual(serializer.is_valid(), True) self.assertEqual(getattr(serializer.fields['slug_field'], 'max_length'), 84) + def test_given_serializer_value(self): + class SlugFieldSerializer(serializers.ModelSerializer): + slug_field = serializers.SlugField(source='slug_field', max_length=20, required=False) + + class Meta: + model = self.SlugFieldModel + + serializer = SlugFieldSerializer(data={}) + self.assertEqual(serializer.is_valid(), True) + self.assertEqual(getattr(serializer.fields['slug_field'], 'max_length'), 20) + class URLFieldTests(TestCase): """ Tests for URLField attribute values """ - def test_default_value(self): - class URLFieldModel(RESTFrameworkModel): - url_field = models.URLField(blank=True) + class URLFieldModel(RESTFrameworkModel): + url_field = models.URLField(blank=True) + class URLFieldWithGivenMaxLengthModel(RESTFrameworkModel): + url_field = models.URLField(max_length=128, blank=True) + + def test_default_model_value(self): class URLFieldSerializer(serializers.ModelSerializer): class Meta: - model = URLFieldModel + model = self.URLFieldModel serializer = URLFieldSerializer(data={}) self.assertEqual(serializer.is_valid(), True) self.assertEqual(getattr(serializer.fields['url_field'], 'max_length'), 200) - def test_given_value(self): - class URLFieldModel(RESTFrameworkModel): - url_field = models.URLField(max_length=128, blank=True) - + def test_given_model_value(self): class URLFieldSerializer(serializers.ModelSerializer): class Meta: - model = URLFieldModel + model = self.URLFieldWithGivenMaxLengthModel serializer = URLFieldSerializer(data={}) self.assertEqual(serializer.is_valid(), True) - self.assertEqual(getattr(serializer.fields['url_field'], 'max_length'), 128) \ No newline at end of file + self.assertEqual(getattr(serializer.fields['url_field'], 'max_length'), 128) + + def test_given_serializer_value(self): + class URLFieldSerializer(serializers.ModelSerializer): + url_field = serializers.URLField(source='url_field', max_length=20, required=False) + + class Meta: + model = self.URLFieldWithGivenMaxLengthModel + + serializer = URLFieldSerializer(data={}) + self.assertEqual(serializer.is_valid(), True) + self.assertEqual(getattr(serializer.fields['url_field'], 'max_length'), 20) \ No newline at end of file From aeea32030b53eb73547696783cc6eb0d3e14f156 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20Gro=C3=9F?= Date: Sat, 18 May 2013 16:27:11 +0200 Subject: [PATCH 3/4] Add newline --- rest_framework/tests/fields.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rest_framework/tests/fields.py b/rest_framework/tests/fields.py index d82f6b235..109fd01f7 100644 --- a/rest_framework/tests/fields.py +++ b/rest_framework/tests/fields.py @@ -808,4 +808,5 @@ class URLFieldTests(TestCase): serializer = URLFieldSerializer(data={}) self.assertEqual(serializer.is_valid(), True) - self.assertEqual(getattr(serializer.fields['url_field'], 'max_length'), 20) \ No newline at end of file + self.assertEqual(getattr(serializer.fields['url_field'], 'max_length'), 20 + \ No newline at end of file From 6714b755f4938e998907e3d949c832f77a76fadc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20Gro=C3=9F?= Date: Sat, 18 May 2013 16:32:31 +0200 Subject: [PATCH 4/4] Fix missing para.. --- rest_framework/tests/fields.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/rest_framework/tests/fields.py b/rest_framework/tests/fields.py index 109fd01f7..dad69975f 100644 --- a/rest_framework/tests/fields.py +++ b/rest_framework/tests/fields.py @@ -808,5 +808,4 @@ class URLFieldTests(TestCase): serializer = URLFieldSerializer(data={}) self.assertEqual(serializer.is_valid(), True) - self.assertEqual(getattr(serializer.fields['url_field'], 'max_length'), 20 - \ No newline at end of file + self.assertEqual(getattr(serializer.fields['url_field'], 'max_length'), 20)