diff --git a/backend/tests/test_template.py b/backend/tests/test_template.py index 4a3dda4..b5a352d 100644 --- a/backend/tests/test_template.py +++ b/backend/tests/test_template.py @@ -1,16 +1,142 @@ -from django.test import TestCase +import pytest +from rest_framework.test import APIClient +from django.utils.timezone import now +from workouts.models import Workout from workouts.models import Exercise +from django.contrib.auth import get_user_model +@pytest.mark.django_db -class User(TestCase): - def setUp(self): - Exercise.objects.create(name="Pushup", description="Pushup", unit="reps") - Exercise.objects.create(name="Running", description="Running", unit="minutes") - Exercise.objects.create(name="Dumbbell curl", description="Lifting dumbbells", unit="reps") - - def test_user_has_coach(self): - all_exercises = Exercise.objects.all() - num_reps = Exercise.objects.filter(unit="reps") +class TestWorkoutAPI: + def setUp(self): + self.client = APIClient() + self.user = get_user_model().objects.create_user(username="testuser", + password="password") + self.client.force_authenticate(self.user) + Exercise.objects.create(name="Pushup", description="Pushup", unit="reps") + Exercise.objects.create(name="Running", description="Running", unit="minutes") - self.assertEqual(len(all_exercises), 3) - self.assertEqual(len(num_reps), 2) +#TC_001 + + def test_create_workout_with_PU_visibility(self): # + response = self.client.post("/api/workouts/", { + "name": "Running", + "date": now().isoformat(), + "notes": "run", + "visibility": "PU", # Adding PU visibility field + "owner": self.user.id, + "exercise_instances": [ + {"exercise": self.pushup.id, "sets": 3, "reps": 15}, + {"exercise": self.running.id, "sets": 1, "duration": 30} + ] + }, format="json") + print(f"Response data: {response.data}") + print(f"Response status code: {response.status_code}") + + assert response.status_code == 201 #Success + + def test_create_workout_with_CO_visibility(self): # + response = self.client.post("/api/workouts/", { + "name": "More Running", + "date": now().isoformat(), + "notes": "run", + "visibility": "CO", # Adding CO visibility field + "owner": self.user.id, + "exercise_instances": [ + {"exercise": self.pushup.id, "sets": 3, "reps": 15}, + {"exercise": self.running.id, "sets": 1, "duration": 30} + ] + }, format="json") + print(f"Response data: {response.data}") + print(f"Response status code: {response.status_code}") + + assert response.status_code == 201 #Success + + def test_create_workout_with_PR_visibility(self): # + response = self.client.post("/api/workouts/", { + "name": "Even more running", + "date": now().isoformat(), + "notes": "run", + "visibility": "PR", # Adding PR visibility field + "owner": self.user.id, + "exercise_instances": [ + {"exercise": self.pushup.id, "sets": 3, "reps": 15}, + {"exercise": self.running.id, "sets": 1, "duration": 30} + ] + }, format="json") + print(f"Response data: {response.data}") + print(f"Response status code: {response.status_code}") + + assert response.status_code == 201 #Success + +#TC_002 + + def test_create_workout_with_invalid_visibility(self): # + response = self.client.post("/api/workouts/", { + "name": "Last run", + "date": now().isoformat(), + "notes": "run", + "visibility": "OK", # Adding invalid visibility field + "owner": self.user.id, + "exercise_instances": [ + {"exercise": self.pushup.id, "sets": 3, "reps": 15}, + {"exercise": self.running.id, "sets": 1, "duration": 30} + ] + }, format="json") + print(f"Response data: {response.data}") + print(f"Response status code: {response.status_code}") + + assert response.status_code == 400 #Error + +#TC_003 + + def test_create_workout_with_zero_exercises(self): # + response = self.client.post("/api/workouts/", { + "name": "Lifting weights", + "date": now().isoformat(), + "notes": "run", + "visibility": "PR", + "owner": self.user.id, + "exercise_instances": [] + }, format="json") #No exercises added, only empty list + print(f"Response data: {response.data}") + print(f"Response status code: {response.status_code}") + + assert response.status_code == 400 #Error + +#TC_004 + + def test_create_workout_with_one_exercise(self): # + response = self.client.post("/api/workouts/", { + "name": "Swimming", + "date": now().isoformat(), + "notes": "run", + "visibility": "PR", + "owner": self.user.id, + "exercise_instances": [ + {"exercise": self.pushup.id, "sets": 3, "reps": 15} + ] + }, format="json") #Only one exercise added + print(f"Response data: {response.data}") + print(f"Response status code: {response.status_code}") + + assert response.status_code == 201 #Success + +#TC_005 + + def test_create_workout_with_invalid_date(self): # + response = self.client.post("/api/workouts/", { + "name": "Climbing", + "date": str("heyy"), #Invalid date + "notes": "run", + "visibility": "PR", + "owner": self.user.id, + "exercise_instances": [ + {"exercise": self.pushup.id, "sets": 3, "reps": 15}, + {"exercise": self.running.id, "sets": 1, "duration": 30} + ] + }, format="json") + print(f"Response data: {response.data}") + print(f"Response status code: {response.status_code}") + + assert response.status_code == 400 #Error \ No newline at end of file