Reputation: 5702
I want to find a minimal audio file (like the testfile_gif
below) for unit testing.
I don't want to load it from the hard drive (like here).
I want the second test to perform like the first one.
import magic
from django.core.files.uploadedfile import SimpleUploadedFile
class TestFiles(TestCase):
def test_working(self):
# Test an Image File #
testfile_gif = (
b'\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x00\x00\x00\x21\xf9\x04'
b'\x01\x0a\x00\x01\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02'
b'\x02\x4c\x01\x00\x3b')
gif_file = SimpleUploadedFile(name='image.gif', content=testfile_gif,
content_type='image/gif')
mime = magic.from_buffer(gif_file.read(1024), mime=True)
self.assertEqual('image/gif', mime)
def test_not_working(self):
# Test an Audio File #
testfile_audio = b'What should be written in here?'
audio_file = SimpleUploadedFile(name='music.mp3',
content=testfile_audio,
content_type='audio/mpeg')
mime = magic.from_buffer(audio_file.read(1024), mime=True)
self.assertEqual('audio/mpeg', mime)
Preferably, I don't want to use any packages (like import mock
).
UPDATE
Here is an mp3
file with audio/mpeg
mime:
b'MM\x00*\x00\x00\x00\x08\x00\x03\x01\x00\x00\x03\x00\x00\x00\x01\x00\x01'
b'\x00\x00\x01\x01\x00\x03\x00\x00\x00\x01\x00\x01\x00\x00\x01\x11\x00\x03'
b'\x00\x00\x00\x01\x00\x00\x00\x00'
Upvotes: 4
Views: 1526