Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File structure checks #84

Merged
merged 21 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
f01a03d
chore: startup of file structure checks
tyboro2002 Mar 7, 2024
b75c47b
chore: fxed get structure
tyboro2002 Mar 7, 2024
620b906
chore: fixed reading zip files
tyboro2002 Mar 7, 2024
72a0ee2
chore: one link for checks in project
BramMeir Mar 7, 2024
196e195
chore: added retrieving of structure checks for project
tyboro2002 Mar 7, 2024
3a74edb
Merge branch 'file_structure_checks' of https://github.com/SELab-2/UG…
tyboro2002 Mar 7, 2024
b1dbb18
fix: update tests
BramMeir Mar 7, 2024
aee9ad8
chore: finalized initialisation of file checks and added data
tyboro2002 Mar 7, 2024
082d497
Merge branch 'file_structure_checks' of https://github.com/SELab-2/UG…
tyboro2002 Mar 7, 2024
20f6ad5
chore: add i18n for errors
tyboro2002 Mar 7, 2024
ce077ce
chore: cleanup and fixed linting warnings
tyboro2002 Mar 7, 2024
83763ce
chore: cleanup the data directory and started tests
tyboro2002 Mar 8, 2024
998c51d
chore: worked at testing the file structure
tyboro2002 Mar 8, 2024
3da961a
chore: add some tests for filestructures
tyboro2002 Mar 9, 2024
df77e10
chore: finished file_structure tests
tyboro2002 Mar 9, 2024
27b9b82
Merge branch 'development' of https://github.com/SELab-2/UGent-7 into…
tyboro2002 Mar 9, 2024
cc0d8e2
chore: fix doable lintning warnings
tyboro2002 Mar 9, 2024
b1b20f2
chore: fixed some more linting errors
tyboro2002 Mar 9, 2024
a5984ba
chore: finaly fixed linter warnings
tyboro2002 Mar 9, 2024
0d4c015
chore: linting
EwoutV Mar 9, 2024
16e2e10
chore: linting
EwoutV Mar 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/.flake8
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Ignore unused imports
ignore = F401

max-line-length = 119
max-line-length = 120

max-complexity = 10

Expand Down
12 changes: 0 additions & 12 deletions backend/api/fixtures/checks.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
- model: api.structurecheck
pk: 1
fields:
name: '.'
project: 123456
obligated_extensions:
- 3
- 4
blocked_extensions:
- 1
- 2

- model: api.extracheck
pk: 1
fields:
Expand Down
3 changes: 1 addition & 2 deletions backend/api/models/project.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from datetime import datetime
from django.db import models
from django.utils import timezone
from api.models.course import Course
Expand All @@ -21,7 +20,7 @@ class Project(models.Model):

start_date = models.DateTimeField(
# The default value is the current date and time
default=datetime.now,
default=timezone.now,
blank=True,
)

Expand Down
10 changes: 6 additions & 4 deletions backend/api/serializers/project_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ class ProjectSerializer(serializers.ModelSerializer):
many=False, read_only=True, view_name="course-detail"
)

structure_checks = serializers.HyperlinkedRelatedField(
many=True, read_only=True, view_name="structure-check-detail"
structure_checks = serializers.HyperlinkedIdentityField(
view_name="project-structure-checks",
read_only=True
)

extra_checks = serializers.HyperlinkedRelatedField(
many=True, read_only=True, view_name="extra-check-detail"
extra_checks = serializers.HyperlinkedIdentityField(
view_name="project-extra-checks",
read_only=True
)

groups = serializers.HyperlinkedIdentityField(
Expand Down
3 changes: 2 additions & 1 deletion backend/api/tests/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from api.models.extension import FileExtension
from api.models.project import Project
from api.models.course import Course
from django.conf import settings


def create_fileExtension(id, extension):
Expand Down Expand Up @@ -300,4 +301,4 @@ def test_extra_checks_exists(self):
# Assert the details of the retrieved Checks match the created Checks
retrieved_checks = content_json[0]
self.assertEqual(int(retrieved_checks["id"]), checks.id)
self.assertEqual(retrieved_checks["run_script"], "http://testserver" + checks.run_script.url)
self.assertEqual(retrieved_checks["run_script"], settings.TESTING_BASE_LINK + checks.run_script.url)
216 changes: 216 additions & 0 deletions backend/api/tests/test_file_structure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
import os
import json
from django.utils import timezone
from django.urls import reverse
from rest_framework.test import APITestCase
from api.views.check_folder_structure import checkZipFile, parseZipFile
from api.models.checks import StructureCheck
from api.models.extension import FileExtension
from api.models.course import Course
from api.models.project import Project
from authentication.models import User
from django.conf import settings


def create_course(id, name, academic_startyear):
"""
Create a Course with the given arguments.
"""
return Course.objects.create(
id=id, name=name, academic_startyear=academic_startyear
)


def create_fileExtension(extension):
"""
Create a FileExtension with the given arguments.
"""
return FileExtension.objects.create(extension=extension)


def create_project(name, description, visible, archived, days, course):
"""Create a Project with the given arguments."""
deadline = timezone.now() + timezone.timedelta(days=days)

return Project.objects.create(
name=name,
description=description,
visible=visible,
archived=archived,
deadline=deadline,
course=course,
)


def create_structureCheck(name, project, obligated, blocked):
"""
Create a StructureCheck with the given arguments.
"""
structureCheck = StructureCheck.objects.create(
name=name,
project=project,
)
for ch in obligated:
structureCheck.obligated_extensions.add(ch)
for ch in blocked:
structureCheck.blocked_extensions.add(ch)

return structureCheck


class FileTestsTests(APITestCase):
def setUp(self):
self.client.force_authenticate(
User.get_dummy_admin()
)
# Set up a temporary directory for MEDIA_ROOT during tests
self.old_media_root = settings.MEDIA_ROOT
settings.MEDIA_ROOT = os.path.normpath(os.path.join(settings.MEDIA_ROOT, '../testing'))

def tearDown(self):
# Restore the original MEDIA_ROOT after tests
settings.MEDIA_ROOT = self.old_media_root

def test_your_parsing(self):
course = create_course(id=3, name="test course", academic_startyear=2024)
project = create_project(
name="test",
description="descr",
visible=True,
archived=False,
days=100,
course=course,
)
parseZipFile(project=project, dir_path="structures/zip_struct1.zip")

response = self.client.get(
reverse("project-detail", args=[str(project.id)]), follow=True
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.accepted_media_type, "application/json")

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

response = self.client.get(
content_json["structure_checks"], follow=True
)

self.assertEqual(response.status_code, 200)
self.assertEqual(response.accepted_media_type, "application/json")

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

self.assertEqual(len(content_json), 7)

expected_project_url = settings.TESTING_BASE_LINK + reverse(
"project-detail", args=[str(project.id)]
)

content = content_json[0]
self.assertEqual(content["name"], ".")
self.assertEqual(content["project"], expected_project_url)
self.assertEqual(len(content["obligated_extensions"]), 0)
self.assertEqual(len(content["blocked_extensions"]), 0)

content = content_json[1]
self.assertEqual(content["name"], "folder_struct1")
self.assertEqual(content["project"], expected_project_url)
self.assertEqual(len(content["obligated_extensions"]), 1)
self.assertEqual(len(content["blocked_extensions"]), 0)

content = content_json[2]
self.assertEqual(content["name"], "folder_struct1/submap1")
self.assertEqual(content["project"], expected_project_url)
self.assertEqual(len(content["obligated_extensions"]), 2)
self.assertEqual(len(content["blocked_extensions"]), 0)

content = content_json[3]
self.assertEqual(content["name"], "folder_struct1/submap1/templates")
self.assertEqual(content["project"], expected_project_url)
self.assertEqual(len(content["obligated_extensions"]), 1)
self.assertEqual(len(content["blocked_extensions"]), 0)

content = content_json[4]
self.assertEqual(content["name"], "folder_struct1/submap2")
self.assertEqual(content["project"], expected_project_url)
self.assertEqual(len(content["obligated_extensions"]), 1)
self.assertEqual(len(content["blocked_extensions"]), 0)

content = content_json[5]
self.assertEqual(content["name"], "folder_struct1/submap2/src")
self.assertEqual(content["project"], expected_project_url)
self.assertEqual(len(content["obligated_extensions"]), 3)
self.assertEqual(len(content["blocked_extensions"]), 0)

content = content_json[6]
self.assertEqual(content["name"], "folder_struct1/submap3")
self.assertEqual(content["project"], expected_project_url)
self.assertEqual(len(content["obligated_extensions"]), 2)
self.assertEqual(len(content["blocked_extensions"]), 0)

def test_your_checking(self):
course = create_course(id=3, name="test course", academic_startyear=2024)
project = create_project(
name="test",
description="descr",
visible=True,
archived=False,
days=100,
course=course,
)

fileExtensionHS = create_fileExtension(extension="hs")
fileExtensionPDF = create_fileExtension(extension="pdf")
fileExtensionDOCX = create_fileExtension(extension="docx")
fileExtensionLATEX = create_fileExtension(extension="latex")
fileExtensionMD = create_fileExtension(extension="md")
fileExtensionPY = create_fileExtension(extension="py")
fileExtensionHPP = create_fileExtension(extension="hpp")
fileExtensionCPP = create_fileExtension(extension="cpp")
fileExtensionTS = create_fileExtension(extension="ts")
fileExtensionTSX = create_fileExtension(extension="tsx")

create_structureCheck(
name=".",
project=project,
obligated=[],
blocked=[])

create_structureCheck(
name="folder_struct1",
project=project,
obligated=[fileExtensionHS],
blocked=[])

create_structureCheck(
name="folder_struct1/submap1",
project=project,
obligated=[fileExtensionPDF, fileExtensionDOCX],
blocked=[])

create_structureCheck(
name="folder_struct1/submap1/templates",
project=project,
obligated=[fileExtensionLATEX],
blocked=[])

create_structureCheck(
name="folder_struct1/submap2",
project=project,
obligated=[fileExtensionMD],
blocked=[])

create_structureCheck(
name="folder_struct1/submap2/src",
project=project,
obligated=[fileExtensionPY, fileExtensionHPP, fileExtensionCPP],
blocked=[])

create_structureCheck(
name="folder_struct1/submap3",
project=project,
obligated=[fileExtensionTS, fileExtensionTSX],
blocked=[])

succes = (True, 'zip.success')
self.assertEqual(checkZipFile(project=project, dir_path="structures/zip_struct1.zip"), succes)
11 changes: 6 additions & 5 deletions backend/api/tests/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from api.models.student import Student
from api.models.group import Group
from api.models.course import Course
from django.conf import settings


def create_course(name, academic_startyear, description=None, parent_course=None):
Expand Down Expand Up @@ -83,7 +84,7 @@ def test_group_exists(self):
self.assertEqual(len(content_json), 1)

retrieved_group = content_json[0]
expected_project_url = "http://testserver" + reverse(
expected_project_url = settings.TESTING_BASE_LINK + reverse(
"project-detail", args=[str(project.id)]
)

Expand Down Expand Up @@ -122,10 +123,10 @@ def test_multiple_groups(self):
self.assertEqual(len(content_json), 2)

retrieved_group1, retrieved_group2 = content_json
expected_project_url1 = "http://testserver" + reverse(
expected_project_url1 = settings.TESTING_BASE_LINK + reverse(
"project-detail", args=[str(project1.id)]
)
expected_project_url2 = "http://testserver" + reverse(
expected_project_url2 = settings.TESTING_BASE_LINK + reverse(
"project-detail", args=[str(project2.id)]
)

Expand Down Expand Up @@ -159,7 +160,7 @@ def test_group_detail_view(self):

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

expected_project_url = "http://testserver" + reverse(
expected_project_url = settings.TESTING_BASE_LINK + reverse(
"project-detail", args=[str(project.id)]
)

Expand Down Expand Up @@ -203,7 +204,7 @@ def test_group_project(self):
# Parse the JSON content from the response
content_json = json.loads(response.content.decode("utf-8"))

expected_course_url = "http://testserver" + reverse(
expected_course_url = settings.TESTING_BASE_LINK + reverse(
"course-detail", args=[str(course.id)]
)

Expand Down
Loading
Loading