def test_boolean_validation(fixture): obj = EmptyStub() field = BooleanField() field.bind('boolean_attribute', obj) # Case: invalid invalid_boolean_name = ['negative', 'affirmative', '+', '-', None] for boolean_candidate in invalid_boolean_name: with expected(AllowedValuesConstraint): field.set_user_input(boolean_candidate) assert field.validation_error is field.get_validation_constraint_named( 'pattern') # Case: valid field.from_input('on') assert obj.boolean_attribute is True assert field.as_input() == 'on' field.from_input('off') assert obj.boolean_attribute is False assert field.as_input() == 'off' # Case: required means True for BooleanField field = BooleanField(required=True) field.bind('boolean_attribute', obj) with expected(AllowedValuesConstraint): field.set_user_input('off') assert field.validation_error is field.get_validation_constraint_named( 'pattern') with expected(NoException): field.from_input('on')
def test_boolean_i18n(): context = LocaleContextStub(locale='af').install() obj = EmptyStub() field = BooleanField() field.bind('boolean_attribute', obj) # Case: valid field.from_input('aan') assert obj.boolean_attribute is True assert field.as_input() == 'aan' field.from_input('af') assert obj.boolean_attribute is False assert field.as_input() == 'af'
def test_checkbox_input_restricted_to_use_with_boolean(web_fixture): """CheckboxInput is for toggling a true or false answer.""" model_object = web_fixture form = Form(web_fixture.view, 'test') # case: with BooleanField boolean_field = BooleanField(label='Boolean') boolean_field.bind('a_choice', model_object) with expected(NoException): CheckboxInput(form, boolean_field) # case: with disallowed field not_a_boolean_field = IntegerField(label='Other') not_a_boolean_field.bind('not_a_boolean', model_object) with expected(IsInstance): CheckboxInput(form, not_a_boolean_field)
def boolean_i18n(self, fixture): @stubclass(ExecutionContext) class AfrikaansContext(ExecutionContext): @property def interface_locale(self): return 'af' with AfrikaansContext(): obj = EmptyStub() field = BooleanField() field.bind('boolean_attribute', obj) # Case: valid field.from_input('aan') vassert( obj.boolean_attribute is True ) vassert( field.as_input() == 'aan' ) field.from_input('af') vassert( obj.boolean_attribute is False ) vassert( field.as_input() == 'af' )
def new_field(self): field = BooleanField() field.bind('field', self) return field