-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from SELab-2/tests_basic_app
Tests basic app
- Loading branch information
Showing
9 changed files
with
1,284 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") | ||
|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.