# -*- coding: utf-8 -*- from webob import Request import webtest from webtest.compat import binary_type from webtest.compat import to_bytes from tests.compat import unittest from tests.compat import u def select_app(environ, start_response): req = Request(environ) status = "200 OK" if req.method == "GET": body = to_bytes( """ 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 = to_bytes( """ display page

You submitted the %(select_type)s

You selected %(selection)s

""" % locals()) headers = [ ('Content-Type', 'text/html; charset=utf-8'), ('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 = to_bytes( """ 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 = to_bytes( """ display page

You submitted the %(select_type)s

You selected %(selection)s

""" % locals()) headers = [ ('Content-Type', 'text/html; charset=utf-8'), ('Content-Length', str(len(body)))] start_response(status, headers) return [body] def select_app_unicode(environ, start_response): req = Request(environ) status = "200 OK" if req.method == "GET": body =\ u(""" form page
""").encode('utf8') 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 = ( u(""" display page

You submitted the %(select_type)s

You selected %(selection)s

""") % locals()).encode('utf8') headers = [ ('Content-Type', 'text/html; charset=utf-8'), ('Content-Length', str(len(body)))] start_response(status, headers) if not isinstance(body, binary_type): raise AssertionError('Body is not %s' % binary_type) return [body] class TestSelect(unittest.TestCase): def test_unicode_select(self): app = webtest.TestApp(select_app_unicode) res = app.get('/') single_form = res.forms["single_select_form"] self.assertEqual(single_form["single"].value, u("МСК")) display = single_form.submit("button") self.assertIn(u("

You selected МСК

"), display, display) res = app.get('/') single_form = res.forms["single_select_form"] self.assertEqual(single_form["single"].value, u("МСК")) single_form.set("single", u("СПБ")) self.assertEqual(single_form["single"].value, u("СПБ")) display = single_form.submit("button") self.assertIn(u("

You selected СПБ

"), display, display) def test_single_select(self): app = webtest.TestApp(select_app) res = app.get('/') self.assertEqual(res.status_int, 200) self.assertEqual(res.headers['content-type'], 'text/html; charset=utf-8') self.assertEqual(res.content_type, 'text/html') single_form = res.forms["single_select_form"] self.assertEqual(single_form["single"].value, "5") display = single_form.submit("button") self.assertIn("

You selected 5

", display, display) res = app.get('/') self.assertEqual(res.status_int, 200) self.assertEqual(res.headers['content-type'], 'text/html; charset=utf-8') self.assertEqual(res.content_type, 'text/html') single_form = res.forms["single_select_form"] self.assertEqual(single_form["single"].value, "5") single_form.set("single", "6") self.assertEqual(single_form["single"].value, "6") display = single_form.submit("button") self.assertIn("

You selected 6

", display, display) def test_single_select_forced_value(self): app = webtest.TestApp(select_app) res = app.get('/') self.assertEqual(res.status_int, 200) self.assertEqual(res.headers['content-type'], 'text/html; charset=utf-8') self.assertEqual(res.content_type, 'text/html') single_form = res.forms["single_select_form"] self.assertEqual(single_form["single"].value, "5") try: single_form.set("single", "984") self.assertTrue(False, "not-an-option value error should have been raised") except ValueError: pass single_form["single"].force_value("984") self.assertEqual(single_form["single"].value, "984") display = single_form.submit("button") self.assertIn("

You selected 984

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

You selected 4

", display, display) res = app.get('/') self.assertEqual(res.status_int, 200) self.assertEqual(res.headers['content-type'], 'text/html; charset=utf-8') self.assertEqual(res.content_type, 'text/html') single_form = res.forms["single_select_form"] self.assertEqual(single_form["single"].value, "4") single_form.set("single", 6) self.assertEqual(single_form["single"].value, "6") display = single_form.submit("button") self.assertIn("

You selected 6

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

You selected 8, 11

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

You selected 9

", display, display) def test_multiple_select_forced_values(self): app = webtest.TestApp(select_app) res = app.get('/') self.assertEqual(res.status_int, 200) self.assertEqual(res.headers['content-type'], 'text/html; charset=utf-8') self.assertEqual(res.content_type, 'text/html') multiple_form = res.forms["multiple_select_form"] self.assertEqual(multiple_form["multiple"].value, ["8", "11"],\ multiple_form["multiple"].value) try: multiple_form.set("multiple", ["24", "88"]) self.assertTrue(False, "not-an-option value error should have been raised") except ValueError: pass multiple_form["multiple"].force_value(["24", "88"]) self.assertEqual(multiple_form["multiple"].value, ["24", "88"],\ multiple_form["multiple"].value) display = multiple_form.submit("button") self.assertIn("

You selected 24, 88

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

You selected

", display, display) res = app.get('/') self.assertEqual(res.status_int, 200) self.assertEqual(res.headers['content-type'], 'text/html; charset=utf-8') self.assertEqual(res.content_type, 'text/html') multiple_form = res.forms["multiple_select_form"] self.assertTrue(multiple_form["multiple"].value is None,\ multiple_form["multiple"].value) multiple_form.set("multiple", ["9"]) self.assertEqual(multiple_form["multiple"].value, ["9"],\ multiple_form["multiple"].value) display = multiple_form.submit("button") self.assertIn("

You selected 9

", display, display)