Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #20462 - Fixed sqlite regex lookups for null values and non-str…

…ing fields.
  • Loading branch information...
commit 64041f0e6e7a773574d7cde978d16305200004ea 1 parent 536703a
Axel Haustant authored May 05, 2013 timgraham committed June 11, 2013
1  AUTHORS
@@ -270,6 +270,7 @@ answer newbie questions, and generally made Django that much better:
270 270
     Brian Harring <ferringb@gmail.com>
271 271
     Brant Harris
272 272
     Ronny Haryanto <http://ronny.haryan.to/>
  273
+    Axel Haustant <noirbizarre@gmail.com>
273 274
     Hawkeye
274 275
     Kent Hauser <kent@khauser.net>
275 276
     Joe Heck <http://www.rhonabwy.com/wp/>
2  django/db/backends/sqlite3/base.py
@@ -519,4 +519,4 @@ def _sqlite_format_dtdelta(dt, conn, days, secs, usecs):
519 519
     return str(dt)
520 520
 
521 521
 def _sqlite_regexp(re_pattern, re_string):
522  
-    return bool(re.search(re_pattern, re_string))
  522
+    return bool(re.search(re_pattern, str(re_string))) if re_string is not None else False
9  tests/lookup/models.py
@@ -57,3 +57,12 @@ class Player(models.Model):
57 57
 
58 58
     def __str__(self):
59 59
         return self.name
  60
+
  61
+
  62
+@python_2_unicode_compatible
  63
+class RegexTestModel(models.Model):
  64
+    name = models.CharField(max_length=100, null=True)
  65
+    integer = models.IntegerField(null=True)
  66
+
  67
+    def __str__(self):
  68
+        return self.name
17  tests/lookup/tests.py
@@ -6,7 +6,7 @@
6 6
 from django.core.exceptions import FieldError
7 7
 from django.test import TestCase, skipUnlessDBFeature
8 8
 
9  
-from .models import Author, Article, Tag, Game, Season, Player
  9
+from .models import Author, Article, Tag, Game, Season, Player, RegexTestModel
10 10
 
11 11
 
12 12
 class LookupTests(TestCase):
@@ -610,6 +610,21 @@ def test_regex_backreferencing(self):
610 610
         self.assertQuerysetEqual(Article.objects.filter(headline__regex=r'b(.).*b\1'),
611 611
             ['<Article: barfoobaz>', '<Article: bazbaRFOO>', '<Article: foobarbaz>'])
612 612
 
  613
+    def test_regex_null(self):
  614
+        """
  615
+        Ensure that a regex lookup does not fail on null/None values
  616
+        """
  617
+        RegexTestModel.objects.create(name=None)
  618
+        self.assertQuerysetEqual(RegexTestModel.objects.filter(name__regex=r'^$'), [])
  619
+
  620
+    def test_regex_non_string(self):
  621
+        """
  622
+        Ensure that a regex lookup does not fail on non-string fields
  623
+        """
  624
+        RegexTestModel.objects.create(name='test', integer=5)
  625
+        self.assertQuerysetEqual(RegexTestModel.objects.filter(integer__regex=r'^5$'),
  626
+            ['<RegexTestModel: test>'])
  627
+
613 628
     def test_nonfield_lookups(self):
614 629
         """
615 630
         Ensure that a lookup query containing non-fields raises the proper

0 notes on commit 64041f0

Please sign in to comment.
Something went wrong with that request. Please try again.