Beispiel #1
0
def create_product():
    return Product(
        id="Test",
        name="Test",
        quantity=12,
        type=ProductType(id="test", name="Test"),
        images=[Image(name="test", url="test_url")],
        price=Price(12.34, "USD"),
    )
Beispiel #2
0
    def _convert_product_model_to_product(
            product_model: ProductModel) -> Product:
        _type = ProductType(id=product_model.product_type.id,
                            name=product_model.product_type.name)
        product = Product(id=product_model.id,
                          name=product_model.name,
                          type=_type,
                          quantity=product_model.quantity)
        product.set_product_price(product_model.price_value,
                                  product_model.price_currency)
        for image in product_model.images:
            product.add_image(image.get("name"), image.get("url"))

        return product
    def create_product(self, product_dto: ProductDTO) -> Product:
        _id = self.repository.generate_id()
        product_type = self.find_or_create_product_type(
            product_dto.product_type_id, product_dto.product_type_name)
        product = Product(
            id=_id,
            name=product_dto.name,
            quantity=product_dto.quantity,
            type=product_type,
        )
        images = product_dto.images or []
        for image in images:
            if not self.image_validator(image.get("url")):
                raise InvalidProductException(
                    "Image with provided url does not exist.")
            product.add_image(name=image.get("name"), url=image.get("url"))

        product.set_product_price(product_dto.price, product_dto.currency)

        self.repository.create_from(product)

        return product
def test_product_quantity_not_valid(product_type):
    with pytest.raises(InvalidProductException, ) as error:
        Product(name="Long", type=product_type, quantity=-1)

    assert error.value.message == "Quantity can not be less than 0"
def test_product_type_not_set():
    with pytest.raises(InvalidProductException, ) as error:
        Product(name="Long", type=None)

    assert error.value.message == "Product type is required."
def test_product_name_is_too_long():
    with pytest.raises(InvalidProductException, ) as error:
        Product(name="Long" * 244)

    assert error.value.message == "Product name is to long."
def test_product_without_name_invalid_product_exception():
    with pytest.raises(InvalidProductException) as error:
        Product(name=None)

    assert error.value.message == "Product name  is required."
def product(product_type):
    return Product(name="Long", type=product_type, quantity=12)