Reputation: 1084
Say I have a model containing a FileField
and I have just created an object, and hence have a file stored to a particular location. How would I assert the file is stored in there?
For my case I have MEDIA_ROOT='/path/to/my/files/'
in my settings.py
and indeed the files do get stored there but I want to have it "officially" tested and verified.
What I now have is;
uploaded_file = settings.MEDIA_ROOT+'/test_audio.mp3'
assert os.path.exists(uploaded_file)
The problem here is that as Django doesn't delete the stored file from the location as with the case of models after testing, the file get stored with a name change after the first test. I'll have to manually delete the file before every test. Below is my test with the present assert statement
class TestAudioFileManagement(TestCase):
def test_audio_upload(self):
"""
Tests uploading of audio
"""
User.objects.create_user(username="somename",
password="somepassword")
self.client.login(username="somename",
password="somepassword")
name = "somename"
audio_file = SimpleUploadedFile(
'test_audio.mp3',
open('/home/afzalsh/works/openradio/test_files/test_audio.mp3','rb').read(),
content_type='audio'
)
self.client.post(reverse('actual_upload_audio'),
{'name':name,
'audio_file':audio_file})
uploaded_file = settings.MEDIA_ROOT+'/test_audio.mp3'
assert os.path.exists(uploaded_file)
A solution anyone?
Upvotes: 0
Views: 97
Reputation: 1619
You can remove it at the end of the test with os.remove(uploaded_file)
Upvotes: 1