jeudi 31 octobre 2019

In a test my form looks right but don't validate

I want to test a form. It is working, but the test doesn't.

One field of this form is popolated by a javascript function. I can use selenium to do so, but I don't want because it's giving problems and also I want isolate the test.

So I'm calling the form in my test, then I'm creating the choices (this is what javascript should do), then I'm setting the fields values.

My models.py:

class Name(models.Model):
    name = models.CharField(_('nome'), max_length=50, default='')
    namelanguage = models.ForeignKey(
        NameLanguage, related_name='%(app_label)s_%(class)s_language',
        verbose_name=_('linguaggio'), on_delete=models.PROTECT)
    nametype = models.ForeignKey(
        NameType, related_name='%(app_label)s_%(class)s_tipo',
        verbose_name=_('tipo'), on_delete=models.PROTECT)
    gender = models.ForeignKey(
       Gender, related_name='%(app_label)s_%(class)s_gender',
       verbose_name=_('sesso'), on_delete=models.PROTECT,
       blank=True, null=True)
    usato = models.PositiveSmallIntegerField(_('usato'), default=0)
    approved = models.BooleanField(null=True, blank=True, default=False)

    def save(self, *args, **kwargs):
        self.name = format_for_save_name(self.name)
        to_save = check_gender_name(self)
        if not to_save:
            return
        else:
            super(Name, self).save(*args, **kwargs)

    def format_for_save_name(name):
        myname = name.lower().strip()
        if myname[0] not in "abcdefghijklmnopqrstuvwxyz#":
            myname = '#' + myname
        return myname

My form.py:

class NameForm(forms.ModelForm):
    class Meta:
        model = Name
        fields = ['namelanguage', 'nametype', 'gender', 'name', 'usato',
              'approved']
        widgets = {
            'gender': forms.RadioSelect(),
            'usato': forms.HiddenInput(),
            'approved': forms.HiddenInput(),
        }

My test_form.py:

def test_form_validation(self):
    maschio = Gender.objects.create(name_en='Male', name_it='Maschio')
    nome = NameType.objects.create(name_en='Name', name_it='Nome')
    romani = NameLanguage.objects.create(
        name_en='Romans', name_it='Romani')
    romani.sintassi.add(nome)
    form = NameForm()
    form.fields['nametype'].disabled = False
    form.fields['nametype'].choices = [(nome.id, nome)]
    form.fields['nametype'].initial = nome.id
    form.fields['gender'].initial = maschio.id
    form.fields['name'].initial = 'Bill'
    form.fields['namelanguage'].initial = romani.id
    # form.fields['usato'].initial = 0
    # form.fields['approved'].initial = False
    print('1', form)
    # self.assertTrue(form.is_valid())
    form.save()

Print() gives a form without errors but form.is_valid is False and (when is commented out) form.save() gives an error when the model try to save the name field:

if myname[0] not in "abcdefghijklmnopqrstuvwxyz#":
IndexError: string index out of range

That is because the name is an empty string and yet my print('1', form) gives all the fields with the right options selected and specifically the name field isn't empty but has value="Bill":

<td><input type="text" name="name" value="Bill" maxlength="50" autofocus="" required id="id_name">

Aucun commentaire:

Enregistrer un commentaire