Example #1
0
def test_signals__encoding_skipped(monkeypatch, mocker,
                                   local_video: models.Video,
                                   video_format: models.Format) -> None:
    """
    Make sure encoding signal reports skipped, if file had been encoded before.
    """
    # encode only to one format
    encoding_format = tasks.settings.VIDEO_ENCODING_FORMATS['FFmpeg'][0]
    monkeypatch.setattr(tasks.settings, 'VIDEO_ENCODING_FORMATS',
                        {'FFmpeg': [encoding_format]})

    mocker.patch.object(tasks, '_encode')  # don't encode anything
    # encoding has already been done for the given format
    video_format.format = encoding_format["name"]
    video_format.save()

    listener = mocker.MagicMock()
    signals.format_started.connect(listener)
    signals.format_finished.connect(listener)

    tasks.convert_video(local_video.file)

    assert listener.call_count == 2
    # check arguments and make sure they are called in the right order
    # format started
    _, kwargs = listener.call_args_list[0]
    assert matches(kwargs, {'signal': signals.format_started, ...:...})

    # format finished, but skipped
    _, kwargs = listener.call_args_list[1]
    assert matches(
        kwargs,
        {
            'signal': signals.format_finished,
            'sender': models.Format,
            'instance': local_video,
            'format':...,
            'result': signals.ConversionResult.SKIPPED,
        },
    )
    assert isinstance(kwargs['format'], models.Format)
    assert kwargs['format'].format == encoding_format['name']
Example #2
0
def test_signals__encoding_failed(monkeypatch, mocker,
                                  local_video: models.Video) -> None:
    """
    Make sure encoding signal reports failed, if the encoding was not succesful.
    """
    # encode only to one format
    encoding_format = tasks.settings.VIDEO_ENCODING_FORMATS['FFmpeg'][0]
    monkeypatch.setattr(tasks.settings, 'VIDEO_ENCODING_FORMATS',
                        {'FFmpeg': [encoding_format]})

    mocker.patch.object(
        tasks, '_encode',
        side_effect=VideoEncodingError())  # encoding should fail

    listener = mocker.MagicMock()
    signals.format_started.connect(listener)
    signals.format_finished.connect(listener)

    tasks.convert_video(local_video.file)

    assert listener.call_count == 2
    # check arguments and make sure they are called in the right order
    # format started
    _, kwargs = listener.call_args_list[0]
    assert matches(kwargs, {'signal': signals.format_started, ...:...})

    # format finished, but failed
    _, kwargs = listener.call_args_list[1]
    assert matches(
        kwargs,
        {
            'signal': signals.format_finished,
            'sender': models.Format,
            'instance': local_video,
            'format':...,
            'result': signals.ConversionResult.FAILED,
        },
    )
    assert isinstance(kwargs['format'], models.Format)
    assert kwargs['format'].format == encoding_format['name']
Example #3
0
def test_sets():
    assert matches(set(), set())
    assert matches({1, 2, 3}, {1, ..., 3})
    assert matches({1, 2, 3}, {1, ...})
    assert matches({1, 2, 3}, {...})

    assert not matches({1, 2, 3}, set())
    assert not matches({1, 2, 3}, {0, 1, 2, ...})
Example #4
0
def test_multiple():
    assert matches([1, 3, 5], [..., ..., ..., 5])
    assert not matches([1, 3, 5], [..., ..., ..., 4])

    assert matches([1, 3, 5], [..., 3, ...])
    assert not matches([1, 3, 5], [..., 4, ...])

    assert matches([1, 2, 3, 4, 5], [..., 2, 3, 4, ...])
    assert not matches([1, 2, 3, 4, 5], [..., 2, 4, ...])

    assert matches([1, 3, 5], [..., ..., 3, ..., ...])
    assert not matches([1, 3, 5], [..., ..., 4, ..., ...])

    assert matches([1, 3, 5], [1, 3, ..., ...])
    assert not matches([1, 2, 5], [1, 3, ..., ...])

    assert matches([0, 1, 2, 4, 5, 6, 7, 8, 9], [0, 1, ..., 5, 6, 7, ..., 9])
    assert matches([0, 1, 2, 4, 5, 6, 7, 8, 9], [0, 1, ..., 5, 6, 7, ...])
    assert matches([0, 1, 2, 4, 5, 6, 7, 8, 9], [..., 5, 6, 7, ..., 9])
    assert not matches([0, 1, 2, 4, 5, 6, 7, 8, 9], [0, 1, ..., 5, 7, ..., 9])
    assert not matches([0, 1, 2, 4, 5, 6, 7, 8, 9], [0, 1, ..., 5, 7, ..., 8])
Example #5
0
def test_simple_negative():
    assert not matches([], [42])
    assert not matches([42], [])
    assert not matches({'a': 1}, {})
    assert not matches({}, {'a': 1})
    assert not matches(None, 42)
    assert not matches(42, None)
    assert not matches(False, True)
    assert not matches(True, False)
    assert not matches(42, 43)
    assert not matches('abc', 'abcd')

    assert not matches([1, 2, 3], [1, 2, 3, 4])
    assert not matches([1, 2, 3, 4], [1, 2, 3])
    assert not matches({'a': 1, 'b': 2}, {'a': 1, 'b': 42})
    assert not matches({'a': 1, 'c': 2}, {'a': 1, 'b': 2})
    assert not matches({'a': 1, 'b': 42}, {'a': 1, 'b': 2})

    assert not matches({'a': 1, 'b': 2}, {'a': 1, 'c':...})
    assert not matches({'a': 1, 'b': 2}, {'a': 2, ...:...})
    assert not matches({'a': 2, 'b': 2}, {'a': 1, ...:...})
    assert not matches({'a': 1, 'b': 2, 'c': 3}, {'a': 1, ...:..., 'c': 4})
    assert not matches({'a': 1, 'b': 2, 'c': 4}, {'a': 1, 'c': 3, ...:...})
    assert not matches({'a': 1, 'b': 2, 'c': 3}, {'a':..., 'c': 4, ...:...})

    assert not matches([1, 2, 3], [..., 2, 3, 4])
    assert not matches([1, 2, 3], [..., 2, 4, 3])
    assert not matches([1, 2, 3], [0, 1, 2, ...])
    assert not matches([1, 2, 3], [1, 3, 2, ...])
    assert not matches([1, 2, 3], [1, 2, ..., 4])
    assert not matches([1, 2, 3], [2, ..., 3])
    assert not matches([1, 2, 3], [1, ..., 4])
    assert not matches([1, 2, 3], [1, ..., 3, 4])
    assert not matches([1, 2, 3], [0, 1, ..., 3])
    assert not matches([1, 2, 4], [1, ..., 3])
    assert not matches([0, 2, 3], [1, ..., 3])

    assert not matches([1, 2, 3, 4, 5], [1, ..., 6])
    assert not matches([1, 2, 3, 4, 5], [1, ..., 5, 6])
    assert not matches([1, 2, 3, 4, 5], [1, 2, 4, ..., 4, 5])
    assert not matches([1, 2, 3, 4, 5], [1, 2, 4, ..., 5])
    assert not matches([1, 2, 3, 4, 5], [1, 2, 3, 4, ..., 4, 5])
    assert not matches([1, 2, 3, 4, 5], [1, 2, 3, ..., 4, 4, 5])
Example #6
0
def test_simple():
    assert matches([], [])
    assert matches({}, {})
    assert matches(None, None)
    assert matches(False, False)
    assert matches(True, True)
    assert matches(42, 42)
    assert matches('abc', 'abc')

    assert matches([1, 2, 3], [1, 2, 3])
    assert matches({'a': 1, 'b': 2}, {'a': 1, 'b': 2})

    assert matches({'a': 1, 'b': 2}, {...:...})
    assert matches({'a': 1, 'b': 2}, {'a': 1, 'b':...})
    assert matches({'a': 1, 'b': 2}, {'a': 1, ...:...})
    assert matches({'a': 1, 'b': 2, 'c': 3}, {'a': 1, ...:...})
    assert matches({'a': 1, 'b': 2, 'c': 3}, {'a': 1, 'c': 3, ...:...})
    assert matches({'a': 1, 'b': 2, 'c': 3}, {'a':..., 'c': 3, ...:...})

    assert matches([1, 2, 3], [..., 2, 3])
    assert matches([1, 2, 3], [1, 2, ...])
    assert matches([1, 2, 3], [1, ..., 3])

    assert matches([1, 2, 3], [Any, 2, 3])
    assert matches([1, 2, 3], [1, 2, Any])
    assert matches([1, 2, 3], [1, Any, 3])

    assert matches([1, 2, 3, 4, 5], [1, ..., 5])
    assert matches([1, 2, 3, 4, 5], [1, 2, ..., 4, 5])
    assert matches([1, 2, 3, 4, 5], [1, 2, 3, ..., 4, 5])
Example #7
0
def test_nested():

    obj = {
        'id':
        42,
        'name':
        'John Doe',
        'email':
        '*****@*****.**',
        'posts': [{
            'id': 1,
            'text': 'some text',
        }, {
            'id': 13,
            'text': 'Lorem Ipsum...',
            'likes': [42, 142, 242],
        }]
    }

    assert matches(
        obj,
        {
            'name': 'John Doe',
            'posts': [...],
            ...:...
        },
    )

    assert obj == Partial({'name': 'John Doe', 'posts': [...], ...:...})

    assert obj == Partial({
        'name':
        'John Doe',
        'posts': [..., {
            ...:...,
            'likes': [..., 142, ...]
        }, ...],
        ...:
        ...
    })

    assert obj == Partial({
        'id':...,
        'name': 'John Doe',
        'email': Regex(r'\[email protected]'),
        'posts': [...],
    })

    assert obj == Partial({
        'id':...,
        'name': 'John Doe',
        'email': Regex(r'\[email protected]'),
        'posts': [{
            'id':...,
            'text': 'some text',
        }, ...],
    })

    assert obj == Partial({
        'id':
        42,
        ...:
        ...,
        'posts': [{
            'id': 1,
            ...:...
        }, {
            'text': Regex(r'Lorem \w+'),
            ...:...
        }],
    })

    assert [obj] == Partial([{'name': 'John Doe', 'posts': [...], ...:...}])

    assert [obj, {}] == Partial([{
        'name': 'John Doe',
        'posts': [...],
        ...:...
    }, ...])
Example #8
0
def test_signals(monkeypatch, mocker, local_video: models.Video) -> None:
    """
    Make sure encoding signals are send.

    There are currently 4 signals:
    - encoding_started
    - format_started
    - format_finished
    - encoding_finished
    """
    # encode only to one format
    encoding_format = tasks.settings.VIDEO_ENCODING_FORMATS['FFmpeg'][0]
    monkeypatch.setattr(tasks.settings, 'VIDEO_ENCODING_FORMATS',
                        {'FFmpeg': [encoding_format]})

    mocker.patch.object(tasks, '_encode')  # don't encode anything

    listener = mocker.MagicMock()
    signals.encoding_started.connect(listener)
    signals.format_started.connect(listener)
    signals.format_finished.connect(listener)
    signals.encoding_finished.connect(listener)

    tasks.convert_video(local_video.file)

    assert listener.call_count == 4
    # check arguments and make sure they are called in the right order
    # encoding_started
    _, kwargs = listener.call_args_list[0]
    assert kwargs == {
        'signal': signals.encoding_started,
        'sender': models.Video,
        'instance': local_video,
    }

    # format started
    _, kwargs = listener.call_args_list[1]
    assert matches(
        kwargs,
        {
            'signal': signals.format_started,
            'sender': models.Format,
            'instance': local_video,
            'format':...,
        },
    )
    assert isinstance(kwargs['format'], models.Format)
    assert kwargs['format'].format == encoding_format['name']
    assert kwargs['format'].progress == 0

    # format finished
    _, kwargs = listener.call_args_list[2]
    assert matches(
        kwargs,
        {
            'signal': signals.format_finished,
            'sender': models.Format,
            'instance': local_video,
            'format':...,
            'result': signals.ConversionResult.SUCCEEDED,
        },
    )
    assert isinstance(kwargs['format'], models.Format)
    assert kwargs['format'].format == encoding_format['name']

    # encoding finished
    _, kwargs = listener.call_args_list[3]
    assert kwargs == {
        'signal': signals.encoding_finished,
        'sender': models.Video,
        'instance': local_video,
    }