def test_iter(self):
        """Testing ChangeFormRow.__iter__"""
        row = ChangeFormRow(self.form, field='username')

        fields = list(row)
        self.assertEqual(len(fields), 1)

        field = fields[0]
        self.assertIsInstance(field, ChangeFormField)
        self.assertEqual(field.field.name, 'username')
    def test_init_with_checkbox(self):
        """Testing ChangeFormField.__init__ with checkbox"""
        row = ChangeFormRow(self.form, field='is_active')
        field = ChangeFormField(form_row=row,
                                admin_field=AdminField(self.form,
                                                       field='is_active',
                                                       is_first=True))

        self.assertEqual(field.classes, 'rb-c-form-field -has-input-first')
        self.assertTrue(field.has_field_first)
        self.assertTrue(field.is_checkbox)
    def test_render(self):
        """Testing ChangeFormField.render"""
        row = ChangeFormRow(self.form, field='is_active')
        field = ChangeFormField(form_row=row,
                                admin_field=AdminField(self.form,
                                                       field='is_active',
                                                       is_first=True))
        html = field.render()

        self.assertIsInstance(html, SafeText)
        self.assertHTMLEqual(
            html, '<input id="id_is_active" name="is_active" type="checkbox"'
            ' checked>')
    def test_label_tag_with_checkbox_field(self):
        """Testing ChangeFormField.label_tag with checkbox field"""
        row = ChangeFormRow(self.form, field='is_active')
        field = ChangeFormField(form_row=row,
                                admin_field=AdminField(self.form,
                                                       field='is_active',
                                                       is_first=True))
        html = field.label_tag()

        self.assertIsInstance(html, SafeText)
        self.assertHTMLEqual(
            html, '<label class="rb-c-form-field__label" for="id_is_active">'
            'Active</label>')
    def test_label_tag(self):
        """Testing ChangeFormField.label_tag"""
        row = ChangeFormRow(self.form, field='username')
        field = ChangeFormField(form_row=row,
                                admin_field=AdminField(self.form,
                                                       field='username',
                                                       is_first=True))
        html = field.label_tag()

        self.assertIsInstance(html, SafeText)
        self.assertHTMLEqual(
            html, '<label class="rb-c-form-field__label"'
            ' for="id_username">Username:</label>')
    def test_init(self):
        """Testing ChangeFormField.__init__"""
        row = ChangeFormRow(self.form, field='username')
        field = ChangeFormField(form_row=row,
                                admin_field=AdminField(self.form,
                                                       field='username',
                                                       is_first=True))

        self.assertEqual(field.classes, 'rb-c-form-field -is-required')
        self.assertTrue(field.is_first)
        self.assertFalse(field.has_field_first)
        self.assertFalse(field.is_checkbox)
        self.assertFalse(field.is_readonly)
        self.assertFalse(field.show_errors)
    def test_init_with_errors(self):
        """Testing ChangeFormField.__init__ with errors"""
        self.form.errors['username'] = self.form.error_class(['Stuff bad'])
        row = ChangeFormRow(self.form, field='username')
        field = ChangeFormField(form_row=row,
                                admin_field=AdminField(self.form,
                                                       field='username',
                                                       is_first=True))

        self.assertEqual(field.classes,
                         'rb-c-form-field -is-required -has-errors')
        self.assertTrue(field.show_errors)
        self.assertHTMLEqual(field.errors,
                             '<ul class="errorlist"><li>Stuff bad</li></ul>')
    def test_label_tag_with_read_only_field(self):
        """Testing ChangeFormField.label_tag with read-only field"""
        row = ChangeFormRow(self.form, field='username')
        field = ChangeFormField(form_row=row,
                                admin_field=AdminReadonlyField(
                                    self.form,
                                    field='username',
                                    is_first=True,
                                    model_admin=self.model_admin))
        html = field.label_tag()

        self.assertIsInstance(html, SafeText)
        self.assertHTMLEqual(
            html, '<label class="rb-c-form-field__label">Username:</label>')
    def test_render_with_read_only_field(self):
        """Testing ChangeFormField.render with read-only field"""
        row = ChangeFormRow(self.form, field='username')
        field = ChangeFormField(
            form_row=row,
            admin_field=AdminReadonlyField(self.form,
                                           field='username',
                                           is_first=True,
                                           model_admin=self.model_admin))
        html = field.render()

        self.assertIsInstance(html, SafeText)
        self.assertHTMLEqual(
            html,
            '<div class="rb-c-form-field__readonly-value">test-user</div>')
    def test_init_with_read_only(self):
        """Testing ChangeFormField.__init__ with read-only field"""
        row = ChangeFormRow(self.form, field='username')
        field = ChangeFormField(
            form_row=row,
            admin_field=AdminReadonlyField(self.form,
                                           field='username',
                                           is_first=True,
                                           model_admin=self.model_admin))

        self.assertEqual(field.classes, 'rb-c-form-field -is-read-only')
        self.assertTrue(field.is_first)
        self.assertTrue(field.is_readonly)
        self.assertFalse(field.has_field_first)
        self.assertFalse(field.is_checkbox)
        self.assertFalse(field.show_errors)
    def test_init_with_multi_choice(self):
        """Testing ChangeFormField.__init__ with ModelMultipleChoiceField"""
        class MyForm(forms.Form):
            field = forms.ModelMultipleChoiceField(
                queryset=ReviewRequest.objects.all(), required=False)

        form = MyForm()
        row = ChangeFormRow(form, field='field')
        field = ChangeFormField(form_row=row,
                                admin_field=AdminField(form,
                                                       field='field',
                                                       is_first=True))

        self.assertEqual(field.classes, 'rb-c-form-field')
        self.assertIsInstance(form.fields['field'].widget,
                              FilteredSelectMultiple)
    def test_classes(self):
        """Testing ChangeFormRow.classes"""
        row = ChangeFormRow(self.form, field='username')

        self.assertEqual(row.classes, 'rb-c-form-row field-username')