Commits

Mark Lavin  committed 1347f08

Tests trying to reproduce #34.

  • Participants
  • Parent commits 54b1c2f

Comments (0)

Files changed (2)

File selectable/tests/__init__.py

 class Thing(models.Model):
     name = models.CharField(max_length=100)
 
+    def __unicode__(self):
+        return self.name
+
+
+class OtherThing(models.Model):
+    name = models.CharField(max_length=100)
+    thing = models.ForeignKey(Thing)
+
+    def __unicode__(self):
+        return self.name
+
 
 class ThingLookup(ModelLookup):
     model = Thing
 
 from selectable.tests.base import *
 from selectable.tests.fields import *
+from selectable.tests.functests import *
 from selectable.tests.forms import *
 from selectable.tests.views import *
 from selectable.tests.widgets import *

File selectable/tests/functests.py

+"""
+Larger functional tests for fields and widgets.
+"""
+
+from django import forms
+
+from selectable.forms import AutoCompleteSelectField
+from selectable.tests import OtherThing, ThingLookup
+from selectable.tests.base import BaseSelectableTestCase
+
+
+__all__ = (
+    'FuncAutoCompleteSelectTestCase',
+)
+
+
+class OtherThingForm(forms.ModelForm):
+
+    thing = AutoCompleteSelectField(lookup_class=ThingLookup)
+
+    class Meta(object):
+        model = OtherThing
+
+
+class FuncAutoCompleteSelectTestCase(BaseSelectableTestCase):
+
+    def setUp(self):
+        self.test_thing = self.create_thing()
+
+    def test_valid_form(self):
+        "Valid form using an AutoCompleteSelectField."
+        data = {
+            'name': self.get_random_string(),
+            'thing_0': self.test_thing.name, # Text input
+            'thing_1': self.test_thing.pk, # Hidden input
+        }
+        form = OtherThingForm(data=data)
+        self.assertTrue(form.is_valid(), str(form.errors))
+
+    def test_invalid_form_missing_selected_pk(self):
+        "Invalid form using an AutoCompleteSelectField."
+        data = {
+            'name': self.get_random_string(),
+            'thing_0': self.test_thing.name, # Text input
+            'thing_1': u'', # Hidden input
+        }
+        form = OtherThingForm(data=data)
+        self.assertFalse(form.is_valid(), u'Form should not be valid')
+        self.assertFalse('name' in form.errors)
+        self.assertTrue('thing' in form.errors)
+
+    def test_invalid_form_missing_name(self):
+        "Invalid form using an AutoCompleteSelectField."
+        data = {
+            'name': u'',
+            'thing_0': self.test_thing.name, # Text input
+            'thing_1': self.test_thing.pk, # Hidden input
+        }
+        form = OtherThingForm(data=data)
+        self.assertFalse(form.is_valid(), u'Form should not be valid')
+        self.assertTrue('name' in form.errors)
+        self.assertFalse('thing' in form.errors)
+
+    def test_invalid_but_still_selected(self):
+        "Invalid form should keep selected item."
+        data = {
+            'name': u'',
+            'thing_0': self.test_thing.name, # Text input
+            'thing_1': self.test_thing.pk, # Hidden input
+        }
+        form = OtherThingForm(data=data)
+        self.assertFalse(form.is_valid(), u'Form should not be valid')
+        rendered_form = form.as_p()
+        # Selected text should be populated
+        thing_0 = 'name="thing_0" value="%s"' % self.test_thing.name
+        self.assertTrue(thing_0 in rendered_form, u"Didn't render selected text.")
+        # Selected pk should be populated
+        thing_1 = 'name="thing_1" value="%s"' % self.test_thing.pk
+        self.assertTrue(thing_1 in rendered_form, u"Didn't render selected pk.")
+
+    def test_populate_from_model(self):
+        "Populate from existing model."
+        other_thing = OtherThing.objects.create(thing=self.test_thing, name='a')
+        form = OtherThingForm(instance=other_thing)
+        rendered_form = form.as_p()
+        # Selected text should be populated
+        thing_0 = 'name="thing_0" value="%s"' % self.test_thing.name
+        self.assertTrue(thing_0 in rendered_form, u"Didn't render selected text.")
+        # Selected pk should be populated
+        thing_1 = 'name="thing_1" value="%s"' % self.test_thing.pk
+        self.assertTrue(thing_1 in rendered_form, u"Didn't render selected pk.")