def test_invalid_file_open(fs):
    fs.create('fake.xlsx')
    client = get_client()
    synchronizer = TranslationSynchronizer(account_id='VA-063-000',
                                           client=client,
                                           silent=True)

    with pytest.raises(click.ClickException) as e:
        synchronizer.open(f'{fs.root_path}/fake.xlsx')

    assert str(e.value).endswith("is not a valid xlsx file.")
def test_invalid_fileformat_open(fs):
    fs.create('fake.xxx')
    client = get_client()
    synchronizer = TranslationSynchronizer(account_id='VA-063-000',
                                           client=client,
                                           silent=True)

    with pytest.raises(click.ClickException) as e:
        synchronizer.open(f'{fs.root_path}/fake.xxx')

    assert "openpyxl does not support .xxx file format" in str(e.value)
def test_sheet_not_found(fs, sample_translation_workbook):
    del sample_translation_workbook['General']
    sample_translation_workbook.save(f'{fs.root_path}/test.xlsx')
    client = get_client()
    synchronizer = TranslationSynchronizer(account_id='VA-063-000',
                                           client=client,
                                           silent=True)

    with pytest.raises(SheetNotFoundError) as e:
        synchronizer.open(f'{fs.root_path}/test.xlsx')

    assert str(e.value) == (
        "File does not contain worksheet 'General' to synchronize, skipping")
def test_sheet_validation(fs, row_to_invalidate, sample_translation_workbook):
    sample_translation_workbook['General'][
        f'A{row_to_invalidate}'].value = 'invalid'
    sample_translation_workbook.save(f'{fs.root_path}/test.xlsx')
    client = get_client()
    synchronizer = TranslationSynchronizer(account_id='VA-063-000',
                                           client=client,
                                           silent=True)

    with pytest.raises(click.ClickException) as e:
        synchronizer.open(f'{fs.root_path}/test.xlsx')

    assert str(e.value).startswith(f"A{row_to_invalidate} must be ")
    assert str(e.value).endswith(", but it is 'invalid'")
def test_update_ok(
    fs,
    mocked_responses,
    mocked_translation_response,
    sample_translation_workbook,
):
    sample_translation_workbook['General']['B8'].value = 'new description'
    sample_translation_workbook.save(f'{fs.root_path}/test.xlsx')
    mocked_responses.add(
        method='GET',
        url=f'{localization_base}/translations/TRN-8100-3865-4869',
        json=mocked_translation_response,
    )

    mocked_responses.add(
        method='PUT',
        url=f'{localization_base}/translations/TRN-8100-3865-4869',
        json=mocked_translation_response,
    )
    client = get_client()
    synchronizer = TranslationSynchronizer(account_id='VA-063-000',
                                           client=client,
                                           silent=True)
    synchronizer.open(f'{fs.root_path}/test.xlsx')

    synchronizer.sync(yes=False)

    assert synchronizer._mstats.get_counts_as_dict() == {
        'processed': 1,
        'created': 0,
        'updated': 1,
        'deleted': 0,
        'skipped': 0,
        'errors': 0,
    }
def test_change_context_instance_not_exists_fail(
    fs,
    mocked_responses,
    mocked_translation_response,
    sample_translation_workbook,
    mocker,
):
    mocker.patch('click.termui.visible_prompt_func', return_value='y')
    sample_translation_workbook['General']['B5'].value = ''
    sample_translation_workbook['General']['B6'].value = 'PRD-INEXISTENT'
    sample_translation_workbook.save(f'{fs.root_path}/test.xlsx')
    mocked_responses.add(
        method='GET',
        url=f'{localization_base}/translations/TRN-8100-3865-4869',
        json=mocked_translation_response,
    )
    mocked_responses.add(
        method='GET',
        url=
        f'{localization_base}/contexts?eq(instance_id,PRD-INEXISTENT)&limit=1&offset=0',
        headers={
            'Content-Range': 'items 0-0/0',
        },
        json=[],
    )
    client = get_client()
    synchronizer = TranslationSynchronizer(account_id='VA-063-000',
                                           client=client,
                                           silent=True)
    synchronizer.open(f'{fs.root_path}/test.xlsx')

    with pytest.raises(click.ClickException) as e:
        synchronizer.sync(yes=False)

    assert str(e.value) == "The Instance ID (PRD-INEXISTENT) doesn't exist"
def test_change_context_fail(
    fs,
    status,
    error_msg,
    mocked_responses,
    mocked_translation_response,
    sample_translation_workbook,
    mocker,
):
    mocker.patch('click.termui.visible_prompt_func', return_value='y')
    sample_translation_workbook['General']['B5'].value = 'LCX-TRIGGER-ERROR'
    sample_translation_workbook['General']['B6'].value = ''
    sample_translation_workbook.save(f'{fs.root_path}/test.xlsx')
    mocked_responses.add(
        method='GET',
        url=f'{localization_base}/translations/TRN-8100-3865-4869',
        json=mocked_translation_response,
    )
    mocked_responses.add(
        method='GET',
        url=f'{localization_base}/contexts/LCX-TRIGGER-ERROR',
        status=status,
    )
    client = get_client()
    synchronizer = TranslationSynchronizer(account_id='VA-063-000',
                                           client=client,
                                           silent=True)
    synchronizer.open(f'{fs.root_path}/test.xlsx')

    with pytest.raises(click.ClickException) as e:
        synchronizer.sync(yes=False)

    assert str(e.value) == error_msg
def test_create_fail(
    fs,
    mocked_responses,
    sample_translation_workbook,
    mocker,
):
    mocker.patch('click.termui.visible_prompt_func', return_value='y')
    sample_translation_workbook['General']['B1'].value = 'TRN-NON-EXISTENT'
    sample_translation_workbook.save(f'{fs.root_path}/test.xlsx')
    mocked_responses.add(
        method='GET',
        url=f'{localization_base}/translations/TRN-NON-EXISTENT',
        status=404,
    )
    mocked_responses.add(
        method='POST',
        url=f'{localization_base}/translations',
        status=500,
    )
    client = get_client()
    synchronizer = TranslationSynchronizer(account_id='VA-063-000',
                                           client=client,
                                           silent=True)
    synchronizer.open(f'{fs.root_path}/test.xlsx')

    synchronizer.sync(yes=False)

    assert synchronizer._mstats.get_counts_as_dict() == {
        'processed': 1,
        'created': 0,
        'updated': 0,
        'deleted': 0,
        'skipped': 0,
        'errors': 1,
    }
def test_create_asks_confirmation(
    fs,
    capsys,
    mocked_responses,
    mocked_translation_response,
    sample_translation_workbook,
    mocker,
):
    mocker.patch('click.termui.visible_prompt_func', return_value='y')
    sample_translation_workbook['General']['B1'].value = 'TRN-NON-EXISTENT'
    sample_translation_workbook.save(f'{fs.root_path}/test.xlsx')
    mocked_responses.add(
        method='GET',
        url=f'{localization_base}/translations/TRN-NON-EXISTENT',
        status=404,
    )

    mocked_responses.add(
        method='POST',
        url=f'{localization_base}/translations',
        json=mocked_translation_response,
    )
    client = get_client()
    synchronizer = TranslationSynchronizer(account_id='VA-063-000',
                                           client=client,
                                           silent=True)
    synchronizer.open(f'{fs.root_path}/test.xlsx')

    synchronizer.sync(yes=False)

    captured = capsys.readouterr()
    assert "A new translation will be created." in captured.out
    assert "Do you want to continue?" in captured.out
def test_change_context_instance_causes_creation(
    fs,
    mocked_responses,
    mocked_translation_response,
    sample_translation_workbook,
    mocker,
):
    mocker.patch('click.termui.visible_prompt_func', return_value='y')
    sample_translation_workbook['General']['B5'].value = ''
    sample_translation_workbook['General']['B6'].value = 'PRD-999-999-999'
    sample_translation_workbook.save(f'{fs.root_path}/test.xlsx')
    mocked_responses.add(
        method='GET',
        url=f'{localization_base}/translations/TRN-8100-3865-4869',
        json=mocked_translation_response,
    )
    mocked_responses.add(
        method='GET',
        url=
        f'{localization_base}/contexts?eq(instance_id,PRD-999-999-999)&limit=1&offset=0',
        headers={
            'Content-Range': 'items 0-0/1',
        },
        json=[{
            'id': 'LCX-9999-9999-9999',
            'instance_id': 'PRD-999-999-999',
            'name': 'another product',
        }],
    )
    mocked_responses.add(
        method='POST',
        url=f'{localization_base}/translations',
        json=mocked_translation_response,
    )
    client = get_client()
    synchronizer = TranslationSynchronizer(account_id='VA-063-000',
                                           client=client,
                                           silent=True)
    synchronizer.open(f'{fs.root_path}/test.xlsx')

    synchronizer.sync(yes=False)

    assert synchronizer._mstats.get_counts_as_dict() == {
        'processed': 1,
        'created': 1,
        'updated': 0,
        'deleted': 0,
        'skipped': 0,
        'errors': 0,
    }
示例#11
0
def cmd_sync_translation(config, input_file, yes):
    acc_id = config.active.id
    acc_name = config.active.name
    if not config.silent:
        click.secho(f'Current active account: {acc_id} - {acc_name}\n',
                    fg='blue')

    if '.xlsx' not in input_file:
        input_file = f'{input_file}/{input_file}.xlsx'

    client = ConnectClient(
        api_key=config.active.api_key,
        endpoint=config.active.endpoint,
        use_specs=False,
        max_retries=3,
        logger=RequestLogger() if config.verbose else None,
    )
    stats = SynchronizerStats()

    translation_sync = TranslationSynchronizer(client, config.silent, acc_id,
                                               stats)
    translation_sync.open(input_file)
    translation_id, should_wait_for_autotranslation = translation_sync.sync(
        yes)
    translation_sync.save(input_file)

    if translation_id:
        if should_wait_for_autotranslation:
            wait_for_autotranslation(client,
                                     translation_id,
                                     silent=config.silent)
        attributes_sync = TranslationAttributesSynchronizer(
            client, config.silent, stats)
        attributes_sync.open(input_file, 'Attributes')
        attributes_sync.sync(translation_id)
        attributes_sync.save(input_file)

    if not config.silent:
        stats.print()
def test_abort_create(fs, mocked_responses, sample_translation_workbook,
                      mocker):
    mocker.patch('click.termui.visible_prompt_func', return_value='n')
    sample_translation_workbook['General']['B1'].value = 'TRN-NON-EXISTENT'
    sample_translation_workbook.save(f'{fs.root_path}/test.xlsx')
    mocked_responses.add(
        method='GET',
        url=f'{localization_base}/translations/TRN-NON-EXISTENT',
        status=404,
    )

    client = get_client()
    synchronizer = TranslationSynchronizer(account_id='VA-063-000',
                                           client=client,
                                           silent=True)
    synchronizer.open(f'{fs.root_path}/test.xlsx')

    with pytest.raises(click.exceptions.Abort):
        synchronizer.sync(yes=False)
def test_get_translation_error_500(
    fs,
    mocked_responses,
    sample_translation_workbook,
):
    sample_translation_workbook.save(f'{fs.root_path}/test.xlsx')
    mocked_responses.add(
        method='GET',
        url=f'{localization_base}/translations/TRN-8100-3865-4869',
        status=500,
    )

    client = get_client()
    synchronizer = TranslationSynchronizer(account_id='VA-063-000',
                                           client=client,
                                           silent=True)
    synchronizer.open(f'{fs.root_path}/test.xlsx')
    with pytest.raises(click.ClickException) as e:
        synchronizer.sync(yes=False)

    assert "500 - Internal Server Error" in str(e.value)
def test_create_always_yes(
    fs,
    capsys,
    mocked_responses,
    mocked_translation_response,
    sample_translation_workbook,
):
    sample_translation_workbook['General']['B1'].value = 'TRN-NON-EXISTENT'
    sample_translation_workbook.save(f'{fs.root_path}/test.xlsx')
    mocked_responses.add(
        method='GET',
        url=f'{localization_base}/translations/TRN-NON-EXISTENT',
        status=404,
    )

    mocked_responses.add(
        method='POST',
        url=f'{localization_base}/translations',
        json=mocked_translation_response,
    )
    client = get_client()
    synchronizer = TranslationSynchronizer(account_id='VA-063-000',
                                           client=client,
                                           silent=True)
    synchronizer.open(f'{fs.root_path}/test.xlsx')

    synchronizer.sync(yes=True)

    assert synchronizer._mstats.get_counts_as_dict() == {
        'processed': 1,
        'created': 1,
        'updated': 0,
        'deleted': 0,
        'skipped': 0,
        'errors': 0,
    }
    captured = capsys.readouterr()
    assert "A new translation will be created." not in captured.out
    assert "Do you want to continue?" not in captured.out
def test_change_context_ambiguity_fail(
    fs,
    mocked_responses,
    mocked_translation_response,
    sample_translation_workbook,
    mocker,
):
    mocker.patch('click.termui.visible_prompt_func', return_value='y')
    sample_translation_workbook['General']['B5'].value = 'LCX-9999-9999-9999'
    sample_translation_workbook['General'][
        'B6'].value = 'PRD-DIFFERENT-PRODUCT'
    sample_translation_workbook.save(f'{fs.root_path}/test.xlsx')
    mocked_responses.add(
        method='GET',
        url=f'{localization_base}/translations/TRN-8100-3865-4869',
        json=mocked_translation_response,
    )
    mocked_responses.add(
        method='GET',
        url=f'{localization_base}/contexts/LCX-9999-9999-9999',
        json={
            'id': 'LCX-9999-9999-9999',
            'instance_id': 'PRD-999-999-999',
            'name': 'another product',
        },
    )
    client = get_client()
    synchronizer = TranslationSynchronizer(account_id='VA-063-000',
                                           client=client,
                                           silent=True)
    synchronizer.open(f'{fs.root_path}/test.xlsx')

    with pytest.raises(click.ClickException) as e:
        synchronizer.sync(yes=False)

    assert str(e.value) == (
        "The Instance ID (PRD-DIFFERENT-PRODUCT) doesn't correspond "
        "to the Context ID (LCX-9999-9999-9999)")
def test_different_account_causes_creation(
    fs,
    mocked_responses,
    mocked_translation_response,
    sample_translation_workbook,
    mocker,
):
    mocker.patch('click.termui.visible_prompt_func', return_value='y')
    sample_translation_workbook.save(f'{fs.root_path}/test.xlsx')
    mocked_responses.add(
        method='GET',
        url=f'{localization_base}/translations/TRN-8100-3865-4869',
        json=mocked_translation_response,
    )

    mocked_responses.add(
        method='POST',
        url=f'{localization_base}/translations',
        json=mocked_translation_response,
    )
    client = get_client()
    synchronizer = TranslationSynchronizer(account_id='VA-999-999',
                                           client=client,
                                           silent=True)
    synchronizer.open(f'{fs.root_path}/test.xlsx')

    synchronizer.sync(yes=False)

    assert synchronizer._mstats.get_counts_as_dict() == {
        'processed': 1,
        'created': 1,
        'updated': 0,
        'deleted': 0,
        'skipped': 0,
        'errors': 0,
    }