Commit 4ac65e6a authored by Imane Misrar's avatar Imane Misrar
Browse files

feat(test_last_visited_university) : add tests on last visited universities

Relates to #149
parent fddea2c8
import json
from backend_app.tests.utils import get_dummy_university, WithUserTestCase
from backend_app.models.lastVisitedUniversities import LastVisitedUniversity
class LastVisitedUniversityTestCase(WithUserTestCase):
@classmethod
def setUpMoreTestData(cls):
cls.api_endpoint = "/api/lastVisitedUniversities/"
cls.university = get_dummy_university()
def test_create_last_visited_university(self):
data = {"university": self.university.pk}
response = self.authenticated_client.post(
self.api_endpoint, data, format="json"
)
self.assertEqual(response.status_code, 201)
def test_get_last_visited_universities_empty(self):
response = self.authenticated_client.get(self.api_endpoint)
self.assertEqual(response.status_code, 200)
content = json.loads(response.content)
self.assertEqual(len(content), 0)
def test_get_last_visited_universities_one(self):
LastVisitedUniversity.objects.create(
user=self.authenticated_user, university=self.university
)
response = self.authenticated_client.get(self.api_endpoint)
self.assertEqual(response.status_code, 200)
content = json.loads(response.content)
self.assertEqual(len(content), 1)
self.assertEqual(content[0]["university"], self.university.pk)
def test_get_last_visited_universities_one_several_times(self):
for i in range(10):
LastVisitedUniversity.objects.create(
user=self.authenticated_user, university=self.university
)
response = self.authenticated_client.get(self.api_endpoint)
self.assertEqual(response.status_code, 200)
content = json.loads(response.content)
self.assertEqual(len(content), 1)
self.assertEqual(content[0]["university"], self.university.pk)
def test_get_last_visited_universities_multiple(self):
universities = [get_dummy_university(i) for i in range(10)]
for univ in universities:
LastVisitedUniversity.objects.create(
user=self.authenticated_user, university=univ
)
response = self.authenticated_client.get(self.api_endpoint)
self.assertEqual(response.status_code, 200)
content = json.loads(response.content)
self.assertEqual(len(content), 5)
for i, res in enumerate(content):
univ_id = res["university"]
target_univ_id = universities[-1 - i].pk
self.assertEqual(univ_id, target_univ_id)
......@@ -94,9 +94,9 @@ def get_dummy_country() -> Country:
)[0]
def get_dummy_university() -> University:
def get_dummy_university(id: int = 1) -> University:
return University.objects.update_or_create(
name="Université de Technologie de Compiègne",
name=f"Université de Technologie de Compiègne_{id}",
defaults=dict(
acronym="UTC",
main_campus_lat=0,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment