Skip to content

Commit

Permalink
Test recipe builder with SI units. Allow grain additions to be equal …
Browse files Browse the repository at this point in the history
…if weights round to within 2 decimal points. Add units to GrainAddition repr
  • Loading branch information
chrisgilmerproj committed Oct 4, 2016
1 parent d54ec6e commit adf61d6
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 29 deletions.
3 changes: 2 additions & 1 deletion brew/grains.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,14 @@ def __repr__(self):
out = "{0}, weight={1}".format(out, self.weight)
if self.grain_type:
out = "{0}, grain_type='{1}'".format(out, self.grain_type)
out = "{0}, units='{1}'".format(out, self.units)
out = "{0})".format(out)
return out

def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
if (self.weight == other.weight) and \
if (round(self.weight, 2) == round(other.weight, 2)) and \
(self.grain_type == other.grain_type) and \
(self.units == other.units) and \
(self.grain == other.grain):
Expand Down
5 changes: 2 additions & 3 deletions brew/hops.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ def __repr__(self):
out = "{0}, utilization_cls={1}".format(out, type(self.utilization_cls).__name__) # nopep8
if self.utilization_cls_kwargs:
out = "{0}, utilization_cls_kwargs={1}".format(out, self.utilization_cls_kwargs) # nopep8
if self.units:
out = "{0}, units='{1}'".format(out, self.units)
out = "{0}, units='{1}'".format(out, self.units)
out = "{0})".format(out)
return out

Expand Down Expand Up @@ -253,7 +252,7 @@ def get_hops_weight(self, sg, target_ibu, final_volume,
:param float percent_contribution: The percent contribution of the hops to the total bitterness
:return: The weight of hops
:rtype: float
"""
""" # nopep8
validate_percentage(percent_contribution)

hops_constant = HOPS_CONSTANT_IMPERIAL
Expand Down
2 changes: 1 addition & 1 deletion brew/recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,6 @@ def get_grain_additions(self, percent_list):
for index, grain in enumerate(self.grain_list):
efficiency = self.percent_brew_house_yield
weight = (percent_list[index] * total_points) / (getattr(grain, attr) * efficiency) # nopep8
grain_add = GrainAddition(grain, weight=round(weight, 2))
grain_add = GrainAddition(grain, weight=weight, units=self.units)
grain_additions.append(grain_add)
return grain_additions
2 changes: 1 addition & 1 deletion tests/test_grains.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def test_str(self):

def test_repr(self):
out = repr(self.grain_add)
self.assertEquals(out, "GrainAddition(Grain('pale 2-row', color=2.0, hwe=308.78), weight=13.96, grain_type='cereal')") # nopep8
self.assertEquals(out, "GrainAddition(Grain('pale 2-row', color=2.0, hwe=308.78), weight=13.96, grain_type='cereal', units='imperial')") # nopep8

def test_get_weight_cereal(self):
grain_add = GrainAddition(pale, weight=13.96,
Expand Down
24 changes: 1 addition & 23 deletions tests/test_recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_str(self):

def test_repr(self):
out = repr(self.recipe)
self.assertEquals(out, "Recipe('pale ale', grain_additions=[GrainAddition(Grain('pale 2-row', color=2.0, hwe=308.78), weight=13.96, grain_type='cereal'), GrainAddition(Grain('crystal C20', color=20.0, hwe=292.09), weight=0.78, grain_type='cereal')], hop_additions=[HopAddition(Hop('centennial', percent_alpha_acids=0.14), weight=0.57, boil_time=60.0, hop_type='pellet', utilization_cls=HopsUtilizationGlennTinseth, units='imperial'), HopAddition(Hop('cascade', percent_alpha_acids=0.07), weight=0.76, boil_time=5.0, hop_type='pellet', utilization_cls=HopsUtilizationGlennTinseth, units='imperial')], yeast=Yeast('Wyeast 1056', percent_attenuation=0.75), percent_brew_house_yield=0.7, start_volume=7.0, final_volume=5.0, units=imperial)") # nopep8
self.assertEquals(out, "Recipe('pale ale', grain_additions=[GrainAddition(Grain('pale 2-row', color=2.0, hwe=308.78), weight=13.96, grain_type='cereal', units='imperial'), GrainAddition(Grain('crystal C20', color=20.0, hwe=292.09), weight=0.78, grain_type='cereal', units='imperial')], hop_additions=[HopAddition(Hop('centennial', percent_alpha_acids=0.14), weight=0.57, boil_time=60.0, hop_type='pellet', utilization_cls=HopsUtilizationGlennTinseth, units='imperial'), HopAddition(Hop('cascade', percent_alpha_acids=0.07), weight=0.76, boil_time=5.0, hop_type='pellet', utilization_cls=HopsUtilizationGlennTinseth, units='imperial')], yeast=Yeast('Wyeast 1056', percent_attenuation=0.75), percent_brew_house_yield=0.7, start_volume=7.0, final_volume=5.0, units=imperial)") # nopep8

def test_eq(self):
recipe1 = Recipe('pale ale')
Expand Down Expand Up @@ -203,25 +203,3 @@ def test_set_units(self):
def test_set_raises(self):
with self.assertRaises(Exception):
self.builder.set_units('bad')

def test_change_units(self):
self.assertEquals(self.builder.units, IMPERIAL_UNITS)
builder = self.builder.change_units()
self.assertEquals(builder.units, SI_UNITS)
self.assertEquals(self.builder.units, IMPERIAL_UNITS)

def test_get_grain_additions(self):
percent_list = [0.95, 0.05]
out = self.builder.get_grain_additions(percent_list)
expected = grain_additions
self.assertEquals(out, expected)

def test_get_grain_additions_raises_sum_invalid(self):
percent_list = [0.90, 0.05]
with self.assertRaises(Exception):
self.builder.get_grain_additions(percent_list)

def test_get_grain_additions_raises_length_mismatch(self):
percent_list = [0.90, 0.05, 0.05]
with self.assertRaises(Exception):
self.builder.get_grain_additions(percent_list)
35 changes: 35 additions & 0 deletions tests/test_recipes_imperial.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
from brew.constants import SUCROSE_PLATO
from brew.grains import GrainAddition
from brew.recipes import Recipe
from fixtures import builder
from fixtures import grain_additions
from fixtures import grain_list
from fixtures import hop_additions
from fixtures import pale
from fixtures import recipe
Expand Down Expand Up @@ -310,3 +312,36 @@ def test_format(self):
def test_validate(self):
data = self.recipe.to_dict()
Recipe.validate(data)


class TestRecipeBuilderImperialUnits(unittest.TestCase):

def setUp(self):
# Define Grains
self.grain_list = grain_list

# Define Recipes
self.builder = builder
self.assertEquals(self.builder.units, IMPERIAL_UNITS)

def test_change_units(self):
self.assertEquals(self.builder.units, IMPERIAL_UNITS)
builder = self.builder.change_units()
self.assertEquals(builder.units, SI_UNITS)
self.assertEquals(self.builder.units, IMPERIAL_UNITS)

def test_get_grain_additions(self):
percent_list = [0.95, 0.05]
out = self.builder.get_grain_additions(percent_list)
expected = grain_additions
self.assertEquals(out, expected)

def test_get_grain_additions_raises_sum_invalid(self):
percent_list = [0.90, 0.05]
with self.assertRaises(Exception):
self.builder.get_grain_additions(percent_list)

def test_get_grain_additions_raises_length_mismatch(self):
percent_list = [0.90, 0.05, 0.05]
with self.assertRaises(Exception):
self.builder.get_grain_additions(percent_list)
35 changes: 35 additions & 0 deletions tests/test_recipes_si.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
from brew.constants import SUCROSE_PLATO
from brew.grains import GrainAddition
from brew.recipes import Recipe
from fixtures import builder
from fixtures import grain_additions
from fixtures import grain_list
from fixtures import hop_additions
from fixtures import pale
from fixtures import recipe
Expand Down Expand Up @@ -321,3 +323,36 @@ def test_format(self):
def test_validate(self):
data = self.recipe.to_dict()
Recipe.validate(data)


class TestRecipeBuilderSIUnits(unittest.TestCase):

def setUp(self):
# Define Grains
self.grain_list = grain_list

# Define Recipes
self.builder = builder.change_units()
self.assertEquals(self.builder.units, SI_UNITS)

def test_change_units(self):
self.assertEquals(self.builder.units, SI_UNITS)
builder = self.builder.change_units()
self.assertEquals(builder.units, IMPERIAL_UNITS)
self.assertEquals(self.builder.units, SI_UNITS)

def test_get_grain_additions(self):
percent_list = [0.95, 0.05]
out = self.builder.get_grain_additions(percent_list)
expected = [ga.change_units() for ga in grain_additions]
self.assertEquals(out[0], expected[0])

def test_get_grain_additions_raises_sum_invalid(self):
percent_list = [0.90, 0.05]
with self.assertRaises(Exception):
self.builder.get_grain_additions(percent_list)

def test_get_grain_additions_raises_length_mismatch(self):
percent_list = [0.90, 0.05, 0.05]
with self.assertRaises(Exception):
self.builder.get_grain_additions(percent_list)

0 comments on commit adf61d6

Please sign in to comment.