Germano Gabbianelli avatar Germano Gabbianelli committed 6cfb822

Added tests for "autocomplete_formfield".

Comments (0)

Files changed (3)

tests/test_project/testapp/autocomplete_settings.py

 class User2Autocomplete(SimpleAutocomplete):
     queryset = None
 
+class FriendsAutocomplete(AutocompleteSettings):
+    search_fields = ('^username',)
+
+class EmailAutocomplete(AutocompleteSettings):
+    queryset = User.objects.all()
+    search_fields = ('^email', '^username')
+    key = value = 'email'
+
+    def label(self, u):
+        return u'%s %s \u003C%s\u003E' % (u.first_name, u.last_name, u.email)
+
 autocomplete.register('testapp.simple', SimpleAutocomplete)
 autocomplete.register('testapp.loginreq', LoginRequiredAutocomplete)
 autocomplete.register('testapp.hasperm', HasPermAutocomplete)
 autocomplete.register('testapp.customrender', CustomRenderingAutocomplete)
 autocomplete.register(Dummy.user2, User2Autocomplete)
 autocomplete.register('testapp.limit', LimitAutocomplete)
-
+autocomplete.register(Dummy.friends, FriendsAutocomplete)
+autocomplete.register('testapp.email', EmailAutocomplete)

tests/test_project/testapp/tests.py

 # -*- coding: utf-8 -*-
 from unittest import TestCase
 from django.test import TestCase as DjangoTestCase
+from django import forms
+
+from autocomplete import widgets, utils
+
+from test_project.testapp import widgets as custom_widgets
+from test_project.testapp.views import autocomplete
+from test_project.testapp.models import Dummy
 
 # TODO:
 # test decorated view Settings.view
         self.assertContains(response, 'gary')
         self.assertNotContains(response, 'gayle')
         self.assertNotContains(response, r'gl\u00ea\u00f1')
+
+
+class AutocompleteFormfieldTests(DjangoTestCase):
+
+    def setUp(self):
+        import test_project.testapp.autocomplete_settings
+    
+    def assertIsInstance(self, obj, cls):
+        """Same as self.assertTrue(isinstance(obj, cls)), with a nicer
+           default message."""
+        if not isinstance(obj, cls):
+            standardMsg = '%r is not an instance of %r' % (obj, cls)
+            self.fail(standardMsg)
+
+    def test_inexistent_settings(self):
+        self.assertRaises(KeyError, utils.autocomplete_formfield,
+            'testapp.simple')
+
+    def test_default_values(self):
+        formfield = utils.autocomplete_formfield(Dummy.user2,
+            view=autocomplete)
+        self.assertIsInstance(formfield, forms.ModelChoiceField)
+        self.assertIsInstance(formfield.widget, widgets.AutocompleteWidget)
+        
+        formfield = utils.autocomplete_formfield(Dummy.friends,
+            view=autocomplete)
+        self.assertIsInstance(formfield, forms.ModelMultipleChoiceField)
+        self.assertIsInstance(formfield.widget, widgets.MultipleAutocompleteWidget)
+
+        formfield = utils.autocomplete_formfield('testapp.customrender',
+            view=autocomplete)
+        self.assertIsInstance(formfield, forms.CharField)
+        self.assertIsInstance(formfield.widget, widgets.AutocompleteWidget)
+
+        formfield = utils.autocomplete_formfield('testapp.email',
+            view=autocomplete)
+        self.assertIsInstance(formfield, forms.CharField)
+        self.assertIsInstance(formfield.widget, widgets.AutocompleteWidget)
+
+    def test_custom_widgets(self):
+        formfield = utils.autocomplete_formfield('testapp.simple',
+            widget_class=custom_widgets.CustomAutocompleteWidget,
+            view=autocomplete)
+        self.assertIsInstance(formfield.widget,
+            custom_widgets.CustomAutocompleteWidget)
+
+        formfield = utils.autocomplete_formfield(Dummy.friends,
+            multiple_widget_class=custom_widgets.CustomMultipleAutocompleteWidget,
+            view=autocomplete)
+        self.assertIsInstance(formfield.widget,
+            custom_widgets.CustomMultipleAutocompleteWidget)
+
+    def test_custom_formfield(self):
+        formfield = utils.autocomplete_formfield('testapp.email', forms.EmailField,
+            view=autocomplete)
+        self.assertIsInstance(formfield, forms.EmailField)

tests/test_project/testapp/widgets.py

+from autocomplete import widgets
+
+class CustomAutocompleteWidget(widgets.AutocompleteWidget):
+    pass
+
+class CustomMultipleAutocompleteWidget(widgets.AutocompleteWidget):
+    pass
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.