Esempio n. 1
0
class TestFormMultiSubmit(unittest.TestCase):

    def setUp(self):
        self.html = '''
            <form>
                <input type="submit" name="submit1" value="value1" />
                <input type="submit" name="submit2" value="value2" />
                <button type="submit" name="submit3" value="value3" />
                <button type="submit" name="submit4" value="value4" />
            </form>
        '''
        self.form = Form(self.html)

    def test_serialize_multi_no_submit_specified(self):
        assert_raises(
            exceptions.InvalidSubmitError,
            lambda: self.form.serialize()
        )

    def test_serialize_multi_wrong_submit_specified(self):
        fake_submit = fields.Submit('<input type="submit" name="fake" />')
        assert_raises(
            exceptions.InvalidSubmitError,
            lambda: self.form.serialize(submit=fake_submit)
        )

    def test_serialize_multi(self):
        submit = self.form.submit_fields['submit1']
        serialized = self.form.serialize(submit)
        assert_equal(serialized.data['submit1'], 'value1')
        assert_false('submit2' in serialized.data)
        assert_false('submit3' in serialized.data)
        assert_false('submit4' in serialized.data)
Esempio n. 2
0
class TestForm(unittest.TestCase):

    def setUp(self):
        self.html = '''
            <form>
                <input name="vocals" />
                <input name="guitar" type="file" />
                <select name="drums">
                    <option value="roger">Roger<br />
                    <option value="john">John<br />
                </select>
                <input type="radio" name="bass" value="Roger">Roger<br />
                <input type="radio" name="bass" value="John">John<br />
                <input name="multi" value="multi1" />
                <input name="multi" value="multi2" />
                <input type="submit" name="submit" value="submit" />
            </form>
        '''
        self.form = Form(self.html)

    def test_fields(self):
        keys = set(('vocals', 'guitar', 'drums', 'bass', 'multi', 'submit'))
        assert_equal(set(self.form.fields.keys()), keys)
        assert_equal(set(self.form.keys()), keys)

    def test_add_field(self):
        html = '<input name="instrument" />'
        field = fields.Input(html)
        self.form.add_field(field)
        assert_true('instrument' in self.form.fields)

    def test_add_field_wrong_type(self):
        assert_raises(ValueError, lambda: self.form.add_field('freddie'))

    def test_repr(self):
        assert_equal(
            repr(self.form),
            '<RoboForm vocals=, guitar=, drums=roger, bass=, '
                'multi=multi1, multi=multi2, submit=submit>'
        )

    def test_repr_empty(self):
        assert_equal(
            repr(Form('<form></form>')),
            '<RoboForm>'
        )

    def test_repr_unicode(self):
        form = Form(u'<form><input name="drüms" value="bäss" /></form>')
        assert_equal(
            repr(form),
            '<RoboForm drüms=bäss>'
        )

    def test_serialize(self):
        serialized = self.form.serialize()
        assert_equal(serialized.data.getlist('multi'), ['multi1', 'multi2'])
        assert_equal(serialized.data['submit'], 'submit')
Esempio n. 3
0
 def test_serialize_skips_disabled(self):
     html = '''
         <form>
             <input name="vocals" />
             <input name="guitar" disabled />
             <input type="submit" name="submit" value="submit" />
         </form>
     '''
     form = Form(html)
     serialized = form.serialize()
     assert_false('guitar' in serialized.data)
Esempio n. 4
0
 def test_serialize_skips_disabled(self):
     html = '''
         <form>
             <input name="vocals" />
             <input name="guitar" disabled />
             <input type="submit" name="submit" value="submit" />
         </form>
     '''
     form = Form(html)
     serialized = form.serialize()
     assert_false('guitar' in serialized.data)
Esempio n. 5
0
class TestForm(unittest.TestCase):
    def setUp(self):
        self.html = '''
            <form>
                <input name="vocals" />
                <input name="guitar" type="file" />
                <select name="drums">
                    <option value="roger">Roger<br />
                    <option value="john">John<br />
                </select>
                <input type="radio" name="bass" value="Roger">Roger<br />
                <input type="radio" name="bass" value="John">John<br />
                <input name="multi" value="multi1" />
                <input name="multi" value="multi2" />
                <input type="submit" name="submit" value="submit" />
            </form>
        '''
        self.form = Form(self.html)

    def test_fields(self):
        keys = set(('vocals', 'guitar', 'drums', 'bass', 'multi', 'submit'))
        assert_equal(set(self.form.fields.keys()), keys)
        assert_equal(set(self.form.keys()), keys)

    def test_add_field(self):
        html = '<input name="instrument" />'
        field = fields.Input(html)
        self.form.add_field(field)
        assert_true('instrument' in self.form.fields)

    def test_add_field_wrong_type(self):
        assert_raises(ValueError, lambda: self.form.add_field('freddie'))

    def test_repr(self):
        assert_equal(
            repr(self.form), '<RoboForm vocals=, guitar=, drums=roger, bass=, '
            'multi=multi1, multi=multi2, submit=submit>')

    def test_repr_empty(self):
        assert_equal(repr(Form('<form></form>')), '<RoboForm>')

    def test_repr_unicode(self):
        form = Form(u'<form><input name="drüms" value="bäss" /></form>')
        assert_equal(repr(form), '<RoboForm drüms=bäss>')

    def test_serialize(self):
        serialized = self.form.serialize()
        assert_equal(serialized.data.getlist('multi'), ['multi1', 'multi2'])
        assert_equal(serialized.data['submit'], 'submit')
Esempio n. 6
0
class TestFormMultiSubmit(unittest.TestCase):
    def setUp(self):
        self.html = '''
            <form>
                <input type="submit" name="submit1" value="value1" />
                <input type="submit" name="submit2" value="value2" />
            </form>
        '''
        self.form = Form(self.html)

    def test_serialize_multi_no_submit_specified(self):
        assert_raises(exceptions.InvalidSubmitError,
                      lambda: self.form.serialize())

    def test_serialize_multi_wrong_submit_specified(self):
        fake_submit = fields.Submit('<input type="submit" name="fake" />')
        assert_raises(exceptions.InvalidSubmitError,
                      lambda: self.form.serialize(submit=fake_submit))

    def test_serialize_multi(self):
        submit = self.form.submit_fields['submit1']
        serialized = self.form.serialize(submit)
        assert_equal(serialized.data['submit1'], 'value1')
        assert_false('submit2' in serialized.data)