def create_from_model(model: ProductOrder):
     picking_status = PickingStatus(model.picking_status)
     return ProductOrderDalModel(model.id,
                                 ProductDalModel.create_from_model(model.product),
                                 model.price,
                                 picking_status
                                 )
    def get_all_products(self) -> [ProductDalModel]:
        """
        Retrieves all products from the database and converts to DAL model

        :return: A list of all products
        """
        products = Product.select()
        dal_models = [ProductDalModel.create_from_model(p) for p in products]
        return dal_models
    def get_product(self, id: int) -> ProductDalModel:
        """
        Retrieves a product from the database with the specified id

        :param id: The id of the product to retrieve
        :return: The product
        """
        product_model = Product.get(Product.id == id)
        product = ProductDalModel.create_from_model(product_model)
        return product
Exemplo n.º 4
0
    def test_add_product_creates_product(self):
        # Arrange
        repo = ProductRepository(DatabaseManager())
        name, description, target_stock = "A", "B", 2
        expected = ProductDalModel(1, name, description, target_stock)

        # Act
        prod = repo.create_product(name, description, target_stock)

        # Assert
        self.assertEqual(prod.name, name)
        self.assertEqual(prod.description, description)
        self.assertEqual(prod.target_stock, target_stock)
    def edit_product(self, product: ProductDalModel) -> ProductDalModel:
        """
        Updates the product in the database

        :param product: The product to update with the new values set
        :return: The new product
        """

        model = Product.get(Product.id == product.id)
        model.name = product.name
        model.description = product.description
        model.target_stock = product.target_stock
        model.save()

        return ProductDalModel.create_from_model(model)
    def create_product(self, name: str, description: str,
                       target_stock: int) -> ProductDalModel:
        """
        Creates a product in the database and assigns an id

        :param name: The name of the product
        :param description: An explanation of the product
        :param target_stock: The ideal stock level
        :return: The new product
        """
        model = Product.create(name=name,
                               description=description,
                               target_stock=target_stock)
        product = ProductDalModel.create_from_model(model)

        return product
Exemplo n.º 7
0
    def test_get_stock_item_returns_stock_item(self):
        repo = StockRepository(DatabaseManager())

        db_product = Product(name="iPhone X", description="Apple iPhone\nwith FaceID", target_stock=3)
        db_product.save()

        db_stock = StockItem(location="A3", quantity=4, product=db_product)
        db_stock.save()

        expected = StockItemDalModel(db_stock.id, db_stock.location, db_stock.quantity, db_product.id)
        expected.product = ProductDalModel(db_product.id, db_product.name,
                                           db_product.description, db_product.target_stock)

        # Act
        stock_item: StockItemDalModel = repo.get_stock_item(db_stock.id)

        # Assert
        self.assertEqual(stock_item, expected)
    def get_or_create_by_name(self,
                              name: str,
                              description: str = "from api",
                              target_stock: int = 1) -> ProductDalModel:
        """
        Ensures that there is a product with the specified name, if one cannot be found it is created

        :param name: The name to find
        :param description: The description of the product if created
        :param target_stock: The target stock level if created
        :return: The product with the name specified
        """
        product_model: Product
        try:
            product_model = Product.get(Product.name == name)
        except peewee.DoesNotExist:
            return self.create_product(name, description, target_stock)

        return ProductDalModel.create_from_model(product_model)
Exemplo n.º 9
0
 def create_from_model(model: StockItem):
     dal_model = StockItemDalModel(model.id, model.location, model.quantity,
                                   model.product_id)
     product = ProductDalModel.create_from_model(model.product)
     dal_model.product = product
     return dal_model