Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Commit

Permalink
test(apps/tasks): fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
HladczukLe committed Dec 11, 2023
1 parent 6269065 commit 6226a87
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
42 changes: 37 additions & 5 deletions apps/tasks/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
InMemoryUploadedFile,
SimpleUploadedFile,
)
from django.http import HttpRequest
from django.test import TestCase
from django.test.client import RequestFactory
from django.urls import resolve, reverse
from django.utils import timezone

from apps.contests.models import Contest
from apps.submissions.forms import SubmissionForm
from apps.submissions.forms import SubmissionForm, UploadFileForm
from apps.submissions.models import Submission, SubmissionStatus
from apps.tasks.admin import TaskAdmin, TaskModelForm
from apps.tasks.models import Task
Expand Down Expand Up @@ -524,8 +523,8 @@ def test_handle_uploaded_file(self) -> None:
file_content = b"Test file content"
uploaded_file = SimpleUploadedFile("test_file.txt", file_content)

request = HttpRequest()
request.FILES["file"] = uploaded_file
request = self.factory.post(self.url, data={"file": uploaded_file})
request.user = self.user

submission = Submission._default_manager.create(
author=self.user,
Expand All @@ -540,10 +539,43 @@ def test_handle_uploaded_file(self) -> None:
self.assertTrue(os.path.exists(destination_path))

submission.refresh_from_db()
self.assertEqual(submission.status, "WJ")
self.assertEqual(submission.status, SubmissionStatus.WAITING_JUDGE)

os.remove(destination_path)

def test_handle_uploaded_file_with_invalid_form(self) -> None:
file_content = b"Test file content"
uploaded_file = SimpleUploadedFile("test_file.txt", file_content)

invalid_request_data = {"file": uploaded_file}
invalid_request = self.factory.post(
self.url, data=invalid_request_data
)
invalid_request.user = self.user

destination_path = os.path.join("apps/tasks/uploads/", "test_file.txt")

submission = Submission._default_manager.create(
author=self.user,
task=self.task,
code="print('Hello, World!')",
status=SubmissionStatus.WAITING_JUDGE,
)

upload_form = UploadFileForm(
invalid_request.POST, invalid_request.FILES
)
self.assertFalse(upload_form.is_valid())
handle_uploaded_file(invalid_request, self.task.id, submission.id)

print(f"File exists: {os.path.exists(destination_path)}")
print(f"File path: {destination_path}")

self.assertFalse(os.path.exists(destination_path))

submission.refresh_from_db()
self.assertEqual(submission.status, SubmissionStatus.WAITING_JUDGE)


class BackgroundJobTaskTest(TestCase):
def setUp(self) -> None:
Expand Down
18 changes: 10 additions & 8 deletions apps/tasks/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,16 @@ def handle_uploaded_file(
uploaded_file = request.FILES.get("file")

if uploaded_file:
with open(
os.path.join(destination_dir, uploaded_file.name or ""), "wb+"
) as destination:
for chunk in uploaded_file.chunks():
destination.write(chunk)

submission.status = SubmissionStatus.WAITING_JUDGE
submission.save()
upload_form = UploadFileForm(request.POST, request.FILES)
if upload_form.is_valid():
with open(
os.path.join(destination_dir, uploaded_file.name or ""), "wb+"
) as destination:
for chunk in uploaded_file.chunks():
destination.write(chunk)

submission.status = SubmissionStatus.WAITING_JUDGE
submission.save()


class DetailView(FormMixinBase, DetailViewBase):
Expand Down

0 comments on commit 6226a87

Please sign in to comment.