From 3cd7d59e0dfa0625f3dc5c6e9eaa7d5924aebef8 Mon Sep 17 00:00:00 2001 From: Mike Shoup Date: Sun, 30 Dec 2018 08:18:33 -0700 Subject: [PATCH] Display miscellaneous ingredients. --- src/synthale/recipes.py | 26 ++++++++++++++++++++++++++ tests/conftest.py | 8 ++++++++ tests/recipes/coffee-stout.xml | 4 ++-- tests/test_recipes.py | 29 +++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 2 deletions(-) diff --git a/src/synthale/recipes.py b/src/synthale/recipes.py index 91b0af3..8f1006f 100644 --- a/src/synthale/recipes.py +++ b/src/synthale/recipes.py @@ -69,6 +69,8 @@ class MarkdownRecipe: '', self.yeast, '', + self.miscs, + '', )) @property @@ -197,6 +199,30 @@ class MarkdownRecipe: ) ) + @property + def miscs(self): + """Return the markdown to represent the recipe's other ingredients.""" + headers = ('Name', 'Use', 'Amount') + rows = [] + for misc in self.recipe.miscs: + if misc.display_amount is not None: + amt = str(misc.display_amount) + elif misc.amount_is_weight: + amt = '{:.2f} kg'.format(misc.amount) + else: + amt = '{:.2f} L'.format(misc.amount) + rows.append(( + misc.name, + misc.use, + amt + )) + return ( + '{}\n{}'.format( + markdown.setext_heading('Other Ingredients', level=2), + markdown.table(headers, rows) + ) + ) + def load_file(path): """Parse BeerXML file located at `path`. diff --git a/tests/conftest.py b/tests/conftest.py index dd41e29..974e0ee 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -28,3 +28,11 @@ def md_weizen(): return MarkdownRecipe( pybeerxml.Parser().parse('tests/recipes/weizen.xml')[0] ) + + +@pytest.fixture +def md_coffee_stout(): + """Return the sample coffee stout recipe as a MarkdownRecipe object.""" + return MarkdownRecipe( + pybeerxml.Parser().parse('tests/recipes/coffee-stout.xml')[0] + ) diff --git a/tests/recipes/coffee-stout.xml b/tests/recipes/coffee-stout.xml index d5e7bab..6bdb57a 100644 --- a/tests/recipes/coffee-stout.xml +++ b/tests/recipes/coffee-stout.xml @@ -157,10 +157,10 @@ Coffee (Brewed) Keg - 24.0 + 0.709765 false - 24.0 + 24.0 oz. 1.0 diff --git a/tests/test_recipes.py b/tests/test_recipes.py index f0af6de..2e97f61 100644 --- a/tests/test_recipes.py +++ b/tests/test_recipes.py @@ -140,6 +140,35 @@ def test_recipe_yeast(md_weizen): ) +def test_recipe_miscs(md_coffee_stout): + """Test valid miscellaneous ingredient table is generated.""" + assert md_coffee_stout.miscs == ( + 'Other Ingredients\n' + '-----------------\n' + '| Name | Use | Amount |\n' + '| --------------- | --- | -------- |\n' + '| Coffee (Brewed) | Keg | 24.0 oz. |' + ) + + md_coffee_stout.recipe.miscs[0].display_amount = None + assert md_coffee_stout.miscs == ( + 'Other Ingredients\n' + '-----------------\n' + '| Name | Use | Amount |\n' + '| --------------- | --- | ------ |\n' + '| Coffee (Brewed) | Keg | 0.71 L |' + ) + + md_coffee_stout.recipe.miscs[0].amount_is_weight = True + assert md_coffee_stout.miscs == ( + 'Other Ingredients\n' + '-----------------\n' + '| Name | Use | Amount |\n' + '| --------------- | --- | ------- |\n' + '| Coffee (Brewed) | Keg | 0.71 kg |' + ) + + def test_write_recipes(md_recipes, tmpdir): """Test write_recipes function.""" write_recipes(md_recipes, str(tmpdir))