def test_no_detection_for_non_transifex(mock_find_files, mock_read, mock_push_strings):
    """No strings should be detected if a format other than Transifex Native
    is used in Python files and templates.
    """
    mock_find_files.return_value = [
        # 2 files with valid extension but no translatable content
        TranslatableFile('dir4/dir5', 'empty.py', 'locdir1'),
        TranslatableFile('dir4/dir5', 'empty.txt', 'locdir1'),
    ]
    mock_read.side_effect = [
        # empty.py - shouldn't detect any strings as non-transifex
        PYTHON_TEMPLATE.format(
            _import='from django.utils.translation import ugettext_lazy as _',
            call1='_',
            call2='_',
            string1=u'A Django string',
            string2=u'Another Django string',
        ),
        # empty.txt - shouldn't detect any strings as non-transifex
        (
            u'{% load i18n %}\n'
            u'{% trans "A Django string %}\n'
            u'{% blocktrans %}Another Django string{% endblocktrans %}',
        ),
    ]
    command = get_transifex_command()
    call_command(command, 'push', domain='djangojs')
    # command.string_collection.strings is like: {<key>: <SourceString>}
    found = command.subcommands['push'].string_collection.strings.values()
    assert set(found) == set([])
def test_dry_run(mock_find_files, mock_read, mock_push_strings):
    mock_push_strings.return_value = 200, {'doesnt': 'matter'}
    mock_find_files.return_value = [
        TranslatableFile('dir1', '1.py', 'locdir1'),
    ]
    mock_read.side_effect = PYTHON_FILES

    command = get_transifex_command()
    call_command(command, 'push', dry_run=1)
    assert not mock_push_strings.called
def test_python_parsing_no_strings(mock_extract, mock_find_files):
    mock_extract.return_value = []
    mock_find_files.return_value = [
        TranslatableFile('dir1', '1.py', 'locdir1'),
        TranslatableFile('dir1/dir2', '2.py', 'locdir1'),
        TranslatableFile('dir1/dir3', '3.py', 'locdir1'),
    ]

    command = get_transifex_command()
    call_command(command, 'push')
    # command.string_collection.strings is like: {<key>: <SourceString>}
    found = command.subcommands['push'].string_collection.strings.values()
    assert set(found) == set([])
def test_python_parsing_raises_unicode_error(mock_read, mock_find_files):
    o = b'\x00\x00'
    mock_read.side_effect = UnicodeDecodeError(
        'funnycodec', o, 1, 2, 'This is just a fake reason!')
    mock_find_files.return_value = [
        TranslatableFile('dir1', '1.py', 'locdir1'),
    ]

    command = get_transifex_command()
    call_command(command, 'push')
    # command.string_collection.strings is like: {<key>: <SourceString>}
    found = command.subcommands['push'].string_collection.strings.values()
    assert set(found) == set([])
def run_and_compare(expected, **kwargs):
    """Run the command and compare the detected strings with the expected ones.

    Any given kwarg is passed as an argument to the command.
    For example `run_and_compare([...], append_tags=u'extra1,extra2')
    is equivalent to running with `--append_tags=extra1,extra2`.

    :param list expected: a list of SourceString objects
    """
    command = get_transifex_command()
    call_command(command, 'push', **kwargs)
    # command.string_collection.strings is like: {<key>: <SourceString>}
    found = command.subcommands['push'].string_collection.strings.values()
    assert set(found) == set(expected)
def test_purge_cache_success(mock_echo, mock_invalidate_cache):
    mock_invalidate_cache.return_value = 200, {
        'data': {
            'count': 5,
        }
    }

    expected = Color.format('[green]\nSuccessfully purged CDS cache.[end]\n'
                            '[high]Status:[end] [warn]{code}[end]\n'
                            '[high]Records purged: {count}[end]\n'.format(
                                code=200,
                                count=5,
                            ))

    # Make sure it's idempotent
    mock_echo.reset_mock()
    command = get_transifex_command()
    call_command(command, 'invalidate', purge=True)
    actual = Color.format(mock_echo.call_args_list[1][0][0])
    assert expected == actual
def test_invalidate_cache_fail(mock_echo, mock_invalidate_cache):
    mock_invalidate_cache.return_value = 500, {
        'message': 'error message',
        'details': 'error details',
    }

    expected = Color.format(
        '[error]\nCould not invalidate CDS.[end]\n'
        '[high]Status:[end] [warn]{code}[end]\n'
        '[high]Message:[end] [warn]{message}[end]\n'.format(
            code=500,
            message='error message',
        ))

    # Make sure it's idempotent
    mock_echo.reset_mock()
    command = get_transifex_command()
    call_command(command, 'invalidate')
    actual = Color.format(mock_echo.call_args_list[1][0][0])
    assert expected == actual
def test_invalidate_cache_success(mock_echo, mock_invalidate_cache):
    mock_invalidate_cache.return_value = 200, {
        'data': {
            'count': 5,
        },
    }

    expected = Color.format(
        '[green]\nSuccessfully invalidated CDS cache.[end]\n'
        '[high]Status:[end] [warn]{code}[end]\n'
        '[high]Records invalidated: {count}[end]\n'
        '[high]Note: It might take a few minutes for '
        'fresh content to be available\n'.format(
            code=200,
            count=5,
        ))

    # Make sure it's idempotent
    mock_echo.reset_mock()
    command = get_transifex_command()
    call_command(command, 'invalidate')
    actual = Color.format(mock_echo.call_args_list[1][0][0])
    assert expected == actual