from testcase import TestCase import htmlview import validators import variabledecode from simplehtmlgen import html from schema import Schema def d(**kw): return kw class GenTest(TestCase): def testMatches(self, description, schema, defaults, options, errors, httpfields, output): if isinstance(httpfields, str): httpfields = self.cgiParse(httpfields) form = htmlview.Form(schema=schema, action='form', formName='form') result = form.html(defaults=defaults, options=options, errors=errors, httpRequest=httpfields) output = [html.input.hidden(name='_formName_', value='form')] \ + output output = html.form(action='form', method='POST', name='form', c=output) self.assertHTMLEqual(result, output) params_testMatches = [] def addGood(schema, output, defaults=None, options=None, errors=None, httpfields=None): if isinstance(schema, type): name = schema.__name__ else: name = schema.__class__.__name__ GenTest.params_testMatches.append( (name, schema, defaults, options, errors, httpfields, output)) class NestedSchema(Schema): preValidators = Schema.preValidators \ + [variabledecode.NestedVariables()] class Name(NestedSchema): fname = validators.String(notEmpty=True, view=htmlview.Text, description='') lname = validators.String(notEmpty=True, view=htmlview.Text, description='') class phone(validators.ForEach): class item1(Schema): number = validators.PhoneNumber(view=htmlview.Text, description='') type = validators.OneOf(['home', 'work'], view=htmlview.Select(selections=[('home', 'home'), ('work', 'work')]), description='') addGood( Name, options=d(phone=d(repetitions=2)), output= ['fname:', html.input.text(name="fname", value=""), html.br, 'lname:', html.input.text(name="lname", value=""), html.br, 'number:', html.input.text(name="phone-0.number", value=""), html.br, 'type:', html.select(name="phone-0.type", c=[html.option("home", value="home"), html.option("work", value="work")]), html.br, 'number:', html.input.text(name="phone-1.number", value=""), html.br, 'type:', html.select(name="phone-1.type", c=[html.option("home", value="home"), html.option("work", value="work")]), html.br, html.input.hidden(name="phone--repetitions", value=2)]) addGood( Name, defaults=d(fname='test', phone=[d(number='111', type='home'), d(number='222', type='work')], lname='test2'), output= ['fname:', html.input.text(name='fname', value='test'), html.br, 'lname:', html.input.text(name='lname', value='test2'), html.br, 'number:', html.input.text(name='phone-0.number', value='111'), html.br, 'type:', html.select(name='phone-0.type', c=[html.option('home', value='home', selected='selected'), html.option('work', value='work')]), html.br, 'number:', html.input.text(name='phone-1.number', value='222'), html.br, 'type:', html.select(name='phone-1.type', c=[html.option('home', value='home'), html.option('work', value='work', selected='selected')]), html.br, html.input.hidden(name="phone--repetitions", value=2)]) addGood( Name, defaults=d(fname='f', lname='l'), options=d(fname=d(hidden=1), phone=d(repetitions=0)), output= ['fname:', html.input.hidden(name='fname', value='f'), html.br, 'lname:', html.input.text(name='lname', value='l'), html.br, html.input.hidden(name="phone--repetitions", value=0)]) addGood( Name, errors={None: 'There were some errors', 'fname': 'Enter a value', 'phone': [None, {'number': 'bad number'}], }, defaults=d(lname='Baker', phone=[d(number='555-3043', type='home'), d(number='444444', type='work'), d(number='948-3848', type='home')]), output= [html.b('There were some errors'), html.br, html.b('Enter a value'), html.br, 'fname:', html.input.text(name='fname', value=''), html.br, 'lname:', html.input.text(name='lname', value='Baker'), html.br, 'number:', ## 1 html.input.text(name='phone-0.number', value='555-3043'), html.br, 'type:', html.select(name='phone-0.type', c=[html.option(selected='selected', value='home', c='home'), html.option(value='work', c='work')]), html.br, html.b('bad number'), html.br, 'number:', ## 2 html.input.text(name='phone-1.number', value='444444'), html.br, 'type:', html.select(name='phone-1.type', c=[html.option(value='home', c='home'), html.option(value='work', c='work', selected='selected')]), html.br, 'number:', ## 3 html.input.text(name='phone-2.number', value='948-3848'), html.br, 'type:', html.select(name='phone-2.type', c=[html.option(selected='selected', value='home', c='home'), html.option(value='work', c='work')]), html.br, html.input.hidden(name='phone--repetitions', value=3), ]) addGood( Name, errors={'fname': 'Enter a value'}, # These should be ignored because of httpfields: defaults=d(fname='Joe', lname='Baker'), httpfields='lname=Smith&phone-0.number=393-4849&phone-0.type=home' '&phone--repetitions=2', output= [html.b('Enter a value'), html.br, 'fname:', html.input.text(name='fname', value=''), html.br, 'lname:', html.input.text(name='lname', value='Smith'), html.br, 'number:', ## 1 html.input.text(name='phone-0.number', value='393-4849'), html.br, 'type:', html.select(name='phone-0.type', c=[html.option(selected='selected', value='home', c='home'), html.option(value='work', c='work')]), html.br, 'number:', ## 2 html.input.text(name='phone-1.number', value=''), html.br, 'type:', html.select(name='phone-1.type', c=[html.option(value='home', c='home'), html.option(value='work', c='work')]), html.br, html.input.hidden(name='phone--repetitions', value=2), ])