2012-04-19 30 views
1

私のモデルでは、ユーザープロファイルのデータを保存するカスタムの場所を指定するファイルシステムを定義しました。それは本当に簡単ですし、次のようになります。カスタムファイルシステムのテスト設定を上書きする - どのように?

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) 

。しかし、今、私はテストで問題に遭遇した:

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' 

ので、設定が上書きされています。私のカスタムファイルシステムは単に設定の上書きに反応しないようです。どうして?上書きが可能か、またはある時点でファイルシステムが開始されていて、後で変更することはできませんか?

答えて

0

私はsocial_user_fsがそのモジュールではグローバルであり、テストの外にそのモジュールからのものをインポートしていると思います。したがって、テストメソッド(およびデコレータ)を呼び出す前に処理されます。

SocialUserProfiletest_saveの中にインポートすると、私はそれが最高のソウルションになると思います。

関連する問題