diff --git a/pybeerxml/mash.py b/pybeerxml/mash.py index 3079877..bfc178c 100644 --- a/pybeerxml/mash.py +++ b/pybeerxml/mash.py @@ -1,5 +1,6 @@ class Mash(object): def __init__(self): + self.name = None self.grain_temp = None self.sparge_temp = None self.ph = None diff --git a/pybeerxml/mash_step.py b/pybeerxml/mash_step.py index a8fd041..64f9081 100644 --- a/pybeerxml/mash_step.py +++ b/pybeerxml/mash_step.py @@ -1,5 +1,6 @@ class MashStep(object): def __init__(self): + self.name = None self.type = None self.infuse_amount = None self.step_temp = None diff --git a/pybeerxml/misc.py b/pybeerxml/misc.py index 04fc027..4628259 100644 --- a/pybeerxml/misc.py +++ b/pybeerxml/misc.py @@ -11,9 +11,13 @@ def __init__(self): @property def amount_is_weight(self): - return bool(self._amount_is_weight) + if isinstance(self._amount_is_weight, str): + return self._amount_is_weight.lower() == "true" + elif isinstance(self._amount_is_weight, int) or isinstance(self._amount_is_weight, float): + return bool(self._amount_is_weight) + else: + return False @amount_is_weight.setter def amount_is_weight(self, value): - print(value) self._amount_is_weight = value diff --git a/tests/test_parser.py b/tests/test_parser.py index d4fdb16..20027f9 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -1,15 +1,19 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + import pytest import os from pybeerxml import Parser, Recipe from pybeerxml.hop import Hop -from xml.etree import ElementTree +from xml.etree.ElementTree import Element, SubElement from math import floor RECIPE_PATH = os.path.join(os.path.dirname(__file__), "Simcoe IPA.xml") +RECIPE_PATH_2 = os.path.join(os.path.dirname(__file__), "Oatmeal Stout.xml") class TestParser: - def test_parse(self): + def test_parse_recipe(self): recipe_parser = Parser() recipes = recipe_parser.parse(RECIPE_PATH) @@ -22,12 +26,12 @@ def test_parse(self): "should be of type Recipe" assert(type(recipe) is Recipe) - "should have the correct amount of ingriedients" + "should have the correct amount of ingredients" assert(len(recipe.hops) == 3) assert(len(recipe.yeasts) == 1) assert(len(recipe.fermentables) == 2) - "should habe mashing steps" + "should have mashing steps" # TODO "should have the correctly calculated properties" @@ -49,27 +53,80 @@ def test_parse(self): assert(recipe.style.name == "American IPA") + def test_parse_recipe_2(self): + + recipe_parser = Parser() + recipes = recipe_parser.parse(RECIPE_PATH_2) + + "should have at least one recipe" + assert(len(recipes) > 0) + + recipe = recipes[0] + + "should be of type Recipe" + assert(type(recipe) is Recipe) + + "should have the correct amount of ingredients" + assert(len(recipe.hops) == 1) + assert(len(recipe.yeasts) == 1) + assert(len(recipe.fermentables) == 5) + + "should have mashing steps" + assert(len(recipe.mash.steps) == 2) + assert(recipe.mash.steps[0].name == "Mash step") + assert(recipe.mash.steps[0].step_time == 60) + assert(recipe.mash.steps[0].step_temp == 68) + + "should have the correctly calculated properties" + assert(round(recipe.og, 4) == 1.0556) + assert(round(recipe.og_plato, 4) == 13.7108) + assert(round(recipe.fg, 4) == 1.0139) + assert(round(recipe.fg_plato, 4) == 3.5467) + assert(floor(recipe.ibu) == 32) + assert(round(recipe.abv, 2) == 5.47) + + "should have the correct recipe metadata" + assert(recipe.name == "Oatmeal Stout no. 1") + assert(recipe.brewer == "Niels Kjøller") # Python 2 Problem? + assert(recipe.efficiency == 75.0) + assert(recipe.batch_size == 25) + assert(recipe.ibu == 32.21660448470247) + + "should have the correct style metadata" + assert(recipe.style.name == "Oatmeal Stout") + + "should have misc ingredients" + assert(len(recipe.miscs) == 1) + assert(recipe.miscs[0].name == "Protafloc") + assert(recipe.miscs[0].use == "Boil") + assert(recipe.miscs[0].use_for == None) + assert(recipe.miscs[0].amount == 0.0016) + assert(recipe.miscs[0].amount_is_weight == True) + assert(recipe.miscs[0].time == 15) + assert(recipe.miscs[0].notes == "Half a tablet @ 15 minutes") + + def test_node_to_object(self): "test XML node parsing to Python object" - node = ElementTree.Element("hop", { - "name" : "Simcoe", - "alpha" : 13, - "amount" : 0.5, - "use" : "boil", - "time" : 30 - }) + node = Element("hop") + SubElement(node, "name").text = "Simcoe" + SubElement(node, "alpha").text = 13 + SubElement(node, "amount").text = 0.5 + SubElement(node, "use").text = "boil" + SubElement(node, "time").text = 30 test_hop = Hop() recipe_parser = Parser() - recipe_parser.node_to_object(node, test_hop) + recipe_parser.nodes_to_object(node, test_hop) + + assert(test_hop.name == "Simcoe") + assert(test_hop.alpha == 13) + assert(test_hop.amount == 0.5) + assert(test_hop.use == "boil") + assert(test_hop.time == 30) - assert(test_hop.name, "Simcoe") - assert(test_hop.alpha, 13) - assert(test_hop.amount, 0.5) - assert(test_hop.use, "boil") - assert(test_hop.time, 30) def test_to_lower(self):