jeudi 7 novembre 2019

Testing a form on Django

I am trying to run a test on a form. It keeps returning invalid. Other forms work fine though.

The form in forms.py:

class SongForm(forms.ModelForm):
class Meta():
    model = Song
    fields = ('title', 'type', 'audio_file', 'description', 'written_by', 'song_text')

The model the form comes from in models.py:

class Song(models.Model):
title          = models.CharField(max_length=120)
SONG_TYPES     = (
    ('KI', 'Kiirtan'),
    ('PS', 'Prabhat Samgiita'),
    ('BH', 'Bhajan'),
)
type              = models.CharField(max_length=30, choices=SONG_TYPES)
capo              = models.PositiveIntegerField(blank=True, null=True, validators=[MaxValueValidator(12)])
description       = models.TextField(blank=True)
uploader          = models.ForeignKey('Profile', null=True, on_delete=models.SET_NULL)
written_by        = models.CharField(max_length=50, blank=True)      
song_text         = models.TextField(blank=True)
audio_file        = models.FileField(upload_to='songs/')
upload_date       = models.DateField(auto_now=False, auto_now_add=True)
edit_date         = models.DateField(auto_now=True, auto_now_add=False)
chords            = models.ManyToManyField('Chord', related_name='chords', through='ChordIndex')

The test in tests/test_forms.py:

def test_Song_form_is_valid(self):
    the_data = {
      'title': 'a song',
      'type': 'KI',
      'description': '',
      'written_by': '',
      'song_text': '',
    }
    form = SongForm(data=the_data)
    self.assertTrue(form.is_valid())

The form works correctly in the web app with a POST method. I even tried copying the values from a valid POST on the test and it still failed!

I've been stuck with this for a while... Any clues? đŸ™đŸŒ

Aucun commentaire:

Enregistrer un commentaire