diff --git a/tests/conftest.py b/tests/conftest.py index 1c0c6dda7..ac29e4a42 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -57,6 +57,7 @@ def pytest_configure(config): 'rest_framework', 'rest_framework.authtoken', 'tests.authentication', + 'tests.generic_relations', 'tests.importable', 'tests', ), diff --git a/tests/generic_relations/__init__.py b/tests/generic_relations/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/generic_relations/migrations/0001_initial.py b/tests/generic_relations/migrations/0001_initial.py new file mode 100644 index 000000000..ea04d8d67 --- /dev/null +++ b/tests/generic_relations/migrations/0001_initial.py @@ -0,0 +1,36 @@ +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('contenttypes', '0002_remove_content_type_name'), + ] + + operations = [ + migrations.CreateModel( + name='Bookmark', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('url', models.URLField()), + ], + ), + migrations.CreateModel( + name='Note', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('text', models.TextField()), + ], + ), + migrations.CreateModel( + name='Tag', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('tag', models.SlugField()), + ('object_id', models.PositiveIntegerField()), + ('content_type', models.ForeignKey(on_delete=models.CASCADE, to='contenttypes.ContentType')), + ], + ), + ] diff --git a/tests/generic_relations/migrations/__init__.py b/tests/generic_relations/migrations/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/generic_relations/models.py b/tests/generic_relations/models.py new file mode 100644 index 000000000..55bc243cb --- /dev/null +++ b/tests/generic_relations/models.py @@ -0,0 +1,46 @@ +from __future__ import unicode_literals + +from django.contrib.contenttypes.fields import ( + GenericForeignKey, GenericRelation +) +from django.contrib.contenttypes.models import ContentType +from django.db import models +from django.utils.encoding import python_2_unicode_compatible + + +@python_2_unicode_compatible +class Tag(models.Model): + """ + Tags have a descriptive slug, and are attached to an arbitrary object. + """ + tag = models.SlugField() + content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) + object_id = models.PositiveIntegerField() + tagged_item = GenericForeignKey('content_type', 'object_id') + + def __str__(self): + return self.tag + + +@python_2_unicode_compatible +class Bookmark(models.Model): + """ + A URL bookmark that may have multiple tags attached. + """ + url = models.URLField() + tags = GenericRelation(Tag) + + def __str__(self): + return 'Bookmark: %s' % self.url + + +@python_2_unicode_compatible +class Note(models.Model): + """ + A textual note that may have multiple tags attached. + """ + text = models.TextField() + tags = GenericRelation(Tag) + + def __str__(self): + return 'Note: %s' % self.text diff --git a/tests/test_relations_generic.py b/tests/generic_relations/test_generic_relations.py similarity index 63% rename from tests/test_relations_generic.py rename to tests/generic_relations/test_generic_relations.py index a3798b0a3..c8de332e1 100644 --- a/tests/test_relations_generic.py +++ b/tests/generic_relations/test_generic_relations.py @@ -1,52 +1,10 @@ from __future__ import unicode_literals -from django.contrib.contenttypes.fields import ( - GenericForeignKey, GenericRelation -) -from django.contrib.contenttypes.models import ContentType -from django.db import models from django.test import TestCase -from django.utils.encoding import python_2_unicode_compatible from rest_framework import serializers - -@python_2_unicode_compatible -class Tag(models.Model): - """ - Tags have a descriptive slug, and are attached to an arbitrary object. - """ - tag = models.SlugField() - content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) - object_id = models.PositiveIntegerField() - tagged_item = GenericForeignKey('content_type', 'object_id') - - def __str__(self): - return self.tag - - -@python_2_unicode_compatible -class Bookmark(models.Model): - """ - A URL bookmark that may have multiple tags attached. - """ - url = models.URLField() - tags = GenericRelation(Tag) - - def __str__(self): - return 'Bookmark: %s' % self.url - - -@python_2_unicode_compatible -class Note(models.Model): - """ - A textual note that may have multiple tags attached. - """ - text = models.TextField() - tags = GenericRelation(Tag) - - def __str__(self): - return 'Note: %s' % self.text +from .models import Bookmark, Note, Tag class TestGenericRelations(TestCase):