Skip to content

Commit

Permalink
Merge pull request #44 from SELab-2/tests_basic_app
Browse files Browse the repository at this point in the history
Tests basic app
  • Loading branch information
tyboro2002 authored Mar 2, 2024
2 parents 1e79fd0 + ebed9ed commit 90adea3
Show file tree
Hide file tree
Showing 9 changed files with 1,284 additions and 47 deletions.
85 changes: 76 additions & 9 deletions backend/api/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,43 @@
from django.urls import reverse

from ..models.admin import Admin
from authentication.models import Faculty


def create_admin(id, first_name, last_name, email):
def create_faculty(name):
"""
Create a Faculty with the given arguments."""
return Faculty.objects.create(
name=name
)


def create_admin(id, first_name, last_name, email, faculty=None):
"""
Create a Admin with the given arguments.
"""
username = f"{first_name}_{last_name}"
return Admin.objects.create(
id=id,
first_name=first_name,
last_name=last_name,
username=username,
email=email,
create_time=timezone.now(),
)
if faculty is None:
return Admin.objects.create(
id=id,
first_name=first_name,
last_name=last_name,
username=username,
email=email,
create_time=timezone.now()
)
else:
admin = Admin.objects.create(
id=id,
first_name=first_name,
last_name=last_name,
username=username,
email=email,
create_time=timezone.now(),
)
for fac in faculty:
admin.faculties.add(fac)
return admin


class AdminModelTests(TestCase):
Expand Down Expand Up @@ -145,3 +167,48 @@ def test_admin_detail_view(self):
self.assertEqual(content_json["first_name"], admin.first_name)
self.assertEqual(content_json["last_name"], admin.last_name)
self.assertEqual(content_json["email"], admin.email)

def test_admin_faculty(self):
"""
Able to retrieve faculty details of a single admin.
"""
# Create an admin for testing with the name "Bob Peeters"
faculty = create_faculty(name="testing faculty")
admin = create_admin(
id=5,
first_name="Bob",
last_name="Peeters",
email="[email protected]",
faculty=[faculty]
)

# Make a GET request to retrieve the admin details
response = self.client.get(
reverse("admin-detail", args=[str(admin.id)]), follow=True)

# Check if the response was successful
self.assertEqual(response.status_code, 200)

# Assert that the response is JSON
self.assertEqual(response.accepted_media_type, "application/json")

# Parse the JSON content from the response
content_json = json.loads(response.content.decode("utf-8"))

# Assert the details of the retrieved admin match the created admin
self.assertEqual(int(content_json["id"]), admin.id)
self.assertEqual(content_json["first_name"], admin.first_name)
self.assertEqual(content_json["last_name"], admin.last_name)
self.assertEqual(content_json["email"], admin.email)

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

# Check if the response was successful
self.assertEqual(response.status_code, 200)

# Assert that the response is JSON
self.assertEqual(response.accepted_media_type, "application/json")

# Parse the JSON content from the response
content_json = json.loads(response.content.decode("utf-8"))
self.assertEqual(content_json["name"], faculty.name)
180 changes: 169 additions & 11 deletions backend/api/tests/test_assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,69 @@
from django.utils import timezone
from django.urls import reverse
from api.models.assistant import Assistant
from api.models.course import Course
from authentication.models import Faculty


def create_assistant(id, first_name, last_name, email):
# Create an assistant with the given arguments.
username = f"{first_name}_{last_name}"
return Assistant.objects.create(
id=id,
first_name=first_name,
last_name=last_name,
username=username,
email=email,
create_time=timezone.now(),
def create_course(name, academic_startyear, description=None,
parent_course=None):
"""
Create a Course with the given arguments.
"""
return Course.objects.create(
name=name,
academic_startyear=academic_startyear,
description=description,
parent_course=parent_course
)


def create_faculty(name):
"""Create a Faculty with the given arguments."""
return Faculty.objects.create(
name=name
)


def create_assistant(
id,
first_name,
last_name,
email,
faculty=None,
courses=None
):
"""
Create a assistant with the given arguments.
"""
username = f"{first_name}_{last_name}"
assistant = Assistant.objects.create(
id=id,
first_name=first_name,
last_name=last_name,
username=username,
email=email,
create_time=timezone.now()
)

if faculty is not None:
for fac in faculty:
assistant.faculties.add(fac)

if courses is not None:
for cours in courses:
assistant.courses.add(cours)

return assistant


class AssistantModelTests(TestCase):
def test_no_assistant(self):
"""
able to retrieve no assistant before publishing it.
"""

response_root = self.client.get(reverse("assistant-list"), follow=True)
# print(response.content)
self.assertEqual(response_root.status_code, 200)
# Assert that the response is JSON
self.assertEqual(response_root.accepted_media_type, "application/json")
Expand Down Expand Up @@ -151,3 +191,121 @@ def test_assistant_detail_view(self):
self.assertEqual(content_json["first_name"], assistant.first_name)
self.assertEqual(content_json["last_name"], assistant.last_name)
self.assertEqual(content_json["email"], assistant.email)

def test_assistant_faculty(self):
"""
Able to retrieve faculty details of a single assistant.
"""
# Create an assistant for testing with the name "Bob Peeters"
faculty = create_faculty(name="testing faculty")
assistant = create_assistant(
id=5,
first_name="Bob",
last_name="Peeters",
email="[email protected]",
faculty=[faculty]
)

# Make a GET request to retrieve the assistant details
response = self.client.get(
reverse("assistant-detail", args=[str(assistant.id)]), follow=True)

# Check if the response was successful
self.assertEqual(response.status_code, 200)

# Assert that the response is JSON
self.assertEqual(response.accepted_media_type, "application/json")

# Parse the JSON content from the response
content_json = json.loads(response.content.decode("utf-8"))

# Assert the details of the retrieved assistant
# match the created assistant
self.assertEqual(int(content_json["id"]), assistant.id)
self.assertEqual(content_json["first_name"], assistant.first_name)
self.assertEqual(content_json["last_name"], assistant.last_name)
self.assertEqual(content_json["email"], assistant.email)

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

# Check if the response was successful
self.assertEqual(response.status_code, 200)

# Assert that the response is JSON
self.assertEqual(response.accepted_media_type, "application/json")

# Parse the JSON content from the response
content_json = json.loads(response.content.decode("utf-8"))
self.assertEqual(content_json["name"], faculty.name)

def test_assistant_courses(self):
"""
Able to retrieve courses details of a single assistant.
"""
# Create an assistant for testing with the name "Bob Peeters"
course1 = create_course(
name="Introduction to Computer Science",
academic_startyear=2022,
description="An introductory course on computer science."
)
course2 = create_course(
name="Intermediate to Computer Science",
academic_startyear=2023,
description="An second course on computer science."
)

assistant = create_assistant(
id=5,
first_name="Bob",
last_name="Peeters",
email="[email protected]",
courses=[course1, course2]
)

# Make a GET request to retrieve the assistant details
response = self.client.get(
reverse("assistant-detail", args=[str(assistant.id)]), follow=True)

# Check if the response was successful
self.assertEqual(response.status_code, 200)

# Assert that the response is JSON
self.assertEqual(response.accepted_media_type, "application/json")

# Parse the JSON content from the response
content_json = json.loads(response.content.decode("utf-8"))

# Assert the details of the retrieved assistant
# match the created assistant
self.assertEqual(int(content_json["id"]), assistant.id)
self.assertEqual(content_json["first_name"], assistant.first_name)
self.assertEqual(content_json["last_name"], assistant.last_name)
self.assertEqual(content_json["email"], assistant.email)

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

# Check if the response was successful
self.assertEqual(response.status_code, 200)

# Assert that the response is JSON
self.assertEqual(response.accepted_media_type, "application/json")

# Parse the JSON content from the response
content_json = json.loads(response.content.decode("utf-8"))

# Assert that the parsed JSON is a list with multiple assistant
self.assertEqual(len(content_json), 2)

content = content_json[0]
self.assertEqual(int(content["id"]), course1.id)
self.assertEqual(content["name"], course1.name)
self.assertEqual(
int(content["academic_startyear"]), course1.academic_startyear)
self.assertEqual(content["description"], course1.description)

content = content_json[1]
self.assertEqual(int(content["id"]), course2.id)
self.assertEqual(content["name"], course2.name)
self.assertEqual(
int(content["academic_startyear"]), course2.academic_startyear)
self.assertEqual(content["description"], course2.description)
1 change: 0 additions & 1 deletion backend/api/tests/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def test_no_fileExtension(self):
"""
response_root = self.client.get(
reverse("fileExtension-list"), follow=True)
# print(response.content)
self.assertEqual(response_root.status_code, 200)
# Assert that the response is JSON
self.assertEqual(response_root.accepted_media_type, "application/json")
Expand Down
Loading

0 comments on commit 90adea3

Please sign in to comment.