2013-07-30 01:19:04 +08:00
|
|
|
from __future__ import unicode_literals
|
2011-10-14 02:04:12 +08:00
|
|
|
|
2013-09-18 14:56:05 +08:00
|
|
|
import inspect
|
|
|
|
|
2014-04-12 11:58:56 +08:00
|
|
|
from django.core import exceptions, serializers
|
2014-03-15 06:18:20 +08:00
|
|
|
from django.db import connection
|
2010-02-25 03:06:59 +08:00
|
|
|
from django.test import TestCase
|
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from .fields import CustomTypedField, Small
|
2014-04-12 11:58:56 +08:00
|
|
|
from .models import ChoicesModel, DataModel, MyModel, OtherModel
|
2010-02-25 03:06:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
class CustomField(TestCase):
|
2014-07-05 14:03:52 +08:00
|
|
|
def test_refresh(self):
|
|
|
|
d = DataModel.objects.create(data=[1, 2, 3])
|
|
|
|
d.refresh_from_db(fields=['data'])
|
|
|
|
self.assertIsInstance(d.data, list)
|
|
|
|
self.assertEqual(d.data, [1, 2, 3])
|
|
|
|
|
2010-02-25 03:06:59 +08:00
|
|
|
def test_defer(self):
|
|
|
|
d = DataModel.objects.create(data=[1, 2, 3])
|
2010-09-13 04:03:48 +08:00
|
|
|
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(d.data, list)
|
2010-09-13 04:03:48 +08:00
|
|
|
|
2010-02-25 03:06:59 +08:00
|
|
|
d = DataModel.objects.get(pk=d.pk)
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(d.data, list)
|
2010-02-25 03:06:59 +08:00
|
|
|
self.assertEqual(d.data, [1, 2, 3])
|
2010-09-13 04:03:48 +08:00
|
|
|
|
2012-05-27 07:08:44 +08:00
|
|
|
d = DataModel.objects.defer("data").get(pk=d.pk)
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(d.data, list)
|
2012-05-27 07:08:44 +08:00
|
|
|
self.assertEqual(d.data, [1, 2, 3])
|
|
|
|
# Refetch for save
|
2010-02-25 03:06:59 +08:00
|
|
|
d = DataModel.objects.defer("data").get(pk=d.pk)
|
|
|
|
d.save()
|
2010-09-13 04:03:48 +08:00
|
|
|
|
2010-02-25 03:06:59 +08:00
|
|
|
d = DataModel.objects.get(pk=d.pk)
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(d.data, list)
|
2010-02-25 03:06:59 +08:00
|
|
|
self.assertEqual(d.data, [1, 2, 3])
|
2010-09-13 04:03:48 +08:00
|
|
|
|
|
|
|
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")
|
|
|
|
|
2014-03-02 22:25:53 +08:00
|
|
|
# The m.data attribute has been initialized correctly. It's a Small
|
2010-09-13 04:03:48 +08:00
|
|
|
# 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)
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(m1.data, Small)
|
2010-09-13 04:03:48 +08:00
|
|
|
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())
|
2012-09-28 15:20:01 +08:00
|
|
|
self.assertJSONEqual(stream, [{
|
|
|
|
"pk": m1.pk,
|
|
|
|
"model": "field_subclassing.mymodel",
|
|
|
|
"fields": {"data": "12", "name": "m"}
|
|
|
|
}])
|
2010-09-13 04:03:48 +08:00
|
|
|
|
|
|
|
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))
|
2013-09-18 14:56:05 +08:00
|
|
|
MyModel.objects.create(name="2", data=Small(2, 3))
|
2010-09-13 04:03:48 +08:00
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
MyModel.objects.all(), [
|
|
|
|
"12",
|
|
|
|
"23",
|
|
|
|
],
|
2012-12-13 19:33:11 +08:00
|
|
|
lambda m: str(m.data),
|
|
|
|
ordered=False
|
2010-09-13 04:03:48 +08:00
|
|
|
)
|
2010-11-03 02:40:53 +08:00
|
|
|
|
|
|
|
def test_field_subclassing(self):
|
|
|
|
o = OtherModel.objects.create(data=Small("a", "b"))
|
|
|
|
o = OtherModel.objects.get()
|
|
|
|
self.assertEqual(o.data.first, "a")
|
|
|
|
self.assertEqual(o.data.second, "b")
|
2013-09-18 14:56:05 +08:00
|
|
|
|
|
|
|
def test_subfieldbase_plays_nice_with_module_inspect(self):
|
|
|
|
"""
|
|
|
|
Custom fields should play nice with python standard module inspect.
|
|
|
|
|
|
|
|
http://users.rcn.com/python/download/Descriptor.htm#properties
|
|
|
|
"""
|
|
|
|
# Even when looking for totally different properties, SubfieldBase's
|
2014-03-02 22:25:53 +08:00
|
|
|
# non property like behavior made inspect crash. Refs #12568.
|
2013-09-18 14:56:05 +08:00
|
|
|
data = dict(inspect.getmembers(MyModel))
|
|
|
|
self.assertIn('__module__', data)
|
|
|
|
self.assertEqual(data['__module__'], 'field_subclassing.models')
|
2014-03-15 06:18:20 +08:00
|
|
|
|
2014-04-12 11:58:56 +08:00
|
|
|
def test_validation_of_choices_for_custom_field(self):
|
|
|
|
# a valid choice
|
|
|
|
o = ChoicesModel.objects.create(data=Small('a', 'b'))
|
|
|
|
o.full_clean()
|
|
|
|
|
|
|
|
# an invalid choice
|
|
|
|
o = ChoicesModel.objects.create(data=Small('d', 'e'))
|
|
|
|
with self.assertRaises(exceptions.ValidationError):
|
|
|
|
o.full_clean()
|
|
|
|
|
2014-03-15 06:18:20 +08:00
|
|
|
|
|
|
|
class TestDbType(TestCase):
|
|
|
|
|
|
|
|
def test_db_parameters_respects_db_type(self):
|
|
|
|
f = CustomTypedField()
|
|
|
|
self.assertEqual(f.db_parameters(connection)['type'], 'custom_field')
|