Skip to content

Commit

Permalink
Send pricelist file to media
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzevaka committed Oct 6, 2023
1 parent 2438887 commit 02f3386
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 35 deletions.
63 changes: 32 additions & 31 deletions connect_ext_ppr/services/pricing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

from connect_ext_ppr.errors import PriceUpdateError
from connect_ext_ppr.client.exception import CBCClientError
from connect_ext_ppr.utils import execute_with_retry, create_dr_file_to_media
from connect_ext_ppr.utils import create_dr_file_to_media, execute_with_retry


PRICELIST_COLUMNS = {
'MPN': str,
Expand Down Expand Up @@ -52,30 +53,30 @@ def apply_pricelist_to_marketplace(
@param CBCService cbc_service:
@param Client connect_client:
@param MarketplaceConfiguration marketplace:
@param Logger logger:
@returns None
@raises ClientError, CBCClientError
"""
logger.info(
'Pricelist uploading %s_%s_%s: started.',
task_ref = '{0}_{1}_{2}'.format(
deployment_request.id,
marketplace.pricelist_id,
marketplace.marketplace,
marketplace.pricelist_id,
)

send_log = _get_send_log(logger, f'Pricelist uploading {task_ref}')

send_log('started')

reseller_id = _identify_reseller_id(
client=connect_client,
batch_id=marketplace.pricelist_id,
marketplace_id=marketplace.marketplace,
hub_id=deployment_request.deployment.hub_id,
)
logger.info(
'Pricelist uploading %s_%s_%s: reseller id %s.',
deployment_request.id,
marketplace.pricelist_id,
marketplace.marketplace,
reseller_id,
)

send_log(f'reseller id "{reseller_id}"')

excel_file, file_name, dataset = _prepare_file(
client=connect_client,
batch_id=marketplace.pricelist_id,
Expand All @@ -84,36 +85,24 @@ def apply_pricelist_to_marketplace(
client=connect_client,
account_id=deployment_request.deployment.account_id,
dr_id=deployment_request.id,
filename=file_name,
filename=f'{task_ref}.xlsx',
content=excel_file,
)
excel_file.seek(0)

logger.info(
'Pricelist uploading %s_%s_%s: filename "%s", dataset "%s".',
deployment_request.id,
marketplace.pricelist_id,
marketplace.marketplace,
file_name,
dataset,
)
send_log(f'filename "{file_name}", dataset "{dataset}".')

try:
data_id = _process_batch(
_process_batch(
cbc_service=cbc_service,
excel_file=excel_file,
file_name=file_name,
reseller_id=reseller_id,
deployment=deployment_request.deployment,
dataset=dataset,
logger=logger,
)
logger.info(
'Pricelist uploading %s_%s_%s: data_id "%s".',
deployment_request.id,
marketplace.pricelist_id,
marketplace.marketplace,
data_id,
send_log=send_log,
)
send_log('finished')
finally:
excel_file.close()

Expand Down Expand Up @@ -353,7 +342,7 @@ def _process_batch(
reseller_id,
deployment,
dataset,
logger,
send_log,
):
excel_file.seek(0)

Expand All @@ -362,7 +351,8 @@ def _process_batch(
exception_class=CBCClientError,
args=(reseller_id, deployment.vendor_id, excel_file),
)
logger.info('Parsed price: %s', parsed_price)

send_log(f'parsed price: "{parsed_price}"')

data_id = parsed_price['dataId']

Expand All @@ -375,6 +365,8 @@ def _process_batch(
),
)

send_log('prise proposal sent')

execute_with_retry(
function=cbc_service.apply_prices,
exception_class=CBCClientError,
Expand All @@ -389,4 +381,13 @@ def _process_batch(
),
)

send_log(f'prise proposal applied, data id "{data_id}"')

return data_id


def _get_send_log(logger, prefix):
def f(message):
logger.info(f'${prefix}: {message}.')

return f
3 changes: 0 additions & 3 deletions connect_ext_ppr/tasks_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,6 @@ def apply_ppr_and_delegate_to_marketplaces(
:rtype bool
:raises TaskException
"""

return True

dr_marketplaces = db.query(MarketplaceConfiguration).filter_by(
active=True,
deployment_request_id=deployment_request.id,
Expand Down
21 changes: 20 additions & 1 deletion tests/services/test_pricing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from copy import deepcopy
from tempfile import NamedTemporaryFile
from unittest import TestCase
from unittest.mock import patch
from unittest.mock import ANY, patch

import openpyxl
import pytest
Expand Down Expand Up @@ -185,6 +185,7 @@ def test_validate_pricelist_negative_invalid_effective_date(mock_fetch_file):

def test_apply_pricelist_to_marketplace_positive(
mocker,
logger,
marketplace,
cbc_service,
batch_output_file,
Expand Down Expand Up @@ -222,11 +223,16 @@ def test_apply_pricelist_to_marketplace_positive(
return_value=None,
)

create_dr_file_to_media_mock = mocker.patch(
'connect_ext_ppr.services.pricing.create_dr_file_to_media',
)

apply_pricelist_to_marketplace(
dep_req,
cbc_service,
connect_client,
mp_conf,
logger,
)

reseller_id_mock.assert_called_once_with(
Expand All @@ -251,10 +257,19 @@ def test_apply_pricelist_to_marketplace_positive(
'msrp': True,
'effective_date': '07/26/2023',
},
send_log=ANY,
)

assert price_excel_file

create_dr_file_to_media_mock.assert_called_once_with(
client=ANY,
account_id=dep_req.deployment.account_id,
dr_id=dep_req.id,
filename=f'{dep_req.id}_MP-123_None.xlsx',
content=ANY,
)


def test_identify_marketplaces_positive(
connect_client,
Expand Down Expand Up @@ -582,6 +597,7 @@ def test_process_batch_positive(
hub_credentials,
no_db_deployment,
batch_dataset,
logger,
):
reseller_id = '10000001'

Expand All @@ -601,6 +617,7 @@ def test_process_batch_positive(
reseller_id,
no_db_deployment,
batch_dataset,
logger,
)

assert data_id == parse_price_file_response['dataId']
Expand Down Expand Up @@ -657,6 +674,8 @@ def test_process_batch_positive(
'MFL-0000-0000-0000.xlsx',
)

assert logger.call_count == 3


def test_determine_dataset_negative_effective_date_column_not_present():
wb = load_workbook(
Expand Down
6 changes: 6 additions & 0 deletions tests/test_tasks_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,7 @@ def test_apply_pricelist_task_ok(
marketplace_config_factory,
mocker,
dbsession,
logger,
):
apply_mock = mocker.patch(
'connect_ext_ppr.tasks_manager.apply_pricelist_to_marketplace',
Expand All @@ -1387,6 +1388,7 @@ def test_apply_pricelist_task_ok(
connect_client,
req_mp,
dbsession,
logger=logger,
)

assert apply_mock.called_once_with(
Expand All @@ -1407,6 +1409,7 @@ def test_apply_pricelist_task_ok_manual(
marketplace_config_factory,
mocker,
dbsession,
logger,
):
apply_mock = mocker.patch(
'connect_ext_ppr.tasks_manager.apply_pricelist_to_marketplace',
Expand All @@ -1431,6 +1434,7 @@ def test_apply_pricelist_task_ok_manual(
connect_client,
req_mp,
dbsession,
logger,
)

assert apply_mock.call_count == 0
Expand All @@ -1443,6 +1447,7 @@ def test_apply_pricelist_task_error(
marketplace_config_factory,
mocker,
dbsession,
logger,
):
mocker.patch(
'connect_ext_ppr.tasks_manager.apply_pricelist_to_marketplace',
Expand Down Expand Up @@ -1472,6 +1477,7 @@ def test_apply_pricelist_task_error(
connect_client,
req_mp,
dbsession,
logger,
)

assert str(e.value) == 'Error while processing pricelist: olala'
Expand Down

0 comments on commit 02f3386

Please sign in to comment.