Skip to content

Commit

Permalink
Throw error if number of recipes less than recipes in db. Fixes #17
Browse files Browse the repository at this point in the history
  • Loading branch information
matrulda committed Mar 11, 2020
1 parent 5a3da5e commit e665866
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
4 changes: 4 additions & 0 deletions gimme_food/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@

class RecipeNotInProperJsonFormat(Exception):
pass


class NotEnoughRecipesInDatabase(Exception):
pass
5 changes: 3 additions & 2 deletions gimme_food/gimme_food_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from gimme_food.utils import present_result, print_banner
from gimme_food.configure import read_config, get_logger
from gimme_food.exceptions import RecipeNotInProperJsonFormat
from gimme_food.exceptions import NotEnoughRecipesInDatabase
from gimme_food.recipe_picker import RecipePicker

@click.command("gimme_food")
Expand All @@ -24,8 +25,8 @@ def run_app(number_of_recipes, config, ingredient, debug):
log.info(f"gimme-food started - version: {__version__}, config: {print_safe_config}, " +
f"number of recipes: {number_of_recipes}, ingredient: {ingredient}, debug: {debug}")
try:
recipe_list = list(make_recipe_db(conf["recipe_folder"]))
except RecipeNotInProperJsonFormat as e:
recipe_list = make_recipe_db(conf["recipe_folder"], number_of_recipes)
except (RecipeNotInProperJsonFormat, NotEnoughRecipesInDatabase) as e:
log.error(e)
sys.exit()
recipe_picker = RecipePicker(recipe_list, number_of_recipes, ingredient)
Expand Down
12 changes: 10 additions & 2 deletions gimme_food/recipe_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
from gimme_food.entities.recipe import Recipe
from gimme_food.exceptions import RecipeNotInProperJsonFormat
from gimme_food.exceptions import NotEnoughRecipesInDatabase
import logging

log = logging.getLogger("master")
Expand All @@ -16,9 +17,16 @@ def read_recipe(file_path):
f"{file_path}, isn't a valid json file. " +
"Try running it in a json validator.")

def make_recipe_db(recipe_folder):
def make_recipe_db(recipe_folder, number_of_recipes):
recipe_list = []
for f in os.listdir(recipe_folder):
if f.endswith(".json"):
log.debug(f"Reading recipe file: {f}")
recipe_dict = read_recipe(os.path.join(recipe_folder, f))
yield Recipe(recipe_dict)
recipe_list.append(Recipe(recipe_dict))
if len(recipe_list) < number_of_recipes:
raise NotEnoughRecipesInDatabase(f"You asked for {number_of_recipes} recipes " +
f"but there is only {len(recipe_list)} in your database, " +
"add more recipes or try a lower number of recipes")
else:
return recipe_list
12 changes: 11 additions & 1 deletion tests/test_recipe_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@
import json
from json.decoder import JSONDecodeError
from gimme_food.exceptions import RecipeNotInProperJsonFormat
from gimme_food.recipe_db import read_recipe
from gimme_food.exceptions import NotEnoughRecipesInDatabase
from gimme_food.recipe_db import *
from gimme_food.configure import read_config

@pytest.fixture
def file_path():
return "gimme_food/examples/recipe_1.json"

@pytest.fixture
def conf():
return read_config(None)

def test_json_error(file_path, mocker):
mocker.patch('json.load', side_effect=JSONDecodeError("", "", 0))
with pytest.raises(RecipeNotInProperJsonFormat):
read_recipe(file_path)

def test_not_enough_recipes(conf):
with pytest.raises(NotEnoughRecipesInDatabase):
make_recipe_db(conf["recipe_folder"], 3)
2 changes: 1 addition & 1 deletion tests/test_recipe_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

@pytest.fixture
def recipe_list():
return list(make_recipe_db("gimme_food/examples"))
return make_recipe_db("gimme_food/examples", 1)

def test_one_ingredient(recipe_list):
recipe_picker = RecipePicker(recipe_list, 1, ("Jordnötssmör",))
Expand Down

0 comments on commit e665866

Please sign in to comment.