def test_ingredient_create_with_invalid_recipe(app_handle): """ Try to add ingredient with invalid recipe id """ recipe_id = random.randint(1, 10000000) ingredient_id = random.randint(1, 10000000) food_item_id = random.randint(1, 10000000) food_item_equivalent_id = random.randint(1, 10000000) # food_item_category_id = random.randint(1, 10000000) ingredient = Ingredient(id=ingredient_id, recipe_id=recipe_id, food_item_id=food_item_id, food_item_equivalent_id=food_item_equivalent_id, quantity=10.0) fooditem = FoodItem( id=food_item_id, # food_item_category_id=food_item_category_id, name="donkey_salt", emission_per_kg=100.0) fooditemequivalent = FoodItemEquivalent(id=food_item_equivalent_id, food_item_id=food_item_id, unit_type=10, conversion_factor=100.0) # fooditemcategory = FoodItemCategory( # id=food_item_category_id, # name="donkey" # ) with app_handle.app_context(): db.session.add(ingredient) db.session.add(fooditem) db.session.add(fooditemequivalent) # db.session.add(fooditemcategory) with pytest.raises(IntegrityError): db.session.commit()
def test_ingredient_create(app_handle): """ Add valid ingredient """ recipe_id = random.randint(1, 10000000) ingredient_id = random.randint(1, 10000000) food_item_id = random.randint(1, 10000000) food_item_equivalent_id = random.randint(1, 10000000) # food_item_category_id = random.randint(1, 10000000) recipe = Recipe(id=recipe_id, name="donkey_recipe") ingredient = Ingredient(id=ingredient_id, recipe_id=recipe_id, food_item_id=food_item_id, food_item_equivalent_id=food_item_equivalent_id, quantity=10.0) fooditem = FoodItem( id=food_item_id, # food_item_category_id=food_item_category_id, name="donkey_salt", emission_per_kg=100.0) fooditemequivalent = FoodItemEquivalent(id=food_item_equivalent_id, food_item_id=food_item_id, unit_type=10, conversion_factor=100.0) # fooditemcategory = FoodItemCategory( # id=food_item_category_id, # name="donkey" # ) with app_handle.app_context(): db.session.add(recipe) db.session.add(ingredient) db.session.add(fooditem) db.session.add(fooditemequivalent) # db.session.add(fooditemcategory) db.session.commit() assert Ingredient.query.count() == 1 ingredient = Ingredient.query.filter_by(id=ingredient_id).first() assert ingredient.recipe.id == recipe_id assert ingredient.food_item.id == food_item_id assert ingredient.food_item_equivalent_id == food_item_equivalent_id
def _populate_db(): for i in range(1, 4): r = Recipe(id=i, name="test-recipe-{}".format(i)) db.session.add(r) f = FoodItem( id=i, name="test-food-item-{}".format(i), emission_per_kg=float(i), ) db.session.add(f) e = FoodItemEquivalent(id=i, food_item_id=i, unit_type="kilogram", conversion_factor=1) db.session.add(e) g = Ingredient( id=i, recipe_id=i, food_item_id=i, food_item_equivalent_id=i, quantity=1.0, ) db.session.add(g) h = FoodItem(id=4, name="lonely-food-item", emission_per_kg=5.5) db.session.add(h) j = FoodItemEquivalent(id=4, food_item_id=4, unit_type="teaspoon", conversion_factor=202.88) db.session.add(j) db.session.commit()
def post(self, recipe_id): """ Add new ingredient to recipe """ if request.json is None: return MasonBuilder.get_error_response( 415, "Request content type must be JSON", "") recipe = Recipe.query.filter_by(id=recipe_id).first() if recipe is None: return MasonBuilder.get_error_response( 404, "Recipe not found.", "Recipe with id {0} not found".format(recipe_id)) # ingredients = Ingredient.query.filter_by(recipe_id=recipe.id).all() keys = request.json.keys() if not set(["food_item_id", "food_item_equivalent_id", "quantity" ]).issubset(keys): return MasonBuilder.get_error_response( 400, "Incomplete request - missing fields", "") food_item = FoodItem.query.filter_by( id=request.json['food_item_id']).first() if food_item is None: return MasonBuilder.get_error_response( 404, "FoodItem not found.", "FoodItem with id {0} not found".format( request.json['food_item_id'])) food_item_equivalent = FoodItemEquivalent.query.filter_by(food_item_id=food_item.id) \ .filter_by(id=request.json['food_item_equivalent_id']).first() if food_item_equivalent is None: return MasonBuilder.get_error_response( 404, "FoodItemEquivalent not found.", "FoodItemEquivalent with id {0} not found".format( request.json['food_item_equivalent_id'])) quantity = 0 try: quantity = float(request.json['quantity']) if quantity <= 0: raise ValueError except ValueError: return MasonBuilder.get_error_response( 400, "Quantity must be a positive number", "") ingredient = Ingredient( recipe_id=recipe.id, food_item_id=food_item.id, food_item_equivalent_id=food_item_equivalent.id, quantity=quantity) db.session.add(ingredient) db.session.commit() headers = { "Location": api.url_for(IngredientItem, recipe_id=recipe.id, ingredient_id=ingredient.id) } response = Response(None, 201, headers=headers) return response