|
| 1 | +import pytest |
| 2 | + |
| 3 | +from mpt_api_client.exceptions import MPTAPIError |
| 4 | +from mpt_api_client.rql.query_builder import RQLQuery |
| 5 | + |
| 6 | +pytestmark = [pytest.mark.flaky] |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +async def created_order_asset(async_mpt_vendor, order_asset_factory, commerce_asset_draft_order_id): |
| 11 | + # Must use this fixture for all tests to prevent api errors |
| 12 | + asset_data = order_asset_factory() |
| 13 | + orders = async_mpt_vendor.commerce.orders |
| 14 | + assets = orders.assets(commerce_asset_draft_order_id) |
| 15 | + asset = await assets.create(asset_data) |
| 16 | + |
| 17 | + yield asset |
| 18 | + |
| 19 | + try: |
| 20 | + await assets.delete(asset.id) |
| 21 | + except MPTAPIError as error: |
| 22 | + print(f"TEARDOWN - Unable to delete order asset: {getattr(error, 'title', str(error))}") # noqa: WPS421 |
| 23 | + |
| 24 | + |
| 25 | +async def test_get_order_asset_by_id( |
| 26 | + async_mpt_vendor, created_order_asset, commerce_asset_draft_order_id |
| 27 | +): |
| 28 | + asset_id = created_order_asset.id |
| 29 | + orders = async_mpt_vendor.commerce.orders.assets(commerce_asset_draft_order_id) |
| 30 | + |
| 31 | + result = await orders.get(asset_id) |
| 32 | + |
| 33 | + assert result is not None |
| 34 | + |
| 35 | + |
| 36 | +async def test_list_order_assets( |
| 37 | + async_mpt_vendor, created_order_asset, commerce_asset_draft_order_id |
| 38 | +): |
| 39 | + limit = 10 |
| 40 | + orders = async_mpt_vendor.commerce.orders |
| 41 | + assets = orders.assets(commerce_asset_draft_order_id) |
| 42 | + |
| 43 | + result = await assets.fetch_page(limit=limit) |
| 44 | + |
| 45 | + assert result is not None |
| 46 | + |
| 47 | + |
| 48 | +async def test_get_order_asset_by_id_not_found( |
| 49 | + async_mpt_vendor, created_order_asset, commerce_asset_draft_order_id, invalid_agreement_asset_id |
| 50 | +): |
| 51 | + orders = async_mpt_vendor.commerce.orders |
| 52 | + assets = orders.assets(commerce_asset_draft_order_id) |
| 53 | + |
| 54 | + with pytest.raises(MPTAPIError, match="404 Not Found"): |
| 55 | + await assets.get(invalid_agreement_asset_id) |
| 56 | + |
| 57 | + |
| 58 | +async def test_filter_order_assets( |
| 59 | + async_mpt_vendor, created_order_asset, commerce_asset_draft_order_id |
| 60 | +): |
| 61 | + select_fields = ["-externalIds"] |
| 62 | + asset_id = created_order_asset.id |
| 63 | + assets = async_mpt_vendor.commerce.orders.assets(commerce_asset_draft_order_id) |
| 64 | + filtered_assets = ( |
| 65 | + assets.filter(RQLQuery(id=asset_id)) |
| 66 | + .filter(RQLQuery(name="E2E Created Order Asset")) |
| 67 | + .select(*select_fields) |
| 68 | + ) |
| 69 | + |
| 70 | + result = [asset async for asset in filtered_assets.iterate()] |
| 71 | + |
| 72 | + assert len(result) == 1 |
| 73 | + |
| 74 | + |
| 75 | +def test_create_order_asset(created_order_asset): |
| 76 | + result = created_order_asset |
| 77 | + |
| 78 | + assert result is not None |
| 79 | + |
| 80 | + |
| 81 | +async def test_update_order_asset( |
| 82 | + async_mpt_vendor, |
| 83 | + created_order_asset, |
| 84 | + commerce_asset_draft_order_id, |
| 85 | +): |
| 86 | + asset_id = created_order_asset.id |
| 87 | + updated_asset_data = { |
| 88 | + "name": "E2E Updated Order Asset", |
| 89 | + } |
| 90 | + orders = async_mpt_vendor.commerce.orders |
| 91 | + assets = orders.assets(commerce_asset_draft_order_id) |
| 92 | + |
| 93 | + result = await assets.update(asset_id, updated_asset_data) |
| 94 | + |
| 95 | + assert result is not None |
| 96 | + |
| 97 | + |
| 98 | +async def test_delete_order_asset( |
| 99 | + async_mpt_vendor, |
| 100 | + created_order_asset, |
| 101 | + commerce_asset_draft_order_id, |
| 102 | +): |
| 103 | + asset_id = created_order_asset.id |
| 104 | + orders = async_mpt_vendor.commerce.orders |
| 105 | + |
| 106 | + result = orders.assets(commerce_asset_draft_order_id) |
| 107 | + |
| 108 | + await result.delete(asset_id) |
| 109 | + |
| 110 | + |
| 111 | +async def test_render_order_asset( |
| 112 | + async_mpt_vendor, |
| 113 | + created_order_asset, |
| 114 | + commerce_asset_draft_order_id, |
| 115 | +): |
| 116 | + asset_id = created_order_asset.id |
| 117 | + orders = async_mpt_vendor.commerce.orders |
| 118 | + assets = orders.assets(commerce_asset_draft_order_id) |
| 119 | + |
| 120 | + result = await assets.render(asset_id) |
| 121 | + |
| 122 | + assert result is not None |
0 commit comments