def test_create_product(db: Session) -> None: name = random_lower_string() category = create_random_product_category(db) brand = create_random_brand(db) product_in = ProductCreate(name=name, category_id=category.id, brand_id=brand.id) product = crud.product.create(db=db, obj_in=product_in) assert product.name == name
def add( new_product: schemas.ProductCreate, current_user: models.User = Depends(get_current_user), db_session: Session = Depends(get_db) ) -> schemas.Product: try: crud.get_by_name(db_session, new_product.name) raise HTTPException( status.HTTP_400_BAD_REQUEST, detail='The product with this name already exists in the system.') except NoResultFound: return crud.create(db_session, new_product.dict())
def test_delete_product(db: Session) -> None: name = random_lower_string() category = create_random_product_category(db) brand = create_random_brand(db) product_in = ProductCreate(name=name, category_id=category.id, brand_id=brand.id) product = crud.product.create(db=db, obj_in=product_in) product2 = crud.product.remove(db=db, id=product.id) product3 = crud.product.get(db=db, id=product.id) assert product3 is None assert product2.id == product.id assert product2.name == name
def test_update_product(db: Session) -> None: name = random_lower_string() category = create_random_product_category(db) brand = create_random_brand(db) product_in = ProductCreate(name=name, category_id=category.id, brand_id=brand.id) product = crud.product.create(db=db, obj_in=product_in) name2 = random_lower_string() product_update = ProductUpdate(name=name2) product2 = crud.product.update(db=db, db_obj=product, obj_in=product_update) assert product.id == product2.id assert product.name == product2.name assert product2.name == name2
def create_random_product(db: Session, *, category_id: Optional[int] = None, brand_id: Optional[int] = None) -> models.Product: if not category_id: category = create_random_product_category(db) category_id = category.id if not brand_id: brand = create_random_brand(db) brand_id = brand.id name = random_lower_string() product_in = ProductCreate(name=name, category_id=category_id, brand_id=brand_id) return crud.product.create(db=db, obj_in=product_in)
def init() -> None: db = SessionLocal() init_db(db) # if the default-store does not exist, create it and some more # sample data code = settings.DEFAULT_STORE if crud.store.get_by_code(db, code=code): return # create default-store store_in = StoreCreate(code=code) default_store = crud.store.create(db=db, obj_in=store_in) # create mm-berlin store_in = StoreCreate(code="mm-berlin") mm_berlin = crud.store.create(db=db, obj_in=store_in) # create some funny brands brand_in = BrandCreate(name="Cat co.") cat_brand = crud.brand.create(db=db, obj_in=brand_in) brand_in = BrandCreate(name="Dog inc.") dog_brand = crud.brand.create(db=db, obj_in=brand_in) # create some funny categories category_in = ProductCategoryCreate(name="cat-toys") cat_category = crud.product_category.create(db=db, obj_in=category_in) category_in = ProductCategoryCreate(name="dog-toys") dog_category = crud.product_category.create(db=db, obj_in=category_in) # create some funny products product_in = ProductCreate(name="piece of string", category_id=cat_category.id, brand_id=cat_brand.id) cat_product_1 = crud.product.create(db=db, obj_in=product_in) product_in = ProductCreate(name="leaf", category_id=cat_category.id, brand_id=cat_brand.id) cat_product_2 = crud.product.create(db=db, obj_in=product_in) product_in = ProductCreate(name="stick", category_id=dog_category.id, brand_id=dog_brand.id) dog_product_1 = crud.product.create(db=db, obj_in=product_in) product_in = ProductCreate(name="ball", category_id=dog_category.id, brand_id=dog_brand.id) dog_product_2 = crud.product.create(db=db, obj_in=product_in) # stock the shops with our supply :) # first cat-products stock_level_in = StockLevelCreate(product_id=cat_product_1.id, store_id=default_store.id, amount=10) crud.stock_level.create(db=db, obj_in=stock_level_in) stock_level_in = StockLevelCreate(product_id=cat_product_2.id, store_id=default_store.id, amount=20) crud.stock_level.create(db=db, obj_in=stock_level_in) stock_level_in = StockLevelCreate(product_id=cat_product_1.id, store_id=mm_berlin.id, amount=5) crud.stock_level.create(db=db, obj_in=stock_level_in) stock_level_in = StockLevelCreate(product_id=cat_product_2.id, store_id=mm_berlin.id, amount=8) crud.stock_level.create(db=db, obj_in=stock_level_in) # now dog-products stock_level_in = StockLevelCreate(product_id=dog_product_1.id, store_id=default_store.id, amount=4) crud.stock_level.create(db=db, obj_in=stock_level_in) stock_level_in = StockLevelCreate(product_id=dog_product_2.id, store_id=default_store.id, amount=23) crud.stock_level.create(db=db, obj_in=stock_level_in) stock_level_in = StockLevelCreate(product_id=dog_product_1.id, store_id=mm_berlin.id, amount=7) # unfortunately mm-berlin run out of balls :( crud.stock_level.create(db=db, obj_in=stock_level_in) stock_level_in = StockLevelCreate(product_id=dog_product_2.id, store_id=mm_berlin.id, amount=0) crud.stock_level.create(db=db, obj_in=stock_level_in)