Exemple #1
0
def test_non_equality():
    obj_1 = models.ProductDraft(
        slug=models.LocalizedString(en="test-2"),
        name=models.LocalizedString(en="test-1"),
        product_type=models.ProductTypeResourceIdentifier(key="dummy"),
    )
    obj_2 = models.ProductDraft(
        slug=models.LocalizedString(en="test-1"),
        name=models.LocalizedString(en="test-1"),
        product_type=models.ProductTypeResourceIdentifier(key="dummy"),
    )
    assert obj_1 != obj_2
def test_get_by_key(old_client):
    product = old_client.products.create(
        models.ProductDraft(
            master_variant=models.ProductVariantDraft(sku="123"),
            publish=True,
            name=models.LocalizedString(nl="Test product"),
            slug=models.LocalizedString(en="my-product"),
            product_type=models.ProductTypeResourceIdentifier(key="dummy"),
        ))

    variant = product.master_data.current.master_variant
    shopping_list = old_client.shopping_lists.create(
        draft=models.ShoppingListDraft(
            key="test-shopping-list",
            name=models.LocalizedString({"nl": "Verlanglijstje"}),
            description=models.LocalizedString(
                {"nl": "Verlanglijstje van LabD"}),
            line_items=[
                models.ShoppingListLineItemDraft(sku=variant.sku, quantity=1)
            ],
        ))
    assert shopping_list.key

    shopping_list = old_client.shopping_lists.get_by_key("test-shopping-list")
    assert shopping_list.name["nl"] == "Verlanglijstje"
    assert shopping_list.description["nl"] == "Verlanglijstje van LabD"
    assert shopping_list.line_items[0].variant.sku == "123"
    assert shopping_list.line_items[0].quantity == 1
Exemple #3
0
def test_resource_delete_conflict(old_client):
    """Test the return value of the update methods.

    It doesn't test the actual update itself.
    TODO: See if this is worth testing since we're using a mocking backend
    """
    product = old_client.products.create(
        models.ProductDraft(
            key="test-product",
            product_type=models.ProductTypeResourceIdentifier(key="dummy"),
            name={"en": "my-product"},
            slug={"en": "foo-bar"},
        ))

    product = old_client.products.update_by_id(
        id=product.id,
        version=product.version,
        actions=[
            models.ProductChangeSlugAction(slug=models.LocalizedString(
                nl="nl-slug2"))
        ],
    )
    assert product.version == 2

    # This should raise a version conflict error
    with pytest.raises(CommercetoolsError) as exc:
        product = old_client.products.delete_by_id(id=product.id, version=1)

    assert exc.value.response.status_code == 409
    assert exc.value.response.errors[0].current_version == 2

    # Force it
    old_client.products.delete_by_id(id=product.id,
                                     version=1,
                                     force_delete=True)
Exemple #4
0
def test_correlation_id_is_set_in_exception(old_client):
    product = old_client.products.create(
        models.ProductDraft(
            key="test-product",
            name=models.LocalizedString(en=f"my-product"),
            slug=models.LocalizedString(en=f"my-product"),
            product_type=models.ProductTypeResourceIdentifier(key="dummy"),
        ))

    product = old_client.products.update_by_id(
        id=product.id,
        version=product.version,
        actions=[
            models.ProductChangeSlugAction(slug=models.LocalizedString(
                nl="nl-slug2"))
        ],
    )

    # This should raise a version conflict error
    with pytest.raises(CommercetoolsError) as exc:
        old_client.products.update_by_id(
            id=product.id,
            version=1,
            actions=[
                models.ProductChangeSlugAction(slug=models.LocalizedString(
                    nl="nl-slug3"))
            ],
        )

    assert exc.value.correlation_id is not None
def create_products(client: ByProjectKeyRequestBuilder):
    for i in range(100):
        client.products().post(
            models.ProductDraft(
                key=f"test-product-{i}",
                product_type=models.ProductTypeResourceIdentifier(key="dummy"),
                name=models.LocalizedString(en=f"my-product-{i}"),
                slug=models.LocalizedString(en=f"my-product-{i}"),
            ))
def test_product_projections_query(ct_platform_client: Client, old_client):
    client = ct_platform_client.with_project_key("test")

    for key in ["product-1", "product-2"]:
        variant = models.ProductVariantDraft()
        client.products().post(
            models.ProductDraft(
                key=key,
                product_type=models.ProductTypeResourceIdentifier(key="dummy"),
                name=models.LocalizedString(en=key),
                slug=models.LocalizedString(en=key),
                master_variant=variant,
                variants=[variant],
                publish=True,
            ))

    key = "product-3"
    client.products().post(
        models.ProductDraft(
            key=key,
            product_type=models.ProductTypeResourceIdentifier(key="dummy"),
            name=models.LocalizedString(en=key),
            slug=models.LocalizedString(en=key),
            master_variant=variant,
            variants=[variant],
            publish=False,
        ))

    # single sort query
    result = client.product_projections().get(
        sort="id asc",
        where=[f'slug(nl-NL="product-3")'],
        expand=["parent.category"])
    assert len(result.results) == 2
    assert result.total == 2
    assert result.results[0].key == "product-1"
    assert result.results[1].key == "product-2"

    # multiple sort queries
    result = client.product_projections().get(sort=["id asc", "name asc"])
    assert len(result.results) == 2
    assert result.total == 2
def cart_draft(ct_platform_client: Client):
    client = ct_platform_client.with_project_key("test")
    product_1 = client.products().post(
        models.ProductDraft(
            key="product-1",
            product_type=models.ProductTypeResourceIdentifier(key="dummy"),
            name=models.LocalizedString(en=f"my-product-1"),
            slug=models.LocalizedString(en=f"my-product-1"),
            publish=True,
        ))
    product_2 = client.products().post(
        models.ProductDraft(
            key="product-2",
            product_type=models.ProductTypeResourceIdentifier(key="dummy"),
            name=models.LocalizedString(en=f"my-product-2"),
            slug=models.LocalizedString(en=f"my-product-2"),
            publish=True,
        ))

    return models.CartDraft(
        customer_id=str(uuid.uuid4()),
        customer_email="*****@*****.**",
        currency="GBP",
        anonymous_id=str(uuid.uuid4()),
        country="GB",
        inventory_mode=models.InventoryMode.NONE,
        tax_mode=models.TaxMode.PLATFORM,
        tax_rounding_mode=models.RoundingMode.HALF_EVEN,
        tax_calculation_mode=models.TaxCalculationMode.LINE_ITEM_LEVEL,
        line_items=[
            models.LineItemDraft(product_id=product_1.id, quantity=1),
            models.LineItemDraft(product_id=product_2.id, quantity=2),
        ],
        locale="en",
        origin=models.CartOrigin.CUSTOMER,
    )
def test_product_projections_get_by_key(old_client):
    variant = models.ProductVariantDraft()
    product_create = old_client.products.create(
        models.ProductDraft(
            key="test-product",
            product_type=models.ProductTypeResourceIdentifier(key="dummy"),
            name=models.LocalizedString(en=f"my-product"),
            slug=models.LocalizedString(en=f"my-product"),
            master_variant=variant,
            variants=[variant],
            publish=False,
        ))
    product = old_client.product_projections.get_by_key(product_create.key,
                                                        staged=True)
    assert product.id == product_create.id
    assert product.key == product_create.key