From bcd2caf5598a71cb468d86b6f286e180d1bf0a19 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 4 Oct 2012 09:18:46 +0100 Subject: [PATCH 1/3] Abstract out the app_label on test models --- docs/api-guide/fields.md | 43 ++++++++++++++++++++++++++++++++++ rest_framework/tests/models.py | 23 ++++++++---------- 2 files changed, 53 insertions(+), 13 deletions(-) create mode 100644 docs/api-guide/fields.md diff --git a/docs/api-guide/fields.md b/docs/api-guide/fields.md new file mode 100644 index 000000000..009d2a79d --- /dev/null +++ b/docs/api-guide/fields.md @@ -0,0 +1,43 @@ + + +# Serializer fields + +> Flat is better than nested. +> +> — [The Zen of Python][cite] + +# Generic Fields + +## Field + +## ModelField + +# Typed Fields + +## BooleanField + +## CharField + +## EmailField + +## DateField + +## DateTimeField + +## IntegerField + +## FloatField + +# Relational Fields + +Relational fields are used to represent model relationships. + +## PrimaryKeyRelatedField + +## ManyPrimaryKeyRelatedField + +## HyperlinkedRelatedField + +## ManyHyperlinkedRelatedField + +[cite]: http://www.python.org/dev/peps/pep-0020/ diff --git a/rest_framework/tests/models.py b/rest_framework/tests/models.py index c90668cad..c5636f35b 100644 --- a/rest_framework/tests/models.py +++ b/rest_framework/tests/models.py @@ -28,25 +28,22 @@ from django.db import models # 'pk': self.id # }) -class Anchor(models.Model): +class RestFrameworkModel(models.Model): """ - A simple model to use as the target of relationships for other test models. + Base for test models that sets app_label, so they play nicely. """ + class Meta: + app_label = 'rest_framework' + abstract = True + + +class Anchor(RestFrameworkModel): text = models.CharField(max_length=100, default='anchor') - class Meta: - app_label = 'rest_framework' - -class BasicModel(models.Model): +class BasicModel(RestFrameworkModel): text = models.CharField(max_length=100) - class Meta: - app_label = 'rest_framework' - -class ManyToManyModel(models.Model): +class ManyToManyModel(RestFrameworkModel): rel = models.ManyToManyField(Anchor) - - class Meta: - app_label = 'rest_framework' From f8edfa26759bd9922b674625cee5921c163ed40e Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 4 Oct 2012 09:19:10 +0100 Subject: [PATCH 2/3] Abstract out the app_label on test models --- rest_framework/tests/models.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rest_framework/tests/models.py b/rest_framework/tests/models.py index c5636f35b..6cc2c3116 100644 --- a/rest_framework/tests/models.py +++ b/rest_framework/tests/models.py @@ -28,7 +28,7 @@ from django.db import models # 'pk': self.id # }) -class RestFrameworkModel(models.Model): +class RESTFrameworkModel(models.Model): """ Base for test models that sets app_label, so they play nicely. """ @@ -37,13 +37,13 @@ class RestFrameworkModel(models.Model): abstract = True -class Anchor(RestFrameworkModel): +class Anchor(RESTFrameworkModel): text = models.CharField(max_length=100, default='anchor') -class BasicModel(RestFrameworkModel): +class BasicModel(RESTFrameworkModel): text = models.CharField(max_length=100) -class ManyToManyModel(RestFrameworkModel): +class ManyToManyModel(RESTFrameworkModel): rel = models.ManyToManyField(Anchor) From d79e18963e64c1714a29ece6ca59a8a4a1c02516 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 4 Oct 2012 09:29:12 +0100 Subject: [PATCH 3/3] Tests for default arguments on fields --- rest_framework/tests/models.py | 12 ++++++ rest_framework/tests/serializer.py | 61 +++++++++++++++++++++++++++--- 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/rest_framework/tests/models.py b/rest_framework/tests/models.py index 6cc2c3116..969c82978 100644 --- a/rest_framework/tests/models.py +++ b/rest_framework/tests/models.py @@ -28,6 +28,10 @@ from django.db import models # 'pk': self.id # }) +def foobar(): + return 'foobar' + + class RESTFrameworkModel(models.Model): """ Base for test models that sets app_label, so they play nicely. @@ -45,5 +49,13 @@ class BasicModel(RESTFrameworkModel): text = models.CharField(max_length=100) +class DefaultValueModel(RESTFrameworkModel): + text = models.CharField(default='foobar', max_length=100) + + +class CallableDefaultValueModel(RESTFrameworkModel): + text = models.CharField(default=foobar, max_length=100) + + class ManyToManyModel(RESTFrameworkModel): rel = models.ManyToManyField(Anchor) diff --git a/rest_framework/tests/serializer.py b/rest_framework/tests/serializer.py index db342c9e0..08a7a9d07 100644 --- a/rest_framework/tests/serializer.py +++ b/rest_framework/tests/serializer.py @@ -202,9 +202,58 @@ class ManyToManyTests(TestCase): self.assertEquals(instance.pk, 2) self.assertEquals(list(instance.rel.all()), []) - # def test_deserialization_for_update(self): - # serializer = self.serializer_class(self.data, instance=self.instance) - # expected = self.instance - # self.assertEquals(serializer.is_valid(), True) - # self.assertEquals(serializer.object, expected) - # self.assertTrue(serializer.object is expected) + +class DefaultValueTests(TestCase): + def setUp(self): + class DefaultValueSerializer(serializers.ModelSerializer): + class Meta: + model = DefaultValueModel + + self.serializer_class = DefaultValueSerializer + self.objects = DefaultValueModel.objects + + def test_create_using_default(self): + data = {} + serializer = self.serializer_class(data) + self.assertEquals(serializer.is_valid(), True) + instance = serializer.save() + self.assertEquals(len(self.objects.all()), 1) + self.assertEquals(instance.pk, 1) + self.assertEquals(instance.text, 'foobar') + + def test_create_overriding_default(self): + data = {'text': 'overridden'} + serializer = self.serializer_class(data) + self.assertEquals(serializer.is_valid(), True) + instance = serializer.save() + self.assertEquals(len(self.objects.all()), 1) + self.assertEquals(instance.pk, 1) + self.assertEquals(instance.text, 'overridden') + + +class CallableDefaultValueTests(TestCase): + def setUp(self): + class CallableDefaultValueSerializer(serializers.ModelSerializer): + class Meta: + model = CallableDefaultValueModel + + self.serializer_class = CallableDefaultValueSerializer + self.objects = CallableDefaultValueModel.objects + + def test_create_using_default(self): + data = {} + serializer = self.serializer_class(data) + self.assertEquals(serializer.is_valid(), True) + instance = serializer.save() + self.assertEquals(len(self.objects.all()), 1) + self.assertEquals(instance.pk, 1) + self.assertEquals(instance.text, 'foobar') + + def test_create_overriding_default(self): + data = {'text': 'overridden'} + serializer = self.serializer_class(data) + self.assertEquals(serializer.is_valid(), True) + instance = serializer.save() + self.assertEquals(len(self.objects.all()), 1) + self.assertEquals(instance.pk, 1) + self.assertEquals(instance.text, 'overridden')