"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/qp/fill/test/utest_form.py $
$Id: utest_form.py 28953 2006-10-20 16:07:28Z dbinger $
"""
from qp.fill.form import Form, get_javascript_code
from qp.fill.widget import MultipleSelectWidget
from qp.pub.common import clear_publisher
from qp.pub.hit import Hit
from qp.pub.publish import DurusPublisher
from sancho.utest import UTest, raises

class FormTest(UTest):

    def _pre(self):
        clear_publisher()

    def _post(self):
        clear_publisher()

    def a(self):
        publisher = DurusPublisher()
        hit = Hit(None, {})
        publisher.process_hit(hit)
        form = Form()
        form.add_submit('a')
        form.add_reset('b')
        form.add_hidden('c')
        form.add_string('d')
        form.add_text('e')
        form.add_checkbox('f')
        form.add_single_select('g', options=[0])
        form.add_multiple_select('h', options=[0])
        form.add_radiobuttons('i', options=[0])
        form.add_float('j')
        form.add_int('k')
        form.render()
        assert not form.is_submitted()

    def b(self):
        publisher = DurusPublisher()
        hit = Hit(None, dict(REQUEST_METHOD="GET", QUERY_STRING='d=1'))
        publisher.process_hit(hit)
        form = Form()
        form.add_submit('a')
        form.add_string('d')
        assert form.is_submitted()
        form.render()

    def c(self):
        publisher = DurusPublisher()
        hit = Hit(None, dict(REQUEST_METHOD="GET", QUERY_STRING='d=1'))
        publisher.process_hit(hit)
        form = Form(method="get")
        form.add_submit('a')
        form.add_string('d')
        assert form.is_submitted()
        form.render()

    def d(self):
        publisher = DurusPublisher()
        hit = Hit(None, dict(REQUEST_METHOD="POST"))
        publisher.process_hit(hit)
        form = Form(id='ok')
        form.add_submit('a')
        form.add_string('d')
        form.set_error('a', 'err')
        assert not form.is_submitted()
        assert 'id="ok"' in form.render()

    def e(self):
        publisher = DurusPublisher()
        hit = Hit(None, dict(REQUEST_METHOD="GET", QUERY_STRING='d=1'))
        hit.get_request().process_inputs()
        publisher.set_hit(hit)
        form = Form(method="get")
        form.add_submit('a')
        form.add_string('d')
        form.has_key('d')
        assert form.get('d') == '1'
        assert form['d'] == '1'
        assert form.is_submitted()
        get_javascript_code()['a'] = 'javascript'
        assert 'javascript' in str(form.render())

    def f(self):
        publisher = DurusPublisher()
        hit = Hit(None, dict(REQUEST_METHOD="GET", QUERY_STRING='d=1'))
        hit.get_request().process_inputs()
        publisher.set_hit(hit)
        form = Form(method="get")
        form.add(MultipleSelectWidget, 'a', options=[range(10)])
        class FakePersistent(object): pass
        a = FakePersistent()
        b = FakePersistent()
        a._p_oid = '11111111'
        b._p_oid = '22222222'
        FakePersistent.__str__ = lambda x: 'ok'
        form.add(MultipleSelectWidget, 'b', options = [a, b])
        del a._p_oid
        raises(AssertionError, # because descriptions are not unique.
               form.add, MultipleSelectWidget, 'c', options = [a, b])

    def g(self):
        publisher = DurusPublisher()
        hit = Hit(None, dict(REQUEST_METHOD="GET", QUERY_STRING='d=1'))
        hit.get_request().process_inputs()
        publisher.set_hit(hit)
        form = Form(method="get")
        form.add(MultipleSelectWidget, 'a', options=[range(10)])
        form.add_string('d')
        assert not form.get_widget('a').is_submitted()
        assert form.get_widget('d').is_submitted()
        assert form.is_submitted()
        assert form.get_widget('a').is_submitted()

if __name__ == '__main__':
    FormTest()
