Skip to content

Commit

Permalink
Tests implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Julie Lundberg Suter committed Mar 25, 2025
1 parent 730104d commit e1e938e
Showing 1 changed file with 138 additions and 12 deletions.
150 changes: 138 additions & 12 deletions backend/tests/test_template.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e1e938e

Please sign in to comment.