Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add tests for csv weight import
Roland Geider41f9f38f4556
diff --git a/weight/helpers.py b/weight/helpers.pyindex 03cf6e545..e2ba16783 100644--- a/weight/helpers.py+++ b/weight/helpers.py@@ -30,7 +30,7 @@ def parse_weight_csv(request, cleaned_data):try:dialect = csv.Sniffer().sniff(cleaned_data['csv_input'])except csv.Error:- logger.debug('Error while sniffing CSV format')+ #logger.debug('Error while sniffing CSV format')dialect='excel'# csv.reader expects a file-like object, so use StringIO@@ -42,12 +42,15 @@ def parse_weight_csv(request, cleaned_data):for row in parsed_csv:try:parsed_date = datetime.datetime.strptime(row[0], cleaned_data['date_format'])- parsed_weight = decimal.Decimal(row[1].replace(',', '.'))+ parsed_weight = decimal.Decimal(row[1].replace(',', '.'))+weight_list.append(WeightEntry(creation_date=parsed_date,weight=parsed_weight,user=request.user))- except (ValueError, decimal.InvalidOperation), e:++ except (ValueError, IndexError, decimal.InvalidOperation), e:error_list.append(row)- logger.debug(e)+ #logger.debug(row)+ #logger.debug(e)return (weight_list, error_list)diff --git a/weight/tests/__init__.py b/weight/tests/__init__.pynew file mode 100644index 000000000..4ba898978--- /dev/null+++ b/weight/tests/__init__.py@@ -0,0 +1,2 @@+from weight.tests.entry import *+from weight.tests.csv_import import *diff --git a/weight/tests/csv_import.py b/weight/tests/csv_import.pynew file mode 100644index 000000000..aa029d314--- /dev/null+++ b/weight/tests/csv_import.py@@ -0,0 +1,96 @@+# This file is part of Workout Manager.+#+# 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.+#+# 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 logging+++from django.test import TestCase+from django.core.urlresolvers import reverse++from weight.models import WeightEntry++from manager.tests.testcase import WorkoutManagerTestCase++logger = logging.getLogger('workout_manager.custom')+++class WeightCsvImportTestCase(WorkoutManagerTestCase):+ '''+ Test case for the CSV import for weight entries+ '''++ def import_csv(self, fail = False):+ '''+ Helper function to test the CSV import+ '''+ response = self.client.get(reverse('weight-import-csv'))++ if fail:+ # There is a redirect+ self.assertEqual(response.status_code, 302)+ else:+ # Logged in users see a page+ self.assertEqual(response.status_code, 200)++ # Do a direct post request+ # 1st step+ count_before = WeightEntry.objects.count()+ csv_input = '''Datum Gewicht KJ+05.01.10 error here 111+22.01.aa 69,2 222+27.01.10 69,6 222+02.02.10 69 222+11.02.10 70,4 222+19.02.10 71 222+26.02.10 71,9 222+19.03.10 72 222'''+ response = self.client.post(reverse('weight-import-csv'),+ {'stage': 1,+ 'csv_input': csv_input,+ 'date_format': '%d.%m.%y'})++ if fail:+ self.assertEqual(response.status_code, 302)++ else:+ self.assertEqual(response.status_code, 200)+ self.assertEqual(len(response.context['weight_list']), 6)+ self.assertEqual(len(response.context['error_list']), 3)++ # 2nd. step+ response = self.client.post(reverse('weight-import-csv'),+ {'stage': 2,+ 'hash': 'fde7e06185aad7151b8c93ee961499ebabb7983d',+ 'csv_input': csv_input,+ 'date_format': '%d.%m.%y'})++ count_after = WeightEntry.objects.count()+ self.assertEqual(response.status_code, 302)++ if fail:+ self.assertEqual(count_before, count_after)+ else:+ self.assertGreater(count_after, count_before)++ def test_import_csv_anonymous(self):+ """Test deleting a category by an unauthorized user"""++ self.import_csv(fail=True)++ def test_import_csv_loged_in(self):+ """Test deleting a category by a logged in user"""++ self.user_login('test')+ self.import_csv(fail=False)+diff --git a/weight/tests.py b/weight/tests/entry.pysimilarity index 100%rename from weight/tests.pyrename to weight/tests/entry.pydiff --git a/weight/urls.py b/weight/urls.pyindex 702a43f4b..f9c320e09 100644--- a/weight/urls.py+++ b/weight/urls.py@@ -18,7 +18,7 @@ urlpatterns = patterns('weight.views',url(r'^weight/export-csv/$', 'export_csv'),url(r'^weight/import-csv/$',- WeightCsvImportFormPreview(WeightCsvImportForm),+ login_required(WeightCsvImportFormPreview(WeightCsvImportForm)),name='weight-import-csv'),url(r'^weight/overview/$',