Skip to content

Commit

Permalink
Update and rename test_boundary_file_size.py to test_cases_AthleteFil…
Browse files Browse the repository at this point in the history
…e_model.py
  • Loading branch information
malenelu authored and GitHub Enterprise committed Apr 3, 2025
1 parent 293ad99 commit a292a8d
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 33 deletions.
33 changes: 0 additions & 33 deletions backend/tests/test_boundary_file_size.py

This file was deleted.

112 changes: 112 additions & 0 deletions backend/tests/test_cases_AthleteFile_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
from django.test import TestCase
from users.models import AthleteFile, User
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.exceptions import ValidationError

max_size = 1024 * 1024 * 5 # Maximum file size of 5MB.

"""
Test cases for the AthleteFile model.
"""
class AthleteFileModelTest(TestCase):

"""
Creates a user for the test
"""
def setUp(self):
self.user = User.objects.create(username="testuser")


"""
TC_002 - Boundary test for file size.
"""
def test_file_size_boundary(self):

# Create file with minimum valid size (1 byte).
min_file = SimpleUploadedFile("min_file.txt", b"x" * 1)

# Create file with minimum valid size (2 bytes).
min_plus_file = SimpleUploadedFile("min_plus_file.txt", b"x" * 2)

# Create file with maximum valid size (5MB - 1 byte).
max_minus_file = SimpleUploadedFile("max_minus_file.txt", b"x" * (max_size - 1))

# Create file with maximum valid size (5MB).
max_file = SimpleUploadedFile("max_file.txt", b"x" * max_size)

# Create AthleteFile objects with both files.
athlete_min_file = AthleteFile(athlete=self.user, owner=self.user, file=min_file)
athlete_min_plus_file = AthleteFile(athlete=self.user, owner=self.user, file=min_plus_file)
athlete_max_minus_file = AthleteFile(athlete=self.user, owner=self.user, file=max_minus_file)
athlete_max_file = AthleteFile(athlete=self.user, owner=self.user, file=max_file)

try:
# Runs validation checks.
athlete_min_file.full_clean()
athlete_min_plus_file.full_clean()
athlete_max_minus_file.full_clean()
athlete_max_file.full_clean()
except ValidationError as e:
# Fails if any validation errors are raised.
self.fail(f"ValidationError raised: {e}")

"""
TC_003 - Robust boundary test for file size above maximum size.
"""
def test_file_size_robust_boundary(self):

# Creates a file with invalid size.
invalid_above_file = SimpleUploadedFile("test_above_max.txt", b"x" * (max_size + 1))
# Creates AthleteFile object with the invalid file.
athlete_above_file = AthleteFile(athlete=self.user, owner=self.user, file=invalid_above_file)

try:
# Runs a validation checks.
athlete_above_file.full_clean()
# Fails if no validation errors are raised.
self.fail("ValidationError not raised for file above maximum size.")
except ValidationError as e:
# Passes if validation error is raised.
self.assertIn('file', e.message_dict)

"""
TC_004 - Special value test for uploading a valid and invalid file to a workout.
"""
def test_file_upload_special_value(self):
# Creates a PDF-file
pdf_file = SimpleUploadedFile("pdf_file.pdf")
# Creates a DOCX-file
docx_file = SimpleUploadedFile("docx_file.docx")
# Creates AthleteFile object with the files
athlete_pdf_file = AthleteFile(athlete=self.user, owner=self.user, file=pdf_file)
athlete_docx_file = AthleteFile(athlete=self.user, owner=self.user, file=docx_file)

try:
# Runs validation check for PDF-file
athlete_pdf_file.full_clean()
except ValidationError as e:
# Fails if any validation errors are raised
self.fail(f"ValidationError raised: {e}")

try:
# Runs validation check for DOCX-file
athlete_docx_file.full_clean()
# Fails if no validation errors are raised
self.fail("ValidationError not raised for file of format DOCX.")
except ValidationError as e:
# Passes if validation error is raised.
self.assertIn('file', e.message_dict)

"""
def test_file_size_robust_below_min(self):
invalid_below_file = SimpleUploadedFile("test_below_min.txt", 0)
athlete_below_file = AthleteFile(athlete=self.user, owner=self.user, file=invalid_below_file)
try:
athlete_below_file.full_clean()
self.fail("ValidationError not raised for file below minimum size.")
except ValidationError as e:
self.assertIn('file', e.message_dict)
"""

0 comments on commit a292a8d

Please sign in to comment.