jeudi 29 août 2019

Django FileField's upload_to not being called during tests

I have a models.py module that looks like this:

def get_path(instance, filename):
    try:
        # Something
    except Exception:
        raise ValidationError("A very specific error message.")

    return f"path/to/{instance.pk}_{filename}"


class FinalDocument(models.Model):
    # ...
    file = models.FileField(upload_to=get_path)

class TempDocument(models.Model):
    # ...
    file = models.FileField()


I have an endpoint that, among other things, grabs a file from a TempDocument instance and assigns it to an existing FinalDocument instance. In real life, this endpoint is failing with "A very specific error message". During tests, however, I cannot for the life of me reproduce this error. I have placed a breakpoint in the upload_to function and it is simply not called. I am assuming this has something to do with different testing vs production settings, but I haven't been able to figure out what. Any idea what I could be missing?

Aucun commentaire:

Enregistrer un commentaire