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 12, 2023
1 parent 6226a87 commit fd27d6d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 49 deletions.
35 changes: 1 addition & 34 deletions apps/tasks/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from django.utils import timezone

from apps.contests.models import Contest
from apps.submissions.forms import SubmissionForm, UploadFileForm
from apps.submissions.forms import SubmissionForm
from apps.submissions.models import Submission, SubmissionStatus
from apps.tasks.admin import TaskAdmin, TaskModelForm
from apps.tasks.models import Task
Expand Down Expand Up @@ -543,39 +543,6 @@ def test_handle_uploaded_file(self) -> None:

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
23 changes: 8 additions & 15 deletions apps/tasks/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.views import generic
from django.views.generic.edit import FormMixin

from apps.submissions.forms import SubmissionForm, UploadFileForm
from apps.submissions.forms import SubmissionForm
from apps.submissions.models import Submission, SubmissionStatus
from apps.tasks.models import Task
from server import celery
Expand Down Expand Up @@ -77,16 +77,14 @@ def handle_uploaded_file(
uploaded_file = request.FILES.get("file")

if uploaded_file:
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)
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()
submission.status = SubmissionStatus.WAITING_JUDGE
submission.save()


class DetailView(FormMixinBase, DetailViewBase):
Expand Down Expand Up @@ -143,9 +141,4 @@ def post(self, request: HttpRequest, *, pk: int) -> HttpResponse:
submission.id,
)

if uploaded_file:
upload_form = UploadFileForm(request.POST, request.FILES)
if upload_form.is_valid():
handle_uploaded_file(request, self.object.id, submission.id)

return redirect(self.get_success_url())

0 comments on commit fd27d6d

Please sign in to comment.