Ejemplo n.º 1
0
 def dump(self):
     dump_product(
         api_url=self.config.active.endpoint,
         api_key=self.config.active.api_key,
         product_id=self.product_id,
         output_path=self.fs.root_path,
         output_file='',
         silent=self.config.silent,
     )
Ejemplo n.º 2
0
def test_export_product_not_exists(fs, mocked_responses):
    mocked_responses.add(
        method='GET',
        url='https://localhost/public/v1/products/PRD-0000',
        status=404,
    )
    with pytest.raises(ClickException) as e:
        dump_product(
            api_url='https://localhost/public/v1',
            api_key='ApiKey SU111:1111',
            product_id='PRD-0000',
            output_path=fs.root_path,
            output_file='output.xlsx',
            silent=True,
        )

    assert str(e.value) == '404 - Not Found: Product PRD-0000 not found.'
Ejemplo n.º 3
0
def cmd_dump_products(config, product_id, output_file, output_path):
    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',
        )
    outfile = dump_product(
        config.active.endpoint,
        config.active.api_key,
        product_id,
        output_file,
        config.silent,
        config.verbose,
        output_path,
    )
    if not config.silent:
        click.secho(
            f'\nThe product {product_id} has been successfully exported to {outfile}.',
            fg='green',
        )
Ejemplo n.º 4
0
def test_export_product(
    fs,
    mocked_responses,
    mocked_product_response,
    mocked_categories_response,
    mocked_media_response,
    mocked_templates_response,
    mocked_items_response,
    mocked_ordering_params_response,
    mocked_fulfillment_params_response,
    mocked_configuration_params_response,
    mocked_actions_response,
    mocked_configurations_response,
    mocked_locales_response,
    mocked_primary_translation_response,
    mocked_translation_attributes_xlsx_response,
    sample_product_workbook,
):
    mocked_responses.add(
        method='GET',
        url='https://localhost/public/v1/products/PRD-276-377-545',
        json=mocked_product_response,
    )
    mocked_responses.add(
        method='GET',
        url=
        'https://localhost/media/VA-392-495/PRD-276-377-545/media/PRD-276-377-545'
        '-logo_aJD74iQ.png',
        body=open('./tests/fixtures/image.png', 'rb').read(),
        status=200,
    )
    mocked_responses.add(
        method='GET',
        url=
        'https://localhost/media/VA-392-495/PRD-276-377-545/media/media.png',
        body=open('./tests/fixtures/image.png', 'rb').read(),
        status=200,
    )
    mocked_responses.add(
        method='GET',
        url=
        'https://localhost/media/VA-392-495/PRD-276-377-545/media/media_jxr1ifH.png',
        body=open('./tests/fixtures/image.png', 'rb').read(),
        status=200,
    )
    mocked_responses.add(
        method='GET',
        url=
        'https://localhost/media/VA-392-495/PRD-276-377-545/media/media_cqhgp78.png',
        body=open('./tests/fixtures/image.png', 'rb').read(),
        status=200,
    )
    mocked_responses.add(
        method='GET',
        url='https://localhost/public/v1/categories',
        json=mocked_categories_response,
    )
    mocked_responses.add(
        method='GET',
        url='https://localhost/public/v1/products/PRD-276-377-545/media',
        json=mocked_media_response,
        headers={
            'Content-Range': 'items 0-2/3',
        },
    )
    mocked_responses.add(
        method='GET',
        url='https://localhost/public/v1/products/PRD-276-377-545/templates',
        json=mocked_templates_response,
        headers={
            'Content-Range': 'items 0-5/6',
        },
    )
    mocked_responses.add(
        method='GET',
        url='https://localhost/public/v1/products/PRD-276-377-545/items',
        json=mocked_items_response,
        headers={
            'Content-Range': 'items 0-17/18',
        },
    )
    ordering_query = re.compile(
        r'https:\/\/localhost\/public\/v1\/products\/PRD-276-377-545\/parameters\?eq\(phase,'
        r'ordering\)&limit\=(100|[1-9]?[0-9])\&offset\=0', )
    fulfillment_query = re.compile(
        r'https:\/\/localhost\/public\/v1\/products\/PRD-276-377-545\/parameters\?eq\(phase,'
        r'fulfillment\)&limit\=(100|[1-9]?[0-9])\&offset\=0', )
    configuration_query = re.compile(
        r'https:\/\/localhost\/public\/v1\/products\/PRD-276-377-545\/parameters\?eq\(phase,'
        r'configuration\)&limit\=(100|[1-9]?[0-9])\&offset\=0', )
    translation_query = re.compile(
        r'https:\/\/localhost\/public\/v1\/localization\/translations\?eq\(context.instance_id,'
        r'PRD-276-377-545\)&limit\=(100|[1-9]?[0-9])\&offset\=0', )
    translation_query2 = re.compile(
        r'https:\/\/localhost\/public\/v1\/localization\/translations\?and\(eq\(context.instance_id,'
        r'PRD-276-377-545\),eq\(primary,true\)\)&limit\=(100|[1-9]?[0-9])\&offset\=0',
    )
    mocked_responses.add(
        method='GET',
        url=ordering_query,
        json=mocked_ordering_params_response,
        headers={
            'Content-Range': 'items 0-11/12',
        },
    )
    mocked_responses.add(
        method='GET',
        url=fulfillment_query,
        json=mocked_fulfillment_params_response,
        headers={
            'Content-Range': 'items 0-1/2',
        },
    )
    mocked_responses.add(
        method='GET',
        url=configuration_query,
        json=mocked_configuration_params_response,
        headers={
            'Content-Range': 'items 0-0/1',
        },
    )
    mocked_responses.add(
        method='GET',
        url='https://localhost/public/v1/products/PRD-276-377-545/actions',
        json=mocked_actions_response,
        headers={
            'Content-Range': 'items 0-1/2',
        },
    )
    mocked_responses.add(
        method='GET',
        url=
        'https://localhost/public/v1/products/PRD-276-377-545/configurations',
        json=mocked_configurations_response,
        headers={
            'Content-Range': 'items 0-17/18',
        },
    )
    mocked_responses.add(
        method='GET',
        url='https://localhost/public/v1/localization/locales',
        json=mocked_locales_response,
    )
    mocked_responses.add(
        method='GET',
        url=
        ('https://localhost/public/v1/localization/translations?'
         'and(eq(context.instance_id,PRD-457-715-047),eq(primary,true))&limit=100&offset=0'
         ),
        json=mocked_primary_translation_response,
    )
    mocked_responses.add(
        method='GET',
        url=translation_query,
        json=mocked_primary_translation_response,
        headers={
            'Content-Range': 'items 0-0/1',
        },
    )
    mocked_responses.add(
        method='GET',
        url=translation_query2,
        json=mocked_primary_translation_response,
        headers={
            'Content-Range': 'items 0-0/1',
        },
    )
    mocked_responses.add(
        method='GET',
        url=
        'https://localhost/public/v1/localization/translations/TRN-1079-0833-9890/attributes',
        body=mocked_translation_attributes_xlsx_response,
        headers={
            'Contet-Type':
            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        },
    )
    output_file = dump_product(
        api_url='https://localhost/public/v1',
        api_key='ApiKey SU111:1111',
        product_id='PRD-276-377-545',
        output_file='output.xlsx',
        output_path=fs.root_path,
        silent=True,
    )

    product_wb = load_workbook(output_file)
    for name in sample_product_workbook.sheetnames:
        assert name in product_wb.sheetnames

    ignore_sheet_cells = {
        'General Information': ['B7'],
    }
    for sheet in sample_product_workbook.sheetnames:
        sample_sheet = sample_product_workbook[sheet]
        product_sheet = product_wb[sheet]
        letter_limit = _get_col_limit_by_type(sheet)

        for col in string.ascii_uppercase:
            if col == letter_limit:
                break
            for row in range(1, sample_sheet.max_row + 1):
                cell = f'{col}{row}'
                if cell not in ignore_sheet_cells.get(sheet, []):
                    assert product_sheet[cell].value == sample_sheet[
                        cell].value
Ejemplo n.º 5
0
def test_export_product(
    fs,
    mocked_responses,
    mocked_product_response,
    mocked_categories_response,
    mocked_media_response,
    mocked_templates_response,
    mocked_items_response,
    mocked_ordering_params_response,
    mocked_fulfillment_params_response,
    mocked_configuration_params_response,
    mocked_actions_response,
    mocked_configurations_response,
    sample_product_workbook,
):
    mocked_responses.add(
        method='GET',
        url='https://localhost/public/v1/products/PRD-276-377-545',
        json=mocked_product_response,
    )
    mocked_responses.add(
        method='GET',
        url=
        'https://localhost/media/VA-392-495/PRD-276-377-545/media/PRD-276-377-545'
        '-logo_aJD74iQ.png',
        body=open('./tests/fixtures/image.png', 'rb').read(),
        status=200,
    )
    mocked_responses.add(
        method='GET',
        url=
        'https://localhost/media/VA-392-495/PRD-276-377-545/media/media.png',
        body=open('./tests/fixtures/image.png', 'rb').read(),
        status=200,
    )
    mocked_responses.add(
        method='GET',
        url=
        'https://localhost/media/VA-392-495/PRD-276-377-545/media/media_jxr1ifH.png',
        body=open('./tests/fixtures/image.png', 'rb').read(),
        status=200,
    )
    mocked_responses.add(
        method='GET',
        url=
        'https://localhost/media/VA-392-495/PRD-276-377-545/media/media_cqhgp78.png',
        body=open('./tests/fixtures/image.png', 'rb').read(),
        status=200,
    )
    mocked_responses.add(
        method='GET',
        url='https://localhost/public/v1/categories',
        json=mocked_categories_response,
    )
    mocked_responses.add(
        method='GET',
        url='https://localhost/public/v1/products/PRD-276-377-545/media',
        json=mocked_media_response,
        headers={
            'Content-Range': 'items 0-2/3',
        },
    )
    mocked_responses.add(
        method='GET',
        url='https://localhost/public/v1/products/PRD-276-377-545/templates',
        json=mocked_templates_response,
        headers={
            'Content-Range': 'items 0-5/6',
        },
    )
    mocked_responses.add(
        method='GET',
        url='https://localhost/public/v1/products/PRD-276-377-545/items',
        json=mocked_items_response,
        headers={
            'Content-Range': 'items 0-17/18',
        },
    )
    ordering_query = re.compile(
        r'https:\/\/localhost\/public\/v1\/products\/PRD-276-377-545\/parameters\?eq\(phase,'
        r'ordering\)&limit\=(100|[1-9]?[0-9])\&offset\=0', )
    fulfillment_query = re.compile(
        r'https:\/\/localhost\/public\/v1\/products\/PRD-276-377-545\/parameters\?eq\(phase,'
        r'fulfillment\)&limit\=(100|[1-9]?[0-9])\&offset\=0', )
    configuration_query = re.compile(
        r'https:\/\/localhost\/public\/v1\/products\/PRD-276-377-545\/parameters\?eq\(phase,'
        r'configuration\)&limit\=(100|[1-9]?[0-9])\&offset\=0', )
    mocked_responses.add(
        method='GET',
        url=ordering_query,
        json=mocked_ordering_params_response,
        headers={
            'Content-Range': 'items 0-11/12',
        },
    )
    mocked_responses.add(
        method='GET',
        url=fulfillment_query,
        json=mocked_fulfillment_params_response,
        headers={
            'Content-Range': 'items 0-1/2',
        },
    )
    mocked_responses.add(
        method='GET',
        url=configuration_query,
        json=mocked_configuration_params_response,
        headers={
            'Content-Range': 'items 0-0/1',
        },
    )
    mocked_responses.add(
        method='GET',
        url='https://localhost/public/v1/products/PRD-276-377-545/actions',
        json=mocked_actions_response,
        headers={
            'Content-Range': 'items 0-1/2',
        },
    )
    mocked_responses.add(
        method='GET',
        url=
        'https://localhost/public/v1/products/PRD-276-377-545/configurations',
        json=mocked_configurations_response,
        headers={
            'Content-Range': 'items 0-17/18',
        },
    )
    output_file = dump_product(
        api_url='https://localhost/public/v1',
        api_key='ApiKey SU111:1111',
        product_id='PRD-276-377-545',
        output_file='output.xlsx',
        output_path=fs.root_path,
        silent=True,
    )

    product_wb = load_workbook(output_file)
    for name in sample_product_workbook.sheetnames:
        assert name in product_wb.sheetnames
    for sheet in sample_product_workbook.sheetnames:
        sample_sheet = sample_product_workbook[sheet]
        product_sheet = product_wb[sheet]
        row_idx = 1
        letter_limit = _get_col_limit_by_type(sheet)

        def letter(c):
            return chr(ord('A') + c)

        letter_idx = 0
        while row_idx <= sample_sheet.max_row:
            while letter(letter_idx) != letter_limit:
                expected = sample_sheet[f'{letter(letter_idx)}{row_idx}'].value
                assert product_sheet[
                    f'{letter(letter_idx)}{row_idx}'].value == expected
                letter_idx = letter_idx + 1
            letter_idx = 0
            row_idx = row_idx + 1