|
| 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 | +def async_vendor_variant_service(async_mpt_vendor, product_id, term_id): |
| 11 | + return async_mpt_vendor.catalog.products.terms(product_id).variants(term_id) |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +async def async_created_variant( |
| 16 | + variant_data, |
| 17 | + pdf_fd, |
| 18 | + async_vendor_variant_service, |
| 19 | +): |
| 20 | + variant = await async_vendor_variant_service.create(variant_data, pdf_fd) |
| 21 | + yield variant |
| 22 | + try: |
| 23 | + await async_vendor_variant_service.delete(variant.id) |
| 24 | + except MPTAPIError as error: |
| 25 | + print(f"TEARDOWN - Unable to delete variant {variant.id}: {error.title}") |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +async def created_variant_from_url( |
| 30 | + variant_data, |
| 31 | + pdf_url, |
| 32 | + async_vendor_variant_service, |
| 33 | +): |
| 34 | + variant_data["type"] = "Online" |
| 35 | + variant_data["assetUrl"] = pdf_url |
| 36 | + variant = await async_vendor_variant_service.create(variant_data) |
| 37 | + yield variant |
| 38 | + try: |
| 39 | + await async_vendor_variant_service.delete(variant.id) |
| 40 | + except MPTAPIError as error: |
| 41 | + print(f"TEARDOWN - Unable to delete variant {variant.id}: {error.title}") |
| 42 | + |
| 43 | + |
| 44 | +def test_create_variant(async_created_variant): |
| 45 | + variant = async_created_variant |
| 46 | + assert variant.name == "e2e - please delete" |
| 47 | + |
| 48 | + |
| 49 | +def test_create_variant_from_url(created_variant_from_url, variant_data): |
| 50 | + assert created_variant_from_url.name == variant_data["name"] |
| 51 | + |
| 52 | + |
| 53 | +async def test_update_variant( |
| 54 | + async_mpt_vendor, product_id, term_id, async_created_variant, async_vendor_variant_service |
| 55 | +): |
| 56 | + service = async_vendor_variant_service |
| 57 | + update_data = {"name": "e2e - delete me (updated)"} |
| 58 | + variant = await service.update(async_created_variant.id, update_data) |
| 59 | + assert variant.name == "e2e - delete me (updated)" |
| 60 | + |
| 61 | + |
| 62 | +async def test_get_variant( |
| 63 | + async_mpt_vendor, product_id, term_id, variant_id, async_vendor_variant_service |
| 64 | +): |
| 65 | + service = async_vendor_variant_service |
| 66 | + variant = await service.get(variant_id) |
| 67 | + assert variant.id == variant_id |
| 68 | + |
| 69 | + |
| 70 | +async def test_get_variant_by_id( |
| 71 | + async_mpt_vendor, product_id, term_id, variant_id, async_vendor_variant_service |
| 72 | +): |
| 73 | + service = async_vendor_variant_service |
| 74 | + variant = await service.get(variant_id) |
| 75 | + assert variant.id == variant_id |
| 76 | + |
| 77 | + |
| 78 | +async def test_iterate_variants( |
| 79 | + async_mpt_vendor, product_id, term_id, async_created_variant, async_vendor_variant_service |
| 80 | +): |
| 81 | + service = async_vendor_variant_service |
| 82 | + variants = [variant async for variant in service.iterate()] |
| 83 | + assert any(variant.id == async_created_variant.id for variant in variants) |
| 84 | + |
| 85 | + |
| 86 | +async def test_filter_variants( |
| 87 | + async_mpt_vendor, product_id, term_id, variant_id, async_vendor_variant_service |
| 88 | +): |
| 89 | + select_fields = ["-description"] |
| 90 | + filtered_variants = async_vendor_variant_service.filter(RQLQuery(id=variant_id)).select( |
| 91 | + *select_fields |
| 92 | + ) |
| 93 | + variants = [variant async for variant in filtered_variants.iterate()] |
| 94 | + assert len(variants) == 1 |
| 95 | + assert variants[0].id == variant_id |
| 96 | + |
| 97 | + |
| 98 | +async def test_delete_variant( |
| 99 | + async_mpt_vendor, product_id, term_id, async_created_variant, async_vendor_variant_service |
| 100 | +): |
| 101 | + service = async_vendor_variant_service |
| 102 | + await service.delete(async_created_variant.id) |
| 103 | + with pytest.raises(MPTAPIError): |
| 104 | + await service.get(async_created_variant.id) |
0 commit comments