summaryrefslogtreecommitdiff
path: root/tests/from_db_value
diff options
context:
space:
mode:
authorMarc Tamlyn <marc.tamlyn@gmail.com>2014-08-12 13:08:40 +0100
committerMarc Tamlyn <marc.tamlyn@gmail.com>2014-09-03 20:36:03 +0100
commite9103402c0fa873aea58a6a11dba510cd308cb84 (patch)
tree947a946de6d7354f22e8c5ec7a98ecc37c98eb08 /tests/from_db_value
parent89559bcfb096ccc625e0e9ab41e2136fcb32a514 (diff)
downloaddjango-e9103402c0fa873aea58a6a11dba510cd308cb84.tar.gz
Fixed #18757, #14462, #21565 -- Reworked database-python type conversions
Complete rework of translating data values from database Deprecation of SubfieldBase, removal of resolve_columns and convert_values in favour of a more general converter based approach and public API Field.from_db_value(). Now works seamlessly with aggregation, .values() and raw queries. Thanks to akaariai in particular for extensive advice and inspiration, also to shaib, manfre and timograham for their reviews.
Diffstat (limited to 'tests/from_db_value')
-rw-r--r--tests/from_db_value/__init__.py0
-rw-r--r--tests/from_db_value/models.py32
-rw-r--r--tests/from_db_value/tests.py30
3 files changed, 62 insertions, 0 deletions
diff --git a/tests/from_db_value/__init__.py b/tests/from_db_value/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/from_db_value/__init__.py
diff --git a/tests/from_db_value/models.py b/tests/from_db_value/models.py
new file mode 100644
index 0000000000..4cc9e62168
--- /dev/null
+++ b/tests/from_db_value/models.py
@@ -0,0 +1,32 @@
+import decimal
+
+from django.db import models
+from django.utils.encoding import python_2_unicode_compatible
+
+
+class Cash(decimal.Decimal):
+ currency = 'USD'
+
+ def __str__(self):
+ s = super(Cash, self).__str__(self)
+ return '%s %s' % (s, self.currency)
+
+
+class CashField(models.DecimalField):
+ def __init__(self, **kwargs):
+ kwargs['max_digits'] = 20
+ kwargs['decimal_places'] = 2
+ super(CashField, self).__init__(**kwargs)
+
+ def from_db_value(self, value, connection):
+ cash = Cash(value)
+ cash.vendor = connection.vendor
+ return cash
+
+
+@python_2_unicode_compatible
+class CashModel(models.Model):
+ cash = CashField()
+
+ def __str__(self):
+ return str(self.cash)
diff --git a/tests/from_db_value/tests.py b/tests/from_db_value/tests.py
new file mode 100644
index 0000000000..69e22d4290
--- /dev/null
+++ b/tests/from_db_value/tests.py
@@ -0,0 +1,30 @@
+from django.db import connection
+from django.db.models import Max
+from django.test import TestCase
+
+from .models import CashModel, Cash
+
+
+class FromDBValueTest(TestCase):
+ def setUp(self):
+ CashModel.objects.create(cash='12.50')
+
+ def test_simple_load(self):
+ instance = CashModel.objects.get()
+ self.assertIsInstance(instance.cash, Cash)
+
+ def test_values(self):
+ values_list = CashModel.objects.values_list('cash', flat=True)
+ self.assertIsInstance(values_list[0], Cash)
+
+ def test_aggregation(self):
+ maximum = CashModel.objects.aggregate(m=Max('cash'))['m']
+ self.assertIsInstance(maximum, Cash)
+
+ def test_defer(self):
+ instance = CashModel.objects.defer('cash').get()
+ self.assertIsInstance(instance.cash, Cash)
+
+ def test_connection(self):
+ instance = CashModel.objects.get()
+ self.assertEqual(instance.cash.vendor, connection.vendor)