Skip to content

Commit

Permalink
[FIX] endpoint location unittest OK
Browse files Browse the repository at this point in the history
  • Loading branch information
boot-sandre committed Oct 4, 2023
1 parent 2ec9d4a commit 71ffd38
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 53 deletions.
3 changes: 3 additions & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ pyjwt
pytz
django-stubs
polyfactory
django-hijack
django-import-export
email-validator
77 changes: 37 additions & 40 deletions skii/endpoint/routers/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,42 @@
RouterListContract = List[RouterContract]


@router.post(
path="/create/",
@router.get(
path="/list/",
response={
200: RouterContract,
200: RouterListContract,
422: FormInvalidResponseContract,
},
)
def record_create(request: HttpRequest, payload: RouterSaveContract):
record_payload = payload.dict()
if "coordinate" in record_payload:
geo_coordinate = record_payload["coordinate"]
del record_payload["coordinate"]
geo_coordinate_obj, created = GeoCoordinate.objects.update_or_create(
geo_coordinate, **geo_coordinate
)
record_payload["coordinate"] = geo_coordinate_obj
record = RouterModel(**record_payload)
record.save()
record.refresh_from_db()
return 200, record
def list(request: HttpRequest):
return 200, RouterModel.objects.all()


@router.get(
path="/read/{pk}/",
path="/fetch/{pk}/",
response={
200: RouterContract,
422: FormInvalidResponseContract,
},
)
def record_read(request: HttpRequest, pk: IntStrUUID4):
def fetch(request: HttpRequest, pk: IntStrUUID4):
return 200, get_object_or_404(RouterModel, pk=pk)


@router.delete(
path="/delete/{pk}/",
response={
200: SkiiMsgContract,
422: FormInvalidResponseContract,
},
)
def record_delete(request: HttpRequest, pk: IntStrUUID4):
qs = RouterModel.objects.all().filter(pk=pk)
if qs.exists():
qs.delete()
return 200, SkiiMsgContract(message="OK")


@router.post(
path="/update/{pk}/",
response={
Expand All @@ -79,29 +82,23 @@ def record_update(request: HttpRequest, pk: IntStrUUID4, payload: RouterSaveCont
return 200, record


@router.get(
path="/delete/{pk}/",
response={
200: SkiiMsgContract,
422: FormInvalidResponseContract,
},
)
def record_delete(request: HttpRequest, pk: IntStrUUID4):
qs = RouterModel.objects.all().filter(pk=pk)
if qs.exists():
qs.delete()
return 200, SkiiMsgContract(message="Record deleted")


@router.get(
path="/list/",
@router.post(
path="/create/",
response={
200: RouterListContract,
200: RouterContract,
422: FormInvalidResponseContract,
},
)
def record_list(request: HttpRequest):
return 200, RouterModel.objects.all()


__all__ = [router]
def create(request: HttpRequest, payload: RouterSaveContract):
record_payload = payload.dict()
if "coordinate" in record_payload:
geo_coordinate = record_payload["coordinate"]
del record_payload["coordinate"]
geo_coordinate_obj, created = GeoCoordinate.objects.update_or_create(
geo_coordinate, **geo_coordinate
)
record_payload["coordinate"] = geo_coordinate_obj
record = RouterModel(**record_payload)
record.save()
record.refresh_from_db()
return 200, record
1 change: 1 addition & 0 deletions skii/platform/factories/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class LocationResourceFactory(factory.django.DjangoModelFactory):
class Meta:
model = LocationResource


address1 = factory.Faker("address")
city = factory.Faker("city")
country = factory.Faker("country_code")
Expand Down
5 changes: 3 additions & 2 deletions skii/platform/schemas/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ class Config:
"city",
"coordinate",
"value",
"country",
]
model_fields_optional = ["description", "address2"]

coordinate: GeoCoordinateSaveContract | None
country: str
coordinate: Optional[GeoCoordinateSaveContract]
country: str = Field(default="", alias="country.code")
# cover: VisualPictureContract | None
value: int = 1
34 changes: 23 additions & 11 deletions tests/endpoint/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class TestApiTeacher(SkiiTestCase):
fields = ["user", "pk"]

def test_record_fetch(self):
agent = self.api_factory.create()
record = self.api_factory.create()
response = self.skii_client.get(
path=f"{self.api_route_prefix}/fetch/{agent.pk}/"
path=f"{self.api_route_prefix}/fetch/{record.pk}/"
)
self.assertListEqual(list(response.json().keys()), self.fields)

Expand All @@ -33,25 +33,25 @@ def test_record_list(self):
self.assertEqual(first=len(response.json()), second=5)

def test_record_delete(self):
agent = self.api_factory.create()
record = self.api_factory.create()
response = self.skii_client.delete(
path=f"{self.api_route_prefix}/delete/{agent.pk}/"
path=f"{self.api_route_prefix}/delete/{record.pk}/"
)
assert response.content == b'{"message": "OK"}'

def test_record_update(self):
agent = self.api_factory.create()
payload = self.api_save_contract.from_orm(agent).dict()
record = self.api_factory.create()
payload = self.api_save_contract.from_orm(record).dict()
response = self.skii_client.post(
path=f"{self.api_route_prefix}/update/{agent.pk}/",
path=f"{self.api_route_prefix}/update/{record.pk}/",
data=payload,
content_type="application/json",
)
self.assertListEqual(list1=list(response.json().keys()), list2=self.fields)

def test_record_create(self):
agent = self.api_factory.build()
payload = self.api_save_contract.from_orm(agent).dict()
record = self.api_factory.build()
payload = self.api_save_contract.from_orm(record).dict()
response = self.skii_client.post(
path=f"{self.api_route_prefix}/create/",
data=payload,
Expand All @@ -71,13 +71,25 @@ class TestApiStudent(TestApiTeacher):


class TestApiLocation(TestApiTeacher):
"""Basic unit testing of Agent models and schema."""
"""Basic unit testing of Resource models and schema."""

api_factory = LocationResourceFactory
api_save_contract = LocationSaveContract

api_route_namespace = "location"
fields = ["country", "cover", "coordinate", "value"]
fields = [
'description',
'label',
'address1',
'address2',
'city',
'country',
'cover',
'illustration',
'coordinate',
'value',
'pk'
]


class TestApiLesson(TestApiTeacher):
Expand Down

0 comments on commit 71ffd38

Please sign in to comment.