Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 如何覆盖自定义文件系统的测试设置?_Django_Django Testing_Django Settings - Fatal编程技术网

Django 如何覆盖自定义文件系统的测试设置?

Django 如何覆盖自定义文件系统的测试设置?,django,django-testing,django-settings,Django,Django Testing,Django Settings,在我的模型中,我定义了一个文件系统,它指定了一个为用户配置文件保存数据的自定义位置。它非常简单,如下所示: social_user_fs = FileSystemStorage(location=settings.SOCIAL_USER_FILES, base_url=settings.SOCIAL_USER_URL) class SocialUserProfile(models.Model): def get_u

在我的模型中,我定义了一个文件系统,它指定了一个为用户配置文件保存数据的自定义位置。它非常简单,如下所示:

social_user_fs = FileSystemStorage(location=settings.SOCIAL_USER_FILES,
                                   base_url=settings.SOCIAL_USER_URL)
class SocialUserProfile(models.Model):

    def get_user_profileimg_path(self, filename):
        return '%s/profile_images/%s' % (self.user_id, filename)
    image = models.ImageField(upload_to=get_user_profileimg_path,
                              storage=social_user_fs,
                              blank=True)
然后我在这样的模型中使用:

social_user_fs = FileSystemStorage(location=settings.SOCIAL_USER_FILES,
                                   base_url=settings.SOCIAL_USER_URL)
class SocialUserProfile(models.Model):

    def get_user_profileimg_path(self, filename):
        return '%s/profile_images/%s' % (self.user_id, filename)
    image = models.ImageField(upload_to=get_user_profileimg_path,
                              storage=social_user_fs,
                              blank=True)
这工作得很好,表现得和我预期的一样。但现在我在测试中遇到了一个问题:

import os

from django.test import TestCase
from django.test.utils import override_settings

from social_user.forms import ProfileImageUploadForm #@UnresolvedImport
from social_user.models import SocialUserProfile #@UnresolvedImport

# point the filesystem to the subfolder data of app/test/
@override_settings(SOCIAL_USER_FILES = os.path.dirname(__file__)+'/testdata',
                   SOCIAL_USER_URL = 'profiles/')

class TestProfileImageUploadForm(TestCase):

    fixtures = ['social_user_profile_fixtures.json']

    def test_save(self):
        profile = SocialUserProfile.objects.get(pk=1)
        import ipdb; ipdb.set_trace()
交互式调试会话为我提供了以下信息:

ipdb> from django.conf import settings
ipdb> settings.SOCIAL_USER_FILES
'/Volumes/Data/Website/Backend/project/social_user/tests/testdata'
ipdb> settings.SOCIAL_USER_URL
'profiles/'
# ok, the settings have been changed, the filesystem should use the new values

ipdb> profile.image.url
'/user_files/profiles/1/profile_images/picture1-1.png' 
# 'profiles/1/profile_images/picture1-1.png'
# would be correct with the new settings
# the actual value still uses the original settings

ipdb> f = file(profile.image.file)
*** IOError: [Errno 2] No such file or directory:
u'/Volumes/Data/Website/Backend/user_files/profiles/1/profile_images/picture1-1.png'
# same here, overridden settings should result in
# '/Volumes/Data/Website/Backend/social_user/tests/testdata/1/profile_images/picture1-1.png'
因此,设置已被覆盖。看起来我的自定义文件系统对覆盖设置没有反应。为什么?是否可以重写,或者文件系统是在某个点启动的,以后不能更改?

我猜social_user_fs在其模块中是全局的,并且您正在测试之外从该模块导入内容。所以它在调用测试方法和装饰器之前得到处理

导入SocialUserProfile内部测试保存,我认为这将是最好的解决方案