Source

WebHelpers / tests / test_modeltags.py

# -*- coding: utf-8 -*-
from util import WebHelpersTestCase
import unittest

from webhelpers.html import HTML
from webhelpers.html.tags import *

class Holder(object):
    def __init__(self, settings):
        for k,v in settings.iteritems():
            setattr(self, k, v)
            
class TestModelTagsHelperWithObject(WebHelpersTestCase):
    def setUp(self):
        super(TestModelTagsHelperWithObject, self).setUp()
        obj = Holder({'name':'Jim', 'phone':'123-456-7890', 'fulltime':True, 'fired':False, 'password':'bacon', 'longtext':"lorem ipsum lorem ipsum\n"*10, 'favcolor':'blue', 'lang':'en'})
        self.m = ModelTags(obj)
        
    def test_check_box(self):
        self.assertEqual(
            self.m.checkbox("fulltime"),
            u'<input checked="checked" name="fulltime" type="checkbox" value="1" />',
        )

    def test_hidden_field(self):
        self.assertEqual(
            self.m.hidden("name"),
            u'<input name="name" type="hidden" value="Jim" />'
        )

    def test_password_field(self):
        self.assertEqual(
            self.m.password('name'), 
            u'<input name="name" type="password" value="Jim" />'
        )
    def test_file_field(self):
        self.assertEqual(
            self.m.file('name'), 
            u'<input name="name" type="file" value="Jim" />'
        )

    def test_radio_button(self):
        self.assertEqual(
            self.m.radio("favcolor", "blue"),
            u'<input checked="checked" id="favcolor_blue" name="favcolor" type="radio" value="blue" />'
        )
        
        self.assertEqual(
            self.m.radio("favcolor", "red"),
            u'<input id="favcolor_red" name="favcolor" type="radio" value="red" />'
        )


    def test_text_area(self):
        self.assertEqual(
            self.m.textarea("longtext"),
            u'<textarea name="longtext">lorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\n</textarea>'
        )

    def test_text_field(self):
        self.assertEqual(
            self.m.text("name"),
            u'<input name="name" type="text" value="Jim" />'
        )
    def test_select(self):
        self.assertEqual(
            self.m.select("lang", [("en", "English"), ("de", "German"), ("jp", "Japanese")]),
            u'<select name="lang">\n<option selected="selected" value="en">English</option>\n<option value="de">German</option>\n<option value="jp">Japanese</option>\n</select>'
        )

class TestModelTagsHelperWithDict(TestModelTagsHelperWithObject):
    def setUp(self):
        super(TestModelTagsHelperWithObject, self).setUp()
        obj = {'name':'Jim', 'phone':'123-456-7890', 'fulltime':True, 'fired':False, 'password':'bacon', 'longtext':"lorem ipsum lorem ipsum\n"*10, 'favcolor':'blue', 'lang':'en'}
        self.m = ModelTags(obj, use_keys=True)

    def test_check_box(self):
        self.assertEqual(
            self.m.checkbox("fulltime"),
            u'<input checked="checked" name="fulltime" type="checkbox" value="1" />',
        )

    def test_hidden_field(self):
        self.assertEqual(
            self.m.hidden("name"),
            u'<input name="name" type="hidden" value="Jim" />'
        )

    def test_password_field(self):
        self.assertEqual(
            self.m.password('name'), 
            u'<input name="name" type="password" value="Jim" />'
        )
    def test_file_field(self):
        self.assertEqual(
            self.m.file('name'), 
            u'<input name="name" type="file" value="Jim" />'
        )

    def test_radio_button(self):
        self.assertEqual(
            self.m.radio("favcolor", "blue"),
            u'<input checked="checked" id="favcolor_blue" name="favcolor" type="radio" value="blue" />'
        )

        self.assertEqual(
            self.m.radio("favcolor", "red"),
            u'<input id="favcolor_red" name="favcolor" type="radio" value="red" />'
        )


    def test_text_area(self):
        self.assertEqual(
            self.m.textarea("longtext"),
            u'<textarea name="longtext">lorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\n</textarea>'
        )

    def test_text_field(self):
        self.assertEqual(
            self.m.text("name"),
            u'<input name="name" type="text" value="Jim" />'
        )
    def test_select(self):
        self.assertEqual(
            self.m.select("lang", [("en", "English"), ("de", "German"), ("jp", "Japanese")]),
            u'<select name="lang">\n<option selected="selected" value="en">English</option>\n<option value="de">German</option>\n<option value="jp">Japanese</option>\n</select>'
        )

class TestModelTagsHelperWithIdGeneration(TestModelTagsHelperWithObject):
    def setUp(self):
        super(TestModelTagsHelperWithObject, self).setUp()
        obj = Holder({'name':'Jim', 'phone':'123-456-7890', 'fulltime':True, 'fired':False, 'password':'bacon', 'longtext':"lorem ipsum lorem ipsum\n"*10, 'favcolor':'blue', 'lang':'en'})
        self.m = ModelTags(obj, id_format='person:%s')

    def test_check_box(self):
        self.assertEqual(
            self.m.checkbox("fulltime"),
            u'<input checked="checked" id="person:fulltime" name="fulltime" type="checkbox" value="1" />',
        )

    def test_hidden_field(self):
        self.assertEqual(
            self.m.hidden("name"),
            u'<input id="person:name" name="name" type="hidden" value="Jim" />'
        )

    def test_password_field(self):
        self.assertEqual(
            self.m.password('name'), 
            u'<input id="person:name" name="name" type="password" value="Jim" />'
        )
    def test_file_field(self):
        self.assertEqual(
            self.m.file('name'), 
            u'<input id="person:name" name="name" type="file" value="Jim" />'
        )

    def test_radio_button(self):
        self.assertEqual(
            self.m.radio("favcolor", "blue"),
            u'<input checked="checked" id="person:favcolor_blue" name="favcolor" type="radio" value="blue" />'
        )

        self.assertEqual(
            self.m.radio("favcolor", "red"),
            u'<input id="person:favcolor_red" name="favcolor" type="radio" value="red" />'
        )


    def test_text_area(self):
        self.assertEqual(
            self.m.textarea("longtext"),
            u'<textarea id="person:longtext" name="longtext">lorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\n</textarea>'
        )

    def test_text_field(self):
        self.assertEqual(
            self.m.text("name"),
            u'<input id="person:name" name="name" type="text" value="Jim" />'
        )
    def test_select(self):
        self.assertEqual(
            self.m.select("lang", [("en", "English"), ("de", "German"), ("jp", "Japanese")]),
            u'<select id="person:lang" name="lang">\n<option selected="selected" value="en">English</option>\n<option value="de">German</option>\n<option value="jp">Japanese</option>\n</select>'
        )

class TestModelTagsHelperWithoutObject(WebHelpersTestCase):
    def setUp(self):
        super(TestModelTagsHelperWithoutObject, self).setUp()
        obj = ''
        self.m = ModelTags(obj)
        
    def test_check_box(self):
        self.assertEqual(
            self.m.checkbox("fulltime"),
            u'<input name="fulltime" type="checkbox" value="1" />',
        )

    def test_hidden_field(self):
        self.assertEqual(
            self.m.hidden("name"),
            u'<input name="name" type="hidden" value="" />'
        )

    def test_password_field(self):
        self.assertEqual(
            self.m.password('name'), 
            u'<input name="name" type="password" value="" />'
        )
    def test_file_field(self):
        self.assertEqual(
            self.m.file('name'), 
            u'<input name="name" type="file" value="" />'
        )

    def test_radio_button(self):
        self.assertEqual(
            self.m.radio("favcolor", "blue"),
            u'<input id="favcolor_blue" name="favcolor" type="radio" value="blue" />'
        )
        
        self.assertEqual(
            self.m.radio("favcolor", "red"),
            u'<input id="favcolor_red" name="favcolor" type="radio" value="red" />'
        )


    def test_text_area(self):
        self.assertEqual(
            self.m.textarea("longtext"),
            u'<textarea name="longtext"></textarea>'
        )

    def test_text_field(self):
        self.assertEqual(
            self.m.text("name"),
            u'<input name="name" type="text" value="" />'
        )
    def test_select(self):
        self.assertEqual(
            self.m.select("lang", [("en", "English"), ("de", "German"), ("jp", "Japanese")]),
            u'<select name="lang">\n<option value="en">English</option>\n<option value="de">German</option>\n<option value="jp">Japanese</option>\n</select>'
        )        
if __name__ == '__main__':
    suite = map(unittest.makeSuite, [
        TestModelTagsHelperWithObject,
        TestModelTagsHelperWithDict,
        TestModelTagsHelperWithIdGeneration,
        TestModelTagsHelperWithoutObject
        ])
    for testsuite in suite:
        unittest.TextTestRunner(verbosity=1).run(testsuite)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.