Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add some simple tests for the dummy generators

Roland Geiderd16ebadb79b9
diff --git a/wger/core/management/commands/dummy-generator-users.py b/wger/core/management/commands/dummy-generator-users.py
index 815f7b72a..c1f88fddd 100644
--- a/wger/core/management/commands/dummy-generator-users.py
+++ b/wger/core/management/commands/dummy-generator-users.py
@@ -77,7 +77,7 @@ class Command(BaseCommand):
case _:
gym_list = [options['add_to_gym']]
- for i in range(1, options['number_users']):
+ for i in range(options['number_users']):
uid = uuid.uuid4()
first_name = faker.first_name()
last_name = faker.last_name()
diff --git a/wger/core/tests/test_generator.py b/wger/core/tests/test_generator.py
new file mode 100644
index 000000000..6c3a588c0
--- /dev/null
+++ b/wger/core/tests/test_generator.py
@@ -0,0 +1,34 @@
+# 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
+
+# Django
+from django.contrib.auth.models import User
+from django.core.management import call_command
+
+# wger
+from wger.core.tests.base_testcase import WgerTestCase
+from wger.gym.models import Gym
+
+
+class UserGeneratorTestCase(WgerTestCase):
+
+ def test_generator(self):
+ # Arrange
+ User.objects.all().delete()
+
+ # Act
+ call_command('dummy-generator-users', '--nr-entries', 10)
+
+ # Assert
+ self.assertEqual(User.objects.count(), 10)
diff --git a/wger/exercises/tests/test_exercise_api_cache.py b/wger/exercises/tests/test_exercise_api_cache.py
index 413b3da3c..429b751b4 100644
--- a/wger/exercises/tests/test_exercise_api_cache.py
+++ b/wger/exercises/tests/test_exercise_api_cache.py
@@ -16,9 +16,7 @@
from django.core.cache import cache
# wger
-from wger.core.tests.base_testcase import (
- WgerTestCase,
-)
+from wger.core.tests.base_testcase import WgerTestCase
from wger.exercises.models import (
Alias,
Exercise,
diff --git a/wger/gym/management/commands/dummy-generator-gyms.py b/wger/gym/management/commands/dummy-generator-gyms.py
index 6477a13ac..2f467fcba 100644
--- a/wger/gym/management/commands/dummy-generator-gyms.py
+++ b/wger/gym/management/commands/dummy-generator-gyms.py
@@ -13,12 +13,9 @@
# You should have received a copy of the GNU Affero General Public License
# Standard Library
-import datetime
import logging
-import random
# Django
-from django.contrib.auth.models import User
from django.core.management.base import BaseCommand
# Third Party
@@ -27,8 +24,6 @@ from faker.providers import DynamicProvider
# wger
from wger.gym.models import Gym
-from wger.weight.models import WeightEntry
-
logger = logging.getLogger(__name__)
@@ -88,7 +83,10 @@ class Command(BaseCommand):
elements=self.names_first,
)
- gym_names_2 = DynamicProvider(provider_name="gym_names2", elements=self.names_second)
+ gym_names_2 = DynamicProvider(
+ provider_name="gym_names2",
+ elements=self.names_second,
+ )
faker = Faker()
faker.add_provider(gym_names_1)
@@ -97,13 +95,14 @@ class Command(BaseCommand):
self.stdout.write(f"** Generating {options['number_gyms']} gyms")
gym_list = []
- for i in range(1, options['number_gyms']):
+ for i in range(options['number_gyms']):
found = False
+
+ # We don't want names like "Iron Iron"
while not found:
part1 = faker.gym_names()
part2 = faker.gym_names2()
- # We don't want names like "Iron Iron"
if part1 != part2:
found = True
diff --git a/wger/gym/tests/test_generator.py b/wger/gym/tests/test_generator.py
new file mode 100644
index 000000000..0ed321779
--- /dev/null
+++ b/wger/gym/tests/test_generator.py
@@ -0,0 +1,33 @@
+# 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
+
+# Django
+from django.core.management import call_command
+
+# wger
+from wger.core.tests.base_testcase import WgerTestCase
+from wger.gym.models import Gym
+
+
+class GymGeneratorTestCase(WgerTestCase):
+
+ def test_generator(self):
+ # Arrange
+ Gym.objects.all().delete()
+
+ # Act
+ call_command('dummy-generator-gyms', '--nr-entries', 10)
+
+ # Assert
+ self.assertEqual(Gym.objects.count(), 10)
diff --git a/wger/manager/management/commands/dummy-generator-workout-diary.py b/wger/manager/management/commands/dummy-generator-workout-diary.py
index d1a4e8f25..dc4fe2c70 100644
--- a/wger/manager/management/commands/dummy-generator-workout-diary.py
+++ b/wger/manager/management/commands/dummy-generator-workout-diary.py
@@ -95,7 +95,7 @@ class Command(BaseCommand):
for workout_set in day.set_set.all():
for setting in workout_set.setting_set.all():
for reps in (8, 10, 12):
- for i in range(1, options['nr_diary_entries']):
+ for i in range(options['nr_diary_entries']):
date = datetime.date.today() - datetime.timedelta(weeks=i)
log = WorkoutLog(
user=user,
diff --git a/wger/manager/management/commands/dummy-generator-workout-plans.py b/wger/manager/management/commands/dummy-generator-workout-plans.py
index fd5f71931..0e5920c84 100644
--- a/wger/manager/management/commands/dummy-generator-workout-plans.py
+++ b/wger/manager/management/commands/dummy-generator-workout-plans.py
@@ -53,14 +53,6 @@ class Command(BaseCommand):
type=int,
help='The number of workout plans to create per user (default: 10)'
)
- parser.add_argument(
- '--diary-entries',
- action='store',
- default=30,
- dest='nr_diary_entries',
- type=int,
- help='The number of workout logs to create per day (default: 30)'
- )
parser.add_argument(
'--user-id',
action='store',
@@ -80,7 +72,7 @@ class Command(BaseCommand):
self.stdout.write(f'- processing user {user.username}')
# Add plan
- for _ in range(0, options['nr_plans']):
+ for _ in range(options['nr_plans']):
uid = str(uuid.uuid4()).split('-')
start_date = datetime.date.today() - datetime.timedelta(days=random.randint(0, 100))
workout = Workout(
diff --git a/wger/manager/tests/test_generator.py b/wger/manager/tests/test_generator.py
new file mode 100644
index 000000000..386b508e9
--- /dev/null
+++ b/wger/manager/tests/test_generator.py
@@ -0,0 +1,51 @@
+# 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
+
+# Django
+from django.core.management import call_command
+
+# wger
+from wger.core.tests.base_testcase import WgerTestCase
+from wger.manager.models import (
+ Workout,
+ WorkoutLog,
+)
+
+
+class RoutineGeneratorTestCase(WgerTestCase):
+
+ def test_generator_routines(self):
+ # Arrange
+ Workout.objects.all().delete()
+
+ # Act
+ call_command('dummy-generator-workout-plans', '--plans', 10)
+
+ # Assert
+ self.assertEqual(Workout.objects.filter(user_id=1).count(), 10)
+
+ def test_generator_diary_entries(self):
+ # Arrange
+ Workout.objects.all().delete()
+
+ # Act
+ call_command('dummy-generator-workout-plans', '--plans', 1)
+ call_command('dummy-generator-workout-diary', '--diary-entries', 10)
+
+ # Assert
+ # Things like nr of training days or exercises are random
+ min_entries = 1 * 1 * 3 * 3 * 10
+ max_entries = 1 * 5 * 3 * 10 * 10
+ self.assertGreaterEqual(WorkoutLog.objects.filter(workout__user_id=1).count(), min_entries)
+ self.assertLessEqual(WorkoutLog.objects.filter(workout__user_id=1).count(), max_entries)
diff --git a/wger/measurements/management/commands/dummy-generator-measurement-categories.py b/wger/measurements/management/commands/dummy-generator-measurement-categories.py
index 73205ca9d..3bc1ae427 100644
--- a/wger/measurements/management/commands/dummy-generator-measurement-categories.py
+++ b/wger/measurements/management/commands/dummy-generator-measurement-categories.py
@@ -105,6 +105,7 @@ class Command(BaseCommand):
else User.objects.all()
if options['nr_categories'] > 10:
+ print(options['nr_categories'])
print('10 Categories is the maximum allowed')
sys.exit()
diff --git a/wger/measurements/management/commands/dummy-generator-measurements.py b/wger/measurements/management/commands/dummy-generator-measurements.py
index 802ccd30d..0815b5c55 100644
--- a/wger/measurements/management/commands/dummy-generator-measurements.py
+++ b/wger/measurements/management/commands/dummy-generator-measurements.py
@@ -70,26 +70,29 @@ class Command(BaseCommand):
if options['user_id'] \
else User.objects.all()
- categories = [Category.objects.get(pk=options['category_id'])] \
- if options['category_id'] \
- else Category.objects.all()
-
new_entries = []
for user in users:
+
+ categories = [Category.objects.get(pk=options['category_id'])] \
+ if options['category_id'] \
+ else Category.objects.filter(user=user)
+
self.stdout.write(f'- processing user {user.username}')
for category in categories:
base_value = random.randint(10, 100)
- for i in range(1, options['nr_measurements']):
+ for i in range(options['nr_measurements']):
date = datetime.date.today() - datetime.timedelta(days=2 * i)
- if not Measurement.objects.filter(category=category, date=date).exists():
- measurement = Measurement(
- category=category,
- value=base_value + 0.5 * i + random.randint(-20, 10),
- date=date,
- )
- new_entries.append(measurement)
+ if Measurement.objects.filter(category=category, date=date).exists():
+ continue
+
+ measurement = Measurement(
+ category=category,
+ value=base_value + 0.5 * i + random.randint(-20, 10),
+ date=date,
+ )
+ new_entries.append(measurement)
# Bulk-create the entries
Measurement.objects.bulk_create(new_entries)
diff --git a/wger/measurements/tests/test_generator.py b/wger/measurements/tests/test_generator.py
new file mode 100644
index 000000000..3ec1acab0
--- /dev/null
+++ b/wger/measurements/tests/test_generator.py
@@ -0,0 +1,59 @@
+# 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
+
+# Django
+from django.core.management import call_command
+
+# wger
+from wger.core.tests.base_testcase import WgerTestCase
+from wger.measurements.models import (
+ Category,
+ Measurement,
+)
+
+
+class MeasurementGeneratorTestCase(WgerTestCase):
+
+ def test_generator_categories(self):
+ # Arrange
+ Category.objects.all().delete()
+
+ # Act
+ call_command(
+ 'dummy-generator-measurement-categories',
+ '--nr-categories',
+ 10,
+ )
+
+ # Assert
+ self.assertEqual(Category.objects.filter(user_id=1).count(), 10)
+
+ def test_generator_entries(self):
+ # Arrange
+ Category.objects.all().delete()
+
+ # Act
+ call_command(
+ 'dummy-generator-measurement-categories',
+ '--nr-categories',
+ 1,
+ )
+ call_command(
+ 'dummy-generator-measurements',
+ '--nr-measurements',
+ 10,
+ )
+
+ # Assert
+ self.assertEqual(Measurement.objects.filter(category__user_id=1).count(), 10)
diff --git a/wger/nutrition/management/commands/dummy-generator-nutrition.py b/wger/nutrition/management/commands/dummy-generator-nutrition.py
index f36b7960c..97a613bf4 100644
--- a/wger/nutrition/management/commands/dummy-generator-nutrition.py
+++ b/wger/nutrition/management/commands/dummy-generator-nutrition.py
@@ -27,7 +27,6 @@ from django.core.management.base import BaseCommand
from django.utils import timezone
# wger
-from wger.core.models import Language
from wger.nutrition.models import (
Ingredient,
LogItem,
diff --git a/wger/nutrition/tests/test_generator.py b/wger/nutrition/tests/test_generator.py
new file mode 100644
index 000000000..eb80843e6
--- /dev/null
+++ b/wger/nutrition/tests/test_generator.py
@@ -0,0 +1,33 @@
+# 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
+
+# Django
+from django.core.management import call_command
+
+# wger
+from wger.core.tests.base_testcase import WgerTestCase
+from wger.nutrition.models import NutritionPlan
+
+
+class NutritionalPlansGeneratorTestCase(WgerTestCase):
+
+ def test_generator(self):
+ # Arrange
+ NutritionPlan.objects.all().delete()
+
+ # Act
+ call_command('dummy-generator-nutrition', '--plans', 10)
+
+ # Assert
+ self.assertEqual(NutritionPlan.objects.filter(user_id=1).count(), 10)
diff --git a/wger/weight/management/commands/dummy-generator-body-weight.py b/wger/weight/management/commands/dummy-generator-body-weight.py
index 8cdbe8159..b8978ef92 100644
--- a/wger/weight/management/commands/dummy-generator-body-weight.py
+++ b/wger/weight/management/commands/dummy-generator-body-weight.py
@@ -21,10 +21,6 @@ import random
from django.contrib.auth.models import User
from django.core.management.base import BaseCommand
-# Third Party
-from faker import Faker
-from faker.providers import DynamicProvider
-
# wger
from wger.weight.models import WeightEntry
@@ -75,7 +71,7 @@ class Command(BaseCommand):
existing_entries = [i.date for i in WeightEntry.objects.filter(user=user)]
# Weight entries
- for i in range(1, options['nr_entries']):
+ for i in range(options['nr_entries']):
creation_date = datetime.date.today() - datetime.timedelta(days=i)
if creation_date not in existing_entries:
diff --git a/wger/weight/tests/test_generator.py b/wger/weight/tests/test_generator.py
new file mode 100644
index 000000000..f0cf05a56
--- /dev/null
+++ b/wger/weight/tests/test_generator.py
@@ -0,0 +1,33 @@
+# 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
+
+# Django
+from django.core.management import call_command
+
+# wger
+from wger.core.tests.base_testcase import WgerTestCase
+from wger.weight.models import WeightEntry
+
+
+class WeightEntryGeneratorTestCase(WgerTestCase):
+
+ def test_generator(self):
+ # Arrange
+ WeightEntry.objects.all().delete()
+
+ # Act
+ call_command('dummy-generator-body-weight', '--nr-entries', 100)
+
+ # Assert
+ self.assertEqual(WeightEntry.objects.filter(user_id=1).count(), 100)