From 7f94fabd6d9dead0e609e1f86c728f2e607c713f Mon Sep 17 00:00:00 2001 From: Mike Shoup Date: Tue, 25 Jun 2019 15:36:19 -0600 Subject: [PATCH] Add more complete tests for updates --- src/humulus/templates/recipes/info.html | 18 +++- tests/conftest.py | 71 +++++++++++++++- tests/test_recipes.py | 108 +++++++++--------------- 3 files changed, 121 insertions(+), 76 deletions(-) diff --git a/src/humulus/templates/recipes/info.html b/src/humulus/templates/recipes/info.html index 43e3991..0360891 100644 --- a/src/humulus/templates/recipes/info.html +++ b/src/humulus/templates/recipes/info.html @@ -86,6 +86,7 @@ {#- Yeast -#} +{% if yeast in recipe %}

Yeast

@@ -104,13 +105,22 @@ - - - - + + + +
{{ recipe.yeast.name }} {{ recipe.yeast.lab }} {{ recipe.yeast.code }}{{ recipe.yeast.low_attenuation|int }}% - {{ recipe.yeast.high_attenuation|int }}%{{ recipe.yeast.min_temperature|int }}°F - {{ recipe.yeast.max_temperature|int }}°F{{ recipe.yeast.flocculation }}{{ recipe.yeast.abv_tolerance|int }}% + {% if 'low_attenuation' in recipe.yeast %}{{ recipe.yeast.low_attenuation|int }}%{% endif %} + {% if 'low_attenuation' in recipe.yeast and 'high_attenuation' in recipe.yeast %} - {% endif %} + {% if 'high_attenuation' in recipe.yeast %}{{ recipe.yeast.high_attenuation|int }}%{% endif %} + + {% if 'min_temperature' in recipe.yeast %}{{ recipe.yeast.min_temperature|int }}°F{% endif %} + {% if 'min_temperature' in recipe.yeast and 'max_temperature' in recipe.yeast %} - {% endif %} + {% if 'max_temperature' in recipe.yeast %}{{ recipe.yeast.max_temperature|int }}°F{% endif %} + {% if 'flocculation' in recipe.yeast %}{{ recipe.yeast.flocculation }}{% endif %}{% if 'abv_tolerance' in recipe.yeast %}{{ recipe.yeast.abv_tolerance|int }}%{% endif %}
+{% endif %} {#- Buttons to do things -#} diff --git a/tests/conftest.py b/tests/conftest.py index 528fb53..103d8c7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -37,13 +37,80 @@ def app(): # Add a test doc put_doc({'data': 'test', '_id': 'foobar'}) - # Add a test recipe + # Add a couple test recipe put_doc({ '_id': 'awesome-lager', 'efficiency': '65', 'name': 'Awesome Lager', 'notes': 'Test', - 'volume': '5.5' + 'volume': '5.5', + 'fermentables': [], + 'hops': [] + }) + put_doc({ + '_id': 'partial-yeast-recipe', + 'efficiency': '75', + 'name': 'Partial Beer', + 'notes': 'Contains only required fields for yeast.', + 'volume': '3.5', + 'fermentables': [], + 'hops': [], + 'yeast': { + 'name': 'US-05', + 'low_attenuation': '60', + 'high_attenuation': '72', + } + }) + put_doc({ + '_id': 'full-recipe', + 'efficiency': '78', + 'name': 'Awesome Beer', + 'notes': 'This is a test beer that contains most possible fields.', + 'volume': '2.5', + 'fermentables': [ + { + 'name': '2row', + 'type': 'Grain', + 'amount': '5', + 'ppg': '37', + 'color': '2' + }, + { + 'name': 'Dextrose', + 'type': 'Sugar', + 'amount': '1', + 'ppg': '46', + 'color': '1' + } + ], + 'hops': [ + { + 'name': 'Nugget (US)', + 'use': 'Boil', + 'alpha': '12.5', + 'duration': '60', + 'amount': '1' + }, + { + 'name': 'CTZ (US)', + 'use': 'Dry-Hop', + 'alpha': '16', + 'duration': '5', + 'amount': '0.5' + } + ], + 'yeast': { + 'name': 'Northern California Ale', + 'type': 'Liquid', + 'lab': 'Inland Island', + 'code': 'INIS-001', + 'flocculation': 'Medium', + 'low_attenuation': '73', + 'high_attenuation': '77', + 'min_temperature': '60', + 'max_temperature': '72', + 'abv_tolerance': '10' + } }) yield app diff --git a/tests/test_recipes.py b/tests/test_recipes.py index b687b34..dbfa623 100644 --- a/tests/test_recipes.py +++ b/tests/test_recipes.py @@ -45,81 +45,49 @@ def test_create(client, app): 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') + # Test GET on a bare minimum recipe + response = client.get('/recipes/update/awesome-lager') assert response.status_code == 200 - assert b'Test Update' in response.data + assert b'Awesome Lager' 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) + # Test GET on a more complete recipe + response = client.get('/recipes/update/full-recipe') + assert response.status_code == 200 + test_items = [ + b'Awesome Beer', + b'2row', + b'Dextrose', + b'Nugget (US)', + b'CTZ (US)', + b'Northern California Ale' + ] + for item in test_items: + assert item in response.data + + # Test GET on a recipe missing most yeast fields + response = client.get('/recipes/update/partial-yeast-recipe') + assert response.status_code == 200 + test_items = [ + b'Partial Beer', + b'US-05' + ] + for item in test_items: + assert item in response.data + + # Get a doc, make an update, and test a POST + id = 'awesome-lager' + with app.app_context(): + doc = get_doc(id) + # Remove unneeded fields + doc.pop('_id') + rev = doc.pop('_rev') + response = client.post('/recipes/update/awesome-lager', + query_string={'rev': rev}, data=doc) 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) + response = client.post('/recipes/update/awesome-lager', + query_string={'rev': ''}, data=doc) assert response.status_code == 302 with client.session_transaction() as session: flash_message = dict(session['_flashes']).pop('danger', None)