diff --git a/backend/tests/test.py b/backend/tests/test.py new file mode 100644 index 0000000..483180d --- /dev/null +++ b/backend/tests/test.py @@ -0,0 +1,41 @@ +import pytest +from rest_framework.test import APIClient +from django.utils.timezone import now +from workouts.models import Workout +from django.contrib.auth import get_user_model + +from django.test import TestCase + +@pytest.mark.django_db +class TestWorkoutAPI(TestCase): + def setUp(self): + self.client = APIClient() + self.user = get_user_model().objects.create_user(username="testuser", password="password") + self.client.force_authenticate(self.user) + + def test_create_workout_valid(self): # + response = self.client.post("/api/workouts/", { + "name": "Morning Run", + "date": now().isoformat(), + "notes": "5km run", + "visibility": "PU", # Adding visibility field which is required + "owner": self.user.id, # Adding owner field which seems to be required + "exercise_instances": [] # Adding exercise_instances field which is required + }, format="json") + + + assert response.status_code == 201 + assert Workout.objects.count() == 1 + + def test_create_workout_invalid_date(self): # + response = self.client.post("/api/workouts/", { + "name": "Invalid Run", + "date": "invalid_date", + "notes": "Wrong format", + "visibility": "PU", # Adding required field + "owner": self.user.id, # Adding required field + "exercise_instances": [] # Adding required field + }, format="json") + + assert response.status_code == 400 # Should return validation error + \ No newline at end of file