Mocking a Custom File Storage Backend
amazon-s3, django, unit-testing
Solution
You can just mock out the `_save` method in `S3Storage` class to avoid uploading to S3. You can use `FileSystemStorage` instead.
My solution for your case would be like this:
import mock
from utils.s3 import S3Storage
from django.core.files.storage import FileSystemStorage
fss = FileSystemStorage()
@mock.patch.object(S3Storage, '_save', fss._save)
def test_something():
assert True
Problem
I've created a custom file storage backend that calls out to Amazon S3 using boto and stores the files there (I know django-storages handles this as well, but we ran into several issues with it). I'm storing it in a utils module and using it in my models like this: ``` from utils.s3 import S3Storage class Photo(models.Model): image = models.ImageField(storage=S3Storage(), upload_to="images") ``` Thus any time a photo is created with an image file, the image file is uploaded to an S3 bucket. I don't want to make calls out to S3 during my tests, but figuring out exactly what to mock in this situation is difficult. I can't mock out the entire image field, because I need to test creating the model through Tastypie. Any ideas?