Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add some tests for the calories views and methods

Roland Geider3712539f6062
diff --git a/wger/manager/fixtures/test-user-data.json b/wger/manager/fixtures/test-user-data.json
index 6d178e7c1..6051fe018 100644
--- a/wger/manager/fixtures/test-user-data.json
+++ b/wger/manager/fixtures/test-user-data.json
@@ -78,7 +78,19 @@
"is_temporary": false,
"show_comments": false,
"show_english_ingredients": false,
- "user": 1
+ "user": 1,
+ "gender": "1",
+ "age": 30,
+ "sport_intensity": "3",
+ "calories": 3500,
+ "height": 180,
+ "sport_hours": 6,
+ "sleep_hours": 7,
+ "work_intensity": "1",
+ "is_temporary": false,
+ "freetime_intensity": "1",
+ "work_hours": 8,
+ "freetime_hours": 8
}
},
{
@@ -88,7 +100,19 @@
"is_temporary": false,
"show_comments": false,
"show_english_ingredients": false,
- "user": 2
+ "user": 2,
+ "gender": "1",
+ "age": 20,
+ "sport_intensity": "3",
+ "calories": 3500,
+ "height": 180,
+ "sport_hours": 6,
+ "sleep_hours": 7,
+ "work_intensity": "1",
+ "is_temporary": false,
+ "freetime_intensity": "1",
+ "work_hours": 8,
+ "freetime_hours": 8
}
},
{
@@ -98,7 +122,19 @@
"is_temporary": true,
"show_comments": false,
"show_english_ingredients": false,
- "user": 3
+ "user": 3,
+ "gender": "2",
+ "age": 50,
+ "sport_intensity": "2",
+ "calories": 3500,
+ "height": 180,
+ "sport_hours": 6,
+ "sleep_hours": 7,
+ "work_intensity": "3",
+ "is_temporary": false,
+ "freetime_intensity": "3",
+ "work_hours": 8,
+ "freetime_hours": 8
}
}
]
diff --git a/wger/manager/models.py b/wger/manager/models.py
index 084cf850a..d7890d4a1 100644
--- a/wger/manager/models.py
+++ b/wger/manager/models.py
@@ -18,6 +18,7 @@
import datetime
import logging
import json
+import decimal
from django.db import models
from django.db.models.signals import post_save
@@ -705,12 +706,18 @@ by the US Department of Agriculture. It is extremely complete, with around
factor = 5
else:
factor = -161
- rate = ((10 * self.weight) # in kg
- + (6.25 * self.height) # in cm
- - (5 * self.age) # in years
- + factor
- )
- return rate
+
+ try:
+ rate = ((10 * self.weight) # in kg
+ + (6.25 * self.height) # in cm
+ - (5 * self.age) # in years
+ + factor
+ )
+ except TypeError:
+ # Any of the entries is missing
+ rate = 0
+
+ return decimal.Decimal(rate).quantize(decimal.Decimal('.01'))
def calculate_activities(self):
'''
@@ -732,8 +739,7 @@ by the US Department of Agriculture. It is extremely complete, with around
work_factor = 2.2
work = self.work_hours * work_factor
- # Sport
- # Sport is given in hours/week, so divide
+ # Sport (entered in hours/week, so we must divide)
if self.sport_intensity == self.INTENSITY_LOW:
sport_factor = 4
elif self.sport_intensity == self.INTENSITY_MEDIUM:
@@ -753,7 +759,7 @@ by the US Department of Agriculture. It is extremely complete, with around
# Total
total = (sleep + work + sport + freetime) / 24.0
- return total
+ return decimal.Decimal(total).quantize(decimal.Decimal('.01'))
# Every new user gets a profile
diff --git a/wger/manager/tests/test_preferences.py b/wger/manager/tests/test_preferences.py
index 8dc5e1446..58d681da5 100644
--- a/wger/manager/tests/test_preferences.py
+++ b/wger/manager/tests/test_preferences.py
@@ -13,9 +13,13 @@
# You should have received a copy of the GNU Affero General Public License
import logging
+import datetime
+import decimal
from django.core.urlresolvers import reverse
+from django.contrib.auth.models import User
+from wger.weight.models import WeightEntry
from wger.manager.tests.testcase import WorkoutManagerTestCase
logger = logging.getLogger('wger.custom')
@@ -121,3 +125,90 @@ class AjaxPreferencesTestCase(WorkoutManagerTestCase):
self.user_login('test')
self.preferences()
+
+
+class PreferencesCalculationsTestCase(WorkoutManagerTestCase):
+ '''
+ Tests the different calculation method in the user profile
+ '''
+
+ def test_last_weight_entry(self):
+ '''
+ Tests that the last weight entry is correctly returned
+ '''
+ self.user_login('test')
+ user = User.objects.get(pk=2)
+ entry = WeightEntry()
+ entry.creation_date = datetime.datetime.today()
+ entry.user = user
+ entry.weight = 100
+ entry.save()
+ self.assertEqual(user.userprofile.weight, 100)
+
+ entry.weight = 150
+ entry.save()
+ self.assertEqual(user.userprofile.weight, 150)
+
+ def test_last_weight_entry_empty(self):
+ '''
+ Tests that the last weight entry is correctly returned if no matches
+ '''
+ self.user_login('test')
+ user = User.objects.get(pk=2)
+ WeightEntry.objects.filter(user=user).delete()
+ self.assertEqual(user.userprofile.weight, 0)
+
+ def test_bmi(self):
+ '''
+ Tests the BMI calculator
+ '''
+
+ self.user_login('test')
+
+ user = User.objects.get(pk=2)
+ bmi = user.userprofile.calculate_bmi()
+ self.assertEqual(bmi, user.userprofile.weight / (1.80 * 1.80))
+
+ def test_basal_metabolic_rate(self):
+ '''
+ Tests the BMR calculator
+ '''
+
+ self.user_login('test')
+
+ # Male
+ user = User.objects.get(pk=2)
+ bmr = user.userprofile.calculate_basal_metabolic_rate()
+ self.assertEqual(bmr, 1860)
+
+ # Female
+ user.userprofile.gender = "2"
+ bmr = user.userprofile.calculate_basal_metabolic_rate()
+ self.assertEqual(bmr, 1694)
+
+ # Data missing
+ user.userprofile.age = None
+ bmr = user.userprofile.calculate_basal_metabolic_rate()
+ self.assertEqual(bmr, 0)
+
+ def test_calculate_activities(self):
+ '''
+ Tests the calories calculator for physical activities
+ '''
+
+ self.user_login('test')
+ user = User.objects.get(pk=2)
+
+ self.assertEqual(user.userprofile.calculate_activities(), decimal.Decimal('1.57'))
+
+ # Gender has no influence
+ user.userprofile.gender = "2"
+ self.assertEqual(user.userprofile.calculate_activities(), decimal.Decimal('1.57'))
+
+ # Change some of the parameters
+ user.userprofile.work_intensity = '3'
+ self.assertEqual(user.userprofile.calculate_activities(), decimal.Decimal('1.80'))
+
+ user.userprofile.work_intensity = '2'
+ user.userprofile.sport_intensity = '2'
+ self.assertEqual(user.userprofile.calculate_activities(), decimal.Decimal('1.52'))
diff --git a/wger/nutrition/templates/rate/form.html b/wger/nutrition/templates/rate/form.html
index 5828b1219..0fc11e666 100644
--- a/wger/nutrition/templates/rate/form.html
+++ b/wger/nutrition/templates/rate/form.html
@@ -73,7 +73,7 @@ $(document).ready(function() {
<p>{% blocktrans %}The basal metabolic rate is the amount of energy expended daily
at rest. This is the minimum energy needed for the body to function, without any
additional physical activity.{% endblocktrans %}</p>
-<form action="{% url 'calories-calculate' %}"
+<form action="{% url 'calories-calculate-bmr' %}"
method="post"
class="ym-form calories-autoform"
id="bmr-form">
diff --git a/wger/nutrition/tests/test_calories_calculator.py b/wger/nutrition/tests/test_calories_calculator.py
new file mode 100644
index 000000000..5eceb72ba
--- /dev/null
+++ b/wger/nutrition/tests/test_calories_calculator.py
@@ -0,0 +1,128 @@
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+import json
+import decimal
+import datetime
+
+from django.core.urlresolvers import reverse
+from django.contrib.auth.models import User
+
+from wger.weight.models import WeightEntry
+from wger.manager.tests.testcase import WorkoutManagerTestCase
+
+
+class CaloresCalculatorTestCase(WorkoutManagerTestCase):
+ '''
+ Tests the calories calculator methods and views
+ '''
+
+ def test_page(self):
+ '''
+ Access the page
+ '''
+
+ response = self.client.get(reverse('calories-calculator'))
+ self.assertEqual(response.status_code, 200)
+
+ def test_calculator(self):
+ '''
+ Tests the calculator itself
+ '''
+
+ self.user_login('test')
+ response = self.client.post(reverse('calories-calculate-activities'),
+ {'sleep_hours': 7,
+ 'work_hours': 8,
+ 'work_intensity': 1,
+ 'sport_hours': 6,
+ 'sport_intensity': 3,
+ 'freetime_hours': 8,
+ 'freetime_intensity': 1})
+ self.assertEqual(response.status_code, 200)
+ result = json.loads(response.content)
+ self.assertEqual(decimal.Decimal(result['factor']), decimal.Decimal('1.57'))
+ self.assertEqual(decimal.Decimal(result['activities']), decimal.Decimal('2920'))
+
+ def test_automatic_weight_entry(self):
+ '''
+ Tests that weight entries are automatically created or updated
+ '''
+
+ self.user_login('test')
+ user = User.objects.get(username=self.current_user)
+
+ # Existing weight entry is old, a new one is created
+ entry1 = WeightEntry.objects.filter(user=user).latest()
+ response = self.client.post(reverse('bmi-calculate'),
+ {'height': 180,
+ 'weight': 80})
+ self.assertEqual(response.status_code, 200)
+ entry2 = WeightEntry.objects.filter(user=user).latest()
+ self.assertEqual(entry1.weight, 83)
+ self.assertEqual(entry2.weight, 80)
+
+ # Existing weight entry is from today, is updated
+ entry2.delete()
+ entry1.creation_date = datetime.date.today()
+ entry1.save()
+ response = self.client.post(reverse('bmi-calculate'),
+ {'height': 180,
+ 'weight': 80})
+ self.assertEqual(response.status_code, 200)
+ entry2 = WeightEntry.objects.filter(user=user).latest()
+ self.assertEqual(entry1.pk, entry2.pk)
+ self.assertEqual(entry2.weight, 80)
+
+ # No existing entries
+ WeightEntry.objects.filter(user=user).delete()
+ response = self.client.post(reverse('bmi-calculate'),
+ {'height': 180,
+ 'weight': 80})
+ self.assertEqual(response.status_code, 200)
+ entry = WeightEntry.objects.filter(user=user).latest()
+ self.assertEqual(entry.weight, 80)
+ self.assertEqual(entry.creation_date, datetime.date.today())
+
+ def test_bmr(self):
+ '''
+ Tests the BMR view
+ '''
+
+ self.user_login('test')
+ response = self.client.post(reverse('calories-calculate-bmr'),
+ {'age': 30,
+ 'height': 180,
+ 'gender': 1,
+ 'weight': 80})
+ self.assertEqual(response.status_code, 200)
+ result = json.loads(response.content)
+ self.assertEqual(result, {'bmr': '1810'})
+
+ def test_total_calories_form(self):
+ '''
+ Tests the form with the total calories
+ '''
+
+ self.user_login('test')
+ user = User.objects.get(username=self.current_user)
+ self.assertEqual(user.userprofile.calories, 3500)
+
+ response = self.client.post(reverse('calories-calculate-total'),
+ {'additional_calories': 0,
+ 'calories': 2000})
+ self.assertEqual(response.status_code, 200)
+
+ user = User.objects.get(username=self.current_user)
+ self.assertEqual(user.userprofile.calories, 2000)
diff --git a/wger/nutrition/urls.py b/wger/nutrition/urls.py
index 3bfa4e701..745a6bf6c 100644
--- a/wger/nutrition/urls.py
+++ b/wger/nutrition/urls.py
@@ -126,7 +126,7 @@ urlpatterns = patterns('wger.nutrition.views',
name='calories-calculator'),
url(r'^calculator/calories/bmr$',
'calculator.calculate_bmr',
- name='calories-calculate'),
+ name='calories-calculate-bmr'),
url(r'^calculator/calories/activities$',
'calculator.calculate_activities',
name='calories-calculate-activities'), # JS