2019-07-23 12:04:06 +00:00
|
|
|
from django.db import connection, models
|
2015-08-12 13:26:57 +00:00
|
|
|
from django.test import SimpleTestCase
|
2014-04-12 03:58:56 +00:00
|
|
|
|
2019-07-23 12:04:06 +00:00
|
|
|
from .fields import CustomDescriptorField, CustomTypedField
|
2014-04-12 03:58:56 +00:00
|
|
|
|
2014-03-14 22:18:20 +00:00
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
class TestDbType(SimpleTestCase):
|
2014-03-14 22:18:20 +00:00
|
|
|
def test_db_parameters_respects_db_type(self):
|
|
|
|
f = CustomTypedField()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(f.db_parameters(connection)["type"], "custom_field")
|
2019-07-23 12:04:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DescriptorClassTest(SimpleTestCase):
|
|
|
|
def test_descriptor_class(self):
|
|
|
|
class CustomDescriptorModel(models.Model):
|
|
|
|
name = CustomDescriptorField(max_length=32)
|
|
|
|
|
|
|
|
m = CustomDescriptorModel()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertFalse(hasattr(m, "_name_get_count"))
|
2019-07-23 12:04:06 +00:00
|
|
|
# The field is set to its default in the model constructor.
|
|
|
|
self.assertEqual(m._name_set_count, 1)
|
2022-02-03 19:24:19 +00:00
|
|
|
m.name = "foo"
|
|
|
|
self.assertFalse(hasattr(m, "_name_get_count"))
|
2019-07-23 12:04:06 +00:00
|
|
|
self.assertEqual(m._name_set_count, 2)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(m.name, "foo")
|
2019-07-23 12:04:06 +00:00
|
|
|
self.assertEqual(m._name_get_count, 1)
|
|
|
|
self.assertEqual(m._name_set_count, 2)
|
2022-02-03 19:24:19 +00:00
|
|
|
m.name = "bar"
|
2019-07-23 12:04:06 +00:00
|
|
|
self.assertEqual(m._name_get_count, 1)
|
|
|
|
self.assertEqual(m._name_set_count, 3)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(m.name, "bar")
|
2019-07-23 12:04:06 +00:00
|
|
|
self.assertEqual(m._name_get_count, 2)
|
|
|
|
self.assertEqual(m._name_set_count, 3)
|