Exemple #1
0
class TestMediaFilenameProperties(TransactionTestCase):
    def setUp(self):
        # Required to create root collection because the TransactionTestCase
        # does not make initial data loaded in migrations available and
        # serialized_rollback=True causes other problems in the test suite.
        # ref: https://docs.djangoproject.com/en/3.0/topics/testing/overview/#rollback-emulation
        Collection.objects.get_or_create(
            name="Root",
            path='0001',
            depth=1,
            numchild=0,
        )

        self.media = Media(title="Test media", duration=100)
        self.media.file.save('example.mp4',
                             ContentFile("A amazing example music video"))

        self.extensionless_media = Media(title="Test media", duration=101)
        self.extensionless_media.file.save(
            'example', ContentFile("A boring example music video"))

    def test_filename(self):
        self.assertEqual('example.mp4', self.media.filename)
        self.assertEqual('example', self.extensionless_media.filename)

    def test_file_extension(self):
        self.assertEqual('mp4', self.media.file_extension)
        self.assertEqual('', self.extensionless_media.file_extension)

    def tearDown(self):
        self.media.delete()
        self.extensionless_media.delete()
Exemple #2
0
 def test_sources_mp4_type(self):
     fake_file = ContentFile(b("A boring example movie"))
     fake_file.name = 'movie.mp4'
     media = Media()
     media.file = File(fake_file)
     self.assertEqual(media.sources, [{
         'src': '/media/movie.mp4',
         'type': 'video/mp4',
     }])
Exemple #3
0
 def test_sources_unknown_type(self):
     fake_file = ContentFile(b("A boring example movie"))
     fake_file.name = 'movie'
     media = Media()
     media.file = File(fake_file)
     self.assertEqual(media.sources, [{
         'src': '/media/movie',
         'type': 'application/octet-stream',
     }])
Exemple #4
0
 def test_sources_unknown_type(self):
     fake_file = ContentFile(b("A boring example movie"))
     fake_file.name = "movie"
     media = Media()
     media.file = File(fake_file)
     self.assertEqual(
         media.sources,
         [{
             "src": "/media/movie",
             "type": "application/octet-stream",
         }],
     )
Exemple #5
0
 def test_sources_mp4_type(self):
     fake_file = ContentFile(b("A boring example movie"))
     fake_file.name = "movie.mp4"
     media = Media()
     media.file = File(fake_file)
     self.assertEqual(
         media.sources,
         [{
             "src": "/media/movie.mp4",
             "type": "video/mp4",
         }],
     )
Exemple #6
0
    def setUp(self):
        # Required to create root collection because the TransactionTestCase
        # does not make initial data loaded in migrations available and
        # serialized_rollback=True causes other problems in the test suite.
        # ref: https://docs.djangoproject.com/en/3.0/topics/testing/overview/#rollback-emulation
        Collection.objects.get_or_create(
            name="Root",
            path="0001",
            depth=1,
            numchild=0,
        )

        self.media = Media(title="Test media", duration=100)
        self.media.file.save("example.mp4",
                             ContentFile("A amazing example music video"))

        self.extensionless_media = Media(title="Test media", duration=101)
        self.extensionless_media.file.save(
            "example", ContentFile("A boring example music video"))
Exemple #7
0
 def test_duration_display_as_tenths(self):
     template = Template('{{ media.duration|floatformat }}')
     for value, result in (
         (None, '0'),
         ('', '0'),
         (0, '0'),
         (0.1, '0.1'),
         (1, '1'),
         (1234567.7654321, '1234567.8'),
     ):
         fake_file = ContentFile(b("A boring example movie"))
         fake_file.name = 'movie.mp4'
         media = Media(
             title="Test media file",
             file=File(fake_file),
             type='video',
         )
         media.duration = value
         media.full_clean()
         media.save()
         media.refresh_from_db()
         actual = template.render(Context({'media': media}))
         self.assertEqual(actual, result)
Exemple #8
0
 def test_duration_display_as_tenths(self):
     template = Template("{{ media.duration|floatformat }}")
     for value, result in (
         (None, "0"),
         ("", "0"),
         (0, "0"),
         (0.1, "0.1"),
         (1, "1"),
         (1234567.7654321, "1234567.8"),
     ):
         fake_file = ContentFile(b("A boring example movie"))
         fake_file.name = "movie.mp4"
         media = Media(
             title="Test media file",
             file=File(fake_file),
             type="video",
         )
         media.duration = value
         media.full_clean()
         media.save()
         media.refresh_from_db()
         actual = template.render(Context({"media": media}))
         self.assertEqual(actual, result)
Exemple #9
0
    def test_duration_validation(self):
        # ensure duration is optional
        fake_file = ContentFile(b("A boring example movie"))
        fake_file.name = 'movie.mp4'
        media = Media(
            title="Test media file",
            file=File(fake_file),
            type='video',
        )
        media.full_clean()

        # ensure cannot be negative
        media.duration = -100
        with self.assertRaises(ValidationError):
            media.full_clean()

        # ensure empty values are valid
        for value in (0, 0.0, None, ''):
            with self.subTest(value=value):
                media.duration = value
                media.full_clean()
                self.assertEqual(media.duration, 0)

        # ensure fractional durations are preserved
        media.duration = 100.5
        media.full_clean()
        self.assertEqual(media.duration, 100.5)