mirror of
https://github.com/shouptech/humulus.git
synced 2026-02-03 18:09:44 +00:00
256 lines
7.5 KiB
Python
256 lines
7.5 KiB
Python
# Copyright 2019 Mike Shoup
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
from decimal import Decimal
|
|
|
|
from humulus.couch import get_db, get_doc, put_doc
|
|
from humulus.recipes import FermentableForm, HopForm, RecipeForm, YeastForm
|
|
|
|
|
|
def test_create(client, app):
|
|
"""Test success in creating a recipe document."""
|
|
# Test GET
|
|
response = client.get('/recipes/create')
|
|
assert response.status_code == 200
|
|
|
|
# Test POST
|
|
data = {
|
|
'efficiency': '65',
|
|
'name': 'Test',
|
|
'notes': 'Test',
|
|
'volume': '5.5',
|
|
}
|
|
response = client.post('/recipes/create', data=data)
|
|
assert response.status_code == 302
|
|
|
|
with app.app_context():
|
|
doc = get_doc('test')
|
|
|
|
assert doc['name'] == 'Test'
|
|
assert doc['notes'] == 'Test'
|
|
assert doc['volume'] == '5.5'
|
|
assert doc['efficiency'] == '65'
|
|
|
|
|
|
def test_update(client, app):
|
|
"""Test success in updating a recipe document."""
|
|
with app.app_context():
|
|
doc = put_doc({
|
|
'_id': 'test-update',
|
|
'name': 'Test Update',
|
|
'efficiency': '60',
|
|
'volume': '5.5',
|
|
'notes': 'This is a test',
|
|
'fermentables': [],
|
|
'hops': []
|
|
})
|
|
|
|
# Test GET
|
|
response = client.get('/recipes/update/test-update')
|
|
assert response.status_code == 200
|
|
assert b'Test Update' in response.data
|
|
|
|
# Make a change to the doc
|
|
data = {
|
|
'name': 'New Name',
|
|
'efficiency': '60',
|
|
'volume': '5.5',
|
|
'notes': 'This is a test'
|
|
}
|
|
response = client.post('/recipes/update/test-update',
|
|
query_string={'rev': doc['_rev']}, data=data)
|
|
assert response.status_code == 302
|
|
with client.session_transaction() as session:
|
|
flash_message = dict(session['_flashes']).pop('danger', None)
|
|
assert flash_message is None
|
|
with app.app_context():
|
|
updated = get_doc('test-update')
|
|
assert updated['name'] == 'New Name'
|
|
|
|
# Test form is filled correctly with ingredients
|
|
with app.app_context():
|
|
doc = put_doc({
|
|
'_id': 'test-update-1',
|
|
'name': 'Test Update With Ingredients',
|
|
'efficiency': '60',
|
|
'volume': '5.5',
|
|
'notes': 'This is a test',
|
|
'fermentables': [{
|
|
'name': '2-row',
|
|
'type': 'Grain',
|
|
'amount': '5',
|
|
'ppg': '37',
|
|
'color': '1.8'
|
|
}],
|
|
'hops': [{
|
|
'name': 'Nugget',
|
|
'use': 'Boil',
|
|
'alpha': '5.5',
|
|
'duration': '60',
|
|
'amount': '1'
|
|
}],
|
|
'yeast': {
|
|
'name': 'California Ale',
|
|
'low_attenuation': '70',
|
|
'high_attenuation': '80',
|
|
'type': 'Liquid',
|
|
'lab': 'Inland Island',
|
|
'code': 'INIS-001',
|
|
'flocculation': 'Medium',
|
|
'min_temperature': '60',
|
|
'max_temperature': '70',
|
|
'abv_tolerance': '15'
|
|
}
|
|
})
|
|
response = client.get('/recipes/update/test-update-1')
|
|
assert b'Test Update With Ingredients' in response.data
|
|
|
|
|
|
# Test response without valid/conflicted rev
|
|
response = client.post('/recipes/update/test-update',
|
|
query_string={'rev': ''}, data=data)
|
|
assert response.status_code == 302
|
|
with client.session_transaction() as session:
|
|
flash_message = dict(session['_flashes']).pop('danger', None)
|
|
assert 'Update conflict' in flash_message
|
|
|
|
|
|
def test_info(client):
|
|
"""Test success in retrieving a recipe document."""
|
|
# Validate 404
|
|
response = client.get('/recipes/info/thisdoesnotexist')
|
|
assert response.status_code == 404
|
|
|
|
# Validate response for existing doc
|
|
response = client.get('/recipes/info/awesome-lager')
|
|
assert response.status_code == 200
|
|
assert b'Awesome Lager' in response.data
|
|
|
|
|
|
def test_yeast_form_doc(app):
|
|
"""Evaluates conditionals in generation of doc from a yeast form."""
|
|
yeast = YeastForm()
|
|
yeast.name.data = 'Test'
|
|
yeast.low_attenuation.data = Decimal('60')
|
|
yeast.high_attenuation.data = Decimal('75')
|
|
|
|
assert yeast.doc == {
|
|
'name': 'Test',
|
|
'low_attenuation': '60',
|
|
'high_attenuation': '75'
|
|
}
|
|
|
|
yeast.type.data = 'Dry'
|
|
yeast.code.data = 'INIS-001'
|
|
yeast.lab.data = 'Inland Island'
|
|
yeast.flocculation.data = 'Low'
|
|
yeast.min_temperature.data = Decimal('40')
|
|
yeast.max_temperature.data = Decimal('50')
|
|
yeast.abv_tolerance.data = Decimal('15')
|
|
|
|
assert yeast.doc == {
|
|
'name': 'Test',
|
|
'low_attenuation': '60',
|
|
'high_attenuation': '75',
|
|
'flocculation': 'Low',
|
|
'type': 'Dry',
|
|
'code': 'INIS-001',
|
|
'lab': 'Inland Island',
|
|
'min_temperature': '40',
|
|
'max_temperature': '50',
|
|
'abv_tolerance': '15'
|
|
}
|
|
|
|
|
|
def test_recipe_form_doc(app):
|
|
"""Test if a recipeform can be turned into a document.
|
|
|
|
This test also tests that subforms can be turned into a document. Subforms
|
|
are not tested individually since they will never be used individually.
|
|
"""
|
|
with app.app_context():
|
|
recipe = RecipeForm()
|
|
|
|
recipe.name.data = 'Test'
|
|
recipe.efficiency.data = Decimal('65')
|
|
recipe.volume.data = Decimal('5.5')
|
|
recipe.notes.data = 'This is a test'
|
|
|
|
assert recipe.doc == {
|
|
'name': 'Test',
|
|
'efficiency': '65',
|
|
'volume': '5.5',
|
|
'notes': 'This is a test',
|
|
'fermentables': [],
|
|
'hops': [],
|
|
}
|
|
|
|
ferm = FermentableForm()
|
|
ferm.name.data = 'Test'
|
|
ferm.type.data = 'Grain'
|
|
ferm.amount.data = Decimal('5.5')
|
|
ferm.ppg.data = Decimal('37')
|
|
ferm.color.data = Decimal('1.8')
|
|
|
|
hop = HopForm()
|
|
hop.name.data = 'Test'
|
|
hop.use.data = 'Boil'
|
|
hop.alpha.data = Decimal('12.5')
|
|
hop.duration.data = Decimal('60')
|
|
hop.amount.data = Decimal('0.5')
|
|
|
|
yeast = YeastForm()
|
|
yeast.name.data = 'Test'
|
|
yeast.low_attenuation.data = '70'
|
|
yeast.high_attenuation.data = '75'
|
|
|
|
recipe.fermentables = [ferm]
|
|
recipe.hops = [hop]
|
|
recipe.yeast = yeast
|
|
|
|
assert recipe.doc == {
|
|
'name': 'Test',
|
|
'efficiency': '65',
|
|
'volume': '5.5',
|
|
'notes': 'This is a test',
|
|
'fermentables': [{
|
|
'name': 'Test',
|
|
'type': 'Grain',
|
|
'amount': '5.5',
|
|
'ppg': '37',
|
|
'color': '1.8',
|
|
}],
|
|
'hops': [{
|
|
'name': 'Test',
|
|
'use': 'Boil',
|
|
'alpha': '12.5',
|
|
'duration': '60',
|
|
'amount': '0.5'
|
|
}],
|
|
'yeast': {
|
|
'name': 'Test',
|
|
'low_attenuation': '70',
|
|
'high_attenuation': '75'
|
|
}
|
|
}
|
|
|
|
|
|
def test_recipe_delete(client):
|
|
"""Test success in deleting a document."""
|
|
response = client.post('/recipes/delete/awesome-lager')
|
|
assert response.status_code == 302
|
|
|
|
# Try to get the doc now
|
|
response = client.get('/recipes/info/awesome-lager')
|
|
assert response.status_code == 404
|