1
0
Fork 0
mirror of https://github.com/shouptech/humulus.git synced 2026-02-03 18:19:42 +00:00

Add test for copyfrom

This commit is contained in:
Emma 2019-07-18 21:20:28 -06:00
parent c60505ba0a
commit 19ab27b8d3
No known key found for this signature in database
GPG key ID: 68434BFE85360755
4 changed files with 219 additions and 23 deletions

View file

@ -154,14 +154,14 @@ class MashStepForm(Form):
CSRF is disabled for this form (using `Form as parent class) CSRF is disabled for this form (using `Form as parent class)
because it is never used by itself. because it is never used by itself.
""" """
name = StringField('Name', validators=[DataRequired()]) name = StringField('Step Name', validators=[DataRequired()])
type = SelectField('Type', type = SelectField('Type',
choices=[(c, c) for c in ['Infusion', choices=[(c, c) for c in ['Infusion',
'Temperature', 'Temperature',
'Decoction']]) 'Decoction']])
temp = DecimalField('Temperature', validators=[DataRequired()]) temp = DecimalField('Temperature (°F)', validators=[DataRequired()])
time = DecimalField('Time', validators=[DataRequired()]) time = DecimalField('Time (min)', validators=[DataRequired()])
amount = DecimalField('Water Amount') amount = DecimalField('Water Amount (gal)')
@property @property
def doc(self): def doc(self):
@ -186,7 +186,7 @@ class MashForm(Form):
CSRF is disabled for this form (using `Form as parent class) CSRF is disabled for this form (using `Form as parent class)
because it is never used by itself. because it is never used by itself.
""" """
name = StringField('Name', validators=[Optional()]) name = StringField('Mash Name', validators=[Optional()])
steps = FieldList( steps = FieldList(
FormField(MashStepForm), FormField(MashStepForm),
min_entries=0, min_entries=0,
@ -287,35 +287,52 @@ class RecipeForm(FlaskForm):
}) })
if 'yeast' in data: if 'yeast' in data:
yeast = data['yeast'] self.yeast.form.name.data = data['yeast']['name']
self.yeast.form.name.data = yeast['name']
self.yeast.form.low_attenuation.data = ( self.yeast.form.low_attenuation.data = (
Decimal(yeast['low_attenuation']) Decimal(data['yeast']['low_attenuation'])
) )
self.yeast.form.high_attenuation.data = ( self.yeast.form.high_attenuation.data = (
Decimal(yeast['high_attenuation']) Decimal(data['yeast']['high_attenuation'])
) )
if 'type' in yeast: if 'type' in data['yeast']:
self.yeast.form.type.data = yeast['type'] self.yeast.form.type.data = data['yeast']['type']
if 'lab' in yeast: if 'lab' in data['yeast']:
self.yeast.form.lab.data = yeast['lab'] self.yeast.form.lab.data = data['yeast']['lab']
if 'code' in yeast: if 'code' in data['yeast']:
self.yeast.form.code.data = yeast['code'] self.yeast.form.code.data = data['yeast']['code']
if 'flocculation' in yeast: if 'flocculation' in data['yeast']:
self.yeast.form.flocculation.data = yeast['flocculation'] self.yeast.form.flocculation.data = (
if 'min_temperature' in yeast: data['yeast']['flocculation']
)
if 'min_temperature' in data['yeast']:
self.yeast.form.min_temperature.data = ( self.yeast.form.min_temperature.data = (
Decimal(yeast['min_temperature']) Decimal(data['yeast']['min_temperature'])
) )
if 'max_temperature' in yeast: if 'max_temperature' in data['yeast']:
self.yeast.form.max_temperature.data = ( self.yeast.form.max_temperature.data = (
Decimal(yeast['max_temperature']) Decimal(data['yeast']['max_temperature'])
) )
if 'abv_tolerance' in yeast: if 'abv_tolerance' in data['yeast']:
self.yeast.form.abv_tolerance.data = ( self.yeast.form.abv_tolerance.data = (
Decimal(yeast['abv_tolerance']) Decimal(data['yeast']['abv_tolerance'])
) )
if 'mash' in data:
if 'name' in data['mash']:
self.mash.form.name.data = data['mash']['name']
if 'steps' in data['mash']:
for step in data['mash']['steps']:
new_step = {
'name': step['name'],
'type': step['type'],
'temp': Decimal(step['temp']),
'time': Decimal(step['time'])
}
if 'amount' in step:
new_step['amount'] = Decimal(step['amount'])
print(new_step)
self.mash.steps.append_entry(new_step)
class ImportForm(FlaskForm): class ImportForm(FlaskForm):
upload = FileField(validators=[FileRequired()]) upload = FileField(validators=[FileRequired()])

View file

@ -130,6 +130,36 @@ function getSpecsURL() {
</div> </div>
</div> </div>
</div> </div>
{#-
Mash & Steps
-#}
<div class="row"><div class="col"><h3>Mash</h3></div></div>
<div id="mash">
{{ render_field_with_errors(form.mash.form.name, 'form-control-sm') }}
{% for step in form.mash.form.steps %}
<div class="border pl-2 pr-2 pt-1 pb-1 mash-form">
<div class="row">
<div class="col-sm-8">{{ render_field_with_errors(step.form.name, 'form-control-sm') }}</div>
<div class="col-sm-4">{{ render_field_with_errors(step.form.type, 'custom-select-sm', base_class='custom-select') }}</div>
</div>
<div class="row">
<div class="col-sm">{{ render_field_with_errors(step.form.temp, 'form-control-sm') }}</div>
<div class="col-sm">{{ render_field_with_errors(step.form.time, 'form-control-sm') }}</div>
<div class="col-sm">{{ render_field_with_errors(step.form.amount, 'form-control-sm') }}</div>
</div>
<div class="row">
<div class="col">
<button type="button" class="float-right btn btn-sm btn-outline-danger rem-step">Remove Step</button>
</div>
</div>
</div>
{% endfor %}
</div>
<div class="row pt-1 pb-1">
<div class="col">
<button type="button" id="add-step" class="btn btn-secondary btn-sm">Add mash step</button>
</div>
</div>
{#- {#-
Recipe Notes Recipe Notes
-#} -#}

View file

@ -200,11 +200,41 @@
</table> </table>
</div></div> </div></div>
{% endif %} {% endif %}
{% if recipe.mash and recipe.mash.name %}
<div class="row border-top"><h2>Mash</h2></div>
<div class="row">{{ recipe.mash.name }}</div>
<div class="table-responsive">
<table class="table table-hover table-sm">
<thead>
<tr>
<th>#</th>
<th>Step Name</th>
<th>Type</th>
<th>Temp</th>
<th>Time</th>
<th>Amount</th>
</tr>
</thead>
{% for step in recipe.mash.steps %}
<tr>
<td>{{ loop.index }}</td>
<td>{{ step.name }}</td>
<td>{{ step.type }}</td>
<td>{% if step.temp %}{{ step.temp }} °F{% endif %}</td>
<td>{% if step.time %}{{ step.time }} min.{% endif %}</td>
<td>{% if step.amount %}{{ step.amount }} gal.{% endif %}</td>
</tr>
{% endfor %}
</table>
</div>
{% endif %}
{#- {#-
Recipe Notes Recipe Notes
-#} -#}
{% if recipe.notes %}
<div class="row border-top"><h2>Recipe Notes</h2></div> <div class="row border-top"><h2>Recipe Notes</h2></div>
<div class="row"><div class="col">{{ recipe.notes }}</div></div> <div class="row"><div class="col">{{ recipe.notes }}</div></div>
{% endif %}
{#- {#-
Buttons to do things Buttons to do things
-#} -#}

View file

@ -450,3 +450,122 @@ def test_recipe_create_json(client, sample_recipes, auth):
response = client.post('/recipes/create/json', buffered=True, response = client.post('/recipes/create/json', buffered=True,
content_type='multipart/form-data', data=data) content_type='multipart/form-data', data=data)
assert response.status_code == 200 assert response.status_code == 200
def test_copyfrom(app, sample_recipes):
recipe = {
'name': 'Test',
'type': 'All-Grain',
'efficiency': '65',
'volume': '5.5',
'notes': 'Notes',
'style': '18A',
'fermentables': [{
'name': 'Test',
'type': 'Grain',
'amount': '1',
'ppg': '36',
'color': '4'
}],
'hops': [{
'name': 'Test',
'use': 'Boil',
'alpha': '5.5',
'duration': '30',
'amount': '1'
}]
}
with app.app_context():
form = RecipeForm()
form.copyfrom(recipe)
assert form.name.data == recipe['name']
assert form.type.data == recipe['type']
assert form.efficiency.data == Decimal(recipe['efficiency'])
assert form.volume.data == Decimal(recipe['volume'])
assert form.notes.data == recipe['notes']
assert len(form.fermentables) == len(recipe['fermentables'])
assert form.fermentables[0].form.name.data == \
recipe['fermentables'][0]['name']
assert form.fermentables[0].form.type.data == \
recipe['fermentables'][0]['type']
assert form.fermentables[0].form.amount.data == \
Decimal(recipe['fermentables'][0]['amount'])
assert form.fermentables[0].form.ppg.data == \
Decimal(recipe['fermentables'][0]['ppg'])
assert form.fermentables[0].form.color.data == \
Decimal(recipe['fermentables'][0]['color'])
assert len(form.hops) == len(recipe['hops'])
assert form.hops[0].form.name.data == recipe['hops'][0]['name']
assert form.hops[0].form.use.data == recipe['hops'][0]['use']
assert form.hops[0].form.alpha.data == Decimal(recipe['hops'][0]['alpha'])
assert form.hops[0].form.duration.data == \
Decimal(recipe['hops'][0]['duration'])
assert form.hops[0].form.amount.data == \
Decimal(recipe['hops'][0]['amount'])
recipe['yeast'] = {
'name': 'Test', 'low_attenuation': '65', 'high_attenuation': '68'
}
recipe['mash'] = {}
with app.app_context():
form = RecipeForm()
form.copyfrom(recipe)
assert form.yeast.form.name.data == recipe['yeast']['name']
assert form.yeast.form.low_attenuation.data == \
Decimal(recipe['yeast']['low_attenuation'])
assert form.yeast.form.high_attenuation.data == \
Decimal(recipe['yeast']['high_attenuation'])
recipe['yeast'].update({
'type': 'Liquid',
'lab': 'Test',
'code': 'Test',
'flocculation': 'Low',
'min_temperature': '65',
'max_temperature': '68',
'abv_tolerance': '15'
})
with app.app_context():
form = RecipeForm()
form.copyfrom(recipe)
assert form.yeast.form.type.data == recipe['yeast']['type']
assert form.yeast.form.lab.data == recipe['yeast']['lab']
assert form.yeast.form.code.data == recipe['yeast']['code']
assert form.yeast.form.flocculation.data == recipe['yeast']['flocculation']
assert form.yeast.form.min_temperature.data == \
Decimal(recipe['yeast']['min_temperature'])
assert form.yeast.form.max_temperature.data == \
Decimal(recipe['yeast']['max_temperature'])
assert form.yeast.form.abv_tolerance.data == \
Decimal(recipe['yeast']['abv_tolerance'])
recipe['mash'] = {
'name': 'Test',
'steps': [{
'name': 'Infusion',
'type': 'Infusion',
'temp': '152',
'time': '60'
}]
}
with app.app_context():
form = RecipeForm()
form.copyfrom(recipe)
assert form.mash.form.name.data == recipe['mash']['name']
assert len(form.mash.form.steps) == len(recipe['mash']['steps'])
assert form.mash.form.steps[0].form.name.data == \
recipe['mash']['steps'][0]['name']
assert form.mash.form.steps[0].form.type.data == \
recipe['mash']['steps'][0]['type']
assert form.mash.form.steps[0].form.temp.data == \
Decimal(recipe['mash']['steps'][0]['temp'])
assert form.mash.form.steps[0].form.time.data == \
Decimal(recipe['mash']['steps'][0]['time'])
recipe['mash']['steps'][0]['amount'] = '3.5'
with app.app_context():
form = RecipeForm()
form.copyfrom(recipe)
assert form.mash.form.steps[0].form.amount.data == \
Decimal(recipe['mash']['steps'][0]['amount'])