Skip to content

Commit

Permalink
Add endpoint test
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkjellid committed Jun 27, 2024
1 parent 214f849 commit 52ceea4
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
2 changes: 1 addition & 1 deletion nest/recipes/plans/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


@router.get("/homes/{home_id}/", response=APIResponse[list[RecipePlanRecord]])
def recipe_plan_list_api(
def recipe_plan_list_for_home_api(
request: HttpRequest, home_id: int
) -> APIResponse[list[RecipePlanRecord]]:
recipe_plans = get_recipe_plans_for_home(home_id=home_id)
Expand Down
4 changes: 2 additions & 2 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,8 @@
},
"/api/v1/recipes/plans/homes/{home_id}/": {
"get": {
"operationId": "recipe_plan_list_api",
"summary": "Recipe Plan List Api",
"operationId": "recipe_plan_list_for_home_api",
"summary": "Recipe Plan List For Home Api",
"parameters": [
{
"in": "path",
Expand Down
5 changes: 5 additions & 0 deletions tests/factories/records.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from nest.products.core.records import ProductRecord
from nest.products.oda.records import OdaProductDetailRecord
from nest.recipes.core.records import RecipeDetailRecord, RecipeRecord
from nest.recipes.plans.records import RecipePlanRecord
from nest.recipes.ingredients.records import (
RecipeIngredientRecord,
)
Expand All @@ -25,6 +26,10 @@ class RecipeIngredientRecordFactory(ModelFactory[RecipeIngredientRecord]):
__model__ = RecipeIngredientRecord


class ReipcePlanRecordFactory(ModelFactory[RecipeRecord]):
__model__ = RecipePlanRecord


class ProductRecordFactory(ModelFactory[ProductRecord]):
__model__ = ProductRecord

Expand Down
2 changes: 1 addition & 1 deletion tests/recipes/test_core_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
endpoint=Endpoint(
url=reverse("api-1.0.0:recipe_list_api"),
view_func=recipe_list_api,
mocks=[FactoryMock("get_recipes", [RecipeRecordFactory.build()])],
mocks=[FactoryMock("get_recipes", [RecipeDetailRecordFactory.build()])],
),
requests={
"authenticated_request": Request(
Expand Down
36 changes: 36 additions & 0 deletions tests/recipes/test_plans_endpoints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from unittest.mock import MagicMock

import pytest
from django.urls import reverse
from store_kit.http import status

from tests.factories.endpoints import EndpointFactory, Endpoint, FactoryMock, Request
from nest.recipes.plans.endpoints import recipe_plan_list_for_home_api
from tests.factories.records import ReipcePlanRecordFactory
from tests.helpers.clients import authenticated_client

recipe_plan_list_for_home_api_factory = EndpointFactory(
endpoint=Endpoint(
url=reverse("api-1.0.0:recipe_plan_list_for_home_api", args=[1]),
view_func=recipe_plan_list_for_home_api,
mocks=[
FactoryMock("get_recipe_plans_for_home", [ReipcePlanRecordFactory.build()])
],
),
requests={
"authenticated_request": Request(
help="Test that normal users are able to get a list of recipe_plans",
client=authenticated_client,
expected_status_code=status.HTTP_200_OK,
expected_mock_calls={"get_recipe_plans_for_home": 1},
),
},
)

request_factories = [recipe_plan_list_for_home_api_factory]


@pytest.mark.parametrize("factory", request_factories)
@pytest.mark.django_db
def test_recipes_core_endpoints(factory: EndpointFactory, mocker: MagicMock) -> None:
factory.make_requests_and_assert(mocker)

0 comments on commit 52ceea4

Please sign in to comment.