From 36b58d0c78a065c273a19eeb4f4d6d5a2b92d425 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 3 Oct 2012 10:51:38 +0100 Subject: [PATCH] Many2Many test --- rest_framework/tests/models.py | 16 +++++++++ rest_framework/tests/serializer.py | 56 +++++++++++++++++++++++------- 2 files changed, 60 insertions(+), 12 deletions(-) diff --git a/rest_framework/tests/models.py b/rest_framework/tests/models.py index 7429d8630..c90668cad 100644 --- a/rest_framework/tests/models.py +++ b/rest_framework/tests/models.py @@ -28,9 +28,25 @@ from django.db import models # 'pk': self.id # }) +class Anchor(models.Model): + """ + A simple model to use as the target of relationships for other test models. + """ + text = models.CharField(max_length=100, default='anchor') + + class Meta: + app_label = 'rest_framework' + class BasicModel(models.Model): text = models.CharField(max_length=100) class Meta: app_label = 'rest_framework' + + +class ManyToManyModel(models.Model): + rel = models.ManyToManyField(Anchor) + + class Meta: + app_label = 'rest_framework' diff --git a/rest_framework/tests/serializer.py b/rest_framework/tests/serializer.py index 16de2c66c..7de79f957 100644 --- a/rest_framework/tests/serializer.py +++ b/rest_framework/tests/serializer.py @@ -1,6 +1,7 @@ import datetime from django.test import TestCase from rest_framework import serializers +from rest_framework.tests.models import * class Comment(object): @@ -94,18 +95,6 @@ class ValidationTests(TestCase): class MetadataTests(TestCase): - # def setUp(self): - # self.comment = Comment( - # 'tomchristie', - # 'Happy new year!', - # datetime.datetime(2012, 1, 1) - # ) - # self.data = { - # 'email': 'tomchristie', - # 'content': 'Happy new year!', - # 'created': datetime.datetime(2012, 1, 1) - # } - def test_empty(self): serializer = CommentSerializer() expected = { @@ -115,3 +104,46 @@ class MetadataTests(TestCase): } for field_name, field in expected.items(): self.assertTrue(isinstance(serializer.data.fields[field_name], field)) + + +class ManyToManyTests(TestCase): + def setUp(self): + class ManyToManySerializer(serializers.ModelSerializer): + class Meta: + model = ManyToManyModel + + self.serializer_class = ManyToManySerializer + + # An anchor instance to use for the relationship + self.anchor = Anchor() + self.anchor.save() + + # A model instance with a many to many relationship to the anchor + self.instance = ManyToManyModel() + self.instance.save() + self.instance.rel.add(self.anchor) + + # A serialized representation of the model instance + self.data = {'id': 1, 'rel': [self.anchor.id]} + + def test_retrieve(self): + serializer = self.serializer_class(instance=self.instance) + expected = self.data + self.assertEquals(serializer.data, expected) + + def test_create(self): + data = {'rel': [self.anchor.id]} + serializer = self.serializer_class(data) + self.assertEquals(serializer.is_valid(), True) + serializer.object.save() + obj = serializer.object.object + self.assertEquals(obj.pk, 2) + self.assertEquals(list(obj.rel.all()), [self.anchor]) + # self.assertFalse(serializer.object is expected) + + # 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)