From 680b512fc1509ca05cf56fada30f6b833735362a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gilberto=20Gon=C3=A7alves?= Date: Sat, 22 Jun 2013 14:05:12 +0100 Subject: [PATCH] Fixed #20587 -- Made convert_values handle None values --- django/db/backends/sqlite3/base.py | 3 +++ tests/backends/tests.py | 37 +++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py index a2d925ac3f8..18c4029649e 100644 --- a/django/db/backends/sqlite3/base.py +++ b/django/db/backends/sqlite3/base.py @@ -250,6 +250,9 @@ class DatabaseOperations(BaseDatabaseOperations): and gets dates and datetimes wrong. For consistency with other backends, coerce when required. """ + if value is None: + return None + internal_type = field.get_internal_type() if internal_type == 'DecimalField': return util.typecast_decimal(field.format_number(value)) diff --git a/tests/backends/tests.py b/tests/backends/tests.py index b6e3e9e36f0..c6cad56ec1a 100644 --- a/tests/backends/tests.py +++ b/tests/backends/tests.py @@ -11,9 +11,12 @@ from django.core.management.color import no_style from django.db import (connection, connections, DEFAULT_DB_ALIAS, DatabaseError, IntegrityError, transaction) from django.db.backends.signals import connection_created +from django.db.backends.sqlite3.base import DatabaseOperations from django.db.backends.postgresql_psycopg2 import version as pg_version from django.db.backends.util import format_number from django.db.models import Sum, Avg, Variance, StdDev +from django.db.models.fields import (AutoField, DateField, DateTimeField, + DecimalField, IntegerField, TimeField) from django.db.utils import ConnectionHandler from django.test import (TestCase, skipUnlessDBFeature, skipIfDBFeature, TransactionTestCase) @@ -402,7 +405,7 @@ class EscapingChecksDebug(EscapingChecks): pass -class SqlliteAggregationTests(TestCase): +class SqliteAggregationTests(TestCase): """ #19360: Raise NotImplementedError when aggregating on date/time fields. """ @@ -418,6 +421,38 @@ class SqlliteAggregationTests(TestCase): models.Item.objects.all().aggregate, aggregate('last_modified')) +class SqliteChecks(TestCase): + + @unittest.skipUnless(connection.vendor == 'sqlite', + "No need to do SQLite checks") + def test_convert_values_to_handle_null_value(self): + database_operations = DatabaseOperations(connection) + self.assertEqual( + None, + database_operations.convert_values(None, AutoField(primary_key=True)) + ) + self.assertEqual( + None, + database_operations.convert_values(None, DateField()) + ) + self.assertEqual( + None, + database_operations.convert_values(None, DateTimeField()) + ) + self.assertEqual( + None, + database_operations.convert_values(None, DecimalField()) + ) + self.assertEqual( + None, + database_operations.convert_values(None, IntegerField()) + ) + self.assertEqual( + None, + database_operations.convert_values(None, TimeField()) + ) + + class BackendTestCase(TestCase): def create_squares_with_executemany(self, args):