def radio_button(self): self.model_object.an_attribute = 2 choices = [ Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two')) ] self.field = ChoiceField(choices) self.field.bind('an_attribute', self.model_object) self.widget = self.form.add_child( RadioButtonSelectInput(self.form, self.field)) radio_button = '<label>'\ '<input name="test-an_attribute" id="id-test-an_attribute-%s"%s '\ 'data-msg-pattern="an_attribute should be one of the following: 1|2" '\ '%s'\ 'form="test" pattern="(1|2)" '\ 'title="an_attribute should be one of the following: 1|2" '\ 'type="radio" value="%s">%s'\ '</label>' outer_div = '<div class="reahl-primitiveinput reahl-radio-button-input">%s</div>' buttons = (radio_button % ('1', '', '', '1', 'One')) +\ (radio_button % ('2', ' checked="checked"', '', '2', 'Two')) self.expected_html = outer_div % buttons self.field_controls_visibility = True
def test_marshalling_of_radio_button_select_input(web_fixture, input_fixture, field_scenario): """When a form is submitted, the value of the selected radio button is persisted.""" fixture = input_fixture choices = [Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two'))] fixture.field = field_scenario.field_class(choices) model_object = fixture.model_object model_object.an_attribute = field_scenario.default_domain_value wsgi_app = web_fixture.new_wsgi_app(child_factory=fixture.new_Form(input_widget_class=RadioButtonSelectInput).factory('myform')) web_fixture.reahl_server.set_app(wsgi_app) web_fixture.driver_browser.open('/') radio_one = XPath.input_labelled('One') radio_two = XPath.input_labelled('Two') # One is pre selected assert web_fixture.driver_browser.is_selected(radio_one) assert not web_fixture.driver_browser.is_selected(radio_two) # click on Two web_fixture.driver_browser.click(radio_two) assert not web_fixture.driver_browser.is_selected(radio_one) web_fixture.driver_browser.click(XPath.button_labelled('click me')) assert model_object.an_attribute == field_scenario.selected_domain_value
def radio_button(self): self.model_object.an_attribute = 2 choices = [ Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two')) ] self.field = ChoiceField(choices) self.field.bind('an_attribute', self.model_object) self.widget = self.form.add_child( RadioButtonInput(self.form, self.field)) validations = html_escape( '{"validate": {"messages": {"pattern": "an_attribute should be one of the following: 1\|2"}}}' ) radio_button = r'<span class="reahl-radio-button">'\ r'<input name="an_attribute"%s form="test" pattern="\(1\|2\)" '\ r'title="an_attribute should be one of the following: 1\|2" '\ r'type="radio" value="%s" class="%s">%s'\ r'</span>' outer_div = r'<div class="reahl-radio-button-input">%s</div>' buttons = (radio_button % ('', '1', validations, 'One')) +\ (radio_button % (' checked="checked"', '2', validations, 'Two')) self.expected_html = outer_div % buttons self.field_controls_visibility = True
def test_unique_choices(fixture): """All choices must have unique values.""" duplicate_choices = [Choice(1, IntegerField(label='One')), Choice(1, IntegerField(label='Two')), Choice(3, IntegerField(label='Three'))] with expected(ProgrammerError, test='Duplicate choices are not allowed'): ChoiceField(duplicate_choices)
def test_marshalling_of_checkbox_select_input(web_fixture, checkbox_fixture): """CheckboxSelectInput is used to choose many things from a list.""" fixture = checkbox_fixture choices = [Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two')), Choice(3, IntegerField(label='Three'))] fixture.field = MultiChoiceField(choices) model_object = fixture.model_object model_object.an_attribute = [1] wsgi_app = web_fixture.new_wsgi_app(child_factory=fixture.new_Form(input_widget_class=CheckboxSelectInput).factory('myform')) web_fixture.reahl_server.set_app(wsgi_app) web_fixture.driver_browser.open('/') assert web_fixture.driver_browser.is_selected(XPath.input_labelled('One')) assert not web_fixture.driver_browser.is_selected(XPath.input_labelled('Two')) assert not web_fixture.driver_browser.is_selected(XPath.input_labelled('Three')) # Case: checkbox is submitted with form (ie checked) web_fixture.driver_browser.set_deselected(XPath.input_labelled('One')) web_fixture.driver_browser.set_selected(XPath.input_labelled('Two')) web_fixture.driver_browser.set_selected(XPath.input_labelled('Three')) web_fixture.driver_browser.click(XPath.button_labelled('click me')) assert model_object.an_attribute == [2, 3] assert fixture.checkbox.value == '2,3' assert not web_fixture.driver_browser.is_selected(XPath.input_labelled('One')) assert web_fixture.driver_browser.is_selected(XPath.input_labelled('Two')) assert web_fixture.driver_browser.is_selected(XPath.input_labelled('Three'))
def grouped_choices(self): self.all_choices = [Choice(1, IntegerField(label='One')), Choice('2', Field(label='Two'))] self.groups = [ChoiceGroup('', self.all_choices)] self.choices = self.groups self.valid_inputs = ['1', '2'] self.input_to_value_map = {'1': 1, '2': '2'} self.expected_validation_constraint = AllowedValuesConstraint
def new_field(self): choices = [ Choice(1, Field(label='One')), Choice(2, Field(label='Two')) ] field = ChoiceField(choices) field.bind('field', self) return field
def multi_value_empty_the_list(self): self.field = MultiChoiceField([Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two')), Choice(3, IntegerField(label='Three'))], default=[2], label='field') self.field_on_query_string = '{field_name}[]-' self.field_value_marshalled = [] self.field_value_as_string = ''
def multi_value_not_submitted(self): self.field = MultiChoiceField([Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two')), Choice(3, IntegerField(label='Three'))], default=[2], label='field') self.field_on_query_string = '' self.field_value_marshalled = [2] self.field_value_as_string = '2'
def fields(self, fields): fields.operand_a = IntegerField(label='A', required=True) fields.operand_b = IntegerField(label='B', required=True) fields.operator = ChoiceField([ Choice('plus', Field(label='+')), Choice('divide', Field(label='÷')) ], required=True)
def fields(self, fields): fields.amount = IntegerField(label='Total amount', required=True) fields.amount_or_percentage = ChoiceField([ Choice('amount', Field(label='Amount')), Choice('percentage', Field(label='Percentage')) ], label='Allocate using', required=True)
def fields(self, fields): fields.operandA = IntegerField(label='A') fields.operandB = ChoiceField([ Choice(4, IntegerField(label='4')), Choice(5, IntegerField(label='5')), Choice(6, IntegerField(label='6')) ], label='B')
def multi_value(self): self.field = MultiChoiceField([Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two')), Choice(3, IntegerField(label='Three'))], default=[], label='field') self.field_on_query_string = '{field_name}[]=1&{field_name}[]=3' self.field_value_marshalled = [1, 3] self.field_value_as_string = '1,3'
def fields(self, fields): choices = [ Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two')), Choice(3, IntegerField(label='Three')) ] fields.no_validation_exception_field = MultiChoiceField( choices, label='Make your invalid choice', default=[]) fields.validation_exception_field = MultiChoiceField( choices, label='Make your choice', default=[], required=True)
def multi_choices(self): self.all_choices = [Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two')), Choice(3, IntegerField(label='Three'))] self.groups = [] self.choices = self.all_choices self.field = self.new_field(MultiChoiceField) self.valid_inputs = [('1',), ['1', '2']] self.input_to_value_map = {('1',): [1], ('1', '2'): [1,2]} self.expected_validation_constraint = MultiChoiceConstraint
def fields(self, fields): fields.agreed_to_terms = BooleanField(label='I agree to the terms and conditions') fields.new_or_existing = ChoiceField([Choice('new', Field(label='New')), Choice('existing', Field(label='Existing'))], label='Are you a new or existing investor?') fields.existing_account_number = IntegerField(label='Existing account number', required=True) fields.name = Field(label='Name', required=True) fields.surname = Field(label='Surname', required=True) fields.amount = IntegerField(label='Total amount', required=True) fields.amount_or_percentage = ChoiceField([Choice('amount', Field(label='Amount')), Choice('percentage', Field(label='Percentage'))], label='Allocate using', required=True)
def select_input_multi(self): self.model_object.an_attribute = [2] choices = [Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two'))] self.field = MultiChoiceField(choices) self.field.bind('an_attribute', self.model_object) self.widget = self.form.add_child(SelectInput(self.form, self.field)) options = '<option id="id-test-an_attribute-91--93--1" value="1">One</option><option id="id-test-an_attribute-91--93--2" selected="selected" value="2">Two</option>' self.expected_html = '<select name="test-an_attribute[]" id="id-test-an_attribute-91--93-" form="test" multiple="multiple" class="reahl-primitiveinput">%s</select>' % (options) self.field_controls_visibility = True
def test_choices_disabled(web_fixture, checkbox_fixture, choice_type_scenario): """A choice that is not writable renders as disabled.""" fixture = checkbox_fixture choices = [Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two', writable=Allowed(False)))] fixture.field = MultiChoiceField(choices) model_object = fixture.model_object model_object.an_attribute = [1] wsgi_app = web_fixture.new_wsgi_app(child_factory=fixture.new_Form(input_widget_class=choice_type_scenario.input_type).factory('myform')) web_fixture.reahl_server.set_app(wsgi_app) web_fixture.driver_browser.open('/') assert web_fixture.driver_browser.is_element_enabled(choice_type_scenario.xpath_function_to_choice('One')) assert not web_fixture.driver_browser.is_element_enabled(choice_type_scenario.xpath_function_to_choice('Two'))
def create_html_widget(self): if isinstance(self.bound_field, BooleanField): main_element = self.create_main_element() self.added_choices.append(self.add_choice_to(main_element, Choice(self.bound_field.true_value, Field(label=self.bound_field.label)))) else: main_element = super(CheckboxInput, self).create_html_widget() return main_element
def checkbox_input_multi(self): selected_checkboxes = [2, 3] self.model_object.an_attribute = selected_checkboxes choices = [Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two')), Choice(3, IntegerField(label='Three')) ] self.field = MultiChoiceField(choices) self.field.bind('an_attribute', self.model_object) self.widget = self.form.add_child(CheckboxSelectInput(self.form, self.field)) not_checked_option = r'<label><input name="test-an_attribute[]" id="id-test-an_attribute-91--93--1" form="test" type="checkbox" value="1">One</label>' checked_options = r'<label><input name="test-an_attribute[]" id="id-test-an_attribute-91--93--2" checked="checked" form="test" type="checkbox" value="2">Two</label>' checked_options += r'<label><input name="test-an_attribute[]" id="id-test-an_attribute-91--93--3" checked="checked" form="test" type="checkbox" value="3">Three</label>' options = not_checked_option + checked_options self.expected_html = r'<div class="reahl-checkbox-input reahl-primitiveinput">%s</div>' % (options) self.field_controls_visibility = True
def test_checkbox_basics_with_multichoice_field(web_fixture, checkbox_fixture): """CheckboxInput can also be used to choose many things from a list, in which case it renders many checkboxes.""" choices = [ Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two', writable=Action(lambda: False))), Choice(3, IntegerField(label='Three')), Choice(4, IntegerField(label='Four')), ] checkbox_fixture.field = MultiChoiceField(choices, label='Make your choice', default=[1]) web_fixture.reahl_server.set_app( web_fixture.new_wsgi_app( child_factory=checkbox_fixture.Form.factory())) browser = web_fixture.driver_browser browser.open('/') assert browser.is_element_present( XPath.label().with_text('Make your choice')) assert browser.get_xpath_count( '//div[@class="reahl-checkbox-input reahl-primitiveinput"]/div/input[@class="custom-control-input"]/following-sibling::label[@class="custom-control-label"]' ) == 4 checkbox_one = XPath.input_labelled('One') checkbox_two = XPath.input_labelled('Two') checkbox_three = XPath.input_labelled('Three') checkbox_four = XPath.input_labelled('Four') assert browser.is_selected(checkbox_one) assert not browser.is_selected(checkbox_two) assert not browser.is_element_enabled(checkbox_two) # assert browser.is_visible(checkbox_two) #cannot do this as the way bootsrap renders, the actual html input has opacity=0 assert not browser.is_selected(checkbox_three) assert not browser.is_selected(checkbox_four) browser.set_deselected(checkbox_one) browser.set_selected(checkbox_three) browser.set_selected(checkbox_four) browser.click(XPath.button_labelled('Submit')) assert not browser.is_selected(checkbox_one) assert browser.is_selected(checkbox_three) assert browser.is_selected(checkbox_four)
def fields(self, fields): collaborators = [ Choice(i.id, IntegerField(label=i.email)) for i in Session.query(EmailAndPasswordSystemAccount).all() ] fields.chosen_collaborator = ChoiceField(collaborators, label='Choose collaborator') fields.may_edit_address = BooleanField( label='May edit existing addresses') fields.may_add_address = BooleanField(label='May add new addresses')
def select_input(self): self.model_object.an_attribute = 2 choices = [Choice(False, BooleanField(label='None')), ChoiceGroup('grouped', [ Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two'))]) ] self.field = ChoiceField(choices) self.field.bind('an_attribute', self.model_object) self.widget = self.form.add_child(SelectInput(self.form, self.field)) group = '<optgroup label="grouped">'+\ '<option id="id-test-an_attribute-1" value="1">One</option>'+\ '<option id="id-test-an_attribute-2" selected="selected" value="2">Two</option>'+\ '</optgroup>' option = '<option id="id-test-an_attribute-off" value="off">None</option>' self.expected_html = r'<select name="test-an_attribute" id="id-test-an_attribute" form="test" class="reahl-primitiveinput">%s%s</select>' % (option, group) self.bound_field = self.widget.bound_field
def select_input(self): self.model_object.an_attribute = 2 choices = [ Choice(False, BooleanField(label='None')), ChoiceGroup('grouped', [ Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two')) ]) ] self.field = ChoiceField(choices) self.field.bind('an_attribute', self.model_object) self.widget = self.form.add_child(SelectInput(self.form, self.field)) group = r'<optgroup label="grouped"><option value="1">One</option><option selected="selected" value="2">Two</option></optgroup>' option = r'<option value="off">None</option>' self.expected_html = r'<select name="an_attribute" form="test">%s%s</select>' % ( option, group) self.field_controls_visibility = True
def multi_choices_required(self): self.all_choices = [Choice(1, IntegerField(label='One'))] self.groups = [] self.choices = self.all_choices field = self.new_field(MultiChoiceField) self.field = field.with_validation_constraint(RequiredConstraint()) self.valid_inputs = [('1', ), ['1']] self.invalid_input = [] self.input_to_value_map = {('1', ): [1]} self.expected_validation_constraint = RequiredConstraint
def fields(self, fields): fields.choice = ChoiceField([Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two')), Choice(3, IntegerField(label='Three'))], label='Choice') fields.choice3 = ChoiceField([Choice(7, IntegerField(label='Seven')), Choice(8, IntegerField(label='Eight')), Choice(9, IntegerField(label='Nine'))], label='Choice3') fields.calculated_state = IntegerField(label='Calculated', writable=Allowed(False))
def fields(self, fields): fields.choice = ChoiceField([Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two')), Choice(3, IntegerField(label='Three'))], label='Choice') fields.choice2 = ChoiceField([Choice(4, IntegerField(label='Four')), Choice(5, IntegerField(label='Five')), Choice(6, IntegerField(label='Six'))], label='Choice2') fields.calculated_state = IntegerField(label='Calculated', writable=Allowed(False))
def fields(self, fields): fields.text_input_field = Field(label='A TextInput') fields.password_field = Field(label='A PasswordInput') fields.text_area_field = Field(label='A TextArea') fields.text_input_without_label = Field( label='A TextInput without a label') fields.cue_field = Field(label='A TextInput in a CueInput') fields.choice_field = ChoiceField([ Choice(False, BooleanField(label='None selected')), ChoiceGroup('Numbers', [ Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two')), Choice(3, IntegerField(label='Three')) ]), ChoiceGroup('Colours', [ Choice('r', Field(label='Red')), Choice('g', Field(label='Green')) ]) ], label='A SelectInput') fields.multi_choice_field = MultiChoiceField( [ Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two')), Choice(3, IntegerField(label='Three')) ], label='A SelectInput allowing multiple choices') fields.another_multi_choice_field = MultiChoiceField( [ Choice('a', Field(label='Newton')), Choice('b', Field(label='Archimedes')), Choice('c', Field(label='Einstein')), ], default=['a', 'c'], label='A CheckboxInput allowing multiple choices') fields.boolean_field = BooleanField(label='A CheckboxInput to toggle') fields.radio_choice_field = ChoiceField( [ Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two')), Choice(3, IntegerField(label='Three')) ], label='A RadioButtonSelectInput') fields.fuzzy_date_field = DateField( label='A fuzzy TextInput for a Date')
def fields(self, fields): fields.factor = ChoiceField( [Choice(i, IntegerField(label=str(i))) for i in range(1, 10)])
def fields(self, fields): types = [ Choice('passport', Field(label='Passport')), Choice('id_card', Field(label='National ID Card')) ] fields.document_type = ChoiceField(types, label='Type', required=True) fields.id_card_number = Field(label='ID card number', required=True) fields.passport_number = Field(label='Passport number', required=True) countries = [ Choice('Australia', Field(label='Australia')), Choice('Chile', Field(label='Chile')), Choice('China', Field(label='China')), Choice('France', Field(label='France')), Choice('Germany', Field(label='Germany')), Choice('Ghana', Field(label='Ghana')), Choice('India', Field(label='India')), Choice('Japan', Field(label='Japan')), Choice('Kenya', Field(label='Kenya')), Choice('Namibia', Field(label='Namibia')), Choice('Nigeria', Field(label='Nigeria')), Choice('South Africa', Field(label='South Africa')), Choice('United States', Field(label='United States of America')), Choice('United Kingdom', Field(label='United Kingdom')), Choice('Zimbabwe', Field(label='Zimbabwe')) ] fields.country = ChoiceField(countries, label='Country', required=True)