From a292a8db5f444264f1dcbf3f08bd8056eec25277 Mon Sep 17 00:00:00 2001 From: Malene Lundemo Date: Thu, 3 Apr 2025 15:40:30 +0200 Subject: [PATCH] Update and rename test_boundary_file_size.py to test_cases_AthleteFile_model.py --- backend/tests/test_boundary_file_size.py | 33 ------ backend/tests/test_cases_AthleteFile_model.py | 112 ++++++++++++++++++ 2 files changed, 112 insertions(+), 33 deletions(-) delete mode 100644 backend/tests/test_boundary_file_size.py create mode 100644 backend/tests/test_cases_AthleteFile_model.py diff --git a/backend/tests/test_boundary_file_size.py b/backend/tests/test_boundary_file_size.py deleted file mode 100644 index c8feffe..0000000 --- a/backend/tests/test_boundary_file_size.py +++ /dev/null @@ -1,33 +0,0 @@ -""" -TC_002 - Boundary test for file size -""" -from django.test import TestCase - -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}") diff --git a/backend/tests/test_cases_AthleteFile_model.py b/backend/tests/test_cases_AthleteFile_model.py new file mode 100644 index 0000000..1faed1a --- /dev/null +++ b/backend/tests/test_cases_AthleteFile_model.py @@ -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) + +"""