Skip to content

Commit

Permalink
chore: add some tests for filestructures
Browse files Browse the repository at this point in the history
  • Loading branch information
tyboro2002 committed Mar 9, 2024
1 parent 998c51d commit 3da961a
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 24 deletions.
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
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)
59 changes: 55 additions & 4 deletions backend/api/tests/test_file_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,59 @@ def test_your_function(self):

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

# print("strucCheck:", content_json["structure_checks"])
# TODO get the structure checks from this now its following
# strucCheck: http://testserver/projects/1/structure_checks/
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)]
)

self.assertEqual(True, True)
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)
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
13 changes: 7 additions & 6 deletions backend/api/tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from api.models.course import Course
from api.models.checks import StructureCheck, ExtraCheck
from api.models.extension import FileExtension
from django.conf import settings


def create_course(id, name, academic_startyear):
Expand Down Expand Up @@ -212,7 +213,7 @@ def test_project_exists(self):

retrieved_project = content_json[0]

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

Expand Down Expand Up @@ -256,7 +257,7 @@ def test_multiple_project(self):

retrieved_project = content_json[0]

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

Expand All @@ -268,7 +269,7 @@ def test_multiple_project(self):

retrieved_project = content_json[1]

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

Expand Down Expand Up @@ -361,7 +362,7 @@ def test_project_structure_checks(self):

retrieved_project = content_json[0]

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

Expand Down Expand Up @@ -449,7 +450,7 @@ def test_project_extra_checks(self):
content_json = json.loads(response.content.decode("utf-8"))[0]

self.assertEqual(int(content_json["id"]), checks.id)
self.assertEqual(content_json["project"], "http://testserver" + reverse(
self.assertEqual(content_json["project"], settings.TESTING_BASE_LINK + reverse(
"project-detail", args=[str(project.id)]
))
self.assertEqual(content_json["run_script"], "http://testserver" + checks.run_script.url)
self.assertEqual(content_json["run_script"], settings.TESTING_BASE_LINK + checks.run_script.url)
11 changes: 6 additions & 5 deletions backend/api/tests/test_submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from api.models.group import Group
from api.models.course import Course
from api.models.checks import ExtraCheck
from django.conf import settings


def create_course(name, academic_startyear, description=None, parent_course=None):
Expand Down Expand Up @@ -98,7 +99,7 @@ def test_submission_exists(self):
# Assert the details of the retrieved submission
# match the created submission
retrieved_submission = content_json[0]
expected_group_url = "http://testserver" + reverse(
expected_group_url = settings.TESTING_BASE_LINK + reverse(
"group-detail", args=[str(group.id)]
)
self.assertEqual(int(retrieved_submission["id"]), submission.id)
Expand Down Expand Up @@ -139,7 +140,7 @@ def test_multiple_submission_exists(self):
# Assert the details of the retrieved submission
# match the created submission
retrieved_submission = content_json[0]
expected_group_url = "http://testserver" + reverse(
expected_group_url = settings.TESTING_BASE_LINK + reverse(
"group-detail", args=[str(group.id)]
)
self.assertEqual(int(retrieved_submission["id"]), submission1.id)
Expand All @@ -150,7 +151,7 @@ def test_multiple_submission_exists(self):
self.assertEqual(retrieved_submission["group"], expected_group_url)

retrieved_submission = content_json[1]
expected_group_url = "http://testserver" + reverse(
expected_group_url = settings.TESTING_BASE_LINK + reverse(
"group-detail", args=[str(group.id)]
)
self.assertEqual(int(retrieved_submission["id"]), submission2.id)
Expand Down Expand Up @@ -188,7 +189,7 @@ def test_submission_detail_view(self):
# Assert the details of the retrieved submission
# match the created submission
retrieved_submission = content_json
expected_group_url = "http://testserver" + reverse(
expected_group_url = settings.TESTING_BASE_LINK + reverse(
"group-detail", args=[str(group.id)]
)
self.assertEqual(int(retrieved_submission["id"]), submission.id)
Expand Down Expand Up @@ -241,7 +242,7 @@ def test_submission_group(self):
# Parse the JSON content from the response
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
6 changes: 5 additions & 1 deletion backend/api/views/check_folder_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
def parseZipFile(project, dir_path): # TODO block paths that start with ..
dir_path = os.path.normpath(os.path.join(settings.MEDIA_ROOT, dir_path))
struct = get_zip_structure(dir_path)
for key, value in struct.items():

sorted_keys = sorted(struct.keys())

for key in sorted_keys:
value = struct[key]
check = StructureCheck.objects.create(
name=key,
project=project
Expand Down
2 changes: 2 additions & 0 deletions backend/ypovoli/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
BASE_DIR = Path(__file__).resolve().parent.parent
MEDIA_ROOT = os.path.normpath(os.path.join(BASE_DIR, "../data/production"))

TESTING_BASE_LINK = "http://testserver"


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/
Expand Down

0 comments on commit 3da961a

Please sign in to comment.