Commits

Mark Lavin committed 88a6d45

Adapting create_item and adding some tests.

Comments (0)

Files changed (3)

selectable/base.py

     search_field = ''
     search_fields = ()
 
+    def __init__(self):
+        super(ModelLookup, self).__init__()
+        if self.search_field and not self.search_fields:
+            self.search_fields = (self.search_field, )
+
     def get_query(self, request, term):
         qs = self.get_queryset()
         if term:
             search_filters = []
-            if self.search_field and not self.search_fields:
-                self.search_fields = (self.search_field, )
             if self.search_fields:
                 for field in self.search_fields:
                     search_filters.append(Q(**{field: term}))
 
     def create_item(self, value):
         data = {}
-        if self.search_field:
-            field_name = re.sub(r'__\w+$', '',  self.search_field)
+        if self.search_fields:
+            field_name = re.sub(r'__\w+$', '',  self.search_fields[0])
             if field_name:
                 data = {field_name: value}
         return self.model(**data)

selectable/tests/__init__.py

 
 class Thing(models.Model):
     name = models.CharField(max_length=100)
+    description = models.CharField(max_length=100)
 
     def __unicode__(self):
         return self.name

selectable/tests/base.py

 
 __all__ = (
     'ModelLookupTestCase',
+    'MultiFieldLookupTestCase',
 )
 
 
     def create_thing(self, data=None):
         data = data or {}
         defaults = {
-            'name': self.get_random_string()
+            'name': self.get_random_string(),
+            'description': self.get_random_string(),
         }
         defaults.update(data)
         return Thing.objects.create(**defaults)
 
 class SimpleModelLookup(ModelLookup):
     model = Thing
-    search_field = 'name__icontains'    
+    search_field = 'name__icontains'
 
 
 class ModelLookupTestCase(BaseSelectableTestCase):
         item = lookup.get_item(thing.pk)
         self.assertEqual(thing, item)
 
+
+class MultiFieldLookup(ModelLookup):
+    model = Thing
+    search_fields = ('name__icontains', 'description__icontains', )
+
+
+class MultiFieldLookupTestCase(ModelLookupTestCase):
+    lookup_cls = MultiFieldLookup
+
+    def test_get_name(self):
+        name = self.__class__.lookup_cls.name()
+        self.assertEqual(name, 'tests-multifieldlookup')
+
+    def test_get_url(self):
+        url = self.__class__.lookup_cls.url()
+        test_url = reverse('selectable-lookup', args=['tests-multifieldlookup'])
+        self.assertEqual(url, test_url)
+
+    def test_description_search(self):
+        lookup = self.get_lookup_instance()
+        thing = self.create_thing(data={'description': 'Thing'})
+        other_thing = self.create_thing(data={'description': 'Other Thing'})
+        qs = lookup.get_query(request=None, term='other')
+        self.assertTrue(thing.pk not in qs.values_list('id', flat=True))
+        self.assertTrue(other_thing.pk in qs.values_list('id', flat=True))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.