具有自定义文件存储后端的Django单元测试

时间:2018-11-18 13:34:17

标签: django django-testing

我正在编写一个单元测试用例以测试在Django 2.x中的文件上传

@pytest.mark.django_db
class TestContact(TestCase):
    def test_model_add_contact(self):
        user = mixer.blend(User, is_superuser=True, username='anuj')

        contact = Contact(
            user=user,
            first_name='Anuj',
            last_name='Sharma',
            gender='m',
            date_of_birth='1996-10-19'
        )
        contact.save()
        avatar_name = tempfile.NamedTemporaryFile(suffix='.jpg').name
        contact.avatar = avatar_name
        contact.save()

        # Avatar test
        assert contact.avatar == avatar_name, 'Should return avatar path'
        assert contact.get_avatar == settings.MEDIA_URL.rstrip('/') + avatar_name, 'Should return url of avatar path'

早期使用默认存储后端时,测试用例成功通过。

但是现在,我使用 django-storage 投递箱作为存储后端

DEFAULT_FILE_STORAGE = 'storages.backends.dropbox.DropBoxStorage'
DROPBOX_OAUTH2_TOKEN = 'access_token'
DROPBOX_ROOT_PATH = 'Koober'

但是现在,测试用例失败了

django.core.exceptions.SuspiciousFileOperation: The joined path (/tmp/tmpwzfymbqf.jpg) is located outside of the base path component (/builds/app/app-py/MyApp)

如何在单元测试中更改存储后端?

0 个答案:

没有答案