Migrated the field_subclsasing doctests. Thanks to Alex Gaynor.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13780 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
521be8cd9c
commit
e2e1802e48
|
@ -2,7 +2,6 @@
|
||||||
Tests for field subclassing.
|
Tests for field subclassing.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from django.core import serializers
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils.encoding import force_unicode
|
from django.utils.encoding import force_unicode
|
||||||
|
|
||||||
|
@ -18,56 +17,3 @@ class MyModel(models.Model):
|
||||||
|
|
||||||
class DataModel(models.Model):
|
class DataModel(models.Model):
|
||||||
data = JSONField()
|
data = JSONField()
|
||||||
|
|
||||||
__test__ = {'API_TESTS': ur"""
|
|
||||||
# Creating a model with custom fields is done as per normal.
|
|
||||||
>>> s = Small(1, 2)
|
|
||||||
>>> print s
|
|
||||||
12
|
|
||||||
>>> m = MyModel(name='m', data=s)
|
|
||||||
>>> m.save()
|
|
||||||
|
|
||||||
# Custom fields still have normal field's attributes.
|
|
||||||
>>> m._meta.get_field('data').verbose_name
|
|
||||||
'small field'
|
|
||||||
|
|
||||||
# The m.data attribute has been initialised correctly. It's a Small object.
|
|
||||||
>>> m.data.first, m.data.second
|
|
||||||
(1, 2)
|
|
||||||
|
|
||||||
# The data loads back from the database correctly and 'data' has the right type.
|
|
||||||
>>> m1 = MyModel.objects.get(pk=m.pk)
|
|
||||||
>>> isinstance(m1.data, Small)
|
|
||||||
True
|
|
||||||
>>> print m1.data
|
|
||||||
12
|
|
||||||
|
|
||||||
# We can do normal filtering on the custom field (and will get an error when we
|
|
||||||
# use a lookup type that does not make sense).
|
|
||||||
>>> s1 = Small(1, 3)
|
|
||||||
>>> s2 = Small('a', 'b')
|
|
||||||
>>> MyModel.objects.filter(data__in=[s, s1, s2])
|
|
||||||
[<MyModel: m>]
|
|
||||||
>>> MyModel.objects.filter(data__lt=s)
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
TypeError: Invalid lookup type: 'lt'
|
|
||||||
|
|
||||||
# Serialization works, too.
|
|
||||||
>>> stream = serializers.serialize("json", MyModel.objects.all())
|
|
||||||
>>> stream
|
|
||||||
'[{"pk": 1, "model": "field_subclassing.mymodel", "fields": {"data": "12", "name": "m"}}]'
|
|
||||||
>>> obj = list(serializers.deserialize("json", stream))[0]
|
|
||||||
>>> obj.object == m
|
|
||||||
True
|
|
||||||
|
|
||||||
# Test retrieving custom field data
|
|
||||||
>>> m.delete()
|
|
||||||
>>> m1 = MyModel(name="1", data=Small(1, 2))
|
|
||||||
>>> m1.save()
|
|
||||||
>>> m2 = MyModel(name="2", data=Small(2, 3))
|
|
||||||
>>> m2.save()
|
|
||||||
>>> for m in MyModel.objects.all(): print unicode(m.data)
|
|
||||||
12
|
|
||||||
23
|
|
||||||
"""}
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
|
from django.core import serializers
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from models import DataModel
|
from fields import Small
|
||||||
|
from models import DataModel, MyModel
|
||||||
|
|
||||||
|
|
||||||
class CustomField(TestCase):
|
class CustomField(TestCase):
|
||||||
|
@ -19,3 +21,55 @@ class CustomField(TestCase):
|
||||||
d = DataModel.objects.get(pk=d.pk)
|
d = DataModel.objects.get(pk=d.pk)
|
||||||
self.assertTrue(isinstance(d.data, list))
|
self.assertTrue(isinstance(d.data, list))
|
||||||
self.assertEqual(d.data, [1, 2, 3])
|
self.assertEqual(d.data, [1, 2, 3])
|
||||||
|
|
||||||
|
def test_custom_field(self):
|
||||||
|
# Creating a model with custom fields is done as per normal.
|
||||||
|
s = Small(1, 2)
|
||||||
|
self.assertEqual(str(s), "12")
|
||||||
|
|
||||||
|
m = MyModel.objects.create(name="m", data=s)
|
||||||
|
# Custom fields still have normal field's attributes.
|
||||||
|
self.assertEqual(m._meta.get_field("data").verbose_name, "small field")
|
||||||
|
|
||||||
|
# The m.data attribute has been initialised correctly. It's a Small
|
||||||
|
# object.
|
||||||
|
self.assertEqual((m.data.first, m.data.second), (1, 2))
|
||||||
|
|
||||||
|
# The data loads back from the database correctly and 'data' has the
|
||||||
|
# right type.
|
||||||
|
m1 = MyModel.objects.get(pk=m.pk)
|
||||||
|
self.assertTrue(isinstance(m1.data, Small))
|
||||||
|
self.assertEqual(str(m1.data), "12")
|
||||||
|
|
||||||
|
# We can do normal filtering on the custom field (and will get an error
|
||||||
|
# when we use a lookup type that does not make sense).
|
||||||
|
s1 = Small(1, 3)
|
||||||
|
s2 = Small("a", "b")
|
||||||
|
self.assertQuerysetEqual(
|
||||||
|
MyModel.objects.filter(data__in=[s, s1, s2]), [
|
||||||
|
"m",
|
||||||
|
],
|
||||||
|
lambda m: m.name,
|
||||||
|
)
|
||||||
|
self.assertRaises(TypeError, lambda: MyModel.objects.filter(data__lt=s))
|
||||||
|
|
||||||
|
# Serialization works, too.
|
||||||
|
stream = serializers.serialize("json", MyModel.objects.all())
|
||||||
|
self.assertEqual(stream, '[{"pk": 1, "model": "field_subclassing.mymodel", "fields": {"data": "12", "name": "m"}}]')
|
||||||
|
|
||||||
|
obj = list(serializers.deserialize("json", stream))[0]
|
||||||
|
self.assertEqual(obj.object, m)
|
||||||
|
|
||||||
|
# Test retrieving custom field data
|
||||||
|
m.delete()
|
||||||
|
|
||||||
|
m1 = MyModel.objects.create(name="1", data=Small(1, 2))
|
||||||
|
m2 = MyModel.objects.create(name="2", data=Small(2, 3))
|
||||||
|
|
||||||
|
self.assertQuerysetEqual(
|
||||||
|
MyModel.objects.all(), [
|
||||||
|
"12",
|
||||||
|
"23",
|
||||||
|
],
|
||||||
|
lambda m: str(m.data)
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in New Issue