Source

django-selectable / selectable / tests / functests.py

Full commit
"""
Larger functional tests for fields and widgets.
"""

from django import forms

from selectable.forms import AutoCompleteSelectField, AutoCompleteSelectMultipleField
from selectable.forms import AutoCompleteSelectWidget, AutoComboboxSelectWidget
from selectable.tests import ManyThing, OtherThing, ThingLookup
from selectable.tests.base import BaseSelectableTestCase, parsed_inputs


__all__ = (
    'FuncAutoCompleteSelectTestCase',
    'FuncSelectModelChoiceTestCase',
    'FuncComboboxModelChoiceTestCase',
    'FuncManytoManyMultipleSelectTestCase',
)


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()
        inputs = parsed_inputs(rendered_form)
        # Selected text should be populated
        thing_0 = inputs['thing_0'][0]
        self.assertEqual(thing_0.attributes['value'].value, self.test_thing.name)
        # Selected pk should be populated
        thing_1 = inputs['thing_1'][0]
        self.assertEqual(int(thing_1.attributes['value'].value), self.test_thing.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()
        inputs = parsed_inputs(rendered_form)
        # Selected text should be populated
        thing_0 = inputs['thing_0'][0]
        self.assertEqual(thing_0.attributes['value'].value, self.test_thing.name)
        # Selected pk should be populated
        thing_1 = inputs['thing_1'][0]
        self.assertEqual(int(thing_1.attributes['value'].value), self.test_thing.pk)


class SelectWidgetForm(forms.ModelForm):

    class Meta(object):
        model = OtherThing
        widgets = {
            'thing': AutoCompleteSelectWidget(lookup_class=ThingLookup)
        }


class FuncSelectModelChoiceTestCase(BaseSelectableTestCase):
    """
    Functional tests for AutoCompleteSelectWidget compatibility
    with a ModelChoiceField.
    """

    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 = SelectWidgetForm(data=data)
        self.assertTrue(form.is_valid(), str(form.errors))


class ComboboxSelectWidgetForm(forms.ModelForm):

    class Meta(object):
        model = OtherThing
        widgets = {
            'thing': AutoComboboxSelectWidget(lookup_class=ThingLookup)
        }


class FuncComboboxModelChoiceTestCase(BaseSelectableTestCase):
    """
    Functional tests for AutoComboboxSelectWidget compatibility
    with a ModelChoiceField.
    """

    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 = ComboboxSelectWidgetForm(data=data)
        self.assertTrue(form.is_valid(), str(form.errors))


class ManyThingForm(forms.ModelForm):

    things = AutoCompleteSelectMultipleField(lookup_class=ThingLookup)

    class Meta(object):
        model = ManyThing


class FuncManytoManyMultipleSelectTestCase(BaseSelectableTestCase):
    """
    Functional tests for AutoCompleteSelectMultipleField compatibility
    with a ManyToManyField.
    """

    def setUp(self):
        self.test_thing = self.create_thing()

    def test_valid_form(self):
        "Valid form using an AutoCompleteSelectMultipleField."
        data = {
            'name': self.get_random_string(),
            'things_0': u'', # Text input
            'things_1': [self.test_thing.pk, ], # Hidden inputs
        }
        form = ManyThingForm(data=data)
        self.assertTrue(form.is_valid(), str(form.errors))

    def test_valid_save(self):
        "Saving data from a valid form."
        data = {
            'name': self.get_random_string(),
            'things_0': u'', # Text input
            'things_1': [self.test_thing.pk, ], # Hidden inputs
        }
        form = ManyThingForm(data=data)
        manything = form.save()
        self.assertEqual(manything.name, data['name'])
        things = manything.things.all()
        self.assertEqual(things.count(), 1)
        self.assertTrue(self.test_thing in things)

    def test_not_required(self):
        "Valid form where many to many is not required."
        data = {
            'name': self.get_random_string(),
            'things_0': u'', # Text input
            'things_1': [], # Hidden inputs
        }
        form = ManyThingForm(data=data)
        form.fields['things'].required = False
        self.assertTrue(form.is_valid(), str(form.errors))

    def test_not_required_save(self):
        "Saving data when many to many is not required."
        data = {
            'name': self.get_random_string(),
            'things_0': u'', # Text input
            'things_1': [], # Hidden inputs
        }
        form = ManyThingForm(data=data)
        form.fields['things'].required = False
        manything = form.save()
        self.assertEqual(manything.name, data['name'])
        things = manything.things.all()
        self.assertEqual(things.count(), 0)