def test_store_flow(ct_platform_client: PlatformClient, store_draft):
    store = ct_platform_client.with_project_key("foo").stores().post(
        store_draft)

    assert store.id

    deleted_store = (
        ct_platform_client.with_project_key("foo").stores().with_id(
            store.id).delete(version=store.version))

    assert store.id == deleted_store.id
def test_store_channel_create(commercetools_api,
                              ct_platform_client: PlatformClient, store_draft):
    channel = (ct_platform_client.with_project_key("foo").channels().post(
        models.ChannelDraft(
            key="FOO", roles=[models.ChannelRoleEnum.PRODUCT_DISTRIBUTION])))
    store_draft.distribution_channels = [
        models.ChannelResourceIdentifier(key="FOO")
    ]

    store = ct_platform_client.with_project_key("foo").stores().post(
        store_draft)

    assert store.distribution_channels[0].id == channel.id
def test_update_actions(commercetools_api, ct_platform_client: PlatformClient,
                        store_draft):
    store = ct_platform_client.with_project_key("foo").stores().post(
        store_draft)

    assert store.languages is None

    store = (ct_platform_client.with_project_key("foo").stores().with_id(
        store.id, ).post(
            models.StoreUpdate(
                version=store.version,
                actions=[models.StoreSetLanguagesAction(languages=["en-US"])],
            )))

    assert store.languages == ["en-US"]
def test_raises_exception(ct_platform_client: PlatformClient):
    channel = (ct_platform_client.with_project_key("foo").channels().post(
        models.ChannelDraft(key="test-channel2",
                            roles=[models.ChannelRoleEnum.INVENTORY_SUPPLY])))

    result = (ct_platform_client.with_project_key("foo").channels().with_id(
        channel.id).post(
            models.ChannelUpdate(
                version=4,
                actions=[
                    models.ChannelSetRolesAction(
                        roles=[models.ChannelRoleEnum.PRIMARY])
                ],
            ),
            options={"force_version": True},
        ))

    result = (ct_platform_client.with_project_key("foo").channels().with_id(
        channel.id).delete(version=1, options={"force_version": True}))
def test_channel_errors(commercetools_api, ct_platform_client: PlatformClient,
                        store_draft):
    store = ct_platform_client.with_project_key("foo").stores().post(
        store_draft)

    ct_platform_client.with_project_key("foo").channels().post(
        models.ChannelDraft(key="BAR",
                            roles=[models.ChannelRoleEnum.INVENTORY_SUPPLY]))

    with pytest.raises(CommercetoolsError):
        (ct_platform_client.with_project_key("foo").stores().with_id(
            store.id).post(
                models.StoreUpdate(
                    version=store.version,
                    actions=[
                        models.StoreSetDistributionChannelsAction(
                            distribution_channels=[
                                models.ChannelResourceIdentifier(key="BAR")
                            ])
                    ],
                )))
def test_cart_discount_get_by_id(ct_platform_client: PlatformClient):
    cart_discount = (
        ct_platform_client.with_project_key("foo").cart_discounts().post(
            models.CartDiscountDraft(
                name=models.LocalizedString({"en": "test discount"}),
                value=models.CartDiscountValueRelative(permyriad=10),
                cart_predicate="",
                sort_order="",
                requires_discount_code=False,
            )))

    assert cart_discount.id

    cart_discount = (
        ct_platform_client.with_project_key("foo").cart_discounts().with_id(
            cart_discount.id).get())
    assert cart_discount.id

    item = (ct_platform_client.with_project_key(
        "foo").cart_discounts().with_id("invalid").get())
    assert item is None
def test_channels_are_set(commercetools_api,
                          ct_platform_client: PlatformClient, store_draft):
    store = ct_platform_client.with_project_key("foo").stores().post(
        store_draft)

    assert store.distribution_channels == []

    channel = (ct_platform_client.with_project_key("foo").channels().post(
        models.ChannelDraft(
            key="FOO", roles=[models.ChannelRoleEnum.PRODUCT_DISTRIBUTION])))

    store = (ct_platform_client.with_project_key("foo").stores().with_id(
        store.id, ).post(
            models.StoreUpdate(
                version=store.version,
                actions=[
                    models.StoreSetDistributionChannelsAction(
                        distribution_channels=[
                            models.ChannelResourceIdentifier(key="FOO")
                        ])
                ],
            )))

    assert store.distribution_channels[0].id == channel.id
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_cart_get_by_id(ct_platform_client: Client, cart_draft):
    cart = ct_platform_client.with_project_key("test").carts().post(cart_draft)

    assert cart.id