Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Fix some tests to be compatible with python3

Roland Geiderf7e3abd86521
diff --git a/wger/core/models.py b/wger/core/models.py
index fd95b2e6a..84a6d89ff 100644
--- a/wger/core/models.py
+++ b/wger/core/models.py
@@ -299,6 +299,12 @@ by the US Department of Agriculture. It is extremely complete, with around
- weight in kg
- height in m
'''
+
+ # If not all the data is available, return 0, otherwise the result
+ # of the calculation below breaks django's template filters
+ if not self.weight or not self.height:
+ return 0
+
return self.weight / (self.height / decimal.Decimal(100) *
self.height / decimal.Decimal(100.0))
diff --git a/wger/core/tests/test_robots_txt.py b/wger/core/tests/test_robots_txt.py
index d1dbdfbdb..55d8f6544 100644
--- a/wger/core/tests/test_robots_txt.py
+++ b/wger/core/tests/test_robots_txt.py
@@ -12,6 +12,7 @@
#
# You should have received a copy of the GNU Affero General Public License
+import six
from django.core.urlresolvers import reverse
from wger.core.models import Language
@@ -27,4 +28,5 @@ class RobotsTxtTestCase(WorkoutManagerTestCase):
response = self.client.get(reverse('robots'))
for lang in Language.objects.all():
- self.assertTrue('wger.de/{0}/sitemap.xml'.format(lang.short_name) in response.content)
+ self.assertTrue('wger.de/{0}/sitemap.xml'.format(lang.short_name)
+ in six.text_type(response.content))
diff --git a/wger/exercises/tests/test_exercise.py b/wger/exercises/tests/test_exercise.py
index 63094f63e..284708a79 100644
--- a/wger/exercises/tests/test_exercise.py
+++ b/wger/exercises/tests/test_exercise.py
@@ -334,7 +334,7 @@ class ExercisesTestCase(WorkoutManagerTestCase):
response = self.client.get(reverse('exercise-search'),
{'term': 'cool'})
self.assertEqual(response.status_code, 200)
- result = json.loads(response.content)
+ result = json.loads(response.content.decode('utf8'))
self.assertEqual(len(result), 1)
self.assertEqual(result[0]['value'], 'Very cool exercise')
self.assertEqual(result[0]['id'], 2)
@@ -346,7 +346,7 @@ class ExercisesTestCase(WorkoutManagerTestCase):
response = self.client.get(reverse('exercise-search'),
{'term': 'Pending'})
self.assertEqual(response.status_code, 200)
- result = json.loads(response.content)
+ result = json.loads(response.content.decode('utf8'))
self.assertEqual(len(result), 0)
def test_search_exercise_anonymous(self):
diff --git a/wger/exercises/tests/test_exercise_images.py b/wger/exercises/tests/test_exercise_images.py
index b9571ce2c..a30a3b494 100644
--- a/wger/exercises/tests/test_exercise_images.py
+++ b/wger/exercises/tests/test_exercise_images.py
@@ -42,7 +42,7 @@ class MainImageTestCase(WorkoutManagerTestCase):
image.status = ExerciseImage.STATUS_ACCEPTED
image.image.save(
filename,
- File(open('wger/exercises/tests/{0}'.format(filename)), 'rb')
+ File(open('wger/exercises/tests/{0}'.format(filename), encoding='latin1'), 'rb')
)
image.save()
diff --git a/wger/manager/tests/testcase.py b/wger/manager/tests/testcase.py
index df3867e90..a7778b4c4 100644
--- a/wger/manager/tests/testcase.py
+++ b/wger/manager/tests/testcase.py
@@ -425,13 +425,7 @@ class WorkoutManagerAccessTestCase(WorkoutManagerTestCase):
if self.__class__.__name__ == 'WorkoutManagerAccessTestCase':
return
- try:
- response = self.client.get(reverse(self.url))
- except NoReverseMatch:
- # URL needs special care and doesn't need to be reversed here,
- # everything was already done in the individual test case
- response = self.client.get(self.url)
-
+ response = self.client.get(get_reverse(self.url))
if fail:
self.assertIn(response.status_code, STATUS_CODES_FAIL)
if response.status_code == 302:
diff --git a/wger/manager/views/set.py b/wger/manager/views/set.py
index d1be032a4..83473ba0e 100644
--- a/wger/manager/views/set.py
+++ b/wger/manager/views/set.py
@@ -23,6 +23,7 @@ from django.shortcuts import get_object_or_404
from django.http import HttpResponseRedirect
from django.http import HttpResponseForbidden
from django.core.urlresolvers import reverse
+from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from django.contrib.auth.decorators import login_required
@@ -195,7 +196,8 @@ def edit(request, pk):
instances = formset['formset'].save(commit=False)
for instance in instances:
# If the setting has already a set, we are editing...
- if hasattr(instance, 'set'):
+ try:
+ instance.set
# Check that we are allowed to do this
if instance.get_owner_object().user != request.user:
@@ -204,7 +206,7 @@ def edit(request, pk):
instance.save()
# ...if not, create a new setting
- else:
+ except ObjectDoesNotExist:
instance.set = set_obj
instance.order = 1
instance.exercise = formset['exercise']
diff --git a/wger/nutrition/models.py b/wger/nutrition/models.py
index 6b7645559..d49071233 100644
--- a/wger/nutrition/models.py
+++ b/wger/nutrition/models.py
@@ -24,6 +24,7 @@ from django.template.defaultfilters import slugify # django.utils.text.slugify
from django.core.validators import MaxValueValidator
from django.core.validators import MinValueValidator
from django.core.exceptions import ValidationError
+from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.core import mail
from django.core.cache import cache
diff --git a/wger/nutrition/templates/bmi/form.html b/wger/nutrition/templates/bmi/form.html
index 138ab2da0..4c47c854e 100644
--- a/wger/nutrition/templates/bmi/form.html
+++ b/wger/nutrition/templates/bmi/form.html
@@ -65,7 +65,7 @@ $(document).ready(function () {
<div id="bmi-result-container" style="display:hidden;">
<h3>{% trans 'Results' %}</h3>
- <p>{% trans 'Your BMI is: ' %} <strong><span id="bmi-result-value">{{user.userprofile.calculate_bmi|floatformat:2}}</span></strong></p>
+ <p>{% trans 'Your BMI is: ' %} <strong><span id="bmi-result-value">{{user.userprofile.calculate_bmi|floatformat:1}}</span></strong></p>
</div>
diff --git a/wger/nutrition/tests/test_bmi.py b/wger/nutrition/tests/test_bmi.py
index e88f9a3d9..325c2f516 100644
--- a/wger/nutrition/tests/test_bmi.py
+++ b/wger/nutrition/tests/test_bmi.py
@@ -38,6 +38,7 @@ class BmiTestCase(WorkoutManagerTestCase):
self.assertEqual(response.status_code, 200)
def test_calculator(self):
+
'''
Tests the calculator itself
'''
@@ -47,7 +48,7 @@ class BmiTestCase(WorkoutManagerTestCase):
{'height': 180,
'weight': 80})
self.assertEqual(response.status_code, 200)
- bmi = json.loads(response.content)
+ bmi = json.loads(response.content.decode('utf8'))
self.assertEqual(decimal.Decimal(bmi['bmi']), decimal.Decimal(24.69).quantize(TWOPLACES))
self.assertEqual(decimal.Decimal(bmi['weight']), decimal.Decimal(80))
self.assertEqual(decimal.Decimal(bmi['height']), decimal.Decimal(180))
diff --git a/wger/nutrition/tests/test_calories_calculator.py b/wger/nutrition/tests/test_calories_calculator.py
index 673c85c10..6ddc74e1a 100644
--- a/wger/nutrition/tests/test_calories_calculator.py
+++ b/wger/nutrition/tests/test_calories_calculator.py
@@ -52,7 +52,7 @@ class CaloriesCalculatorTestCase(WorkoutManagerTestCase):
'freetime_hours': 8,
'freetime_intensity': 1})
self.assertEqual(response.status_code, 200)
- result = json.loads(response.content)
+ result = json.loads(response.content.decode('utf8'))
self.assertEqual(decimal.Decimal(result['factor']),
decimal.Decimal(1.57).quantize(TWOPLACES))
self.assertEqual(decimal.Decimal(result['activities']), decimal.Decimal(2920))
@@ -109,7 +109,7 @@ class CaloriesCalculatorTestCase(WorkoutManagerTestCase):
'gender': 1,
'weight': 80})
self.assertEqual(response.status_code, 200)
- result = json.loads(response.content)
+ result = json.loads(response.content.decode('utf8'))
self.assertEqual(result, {'bmr': '1780'})
def test_automatic_weight_entry_bmr(self):
diff --git a/wger/nutrition/tests/test_ingredient.py b/wger/nutrition/tests/test_ingredient.py
index 79ae9fdaa..a7e735bfc 100644
--- a/wger/nutrition/tests/test_ingredient.py
+++ b/wger/nutrition/tests/test_ingredient.py
@@ -19,6 +19,7 @@ import datetime
from django.core.urlresolvers import reverse
from django.core.exceptions import ValidationError
from wger.core.tests import api_base_test
+from wger.core.models import Language
from wger.nutrition.models import Ingredient
from wger.nutrition.models import Meal
@@ -172,7 +173,7 @@ class IngredientSearchTestCase(WorkoutManagerTestCase):
kwargs = {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
response = self.client.get(reverse('ingredient-search'), {'term': 'test'}, **kwargs)
self.assertEqual(response.status_code, 200)
- result = json.loads(response.content)
+ result = json.loads(response.content.decode('utf8'))
self.assertEqual(len(result), 2)
self.assertEqual(result[0]['value'], 'Ingredient, test, 2, organic, raw')
self.assertEqual(result[1]['value'], 'Test ingredient 1')
@@ -180,7 +181,7 @@ class IngredientSearchTestCase(WorkoutManagerTestCase):
# Search for an ingredient pending review (0 hits, "Pending ingredient")
response = self.client.get(reverse('ingredient-search'), {'term': 'Pending'}, **kwargs)
self.assertEqual(response.status_code, 200)
- result = json.loads(response.content)
+ result = json.loads(response.content.decode('utf8'))
self.assertEqual(len(result), 0)
def test_search_ingredient_anonymous(self):
@@ -216,7 +217,7 @@ class IngredientValuesTestCase(WorkoutManagerTestCase):
'unit': ''})
self.assertEqual(response.status_code, 200)
- result = json.loads(response.content)
+ result = json.loads(response.content.decode('utf8'))
self.assertEqual(len(result), 8)
self.assertEqual(result, {u'sodium': u'0.01',
u'energy': u'1.76',
@@ -234,7 +235,7 @@ class IngredientValuesTestCase(WorkoutManagerTestCase):
'unit': 2})
self.assertEqual(response.status_code, 200)
- result = json.loads(response.content)
+ result = json.loads(response.content.decode('utf8'))
self.assertEqual(len(result), 8)
self.assertEqual(result, {u'sodium': u'0.61',
u'energy': u'196.24',
@@ -269,6 +270,8 @@ class IngredientTestCase(WorkoutManagerTestCase):
'''
Tests the custom compare method based on values
'''
+ language = Language.objects.get(pk=1)
+
ingredient1 = Ingredient.objects.get(pk=1)
ingredient2 = Ingredient.objects.get(pk=1)
ingredient2.name = 'A different name altogether'
@@ -278,10 +281,12 @@ class IngredientTestCase(WorkoutManagerTestCase):
ingredient1.name = 'ingredient name'
ingredient1.energy = 150
ingredient1.protein = 30
+ ingredient1.language = language
ingredient2 = Ingredient()
ingredient2.name = 'ingredient name'
ingredient2.energy = 150
+ ingredient2.language = language
self.assertFalse(ingredient1 == ingredient2)
ingredient2.protein = 31