aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/tests/test_genericrelations.py
diff options
context:
space:
mode:
authorAndreas Pelme2014-03-02 12:40:30 +0100
committerAndreas Pelme2014-03-02 12:40:30 +0100
commit971578ca345c3d3bae7fd93b87c41d43483b6f05 (patch)
tree72a6d3d0ced5750a8ba01bedc3a6f8a936e05e2f /rest_framework/tests/test_genericrelations.py
parent62786a7ad64918022f11f1b95ce84adb8d798830 (diff)
downloaddjango-rest-framework-971578ca345c3d3bae7fd93b87c41d43483b6f05.tar.bz2
Support for running the test suite with py.test
* Get rid of runtests.py * Moved test code from rest_framework/tests and rest_framework/runtests to tests * Invoke py.test from setup.py * Invoke py.test from Travis * Invoke py.test from tox * Changed setUpClass to be just plain setUp in test_permissions.py * Updated contribution guideline to show how to invoke py.test
Diffstat (limited to 'rest_framework/tests/test_genericrelations.py')
-rw-r--r--rest_framework/tests/test_genericrelations.py129
1 files changed, 0 insertions, 129 deletions
diff --git a/rest_framework/tests/test_genericrelations.py b/rest_framework/tests/test_genericrelations.py
deleted file mode 100644
index 2d341344..00000000
--- a/rest_framework/tests/test_genericrelations.py
+++ /dev/null
@@ -1,129 +0,0 @@
-from __future__ import unicode_literals
-from django.contrib.contenttypes.models import ContentType
-from django.contrib.contenttypes.generic import GenericRelation, GenericForeignKey
-from django.db import models
-from django.test import TestCase
-from rest_framework import serializers
-
-
-class Tag(models.Model):
- """
- Tags have a descriptive slug, and are attached to an arbitrary object.
- """
- tag = models.SlugField()
- content_type = models.ForeignKey(ContentType)
- object_id = models.PositiveIntegerField()
- tagged_item = GenericForeignKey('content_type', 'object_id')
-
- def __unicode__(self):
- return self.tag
-
-
-class Bookmark(models.Model):
- """
- A URL bookmark that may have multiple tags attached.
- """
- url = models.URLField()
- tags = GenericRelation(Tag)
-
- def __unicode__(self):
- return 'Bookmark: %s' % self.url
-
-
-class Note(models.Model):
- """
- A textual note that may have multiple tags attached.
- """
- text = models.TextField()
- tags = GenericRelation(Tag)
-
- def __unicode__(self):
- return 'Note: %s' % self.text
-
-
-class TestGenericRelations(TestCase):
- def setUp(self):
- self.bookmark = Bookmark.objects.create(url='https://www.djangoproject.com/')
- Tag.objects.create(tagged_item=self.bookmark, tag='django')
- Tag.objects.create(tagged_item=self.bookmark, tag='python')
- self.note = Note.objects.create(text='Remember the milk')
- Tag.objects.create(tagged_item=self.note, tag='reminder')
-
- def test_generic_relation(self):
- """
- Test a relationship that spans a GenericRelation field.
- IE. A reverse generic relationship.
- """
-
- class BookmarkSerializer(serializers.ModelSerializer):
- tags = serializers.RelatedField(many=True)
-
- class Meta:
- model = Bookmark
- exclude = ('id',)
-
- serializer = BookmarkSerializer(self.bookmark)
- expected = {
- 'tags': ['django', 'python'],
- 'url': 'https://www.djangoproject.com/'
- }
- self.assertEqual(serializer.data, expected)
-
- def test_generic_nested_relation(self):
- """
- Test saving a GenericRelation field via a nested serializer.
- """
-
- class TagSerializer(serializers.ModelSerializer):
- class Meta:
- model = Tag
- exclude = ('content_type', 'object_id')
-
- class BookmarkSerializer(serializers.ModelSerializer):
- tags = TagSerializer()
-
- class Meta:
- model = Bookmark
- exclude = ('id',)
-
- data = {
- 'url': 'https://docs.djangoproject.com/',
- 'tags': [
- {'tag': 'contenttypes'},
- {'tag': 'genericrelations'},
- ]
- }
- serializer = BookmarkSerializer(data=data)
- self.assertTrue(serializer.is_valid())
- serializer.save()
- self.assertEqual(serializer.object.tags.count(), 2)
-
- def test_generic_fk(self):
- """
- Test a relationship that spans a GenericForeignKey field.
- IE. A forward generic relationship.
- """
-
- class TagSerializer(serializers.ModelSerializer):
- tagged_item = serializers.RelatedField()
-
- class Meta:
- model = Tag
- exclude = ('id', 'content_type', 'object_id')
-
- serializer = TagSerializer(Tag.objects.all(), many=True)
- expected = [
- {
- 'tag': 'django',
- 'tagged_item': 'Bookmark: https://www.djangoproject.com/'
- },
- {
- 'tag': 'python',
- 'tagged_item': 'Bookmark: https://www.djangoproject.com/'
- },
- {
- 'tag': 'reminder',
- 'tagged_item': 'Note: Remember the milk'
- }
- ]
- self.assertEqual(serializer.data, expected)