from webob import Request import webtest def select_app(environ, start_response): req = Request(environ) status = "200 OK" if req.method == "GET": body =\ """ form page
""" else: select_type = req.POST.get("button") if select_type == "single": selection = req.POST.get("single") elif select_type == "multiple": selection = ", ".join(req.POST.getall("multiple")) body =\ """ display page

You submitted the %(select_type)s

You selected %(selection)s

""" % locals() headers = [ ('Content-Type', 'text/html'), ('Content-Length', str(len(body)))] start_response(status, headers) return [body] def select_app_without_default(environ, start_response): req = Request(environ) status = "200 OK" if req.method == "GET": body =\ """ form page
""" else: select_type = req.POST.get("button") if select_type == "single": selection = req.POST.get("single") elif select_type == "multiple": selection = ", ".join(req.POST.getall("multiple")) body =\ """ display page

You submitted the %(select_type)s

You selected %(selection)s

""" % locals() headers = [ ('Content-Type', 'text/html'), ('Content-Length', str(len(body)))] start_response(status, headers) return [body] def test_single_select(): app = webtest.TestApp(select_app) res = app.get('/') assert res.status_int == 200 assert res.headers['content-type'] == 'text/html' assert res.content_type == 'text/html' single_form = res.forms["single_select_form"] assert single_form["single"].value == "5" display = single_form.submit("button") assert "

You selected 5

" in display, display res = app.get('/') assert res.status_int == 200 assert res.headers['content-type'] == 'text/html' assert res.content_type == 'text/html' single_form = res.forms["single_select_form"] assert single_form["single"].value == "5" single_form.set("single", "6") assert single_form["single"].value == "6" display = single_form.submit("button") assert "

You selected 6

" in display, display def test_single_select_no_default(): app = webtest.TestApp(select_app_without_default) res = app.get('/') assert res.status_int == 200 assert res.headers['content-type'] == 'text/html' assert res.content_type == 'text/html' single_form = res.forms["single_select_form"] assert single_form["single"].value == "4" display = single_form.submit("button") assert "

You selected 4

" in display, display res = app.get('/') assert res.status_int == 200 assert res.headers['content-type'] == 'text/html' assert res.content_type == 'text/html' single_form = res.forms["single_select_form"] assert single_form["single"].value == "4" single_form.set("single", "6") assert single_form["single"].value == "6" display = single_form.submit("button") assert "

You selected 6

" in display, display def test_multiple_select(): app = webtest.TestApp(select_app) res = app.get('/') assert res.status_int == 200 assert res.headers['content-type'] == 'text/html' assert res.content_type == 'text/html' multiple_form = res.forms["multiple_select_form"] assert multiple_form["multiple"].value == ['8', '11'],\ multiple_form["multiple"].value display = multiple_form.submit("button") assert "

You selected 8, 11

" in display, display res = app.get('/') assert res.status_int == 200 assert res.headers['content-type'] == 'text/html' assert res.content_type == 'text/html' multiple_form = res.forms["multiple_select_form"] assert multiple_form["multiple"].value == ["8", "11"],\ multiple_form["multiple"].value multiple_form.set("multiple", ["9"]) assert multiple_form["multiple"].value == ["9"],\ multiple_form["multiple"].value display = multiple_form.submit("button") assert "

You selected 9

" in display, display def test_multiple_select_no_default(): app = webtest.TestApp(select_app_without_default) res = app.get('/') assert res.status_int == 200 assert res.headers['content-type'] == 'text/html' assert res.content_type == 'text/html' multiple_form = res.forms["multiple_select_form"] assert multiple_form["multiple"].value is None,\ repr(multiple_form["multiple"].value) display = multiple_form.submit("button") assert "

You selected

" in display, display res = app.get('/') assert res.status_int == 200 assert res.headers['content-type'] == 'text/html' assert res.content_type == 'text/html' multiple_form = res.forms["multiple_select_form"] assert multiple_form["multiple"].value is None,\ multiple_form["multiple"].value multiple_form.set("multiple", ["9"]) assert multiple_form["multiple"].value == ["9"],\ multiple_form["multiple"].value display = multiple_form.submit("button") assert "

You selected 9

" in display, display