Skip to content

Commit

Permalink
LITE-28124 Add parameters dependencies support for product operations
Browse files Browse the repository at this point in the history
  • Loading branch information
bdjilka committed Aug 12, 2023
1 parent 114a6db commit d1ff39c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
57 changes: 57 additions & 0 deletions connect/cli/plugins/product/sync/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def __init__(self, client, progress, stats):
self._worksheet_name = None
self.__stats = stats
self._mstats = None
self._id_mapping = {}
self._param_deps = {}
super(ParamsSynchronizer, self).__init__(client, progress)

def open(self, input_file, worksheet):
Expand Down Expand Up @@ -69,6 +71,7 @@ def sync(self): # noqa: CCR001
continue

param_payload = {}
dependency = None
if data.json_properties:
param_payload = json.loads(data.json_properties)
param_payload['name'] = data.id
Expand All @@ -79,6 +82,9 @@ def sync(self): # noqa: CCR001
param_payload['type'] = data.type
if 'constraints' not in param_payload:
param_payload['constraints'] = {}
if 'dependency' in param_payload['constraints']:
dependency = param_payload['constraints']['dependency']
del param_payload['constraints']['dependency']
param_payload['constraints']['required'] = False if data.required == '-' else True
param_payload['constraints']['unique'] = False if data.unique == '-' else True
param_payload['constraints']['hidden'] = False if data.hidden == '-' else True
Expand All @@ -98,6 +104,9 @@ def sync(self): # noqa: CCR001
)
self._update_sheet_row(ws, row_idx, param)
self._mstats.updated()
self._id_mapping[data.id] = param['id']
if dependency:
self._param_deps[param['id']] = dependency
except Exception as e:
self._mstats.error(str(e), row_idx)

Expand All @@ -106,16 +115,24 @@ def sync(self): # noqa: CCR001
original_param = self._get_original_param(data)
if original_param:
self._updated_or_skipped(ws, row_idx, original_param, param_payload)
self._id_mapping[data.id] = original_param['id']
continue
param = self._client.products[self._product_id].parameters.create(
param_payload,
)
self._update_sheet_row(ws, row_idx, param)
self._mstats.created()
self._id_mapping[data.id] = param['id']
if dependency:
self._param_deps[param['id']] = dependency
except Exception as e:
self._mstats.error(str(e), row_idx)

self._progress.update(task, completed=ws.max_row - 1)

if self._param_type == 'ordering':
self._process_constraints_dependency(ws)

@staticmethod
def _update_sheet_row(ws, row_idx, param=None):
ws.cell(row_idx, 3, value='-')
Expand Down Expand Up @@ -244,3 +261,43 @@ def _updated_or_skipped(self, ws, row_idx, original, payload):
)
self._update_sheet_row(ws, row_idx, param)
self._mstats.updated()

def _process_constraints_dependency(self, ws):
task = self._progress.add_task(
'Processing param dependencies',
total=len(self._param_deps.keys()),
)
for row_idx in range(2, ws.max_row + 1):
data = _RowData(*[ws.cell(row_idx, col_idx).value for col_idx in range(1, 15)])
if data.verbose_id not in self._param_deps:
continue

self._progress.update(
task,
description=f'Processing param dependency {data.id}',
advance=1,
)

param_payload = {'constraints': json.loads(data.json_properties)['constraints']}
param_payload['constraints']['required'] = False if data.required == '-' else True
param_payload['constraints']['unique'] = False if data.unique == '-' else True
param_payload['constraints']['hidden'] = False if data.hidden == '-' else True

dependency = self._param_deps[data.verbose_id]
dependency['parameter']['id'] = self._id_mapping[dependency['parameter']['name']]
param_payload['constraints']['dependency'] = dependency

try:
param = (
self._client.products[self._product_id]
.parameters[data.verbose_id]
.update(
param_payload,
)
)
self._update_sheet_row(ws, row_idx, param)
self._mstats.updated()
except Exception as e:
self._mstats.error(str(e), row_idx)

self._progress.update(task, completed=len(self._param_deps.keys()))
Binary file modified tests/fixtures/params_sync.xlsx
Binary file not shown.
15 changes: 11 additions & 4 deletions tests/plugins/product/sync/test_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,9 +651,9 @@ def test_validate_update(
synchronizer.sync()

assert stats['Ordering Parameters'].get_counts_as_dict() == {
'processed': 1,
'processed': 2,
'created': 0,
'updated': 1,
'updated': 2,
'deleted': 0,
'skipped': 0,
'errors': 0,
Expand Down Expand Up @@ -695,16 +695,23 @@ def test_validate_create(
json=response,
)

mocked_responses.add(
method='PUT',
url='https://localhost/public/v1/products/PRD-276-377-545/parameters/PRM-276-377-545-0008',
json={},
status=400,
)

synchronizer.open(f'{fs.root_path}/test.xlsx', 'Ordering Parameters')
synchronizer.sync()

assert stats['Ordering Parameters'].get_counts_as_dict() == {
'processed': 1,
'processed': 2,
'created': 1,
'updated': 0,
'deleted': 0,
'skipped': 0,
'errors': 0,
'errors': 1,
}


Expand Down

0 comments on commit d1ff39c

Please sign in to comment.