diff --git a/backend/tests/test_tc003.py b/backend/tests/test_tc003.py new file mode 100644 index 0000000..000cc2f --- /dev/null +++ b/backend/tests/test_tc003.py @@ -0,0 +1,98 @@ +from rest_framework.test import APITestCase +from django.contrib.auth import get_user_model +from workouts.models import Workout +from django.utils.timezone import now +from rest_framework.test import APIClient + + +class TestTC003(APITestCase): + """ + TestTC003 is a test case class designed to validate the creation and management of workouts and their associated exercise instances in the API. It focusses on the creation of workouts with possible illegal data, such as negative sets. + + This test case includes: + - Validation of creating a workout with valid data (`test_create_workout_valid`). + - Validation of adding an exercise instance to a workout with proper details. + - Testing error handling when invalid data is provided, such as negative sets (`test_create_workout_invalid_date`). + + The tests ensure that the API correctly handles workout creation, exercise instance association, and validation of input data. + """ + def setUp(self): + self.client = APIClient() + self.user = get_user_model().objects.create_user(username="testuser", password="password") + self.client.force_authenticate(self.user) + # Create an exercise instance + response = self.client.post("/api/exercises/", { + "name": "Example_Exercise", + "description": "This is an example exercise", + "unit": "reps" + }, format="json") + print(f"Exercise created: {response.data}") + print(f"Response status code: {response.status_code}") + assert response.status_code == 201 + self.exercise = response.data + + def test_create_workout_valid(self): # first adding workout without exervises + response = self.client.post("/api/workouts/", { + "name": "Valid Exercise", + "date": now().isoformat(), + "notes": "Test case with valid number of sets", + "visibility": "PU", + "owner": self.user.id, + "exercise_instances": [] # Adding exercise_instances field which is required + }, format="json") + + print(f"Response data: {response.data}") + print(f"Response status code: {response.status_code}") + assert response.status_code == 201 + assert Workout.objects.count() == 1 + + # Now adding exercise to workout + response2 = self.client.post("/api/exercise-instances/", { + "exercise": self.exercise["url"], + "sets": 12, + "number": 3, + "exerciseDetail": { + "url": self.exercise["url"], + "id": self.exercise["id"], + "name": self.exercise["name"], + "description": self.exercise["description"], + "unit": self.exercise["unit"], + "instances": [] + }, + "workout": response.data["url"] + }, format="json") + + print(f"Response data: {response2.data}") + print(f"Response status code: {response2.status_code}") + assert response2.status_code == 201 + + + + def test_create_workout_invalid_date(self): # + response = self.client.post("/api/workouts/", { + "name": "Invalid Run", + "date": now().isoformat(), + "notes": "Wrong format", + "visibility": "PU", # Adding required field + "owner": self.user.id, # Adding required field + "exercise_instances": [] # Adding required field + }, format="json") + + response2 = self.client.post("/api/exercise-instances/", { + "exercise": self.exercise["url"], + "sets": -12, + "number": 3, + "exerciseDetail": { + "url": self.exercise["url"], + "id": self.exercise["id"], + "name": self.exercise["name"], + "description": self.exercise["description"], + "unit": self.exercise["unit"], + "instances": [] + }, + "workout": response.data["url"] + }, format="json") + + print(f"Error response data: {response2.data}") + print(f"Error response status code: {response2.status_code}") + assert response2.status_code == 400 # Should return validation error \ No newline at end of file