Skip to content

Commit

Permalink
chore: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EwoutV committed May 21, 2024
1 parent bd61e50 commit cf65868
Show file tree
Hide file tree
Showing 24 changed files with 387 additions and 242 deletions.
4 changes: 2 additions & 2 deletions backend/api/logic/parse_zip_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def parse_zip(project: Project, zip_file: InMemoryUploadedFile) -> bool:

zip_file.seek(0)

with zipfile.ZipFile(zip_file, 'r') as zip:
files = zip.namelist()
with zipfile.ZipFile(zip_file, 'r') as file:
files = file.namelist()
directories = [file for file in files if file.endswith('/')]

# Check if all directories start the same
Expand Down
11 changes: 3 additions & 8 deletions backend/api/serializers/checks_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,13 @@ class Meta:


class ExtraCheckSerializer(serializers.ModelSerializer):
project = serializers.HyperlinkedRelatedField(
project = serializers.HyperlinkedIdentityField(
view_name="project-detail",
read_only=True
)

docker_image = serializers.HyperlinkedRelatedField(
view_name="docker-image-detail",
queryset=DockerImage.objects.all()
docker_image = serializers.HyperlinkedIdentityField(
view_name="docker-image-detail"
)

class Meta:
Expand All @@ -77,10 +76,6 @@ class Meta:
def validate(self, attrs):
data = super().validate(attrs)

# Only check if docker image is present when it is not a partial update
if not self.partial:
if "docker_image" not in data:
raise serializers.ValidationError(_("extra_check.error.docker_image"))

if "time_limit" in data and not 10 <= data["time_limit"] <= 1000:
raise serializers.ValidationError(_("extra_check.error.time_limit"))
Expand Down
11 changes: 6 additions & 5 deletions backend/api/serializers/fields/expandable_hyperlinked_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
from rest_framework.serializers import Serializer


class ExpandableHyperlinkedIdentityField(serializers.BaseSerializer, serializers.HyperlinkedIdentityField):
class ExpandableHyperlinkedIdentityField(serializers.HyperlinkedIdentityField):
"""A HyperlinkedIdentityField with nested serializer expanding"""

def __init__(self, serializer: Type[Serializer], view_name: str = None, **kwargs):
self.serializer = serializer
super().__init__(view_name=view_name, **kwargs)
Expand All @@ -26,8 +27,8 @@ def to_representation(self, value):
instance = value

return self.serializer(instance,
many=self._kwargs.pop('many'),
context=self.context
).data
many=self._kwargs.pop('many'),
context=self.context
).data

return super(serializers.HyperlinkedIdentityField, self).to_representation(value)
return super().to_representation(value)
17 changes: 10 additions & 7 deletions backend/api/serializers/project_serializer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from django.core.files.uploadedfile import InMemoryUploadedFile

from api.logic.parse_zip_files import parse_zip
from api.models.group import Group
from api.models.project import Project
from api.models.submission import Submission, ExtraCheckResult, StructureCheckResult, StateEnum
Expand Down Expand Up @@ -175,13 +178,13 @@ def create(self, validated_data):
Group.objects.create(project=project)

# If a zip_structure is provided, parse it to create the structure checks
# zip_structure: InMemoryUploadedFile | None = self.context['request'].FILES.get('zip_structure')
#
# if zip_structure:
# result = parse_zip(project, zip_structure)
#
# if not result:
# raise ValidationError(gettext("project.errors.zip_structure"))
zip_structure: InMemoryUploadedFile | None = self.context['request'].FILES.get('zip_structure')

if zip_structure:
result = parse_zip(project, zip_structure)

if not result:
raise ValidationError(gettext("project.errors.zip_structure"))

return project

Expand Down
2 changes: 2 additions & 0 deletions backend/api/tests/test_file_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def test_parsing(self):

content_json = json.loads(response.content.decode("utf-8"))

print(project, content_json)

self.assertEqual(len(content_json), 6)

expected_project_url = settings.TESTING_BASE_LINK + reverse(
Expand Down
36 changes: 34 additions & 2 deletions backend/api/views/project_view.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import logging

from rest_framework.parsers import MultiPartParser

from api.models.group import Group
from api.models.project import Project
from api.models.submission import Submission
Expand All @@ -21,6 +25,9 @@
from rest_framework.viewsets import GenericViewSet


logger = logging.getLogger("ypovoli")


# TODO: Error message when creating a project with wrongly formatted date looks a bit weird
class ProjectViewSet(RetrieveModelMixin,
UpdateModelMixin,
Expand Down Expand Up @@ -160,6 +167,8 @@ def _add_extra_check(self, request: Request, **_):

project: Project = self.get_object()

logger.info(request.POST.dict())

serializer = ExtraCheckSerializer(
data=request.data,
context={
Expand All @@ -168,14 +177,37 @@ def _add_extra_check(self, request: Request, **_):
}
)

# TODO: Weird error message when invalid docker_image id
if serializer.is_valid(raise_exception=True):
serializer.save(project=project)
serializer.save(project=project, docker_image_id=request.data.get('docker_image'))

return Response({
"message": gettext("project.success.extra_check.add")
})

@extra_checks.mapping.put
@swagger_auto_schema(request_body=ExtraCheckSerializer)
def set_extra_checks(self, request: Request, **_):
"""Set the extra checks of the given project"""
project: Project = self.get_object()

# Delete all current extra checks of the project
project.extra_checks.all().delete()

# Create the new extra checks
serializer = ExtraCheckSerializer(
data=request.data,
many=True,
context={
"project": project,
"request": request
}
)

if serializer.is_valid(raise_exception=True):
serializer.save(project=project)

return Response(serializer.validated_data)

@action(detail=True, permission_classes=[IsAdminUser | ProjectGroupPermission])
def submission_status(self, _: Request):
"""Returns the current submission status for the given project
Expand Down
28 changes: 28 additions & 0 deletions backend/ypovoli/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,34 @@
},
}

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'simple',
},
},
'formatters': {
'simple': {
'format': '{levelname} {message}',
'style': '{',
},
},
'root': {
'handlers': ['console'],
'level': 'DEBUG',
},
'loggers': {
'ypovoli': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': False,
}
},
}

# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/assets/lang/app/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@
"noStudents": "No students in this group",
"locked": "Closed",
"unlocked": "Open",
"structureChecks": "Submission structure",
"extraChecks": {
"title": "Automatic checks on a submission",
"empty": "No checks addeed",
"add": "New check",
"name": "Name",
"public": "Public",
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/assets/lang/app/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
"leaveGroup": "Verlaat groep",
"create": "Creëer nieuw project",
"save": "Project opslaan",
"edit": "Project bewerken",
"structureChecks": "Indieningsstructuur",
"name": "Projectnaam",
"description": "Beschrijving",
"startDate": "Start project",
Expand All @@ -74,6 +76,7 @@
"extraChecks": {
"title": "Automatische checks op een indiening",
"add": "Nieuwe check",
"empty": "Nog geen extra checks toegevoegd",
"name": "Naam",
"public": "Publiek",
"bashScript": "Bash script",
Expand Down
11 changes: 11 additions & 0 deletions frontend/src/components/Loading.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script setup lang="ts">
import ProgressSpinner from 'primevue/progressspinner';
</script>

<template>
<div class="h-full p-3 flex align-items-center justify-content-center surface-100 h-100">
<ProgressSpinner />
</div>
</template>

<style scoped lang="scss"></style>
Loading

0 comments on commit cf65868

Please sign in to comment.