Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Testsuite for the email reminder command

Roland Geidere5ad5e2e90e6
diff --git a/wger/manager/fixtures/test-user-data.json b/wger/manager/fixtures/test-user-data.json
index b0b3f2938..bd9ed9fca 100644
--- a/wger/manager/fixtures/test-user-data.json
+++ b/wger/manager/fixtures/test-user-data.json
@@ -250,7 +250,10 @@
"work_intensity": "1",
"freetime_intensity": "1",
"work_hours": 8,
- "freetime_hours": 8
+ "freetime_hours": 8,
+ "workout_reminder_active": false,
+ "workout_reminder": 20,
+ "workout_duration": 8
}
},
{
@@ -271,7 +274,10 @@
"work_intensity": "1",
"freetime_intensity": "1",
"work_hours": 8,
- "freetime_hours": 8
+ "freetime_hours": 8,
+ "workout_reminder_active": true,
+ "workout_reminder": 14,
+ "workout_duration": 12
}
},
{
@@ -292,7 +298,10 @@
"work_intensity": "3",
"freetime_intensity": "3",
"work_hours": 8,
- "freetime_hours": 8
+ "freetime_hours": 8,
+ "workout_reminder_active": true,
+ "workout_reminder": 10,
+ "workout_duration": 15
}
}
]
diff --git a/wger/manager/management/commands/email-remainders.py b/wger/manager/management/commands/email-reminders.py
similarity index 96%
rename from wger/manager/management/commands/email-remainders.py
rename to wger/manager/management/commands/email-reminders.py
index e6d46359c..cdc3c7a1c 100644
--- a/wger/manager/management/commands/email-remainders.py
+++ b/wger/manager/management/commands/email-reminders.py
@@ -49,7 +49,7 @@ class Command(BaseCommand):
(current_workout, schedule) = Schedule.objects.get_current_workout(profile.user)
# No schedules, use the default workout length in user profile
- if not schedule:
+ if not schedule and current_workout:
delta = (current_workout.creation_date
+ datetime.timedelta(weeks=profile.workout_duration)
- datetime.date.today())
@@ -62,7 +62,7 @@ class Command(BaseCommand):
profile.workout_duration)
# non-loop schedule, take the step's duration
- elif not schedule.is_loop:
+ elif schedule and not schedule.is_loop:
schedule_step = schedule.get_current_scheduled_workout()
@@ -84,7 +84,7 @@ class Command(BaseCommand):
pass
if counter:
- self.stdout.write("Sent {0} email remainders".format(counter))
+ self.stdout.write("Sent {0} email reminders".format(counter))
@staticmethod
def send_email(user, workout, days):
diff --git a/wger/manager/tests/test_email_reminder.py b/wger/manager/tests/test_email_reminder.py
new file mode 100644
index 000000000..eb6360299
--- /dev/null
+++ b/wger/manager/tests/test_email_reminder.py
@@ -0,0 +1,163 @@
+# 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 datetime
+
+from django.core.management import call_command
+from django.core import mail
+from django.contrib.auth.models import User
+
+from wger.manager.tests.testcase import WorkoutManagerTestCase
+from wger.manager.models import Schedule
+from wger.manager.models import Workout
+
+
+class EmailReminderTestCase(WorkoutManagerTestCase):
+ '''
+ Tests the email reminder command.
+
+ User 2 has setting in profile active
+ '''
+
+ def atest_reminder_no_workouts(self):
+ '''
+ Test with no schedules or workouts
+ '''
+ Schedule.objects.all().delete()
+ Workout.objects.all().delete()
+
+ call_command('email-reminders')
+ self.assertEqual(len(mail.outbox), 0)
+
+ def atest_reminder_one_workout(self):
+ '''
+ Test user with no schedules but one workout
+
+ User 2, workout created 2012-11-20
+ '''
+
+ Schedule.objects.all().delete()
+ Workout.objects.exclude(user=User.objects.get(pk=2)).delete()
+
+ call_command('email-reminders')
+ self.assertEqual(len(mail.outbox), 1)
+
+ def atest_reminder_setting_off(self):
+ '''
+ Test user with no schedules, one workout but setting in profile off
+ '''
+
+ user = User.objects.get(pk=2)
+ user.userprofile.workout_reminder_active = False
+ user.userprofile.save()
+ Schedule.objects.all().delete()
+ Workout.objects.exclude(user=user).delete()
+
+ call_command('email-reminders')
+ self.assertEqual(len(mail.outbox), 0)
+
+ def atest_reminder_empty_schedule(self):
+ '''
+ Test user with emtpy schedules and no workouts
+ '''
+
+ user = User.objects.get(pk=2)
+ user.userprofile.workout_reminder_active = False
+ user.userprofile.save()
+ Schedule.objects.all().delete()
+ Workout.objects.all().delete()
+
+ schedule = Schedule()
+ schedule.user = user
+ schedule.is_active = True
+ schedule.is_loop = False
+ schedule.name = 'test schedule'
+ schedule.start_date = datetime.date(2013, 1, 10)
+
+ call_command('email-reminders')
+ self.assertEqual(len(mail.outbox), 0)
+
+ def atest_reminder_schedule(self):
+ '''
+ Test user with a schedule and a workout
+ '''
+
+ user = User.objects.get(pk=2)
+ Workout.objects.exclude(user=user).delete()
+ Schedule.objects.exclude(user=user).delete()
+ call_command('email-reminders')
+ self.assertEqual(len(mail.outbox), 1)
+
+ def test_reminder_schedule_recent(self):
+ '''
+ Test user with a schedule that has not finished
+ '''
+
+ user = User.objects.get(pk=1)
+ user.userprofile.workout_reminder_active = True
+ user.userprofile.save()
+ Workout.objects.exclude(user=user).delete()
+ Schedule.objects.exclude(user=user).delete()
+
+ schedule = Schedule.objects.get(pk=2)
+ schedule.start_date = datetime.date.today() - datetime.timedelta(weeks=4)
+ schedule.is_active = True
+ schedule.is_loop = False
+ schedule.save()
+
+ call_command('email-reminders')
+ self.assertEqual(len(mail.outbox), 0)
+
+ def test_reminder_schedule_recent_2(self):
+ '''
+ Test user with a schedule that is about to finish
+ '''
+
+ user = User.objects.get(pk=1)
+ user.userprofile.workout_reminder_active = True
+ user.userprofile.save()
+ Workout.objects.exclude(user=user).delete()
+ Schedule.objects.exclude(user=user).delete()
+
+ # Schedule: 3, 5 and 2 weeks
+ schedule = Schedule.objects.get(pk=2)
+ schedule.start_date = datetime.date.today() - datetime.timedelta(weeks=9)
+ schedule.is_active = True
+ schedule.is_loop = False
+ schedule.save()
+
+ call_command('email-reminders')
+ self.assertEqual(len(mail.outbox), 1)
+
+ def test_reminder_schedule_recent_3(self):
+ '''
+ Test user with a schedule that is about to finish
+ '''
+
+ user = User.objects.get(pk=1)
+ user.userprofile.workout_reminder_active = True
+ user.userprofile.workout_reminder = 5
+ user.userprofile.save()
+ Workout.objects.exclude(user=user).delete()
+ Schedule.objects.exclude(user=user).delete()
+
+ # Schedule: 3, 5 and 2 weeks
+ schedule = Schedule.objects.get(pk=2)
+ schedule.start_date = datetime.date.today() - datetime.timedelta(weeks=9)
+ schedule.is_active = True
+ schedule.is_loop = False
+ schedule.save()
+
+ call_command('email-reminders')
+ self.assertEqual(len(mail.outbox), 0)