-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: enhance test documentation and improve test structure - Added c…
…omprehensive docstrings, improved test class and method documentation, standardized test setup and assertions, added clear expected outcomes
- Loading branch information
Showing
4 changed files
with
549 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| """ | ||
| Test suite for comment submission functionality. | ||
| This module contains tests that verify: | ||
| 1. Valid comment submission | ||
| 2. Input validation for comment content | ||
| 3. Workout association validation | ||
| 4. Access control for private workouts | ||
| 5. Comment visibility rules | ||
| Each test case includes: | ||
| - Setup: Required data and conditions | ||
| - Action: The operation being tested | ||
| - Assertion: Expected outcomes and validations | ||
| """ | ||
|
|
||
| import pytest | ||
| from rest_framework.test import APIClient | ||
| from django.contrib.auth import get_user_model | ||
| from django.urls import reverse | ||
| from workouts.models import Workout | ||
| from comments.models import Comment | ||
| from datetime import datetime | ||
| from django.test import TestCase | ||
| from rest_framework import status | ||
|
|
||
| @pytest.mark.django_db | ||
| class TestCommentSubmission(APITestCase): | ||
| """ | ||
| Test cases for comment submission endpoints. | ||
| Tests cover: | ||
| - Successful comment submission | ||
| - Validation of required fields | ||
| - Workout association validation | ||
| - Access control for private workouts | ||
| - Comment visibility rules | ||
| """ | ||
|
|
||
| def setUp(self): | ||
| """Set up test data and URLs.""" | ||
| self.client = APIClient() | ||
| self.url = reverse('comment-list') | ||
|
|
||
| # Create a test user | ||
| User = get_user_model() | ||
| self.user = User.objects.create_user( | ||
| username='testuser', | ||
| email='test@example.com', | ||
| password='ValidPass123!' | ||
| ) | ||
|
|
||
| # Create a test workout | ||
| self.workout = Workout.objects.create( | ||
| name="Test Workout", | ||
| date=datetime.now(), | ||
| notes="Test notes", | ||
| owner=self.user, | ||
| visibility="CO" | ||
| ) | ||
|
|
||
| # Authenticate the client | ||
| self.client.force_authenticate(user=self.user) | ||
| self.comment_data = { | ||
| 'content': 'Test comment', | ||
| 'workout': self.workout.id | ||
| } | ||
|
|
||
| def test_valid_comment_submission(self): | ||
| """ | ||
| Test successful comment submission with valid data. | ||
| Expected: | ||
| - 201 Created status code | ||
| - Comment is created in database | ||
| - Response contains comment data with correct associations | ||
| """ | ||
| response = self.client.post(self.url, self.comment_data) | ||
| self.assertEqual(response.status_code, status.HTTP_201_CREATED) | ||
| self.assertTrue(Comment.objects.filter(content='Test comment').exists()) | ||
| self.assertEqual(response.data['workout'], self.workout.id) | ||
|
|
||
| def test_invalid_comment_submission_empty_content(self): | ||
| """ | ||
| Test comment submission with empty content. | ||
| Expected: | ||
| - 400 Bad Request status code | ||
| - Error message about missing content | ||
| """ | ||
| data = self.comment_data.copy() | ||
| data['content'] = '' | ||
| response = self.client.post(self.url, data) | ||
| self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
| self.assertIn('content', response.data) | ||
|
|
||
| def test_invalid_comment_submission_no_workout(self): | ||
| """ | ||
| Test comment submission without workout association. | ||
| Expected: | ||
| - 400 Bad Request status code | ||
| - Error message about missing workout | ||
| """ | ||
| data = self.comment_data.copy() | ||
| del data['workout'] | ||
| response = self.client.post(self.url, data) | ||
| self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
| self.assertIn('workout', response.data) | ||
|
|
||
| def test_comment_submission_on_private_workout(self): | ||
| """ | ||
| Test comment submission on a private workout. | ||
| Expected: | ||
| - 403 Forbidden status code | ||
| - Error message about insufficient permissions | ||
| """ | ||
| private_workout = Workout.objects.create( | ||
| name="Private Workout", | ||
| date=datetime.now(), | ||
| notes="Private notes", | ||
| owner=self.user, | ||
| visibility="PR" | ||
| ) | ||
| data = self.comment_data.copy() | ||
| data['workout'] = private_workout.id | ||
| response = self.client.post(self.url, data) | ||
| self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| """ | ||
| Test suite for media upload functionality. | ||
| This module contains tests that verify: | ||
| 1. Valid media file uploads | ||
| 2. File size validation | ||
| 3. File type validation | ||
| 4. Owner permissions | ||
| 5. Access control for private workouts | ||
| Each test case includes: | ||
| - Setup: Required data and conditions | ||
| - Action: The operation being tested | ||
| - Assertion: Expected outcomes and validations | ||
| """ | ||
|
|
||
| import pytest | ||
| from rest_framework.test import APIClient | ||
| from django.contrib.auth import get_user_model | ||
| from django.urls import reverse | ||
| from workouts.models import Workout, WorkoutFile | ||
| from django.core.files.uploadedfile import SimpleUploadedFile | ||
| from datetime import datetime | ||
| import os | ||
| from django.test import TestCase | ||
| from rest_framework import status | ||
|
|
||
| @pytest.mark.django_db | ||
| class TestMediaUpload(APITestCase): | ||
| """ | ||
| Test cases for media upload endpoints. | ||
| Tests cover: | ||
| - Successful file uploads | ||
| - File size restrictions | ||
| - File type validation | ||
| - Owner permissions | ||
| - Access control for private workouts | ||
| """ | ||
|
|
||
| def setUp(self): | ||
| """Set up test data and URLs.""" | ||
| self.client = APIClient() | ||
| self.url = reverse('workout-file-list') | ||
|
|
||
| # Create a test user | ||
| User = get_user_model() | ||
| self.user = User.objects.create_user( | ||
| username='testuser', | ||
| email='test@example.com', | ||
| password='ValidPass123!' | ||
| ) | ||
|
|
||
| # Create a test workout | ||
| self.workout = Workout.objects.create( | ||
| name="Test Workout", | ||
| date=datetime.now(), | ||
| notes="Test notes", | ||
| owner=self.user, | ||
| visibility="CO" | ||
| ) | ||
|
|
||
| # Authenticate the client | ||
| self.client.force_authenticate(user=self.user) | ||
|
|
||
| self.test_file = SimpleUploadedFile( | ||
| "test.txt", | ||
| b"test content", | ||
| content_type="text/plain" | ||
| ) | ||
| self.large_file = SimpleUploadedFile( | ||
| "large.txt", | ||
| b"x" * (6 * 1024 * 1024), # 6MB file | ||
| content_type="text/plain" | ||
| ) | ||
| self.invalid_file = SimpleUploadedFile( | ||
| "test.exe", | ||
| b"test content", | ||
| content_type="application/x-msdownload" | ||
| ) | ||
|
|
||
| def test_valid_media_upload(self): | ||
| """ | ||
| Test successful media file upload. | ||
| Expected: | ||
| - 201 Created status code | ||
| - File is uploaded and associated with workout | ||
| - Response contains file data | ||
| """ | ||
| data = { | ||
| 'file': self.test_file, | ||
| 'workout': self.workout.id | ||
| } | ||
| response = self.client.post(self.url, data, format='multipart') | ||
| self.assertEqual(response.status_code, status.HTTP_201_CREATED) | ||
| self.assertTrue(WorkoutFile.objects.filter(workout=self.workout).exists()) | ||
| self.assertIn('file', response.data) | ||
|
|
||
| def test_invalid_media_upload_no_file(self): | ||
| """ | ||
| Test upload attempt without file. | ||
| Expected: | ||
| - 400 Bad Request status code | ||
| - Error message about missing file | ||
| """ | ||
| data = {'workout': self.workout.id} | ||
| response = self.client.post(self.url, data, format='multipart') | ||
| self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
| self.assertIn('file', response.data) | ||
|
|
||
| def test_invalid_media_upload_large_file(self): | ||
| """ | ||
| Test upload attempt with file exceeding size limit. | ||
| Expected: | ||
| - 400 Bad Request status code | ||
| - Error message about file size limit | ||
| """ | ||
| data = { | ||
| 'file': self.large_file, | ||
| 'workout': self.workout.id | ||
| } | ||
| response = self.client.post(self.url, data, format='multipart') | ||
| self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
| self.assertIn('file', response.data) | ||
|
|
||
| def test_invalid_media_upload_wrong_owner(self): | ||
| """ | ||
| Test upload attempt for workout owned by another user. | ||
| Expected: | ||
| - 403 Forbidden status code | ||
| - Error message about insufficient permissions | ||
| """ | ||
| other_user = get_user_model().objects.create_user( | ||
| username='otheruser', | ||
| email='other@example.com', | ||
| password='ValidPass123!' | ||
| ) | ||
| other_workout = Workout.objects.create( | ||
| name="Other Workout", | ||
| date=datetime.now(), | ||
| notes="Other notes", | ||
| owner=other_user, | ||
| visibility="CO" | ||
| ) | ||
| data = { | ||
| 'file': self.test_file, | ||
| 'workout': other_workout.id | ||
| } | ||
| response = self.client.post(self.url, data, format='multipart') | ||
| self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
|
|
||
| def test_invalid_media_upload_extension(self): | ||
| """ | ||
| Test upload attempt with invalid file extension. | ||
| Expected: | ||
| - 400 Bad Request status code | ||
| - Error message about invalid file type | ||
| """ | ||
| data = { | ||
| 'file': self.invalid_file, | ||
| 'workout': self.workout.id | ||
| } | ||
| response = self.client.post(self.url, data, format='multipart') | ||
| self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
| self.assertIn('file', response.data) |
Oops, something went wrong.