Esempio n. 1
0
class TestAspectRatioCalculations(TestCase):

    @parameterized.expand(
        [
            ([333, 333], "1:1"),
            ([333, 666], "1:2"),
            ([1366, 768], "16:9"),
            ([1360, 768], "16:9"),
            ([1920, 1080], "16:9"),
            ([2560, 1080], "21:9"),
            ([3440, 1440], "21:9"),
        ],
        name_func=make_parameterized_test_name_generator_for_scalar_values(['resolution', 'aspect']),
    )
    def test_effective_aspect_ratio(self, resolution, expected_aspect_ratio):
        aspect_ratio = formats.get_effective_aspect_ratio(resolution)
        self.assertEqual(aspect_ratio, expected_aspect_ratio)

    @parameterized.expand(
        [
            ([333, 666], "1:2"),
            ([1024, 768], "4:3"),
            ([1920, 1080], "16:9"),
            ([1280, 1024], "5:4"),
            ([1360, 768], "85:48"),
            ([1366, 768], "683:384"),
            ([2560, 1080], "64:27"),
            ([3440, 1440], "43:18"),
        ],
        name_func=make_parameterized_test_name_generator_for_scalar_values(['resolution', 'aspect']),
    )
    def test_calculate_aspect_ratio(self, resolution, expected_aspect_ratio):
        aspect_ratio = formats.calculate_aspect_ratio(resolution)
        self.assertEqual(aspect_ratio, expected_aspect_ratio)
    def test_should_return_a_function_that_generates_names(self):
        generator = make_parameterized_test_name_generator_for_scalar_values(
            ['a', 'b', 'c'])
        self.assertTrue(callable(generator))

        name = generator(dummy_function, 777, [['x', 'y', 'z']])
        self.assertEqual(name, "dummy_function_777_a_x_b_y_c_z")
    def test_should_handle_basic_scalar_types(self):
        generator = make_parameterized_test_name_generator_for_scalar_values(
            ['a', 'b', 'c', 'd', 'e', 'f'])
        name = generator(dummy_function, 777, [[
            3,
            5.0,
            'stuff',
            False,
            True,
            None,
        ]])

        self.assertEqual(
            name, "dummy_function_777_a_3_b_5.0_c_stuff_d_False_e_True_f_None")
Esempio n. 4
0
class TestSparseRange(TestCase):
    @parameterized.expand(
        [
            (set(), 0, False),
            ({1, 2, 3}, 1, True),
            ({1, 2, 3}, 3, True),
            ({1, 2, 3}, 5, False),
            ({-5, 0, 5}, -5, True),
            ({-5, 0, 5}, -4, False),
            ({(1, 5)}, 0, False),
            ({(1, 5)}, 1, True),
            ({(1, 5)}, 3, True),
            ({(1, 5)}, 5, True),
            ({(1, 5)}, 6, False),
            ({(1, 5), (10, 15)}, 3, True),
            ({(1, 5), (10, 15)}, 7, False),
            ({(1, 5), (10, 15)}, 12, True),
            ({1, (10, 15)}, 1, True),
            ({1, (10, 15)}, 3, False),
            ({1, (10, 15)}, 12, True),
            ({(10, None)}, 12, True),
            ({(10, None)}, 12000, True),
            ({(10, None)}, 9, False),
            ({(None, 10)}, 11, False),
            ({(None, 10)}, 9, True),
            ({(None, 10)}, -12000, True),
            ({(None, None)}, -12000, True),
            ({(None, None)}, 0, True),
            ({(None, None)}, 12000, True),
            ({(10, None), 12}, 10, True),
            ({(10, None), 12}, 11, True),
            ({(10, None), 12}, 12, True),
            ({1, 2, 3, (1, 3)}, 0, False),
            ({1, 2, 3, (1, 3)}, 1, True),
            ({1, 2, 3, (1, 3)}, 2, True),
            ({1, 2, 3, (1, 3)}, 3, True),
            ({1, 2, 3, (1, 3)}, 4, False),
        ],
        name_func=make_parameterized_test_name_generator_for_scalar_values(
            ['subrange', 'value', 'result']),
    )
    def test_contains(self, subranges, value, expected_result):
        self.assertEqual(
            utils.SparseRange(subranges).contains(value), expected_result)
    def test_should_not_fail_if_it_gets_a_collection_or_object(self):
        generator = make_parameterized_test_name_generator_for_scalar_values(
            ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])
        name = generator(dummy_function, 777, [[
            [],
            [1, 2, 3],
            (1, 2, 3),
            {
                'a': 1
            },
            {1, 2, 3},
            [[], (), {}, set()],
            [[1, 2], ([([{
                4: 5
            }], )], ), {}, set()],
            object(),
        ]])

        # It should not fail but the name does not have to be sensibly formatted.
        # We don't want to rely here on its exact structure.
        self.assertTrue(name.startswith("dummy_function_777_"))
Esempio n. 6
0
class TestFrameRate(TestCase):
    def test_should_have_a_default_for_divisor(self):
        self.assertEqual(frame_rate.FrameRate(10), (10, 1))
        self.assertEqual(frame_rate.FrameRate(10, 5), (10, 5))
        self.assertEqual(frame_rate.FrameRate(10, 6), (10, 6))

    @parameterized.expand(
        [
            (frame_rate.FrameRate(30), (30, 1)),
            (frame_rate.FrameRate('30'), ('30', 1)),
            (0, (0, 1)),
            (30, (30, 1)),
            (30.0, (30, 1)),
            ('30', (30, 1)),
            ('30/1', (30, 1)),
            ((30, 1), (30, 1)),
            ([30, 1], (30, 1)),
        ],
        name_func=make_parameterized_test_name_generator_for_scalar_values(
            ['input', 'output']),
    )
    def test_decode_should_parse_valid_representations(self, raw_value,
                                                       expected_tuple):
        decoded_frame_rate = frame_rate.FrameRate.decode(raw_value)
        self.assertIsInstance(decoded_frame_rate, frame_rate.FrameRate)
        self.assertEqual(decoded_frame_rate,
                         frame_rate.FrameRate(*expected_tuple))

    @parameterized.expand(
        [
            ('', ),
            ('abc', ),
            ('30.0'),
            ('30.1'),
            ('30/30/30', ),
            (-30, ),
            (30.1, ),
            ((), ),
            ([], ),
            ((30, 1, 30), ),
            ((30.1, 1), ),
            ({}, ),
            (set(), ),
        ],
        name_func=make_parameterized_test_name_generator_for_scalar_values(
            ['input']),
    )
    def test_decode_should_reject_invalid_representations(self, raw_value):
        with self.assertRaises(ValueError):
            frame_rate.FrameRate.decode(raw_value)

    @parameterized.expand(
        [
            (frame_rate.FrameRate(30), (30, 1)),
            ((30, 1), (30, 1)),
            ([30, 1], (30, 1)),
            ((0, ), (0, 1)),
            ((30, ), (30, 1)),
            ((30, 1), (30, 1)),
            ((60, 2), (60, 2)),
            ((62, 4), (62, 4)),
        ],
        name_func=make_parameterized_test_name_generator_for_scalar_values(
            ['input', 'output']),
    )
    def test_from_collection_should_parse_valid_representations(
            self, collection, expected_tuple):
        parsed_frame_rate = frame_rate.FrameRate.from_collection(collection)
        self.assertIsInstance(parsed_frame_rate, frame_rate.FrameRate)
        self.assertEqual(parsed_frame_rate,
                         frame_rate.FrameRate(*expected_tuple))

    @parameterized.expand(
        [
            ((), ),
            ([], ),
            ((30, 1, 30), ),
            ([30, 1, 30], ),
            ((30.0, 1), ),
            ((30.1, 1), ),
            ((30, '1'), ),
            ((30, 1.5), ),
            ((-1, ), ),
            ((10, -10), ),
            ((-10, 10), ),
            ((-10, -10), ),
            ((10, 0), ),
            (frame_rate.FrameRate('30'), ),
        ],
        name_func=make_parameterized_test_name_generator_for_scalar_values(
            ['input']),
    )
    def test_from_collection_should_reject_invalid_representations(
            self, collection):
        with self.assertRaises(ValueError):
            frame_rate.FrameRate.from_collection(collection)

    @parameterized.expand(
        [
            ('0', (0, 1)),
            ('30', (30, 1)),
            ('30/1', (30, 1)),
            ('60/2', (60, 2)),
            ('62/4', (62, 4)),
        ],
        name_func=make_parameterized_test_name_generator_for_scalar_values(
            ['input', 'output']),
    )
    def test_from_string_should_parse_valid_representations(
            self, string_value, expected_tuple):
        parsed_frame_rate = frame_rate.FrameRate.from_string(string_value)
        self.assertIsInstance(parsed_frame_rate, frame_rate.FrameRate)
        self.assertEqual(parsed_frame_rate,
                         frame_rate.FrameRate(*expected_tuple))

    @parameterized.expand(
        [
            ('', ),
            ('abc', ),
            ('29.5', ),
            ('30/1/2', ),
            ('30/', ),
            ('/1', ),
            ('30/1.5', ),
            ('-1', ),
            ('10/-10', ),
            ('-10/10', ),
            ('-10/-10', ),
        ],
        name_func=make_parameterized_test_name_generator_for_scalar_values(
            ['input']),
    )
    def test_from_string_should_reject_invalid_representations(
            self, string_value):
        with self.assertRaises(ValueError):
            frame_rate.FrameRate.from_string(string_value)

    @parameterized.expand(
        [
            ((0, 1), (0, 1)),
            ((30, 1), (30, 1)),
            ((5, 13), (5, 13)),
            ((60, 2), (30, 1)),
            ((62, 4), (31, 2)),
        ],
        name_func=make_parameterized_test_name_generator_for_scalar_values(
            ['input', 'output']),
    )
    def test_normalized_should_return_values_without_common_divisors(
            self, input_tuple, expected_tuple):
        normalized_frame_rate = frame_rate.FrameRate(*input_tuple).normalized()
        self.assertIsInstance(normalized_frame_rate, frame_rate.FrameRate)
        self.assertEqual(normalized_frame_rate,
                         frame_rate.FrameRate(*expected_tuple))

    def test_to_float(self):
        self.assertEqual(frame_rate.FrameRate(10).to_float(), 10.0)
        self.assertEqual(frame_rate.FrameRate(5, 2).to_float(), 2.5)
        self.assertEqual(frame_rate.FrameRate(10, 2).to_float(), 5.0)

    def test_should_have_unambiguous_string_representation(self):
        self.assertEqual(str(frame_rate.FrameRate(10)), "10/1")
        self.assertEqual(str(frame_rate.FrameRate(10, 5)), "10/5")
        self.assertEqual(str(frame_rate.FrameRate(10, 6)), "10/6")
Esempio n. 7
0
class TestQueryEncoderInfo(TestCase):
    def test_should_return_sample_rates(self):
        sample_ffmpeg_output = (
            'Encoder libmp3lame [libmp3lame MP3 (MPEG audio layer 3)]:\n'
            'General capabilities: delay small\n'
            'Threading capabilities: none\n'
            'Supported sample rates: 44100 48000 32000 22050 24000 16000 11025 12000 8000\n'
            'Supported sample formats: s32p fltp s16p\n'
            'Supported channel layouts: mono stereo\n')
        expected_encoder_info = {
            'sample_rates':
            [44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000]
        }

        with mock.patch.object(commands,
                               'exec_cmd_to_string',
                               return_value=sample_ffmpeg_output):
            encoder_info = commands.query_encoder_info('mp3')

        self.assertEqual(encoder_info, expected_encoder_info)

    def test_sample_rates_field_should_be_omitted_if_not_found_in_ffmpeg_output(
            self):
        sample_ffmpeg_output = (
            'Encoder libx264[libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10]:\n'
            'General capabilities: delay threads\n'
            'Threading capabilities: auto\n')

        with mock.patch.object(commands,
                               'exec_cmd_to_string',
                               return_value=sample_ffmpeg_output):
            encoder_info = commands.query_encoder_info('h264')

        self.assertEqual(encoder_info, {})

    def test_should_raise_if_multiple_matches_found_in_ffmpeg_output(self):
        sample_ffmpeg_output = (
            'Encoder libmp3lame [libmp3lame MP3 (MPEG audio layer 3)]:\n'
            'Supported sample rates: 44100 48000 32000 22050 24000 16000 11025 12000 8000\n'
            'Supported sample rates: 22050 8000\n'
            'Supported sample formats: s32p fltp s16p\n')

        with mock.patch.object(commands,
                               'exec_cmd_to_string',
                               return_value=sample_ffmpeg_output):
            with self.assertRaises(exceptions.InvalidSampleRateInfo):
                commands.query_encoder_info('mp3')

    def test_encoder_not_recognized_by_ffmpeg_should_result_in_sample_rates_not_being_found(
            self):
        sample_ffmpeg_output = (
            "Codec 'invalid encoder' is not recognized by FFmpeg.\n")

        with mock.patch.object(commands,
                               'exec_cmd_to_string',
                               return_value=sample_ffmpeg_output):
            encoder_info = commands.query_encoder_info('invalid encoder')

        self.assertIsInstance(encoder_info, dict)
        self.assertNotIn('sample_rates', encoder_info)

    @parameterized.expand([
        ('Supported sample rates: 44100 48000 32000 22050 24000 16000 11025 12000 8000\n',
         ['44100 48000 32000 22050 24000 16000 11025 12000 8000']),
        ('Supported sample rates: 44100 48000 \n', ['44100 48000']),
        ('  Supported sample rates: 44100 48000 \n', ['44100 48000']),
        ('Supported sample rates: 44100 48000. something after \n',
         ['44100 48000. something after']),
        ('Supported sample rates: 44100 48000 Supported sample rates: 16000 24000 \n',
         ['44100 48000 Supported sample rates: 16000 24000']),
        ('Supported sample rates:\n', ['']),
        ('Supported sample rates: \n', ['']),
    ])
    def test_sample_rate_parsing_corner_cases(self, input_line,
                                              expected_result):
        sample_ffmpeg_output = ('some text before sample line \n'
                                f'{input_line}'
                                'some text after sample line \n')
        result = commands._parse_supported_sample_rates_out_of_encoder_info(
            sample_ffmpeg_output)
        self.assertEqual(result, expected_result)

    @parameterized.expand(
        [
            ('', []),
            ('44100 48000', [44100, 48000]),
            ('44100 -48000 0', [44100, -48000, 0]),
            ('44100 44100 44100', [44100, 44100, 44100]),
            ('4410044100441004410044100441004410044100',
             [4410044100441004410044100441004410044100]),
        ],
        name_func=make_parameterized_test_name_generator_for_scalar_values(
            ['input', 'output']),
    )
    def test_should_not_raise_if_sample_rates_are_integers(
            self, input_line, expected_rates):
        sample_ffmpeg_output = ('some text before sample line \n'
                                f'Supported sample rates: {input_line} \n'
                                'some text after sample line \n')

        with mock.patch.object(commands,
                               'exec_cmd_to_string',
                               return_value=sample_ffmpeg_output):
            encoder_info = commands.query_encoder_info('mp3')

        self.assertEqual(encoder_info, {'sample_rates': expected_rates})

    @parameterized.expand(
        [
            ('mp3', ),
            ('x y z', ),
            ('forty four thousand', ),
            ('44100.0 48000.0', ),
            ('44100.33 48000.33', ),
            ('1.2.3', ),
            ('44100 48000. something after', ),
            ('44100 48000 Supported sample rates: 16000 24000', ),
        ],
        name_func=make_parameterized_test_name_generator_for_scalar_values(
            ['input']),
    )
    def test_should_raise_if_sample_rates_are_not_integers(self, input_line):
        sample_ffmpeg_output = ('some text before sample line \n'
                                f'Supported sample rates: {input_line} \n'
                                'some text after sample line \n')

        with mock.patch.object(commands,
                               'exec_cmd_to_string',
                               return_value=sample_ffmpeg_output):
            with self.assertRaises(exceptions.InvalidSampleRateInfo):
                commands.query_encoder_info('mp3')