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)
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)
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)