def test_ingredient_create_with_invalid_fooditemequivalent(app_handle): """ Try to add ingredient with invalid fooditemequivalent 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) 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) # 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(fooditemcategory) with pytest.raises(IntegrityError): db.session.commit()
def test_recipe_create_with_invalid_name(app_handle): """ Try adding a recipe with empty name """ recipe = Recipe(name="", ) with app_handle.app_context(): db.session.add(recipe) with pytest.raises(IntegrityError): db.session.commit()
def test_recipe_create(app_handle): """ Add valid recipe """ recipe = Recipe(name="donkey-recipe") with app_handle.app_context(): db.session.add(recipe) db.session.commit() assert Recipe.query.count() == 1
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 post(self): if request.json is None: return MasonBuilder.get_error_response( 415, "Request content type must be JSON", "") keys = request.json.keys() if 'name' not in keys: return MasonBuilder.get_error_response( 400, "Incomplete request - missing fields", ["Missing field:name"]) name = request.json['name'] if len(name) < 1: return MasonBuilder.get_error_response(400, "Name is too short", "") elif len(name) > 64: return MasonBuilder.get_error_response(400, "Name is too long", "") recipe = Recipe(name=name) db.session.add(recipe) db.session.commit() headers = {"Location": api.url_for(RecipeItem, recipe_id=recipe.id)} response = Response(status=201, headers=headers) return response
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()