mirror of
https://github.com/django/django.git
synced 2024-12-24 18:16:19 +00:00
5ed20b3aa3
Allows model fields to override the descriptor class used on the model instance attribute.
35 lines
1.2 KiB
Python
35 lines
1.2 KiB
Python
from django.db import connection, models
|
|
from django.test import SimpleTestCase
|
|
|
|
from .fields import CustomDescriptorField, CustomTypedField
|
|
|
|
|
|
class TestDbType(SimpleTestCase):
|
|
|
|
def test_db_parameters_respects_db_type(self):
|
|
f = CustomTypedField()
|
|
self.assertEqual(f.db_parameters(connection)['type'], 'custom_field')
|
|
|
|
|
|
class DescriptorClassTest(SimpleTestCase):
|
|
def test_descriptor_class(self):
|
|
class CustomDescriptorModel(models.Model):
|
|
name = CustomDescriptorField(max_length=32)
|
|
|
|
m = CustomDescriptorModel()
|
|
self.assertFalse(hasattr(m, '_name_get_count'))
|
|
# The field is set to its default in the model constructor.
|
|
self.assertEqual(m._name_set_count, 1)
|
|
m.name = 'foo'
|
|
self.assertFalse(hasattr(m, '_name_get_count'))
|
|
self.assertEqual(m._name_set_count, 2)
|
|
self.assertEqual(m.name, 'foo')
|
|
self.assertEqual(m._name_get_count, 1)
|
|
self.assertEqual(m._name_set_count, 2)
|
|
m.name = 'bar'
|
|
self.assertEqual(m._name_get_count, 1)
|
|
self.assertEqual(m._name_set_count, 3)
|
|
self.assertEqual(m.name, 'bar')
|
|
self.assertEqual(m._name_get_count, 2)
|
|
self.assertEqual(m._name_set_count, 3)
|