def recipe_put(recipe_id): """Update a specified recipe""" recipe = helpers.raise_or_return(schemas.recipe.put) if not recipe: raise exc.APIException('no data provided for update') recipe['id'] = recipe_id recipe = db_helpers.update_recipe(recipe) return schemas.recipe.dump(recipe).data
def test_raise_or_return_error(): """Test the raise_or_return function with error""" mock_schema_load = mock.Mock(side_effect=AttributeError) mock_schema = mock.Mock(load=mock_schema_load) api_exc = ('Request malformed', 400, {'errors': 'JSON might be incorrect'}) with pytest.raises(helpers.APIException) as excinfo: helpers.raise_or_return(mock_schema) assert excinfo.value.args == api_exc mock_schema_load = mock.Mock(return_value=(None, mock.sentinel.errors)) mock_schema = mock.Mock(load=mock_schema_load) api_exc = ('Request malformed', 400, {'errors': mock.sentinel.errors}) with pytest.raises(helpers.APIException) as excinfo: helpers.raise_or_return(mock_schema) assert excinfo.value.args == api_exc
def test_raise_or_return(monkeypatch): """Test the raise_or_return function without error""" mock_schema_load = mock.Mock(return_value=(mock.sentinel.rv, None)) mock_schema = mock.Mock(load=mock_schema_load) mock_flask_request = mock.Mock(json=mock.sentinel.request_json) schema_load_calls = [mock.call(mock.sentinel.request_json)] monkeypatch.setattr('flask.request', mock_flask_request) rv = helpers.raise_or_return(mock_schema) assert mock_schema_load.call_args_list == schema_load_calls assert rv == mock.sentinel.rv
def recipes_post(): """Create a recipe""" recipe = helpers.raise_or_return(schemas.recipe.post) utensils = recipe.pop('utensils') ingredients = recipe.pop('ingredients') try: recipe = models.Recipe.create(**recipe) except peewee.IntegrityError: raise exc.APIException('recipe already exists', 409) db_helpers.recipe_insert_utensils(recipe.id, utensils) db_helpers.recipe_insert_ingredients(recipe.id, ingredients) recipe.utensils = utensils recipe.ingredients = ingredients return schemas.recipe.dump(recipe).data, 201
def recipes_put(): """Update multiple recipes""" recipes = helpers.raise_or_return(schemas.recipe.put, True) return schemas.recipe.dump(recipes, many=True).data