From c2c29295aee5278d8b47ed604e6b5b27a3d2fa63 Mon Sep 17 00:00:00 2001 From: ANDRE Simon Date: Sat, 16 Sep 2023 00:04:54 +0200 Subject: [PATCH] [NEW] introduce skii test case --- skii/endpoint/api.py | 3 +-- skii/endpoint/routers/lesson.py | 4 +--- skii/endpoint/routers/location.py | 4 +--- skii/endpoint/routers/student.py | 9 +++---- skii/endpoint/routers/teacher.py | 9 ++++--- skii/endpoint/schemas/ninja.py | 3 ++- skii/platform/entities.py | 8 +++++-- skii/platform/models/event.py | 1 + skii/platform/schemas/agent.py | 9 +++---- skii/platform/schemas/event.py | 1 - tests/account/test_api.py | 4 ++-- tests/endpoint/test_api.py | 39 +++++++------------------------ tests/platform/test_models.py | 4 ++-- tests/test_testcase.py | 4 ++-- tests/testcase.py | 37 ++++++++++++++++++++++++++--- 15 files changed, 73 insertions(+), 66 deletions(-) diff --git a/skii/endpoint/api.py b/skii/endpoint/api.py index 876f362..105de54 100644 --- a/skii/endpoint/api.py +++ b/skii/endpoint/api.py @@ -48,7 +48,6 @@ def render(self, request, data, *, response_status): class SkiiParser(Parser): - def parse_body(self, request: HttpRequest) -> DictStrAny: return cast(DictStrAny, json.loads(request.body)) @@ -75,7 +74,7 @@ def parse_querydict( "docs_decorator": staff_member_required, "urls_namespace": "skii", "renderer": SkiiJsonRenderer(), - "parser": SkiiParser() + "parser": SkiiParser(), } # Create skii app dedicated api diff --git a/skii/endpoint/routers/lesson.py b/skii/endpoint/routers/lesson.py index 37bf55b..970db60 100644 --- a/skii/endpoint/routers/lesson.py +++ b/skii/endpoint/routers/lesson.py @@ -83,9 +83,7 @@ def record_delete(request: HttpRequest, record_id: int | str): 422: FormInvalidResponseContract, }, ) -def record_save( - request: HttpRequest, record_id: int | str, payload: LessonContract -): +def record_save(request: HttpRequest, record_id: int | str, payload: LessonContract): lesson_payload = payload.dict() lesson_obj = get_object_or_404(Lesson, pk=record_id) for attr, value in lesson_payload.items(): diff --git a/skii/endpoint/routers/location.py b/skii/endpoint/routers/location.py index 978aa3e..06565c7 100644 --- a/skii/endpoint/routers/location.py +++ b/skii/endpoint/routers/location.py @@ -58,9 +58,7 @@ def record_delete(request: HttpRequest, record_id: int | str): 422: FormInvalidResponseContract, }, ) -def record_save( - request: HttpRequest, record_id: int | str, payload: LocationContract -): +def record_save(request: HttpRequest, record_id: int | str, payload: LocationContract): location_payload = payload.dict() location_obj = get_object_or_404(Location, pk=record_id) for attr, value in location_payload.items(): diff --git a/skii/endpoint/routers/student.py b/skii/endpoint/routers/student.py index 2378db9..d3240db 100644 --- a/skii/endpoint/routers/student.py +++ b/skii/endpoint/routers/student.py @@ -7,10 +7,12 @@ from apps.base.schemas import FormInvalidResponseContract from skii.platform.models.agent import StudentAgent -from skii.platform.schemas.agent import ( - StudentContract, StudentSaveContract +from skii.platform.schemas.agent import StudentContract, StudentSaveContract +from skii.endpoint.schemas.ninja import ( + SkiiRecordContract, + SkiiListContract, + SkiiMsgContract, ) -from skii.endpoint.schemas.ninja import SkiiRecordContract, SkiiListContract, SkiiMsgContract UserModel = get_user_model() @@ -57,7 +59,6 @@ def fetch(request: HttpRequest, pk: int | str): return 200, dict(data=record) - @sub_route.delete( path="/delete/{pk}/", response={ diff --git a/skii/endpoint/routers/teacher.py b/skii/endpoint/routers/teacher.py index a955ba4..5b13b47 100644 --- a/skii/endpoint/routers/teacher.py +++ b/skii/endpoint/routers/teacher.py @@ -7,9 +7,7 @@ from apps.base.schemas import FormInvalidResponseContract from skii.platform.models.agent import TeacherAgent -from skii.platform.schemas.agent import ( - TeacherContract, TeacherSaveContract -) +from skii.platform.schemas.agent import TeacherContract, TeacherSaveContract from skii.endpoint.schemas.ninja import SkiiMsgContract @@ -28,6 +26,7 @@ ListResponseContract = List[SubRouteContract] ResponseContract = SubRouteContract + @sub_route.get( path="/list/", response={ @@ -74,9 +73,9 @@ def delete(request: HttpRequest, pk: int | str): def update(request: HttpRequest, pk: int | str, payload: SubRouteSaveContract): payload = payload.dict() user_payload = payload.pop("user") - record = get_object_or_404(SubRouteModel,pk=pk) + record = get_object_or_404(SubRouteModel, pk=pk) user_obj = get_object_or_404(UserModel, id=user_payload["id"]) - for attr, value in payload.items(): + for attr, value in payload.items(): setattr(record, attr, value) record.save() for attr, value in user_payload.items(): diff --git a/skii/endpoint/schemas/ninja.py b/skii/endpoint/schemas/ninja.py index f97753a..60f0469 100644 --- a/skii/endpoint/schemas/ninja.py +++ b/skii/endpoint/schemas/ninja.py @@ -18,6 +18,7 @@ class SkiiMsgContract(Schema): class IdentifierContract(Schema): pk: Optional[UUID4] + class Config: fields = {} @@ -47,4 +48,4 @@ class FormInvalidResponseContract(Schema): } """ - errors: Dict[str, List[Dict[str, Any]]] \ No newline at end of file + errors: Dict[str, List[Dict[str, Any]]] diff --git a/skii/platform/entities.py b/skii/platform/entities.py index 380a79e..18db23c 100644 --- a/skii/platform/entities.py +++ b/skii/platform/entities.py @@ -264,8 +264,12 @@ class RessourceEntity(UUIDLabelEntity): class Meta: abstract = True - amount = models.DecimalField(verbose_name=_("Time ressource"), default=Deci(0.0), - max_digits=18, decimal_places=2,) + amount = models.DecimalField( + verbose_name=_("Time ressource"), + default=Deci(0.0), + max_digits=18, + decimal_places=2, + ) def __str__(self): return f"{self.short_prefix_uuid}: {self.amount}" diff --git a/skii/platform/models/event.py b/skii/platform/models/event.py index 0d1c5b3..8370023 100644 --- a/skii/platform/models/event.py +++ b/skii/platform/models/event.py @@ -54,6 +54,7 @@ def __str__(self) -> str: @property def gant_config(self): from skii.platform.schemas.vuejs import GanttConfigContract + return GanttConfigContract( { "start": self.start.strftime(format="%Y-%m-%d %H:%M"), diff --git a/skii/platform/schemas/agent.py b/skii/platform/schemas/agent.py index bbc2d5a..113ccdf 100644 --- a/skii/platform/schemas/agent.py +++ b/skii/platform/schemas/agent.py @@ -7,11 +7,8 @@ class UserSchema(Schema): - """Complete dj user schema generated from dj models + """DJ user schema used to read record""" - Mostly to read records - All technicals/Identifiant fields is excluded - """ id: Optional[int] first_name: Optional[str] last_name: Optional[str] @@ -20,10 +17,11 @@ class UserSchema(Schema): class UserSaveSchema(ModelSchema): + """DJ user schema used to save record""" class Config: model = get_user_model() - model_fields = ['username', 'first_name', 'last_name'] + model_fields = ["username", "first_name", "last_name"] class StudentContract(IdentifierContract): @@ -40,4 +38,3 @@ class StudentSaveContract(IdentifierContract): class TeacherSaveContract(IdentifierContract): user: UserSchema - diff --git a/skii/platform/schemas/event.py b/skii/platform/schemas/event.py index b3e8628..0b345a5 100644 --- a/skii/platform/schemas/event.py +++ b/skii/platform/schemas/event.py @@ -35,4 +35,3 @@ class LocationContract(Schema): class LocationSaveContract(LocationContract): country: str - diff --git a/tests/account/test_api.py b/tests/account/test_api.py index 6adbb88..6d79309 100644 --- a/tests/account/test_api.py +++ b/tests/account/test_api.py @@ -1,7 +1,7 @@ from django.contrib.auth import get_user_model from apps.account.utils.token import encode_token from main.api import api -from ..testcase import NinjaTestCase +from ..testcase import SkiiTestCase import json PWD = "testpwd" @@ -20,7 +20,7 @@ def test_admin_account_state(admin_client): assert response.content == b'{"is_connected": true, "username": "admin"}' -class TestAccount(NinjaTestCase): +class TestAccount(SkiiTestCase): def test_anonymous_account_state(self): response = self.client.get(f"{api.root_path}account/state") assert response.status_code == 200 diff --git a/tests/endpoint/test_api.py b/tests/endpoint/test_api.py index 8f09153..07fbadd 100644 --- a/tests/endpoint/test_api.py +++ b/tests/endpoint/test_api.py @@ -1,6 +1,4 @@ -from django.test.client import Client -from skii.endpoint.api import api_skii -from ..testcase import NinjaTestCase +from ..testcase import SkiiTestCase, SkiiClient from skii.platform.schemas.agent import TeacherSaveContract from skii.platform.factories.factories import ( @@ -8,31 +6,12 @@ ) -class SkiiClient(Client): - pass - - -class TestApiAgent(NinjaTestCase): +class TestApiAgent(SkiiTestCase): """ Basic unit testing of Agent models and schema. """ client_class = SkiiClient - - def create_client_helper(self): - """Create test user and log them to dedicated client.""" - res = super().create_client_helper() - self.skii_client = self.client_class() - self.skii_client.force_login(self.admin_user) - return res - - def link_api_helper(self): - """ Override original method to link Skii API""" - self.api = api_skii - self.root_path = self.api.root_path - self.docs_url = self.api.docs_url - self.student_route = "student" - self.teacher_route = "teacher" - self.student_url = f"{self.root_path}{self.student_route}" - self.teacher_url = f"{self.root_path}{self.teacher_route}" + api_factory = TeacherAgentFactory + api_save_contract = TeacherSaveContract def test_agent_teacher_fetch(self): teacher = TeacherAgentFactory.create() @@ -43,7 +22,7 @@ def test_agent_teacher_fetch(self): ['pk', 'user']) def test_agent_teacher_list(self): - TeacherAgentFactory.create_batch(5) + self.api_factory.create_batch(5) response = self.skii_client.get(path=f"{self.teacher_url}/list/") self.assertEqual(first=response.status_code, second=200) @@ -51,13 +30,13 @@ def test_agent_teacher_list(self): second=5) def test_agent_teacher_delete(self): - teacher = TeacherAgentFactory.create() + teacher = self.api_factory.create() response = self.skii_client.delete( path=f"{self.teacher_url}/delete/{teacher.pk}/") assert response.content == b'{"message": "OK"}' def test_agent_teacher_create(self): - teacher = TeacherAgentFactory.create() + teacher = self.api_factory.create() payload = TeacherSaveContract.from_orm(teacher).dict() del payload["pk"] del payload["user"]["id"] @@ -72,8 +51,8 @@ def test_agent_teacher_create(self): list2=['pk', 'user']) def test_agent_teacher_update(self): - teacher = TeacherAgentFactory.create() - payload = TeacherSaveContract.from_orm(teacher).dict() + teacher = self.api_factory.create() + payload = self.api_save_contract.from_orm(teacher).dict() response = self.skii_client.post( path=f"{self.teacher_url}/update/{teacher.pk}/", data=payload, diff --git a/tests/platform/test_models.py b/tests/platform/test_models.py index 4ce27a4..ba9d054 100644 --- a/tests/platform/test_models.py +++ b/tests/platform/test_models.py @@ -1,4 +1,4 @@ -from ..testcase import NinjaTestCase +from ..testcase import SkiiTestCase from skii.platform.factories.factories import ( TeacherAgentFactory, @@ -6,7 +6,7 @@ ) -class TestAgent(NinjaTestCase): +class TestAgent(SkiiTestCase): """ Basic unit testing of Agent models and schema. """ def test_agent_student_create(self): diff --git a/tests/test_testcase.py b/tests/test_testcase.py index dfb356f..e5e9eab 100644 --- a/tests/test_testcase.py +++ b/tests/test_testcase.py @@ -1,7 +1,7 @@ -from .testcase import NinjaTestCase +from .testcase import SkiiTestCase -class TestNinjaTestCase(NinjaTestCase): +class TestSkiiTestCase(SkiiTestCase): def test_get_public(self): res = self.client.get("/") assert res.status_code == 200 diff --git a/tests/testcase.py b/tests/testcase.py index ed47f0c..393e396 100644 --- a/tests/testcase.py +++ b/tests/testcase.py @@ -1,7 +1,9 @@ -from django.test import TestCase -from main.api import api +from django.test import TestCase, Client from django.contrib.auth import get_user_model +from main.api import api as api_main +from skii.endpoint.api import api_skii + class NinjaTestCase(TestCase): def create_client_helper(self): @@ -19,10 +21,39 @@ def create_client_helper(self): def link_api_helper(self): """Can override this method to link another project api to the test case.""" - self.api = api + self.api = api_main self.root_path = self.api.root_path self.docs_url = self.api.docs_url def setUp(self): self.create_client_helper() self.link_api_helper() + + +class SkiiClient(Client): + pass + + +class SkiiTestCase(NinjaTestCase): + client_class = SkiiClient + api_factory = None + api_save_contract = None + + def create_client_helper(self): + """Create test user and log them to dedicated client.""" + res = super().create_client_helper() + self.skii_client = self.client_class() + self.skii_client.force_login(self.admin_user) + return res + + def link_api_helper(self): + """ Override original method to link Skii API""" + self.api = api_skii + self.root_path = self.api.root_path + self.docs_url = self.api.docs_url + self.student_route = "student" + self.teacher_route = "teacher" + self.student_url = f"{self.root_path}{self.student_route}" + self.teacher_url = f"{self.root_path}{self.teacher_route}" + +