Commits

Thomas Wanschik committed f8adcbd

fixed LIstField and added tests for ListField

  • Participants
  • Parent commits 24c1e2b

Comments (0)

Files changed (3)

     def __init__(self, field_type, *args, **kwargs):
         self.field_type = field_type
         super(ListField, self).__init__(*args, **kwargs)
-    
+
     def db_type(self, connection):
         return 'ListField:' + self.field_type.db_type(connection=connection)
 
         return self.call_for_each('get_prep_value', value)
 
     def get_db_prep_value(self, value, connection, prepared=False):
-        return self.call_for_each('get_db_prep_value', value, connection, prepared)
+        return self.call_for_each('get_db_prep_value', value, connection=connection,
+            prepared=prepared)
 
     def get_db_prep_save(self, value, connection):
-        return self.call_for_each('get_db_prep_save', value, connection)
+        return self.call_for_each('get_db_prep_save', value, connection=connection)
 
     def formfield(self, **kwargs):
         return None

models.py

Empty file added.
+from .fields import ListField
+from django.db import models
+from django.db.models import Q
+#from django.db.utils import DatabaseError
+from django.test import TestCase
+
+class ListModel(models.Model):
+    floating_point = models.FloatField()
+    names = ListField(models.CharField(max_length=500))
+
+class FilterTest(TestCase):
+    floats = [5.3, 2.6, 9.1, 1.58]
+    names = [u'Kakashi', u'Naruto', u'Sasuke', u'Sakura',]
+
+    def setUp(self):
+        for i, float in enumerate(FilterTest.floats):
+            ListModel(floating_point=float, names=FilterTest.names[:i+1]).save()
+
+    def test_startswith(self):
+        self.assertEquals([entity.names for entity in
+                          ListModel.objects.filter(names__startswith='Sa')],
+                          [['Kakashi', 'Naruto', 'Sasuke',],
+                            ['Kakashi', 'Naruto', 'Sasuke', 'Sakura',]])
+
+    def test_gt(self):
+        # test gt on list
+        self.assertEquals([entity.names for entity in
+                          ListModel.objects.filter(names__gt='Kakashi')],
+                          [[u'Kakashi', u'Naruto',],
+                            [u'Kakashi', u'Naruto', u'Sasuke',],
+                            [u'Kakashi', u'Naruto', u'Sasuke', u'Sakura',]])
+
+    def test_lt(self):
+        # test lt on list
+        self.assertEquals([entity.names for entity in
+                          ListModel.objects.filter(names__lt='Naruto')],
+                          [[u'Kakashi',], [u'Kakashi', u'Naruto',],
+                            [u'Kakashi', u'Naruto', u'Sasuke',],
+                            [u'Kakashi', u'Naruto', u'Sasuke', u'Sakura',]])
+
+    def test_gte(self):
+        # test gte on list
+        self.assertEquals([entity.names for entity in
+                          ListModel.objects.filter(names__gte='Sakura')],
+                          [[u'Kakashi', u'Naruto', u'Sasuke',],
+                            [u'Kakashi', u'Naruto', u'Sasuke', u'Sakura',]])
+
+    def test_lte(self):
+        # test lte on list
+        self.assertEquals([entity.names for entity in
+                          ListModel.objects.filter(names__lte='Kakashi')],
+                          [[u'Kakashi',], [u'Kakashi', u'Naruto',],
+                            [u'Kakashi', u'Naruto', u'Sasuke',],
+                            [u'Kakashi', u'Naruto', u'Sasuke', u'Sakura',]])
+
+    def test_equals(self):
+        # test equality filter on primary_key field
+        self.assertEquals([entity.names for entity in
+                          ListModel.objects.filter(names='Sakura')],
+                          [[u'Kakashi', u'Naruto', u'Sasuke', u'Sakura',]])
+
+        # test using exact
+        self.assertEquals([entity.names for entity in
+                          ListModel.objects.filter(names__exact='Sakura')],
+                          [[u'Kakashi', u'Naruto', u'Sasuke', u'Sakura',]])
+
+    def test_is_null(self):
+        self.assertEquals(ListModel.objects.filter(
+            names__isnull=True).count(), 0)
+
+    def test_exclude(self):
+        self.assertEquals([entity.names for entity in
+                            ListModel.objects.all().exclude(
+                            names__lt='Sakura')],
+                            [[u'Kakashi', u'Naruto', u'Sasuke',],
+                                [u'Kakashi', u'Naruto', u'Sasuke', u'Sakura',]])
+
+    def test_chained_filter(self):
+        self.assertEquals([entity.names for entity in
+                          ListModel.objects.filter(names='Sasuke').filter(
+                            names='Sakura')], [['Kakashi', 'Naruto',
+                            'Sasuke', 'Sakura'],])
+
+        self.assertEquals([entity.names for entity in
+                          ListModel.objects.filter(names__startswith='Sa').filter(
+                            names='Sakura')], [['Kakashi', 'Naruto',
+                            'Sasuke', 'Sakura']])
+
+        # test across multiple columns. On app engine only one filter is allowed
+        # to be an inequality filter
+        self.assertEquals([entity.names for entity in
+                          ListModel.objects.filter(floating_point=9.1).filter(
+                            names__startswith='Sa')], [['Kakashi', 'Naruto',
+                            'Sasuke',],])
+
+# passes on production but not on sdk
+#    def test_Q_objects(self):
+#        self.assertEquals([entity.names for entity in
+#            ListModel.objects.exclude(Q(names__lt='Sakura') | Q(names__gte='Sasuke'))],
+#                [['Kakashi', 'Naruto', 'Sasuke', 'Sakura'], ])