Skip to content

Commit

Permalink
226091 Add create action for item, when product definition is updated
Browse files Browse the repository at this point in the history
  • Loading branch information
d3rky committed Jun 4, 2024
1 parent ec37aa9 commit e202c98
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 13 deletions.
46 changes: 40 additions & 6 deletions swo/mpt/cli/core/products/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
from swo.mpt.cli.core.errors import FileNotExistsError
from swo.mpt.cli.core.mpt.client import MPTClient
from swo.mpt.cli.core.mpt.flows import (
create_item,
create_item as mpt_create_item,
)
from swo.mpt.cli.core.mpt.flows import (
create_item_group,
create_parameter,
create_parameter_group,
Expand Down Expand Up @@ -277,7 +279,7 @@ def to_parameter_json(
return parameter_json


def to_item_create_json(
def to_item_sync_json(
product_id: str,
item_group_mapping: dict[str, ItemGroup],
item_parameters_id_mapping: dict[str, Parameter],
Expand All @@ -299,10 +301,11 @@ def to_item_create_json(
return _to_item_json(product_id, group.id, values, parameters)


def to_item_update_json(
def to_item_update_or_create_json(
product_id: str, values: list[SheetValue], is_operations: bool
) -> dict:
group_id = find_value_for(constants.ITEMS_GROUP_ID, values)[2]
# TODO: Add item parameter update
return _to_item_json(product_id, group_id, values, [], is_operations)


Expand Down Expand Up @@ -584,6 +587,9 @@ def update_items(
mpt_client, active_account, item_id, product_id, sheet_value
)
stats.add_synced(ws.title)
case ItemAction.CREATE:
create_item(mpt_client, active_account, product_id, ws, sheet_value)
stats.add_synced(ws.title)
case _:
stats.add_skipped(ws.title)
except Exception as e:
Expand All @@ -601,7 +607,7 @@ def update_item(
product_id: str,
sheet_value: list[SheetValue],
) -> None:
item_json = to_item_update_json(
item_json = to_item_update_or_create_json(
product_id, sheet_value, active_account.type == "Operations"
)
mpt_update_item(mpt_client, item_id, item_json)
Expand Down Expand Up @@ -762,9 +768,9 @@ def sync_items(
for sheet_value in values:
try:
sheet_value = setup_unit_of_measure(mpt_client, sheet_value)
item = create_item(
item = mpt_create_item(
mpt_client,
to_item_create_json(
to_item_sync_json(
product.id,
items_groups_mapping,
item_parameters_id_mapping,
Expand Down Expand Up @@ -801,6 +807,34 @@ def sync_items(
return ws, id_mapping


def create_item(
mpt_client: MPTClient,
active_account: Account,
product_id: str,
ws: Worksheet,
sheet_value: list[SheetValue],
) -> None:
sheet_value = setup_unit_of_measure(mpt_client, sheet_value)
item = mpt_create_item(
mpt_client,
to_item_update_or_create_json(
product_id,
sheet_value,
active_account.type == "Operations",
),
)

id_index, _, sheet_id_value = find_value_for(constants.ID_COLUMN_NAME, sheet_value)

# TODO: refactor, should be done out of this function
ws[id_index] = item.id

uom_id_index, _, sheet_id_value = find_value_for(
constants.ITEMS_UNIT_ID, sheet_value
)
ws[uom_id_index] = sheet_id_value


def setup_unit_of_measure(
mpt_client: MPTClient, values: list[SheetValue]
) -> list[SheetValue]:
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def mock_sync_product(
another_parameter,
],
)
mocker.patch("swo.mpt.cli.core.products.flows.create_item", return_value=item)
mocker.patch("swo.mpt.cli.core.products.flows.mpt_create_item", return_value=item)
mocker.patch("swo.mpt.cli.core.products.flows.search_uom_by_name", return_value=uom)
mocker.patch(
"swo.mpt.cli.core.products.flows.create_template", return_value=template
Expand Down
Binary file modified tests/product_files/PRD-1234-1234-1234-file-update.xlsx
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ def test_sync_item(
):
stats = ProductStatsCollector()
item_mock = mocker.patch(
"swo.mpt.cli.core.products.flows.create_item", return_value=item
"swo.mpt.cli.core.products.flows.mpt_create_item", return_value=item
)
mocker.patch("swo.mpt.cli.core.products.flows.search_uom_by_name", return_value=uom)

Expand Down Expand Up @@ -624,7 +624,7 @@ def test_sync_item_exception(
):
stats = ProductStatsCollector()
mocker.patch(
"swo.mpt.cli.core.products.flows.create_item",
"swo.mpt.cli.core.products.flows.mpt_create_item",
side_effect=MPTAPIError("Error", "Error"),
)
mocker.patch("swo.mpt.cli.core.products.flows.search_uom_by_name", return_value=uom)
Expand Down Expand Up @@ -783,7 +783,7 @@ def test_sync_product(
],
)
item_mock = mocker.patch(
"swo.mpt.cli.core.products.flows.create_item", return_value=item
"swo.mpt.cli.core.products.flows.mpt_create_item", return_value=item
)
mocker.patch("swo.mpt.cli.core.products.flows.search_uom_by_name", return_value=uom)
template_mock = mocker.patch(
Expand Down Expand Up @@ -874,7 +874,7 @@ def test_sync_product_extra_columns(
],
)
item_mock = mocker.patch(
"swo.mpt.cli.core.products.flows.create_item", return_value=item
"swo.mpt.cli.core.products.flows.mpt_create_item", return_value=item
)
mocker.patch("swo.mpt.cli.core.products.flows.search_uom_by_name", return_value=uom)
template_mock = mocker.patch(
Expand Down Expand Up @@ -953,7 +953,12 @@ def test_sync_product_exception(


def test_sync_product_update_product(
mocker, mpt_client, new_update_product_file, active_vendor_account
mocker,
mpt_client,
new_update_product_file,
active_vendor_account,
uom,
item,
):
review_mock = mocker.patch(
"swo.mpt.cli.core.products.flows.review_item",
Expand All @@ -971,6 +976,11 @@ def test_sync_product_update_product(
"swo.mpt.cli.core.products.flows.unpublish_item",
return_value=None,
)
create_mock = mocker.patch(
"swo.mpt.cli.core.products.flows.mpt_create_item",
return_value=item,
)
mocker.patch("swo.mpt.cli.core.products.flows.search_uom_by_name", return_value=uom)

stats = ProductStatsCollector()
stats, _ = sync_product_definition(
Expand Down Expand Up @@ -1000,11 +1010,30 @@ def test_sync_product_update_product(
"unit": {"id": "UNT-1916"},
},
)
create_mock.assert_called_once_with(
mpt_client,
{
"name": "Customer",
"description": "Description 6",
"group": {"id": "IGR-1213-3316-0002"},
"product": {"id": "PRD-1213-3316"},
"quantityNotApplicable": False,
"terms": {"commitment": "1y", "period": "1m"},
"unit": {"id": "UOM-1234-1234"},
"parameters": [],
"externalIds": {"vendor": "65AB123BASD2"},
},
)
assert stats.tabs[constants.TAB_ITEMS]["skipped"] == 1


def test_sync_product_update_product_operations(
mocker, mpt_client, new_update_product_file, active_operations_account
mocker,
mpt_client,
new_update_product_file,
active_operations_account,
uom,
item,
):
review_mock = mocker.patch(
"swo.mpt.cli.core.products.flows.review_item",
Expand All @@ -1018,6 +1047,15 @@ def test_sync_product_update_product_operations(
"swo.mpt.cli.core.products.flows.mpt_update_item",
return_value=None,
)
unpublish_mock = mocker.patch(
"swo.mpt.cli.core.products.flows.unpublish_item",
return_value=None,
)
create_mock = mocker.patch(
"swo.mpt.cli.core.products.flows.mpt_create_item",
return_value=item,
)
mocker.patch("swo.mpt.cli.core.products.flows.search_uom_by_name", return_value=uom)

stats = ProductStatsCollector()
stats, _ = sync_product_definition(
Expand All @@ -1031,6 +1069,7 @@ def test_sync_product_update_product_operations(

review_mock.assert_called_once_with(mpt_client, "ITM-1213-3316-0001")
publish_mock.assert_called_once_with(mpt_client, "ITM-1213-3316-0002")
unpublish_mock.assert_called_once_with(mpt_client, "ITM-1213-3316-0005")
update_mock.assert_called_once_with(
mpt_client,
"ITM-1213-3316-0003",
Expand All @@ -1048,6 +1087,20 @@ def test_sync_product_update_product_operations(
"unit": {"id": "UNT-1916"},
},
)
create_mock.assert_called_once_with(
mpt_client,
{
"name": "Customer",
"description": "Description 6",
"group": {"id": "IGR-1213-3316-0002"},
"product": {"id": "PRD-1213-3316"},
"quantityNotApplicable": False,
"terms": {"commitment": "1y", "period": "1m"},
"unit": {"id": "UOM-1234-1234"},
"parameters": [],
"externalIds": {"operations": "NAV123456"},
},
)
assert stats.tabs[constants.TAB_ITEMS]["skipped"] == 1


Expand Down

0 comments on commit e202c98

Please sign in to comment.