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): 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 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 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 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 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 test_checkbox_select_input_allowed_fields(web_fixture): """A CheckboxSelectInput can only be used with a MultiChoiceField.""" model_object = web_fixture form = Form(web_fixture.view, 'test') choice_field = ChoiceField([]) choice_field.bind('an_attribute', model_object) with expected(ProgrammerError, test='<class \'reahl.component.modelinterface.ChoiceField\'> is not allowed to be used with <class \'reahl.web.ui.CheckboxSelectInput\'>'): CheckboxSelectInput(form, choice_field) multi_choice_field = MultiChoiceField([]) multi_choice_field.bind('another_attribute', model_object) with expected(NoException): CheckboxSelectInput(form, multi_choice_field)
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): fields.choice = MultiChoiceField([Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two')), Choice(3, IntegerField(label='Three'))], label='Choice')
def fields(self, fields): fields.choice = MultiChoiceField([Choice(1, IntegerField(label='One'))], label='Choice')