-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Phil Varner
committed
Nov 11, 2024
1 parent
9b4340e
commit 87770e4
Showing
8 changed files
with
114 additions
and
22 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
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
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from pydantic import BaseModel, Field | ||
|
||
CORE = "https://stapi.example.com/v0.1.0/core" | ||
OPPORTUNITIES = "https://stapi.example.com/v0.1.0/opportunities" | ||
|
||
|
||
class Conformance(BaseModel): | ||
conforms_to: list[str] = Field(default_factory=list, alias="conformsTo") | ||
|
||
def __init__( | ||
self, | ||
*args, | ||
conforms_to: list[str], | ||
**kwargs, | ||
) -> None: | ||
super().__init__(*args, **kwargs) | ||
self.conforms_to = conforms_to |
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 |
---|---|---|
@@ -1,7 +1,11 @@ | ||
from pydantic import BaseModel | ||
from pydantic import BaseModel, Field | ||
|
||
from stapi_fastapi.models.shared import Link | ||
|
||
|
||
class RootResponse(BaseModel): | ||
links: list[Link] | ||
id: str | ||
conformsTo: list[str] = Field(default_factory=list) | ||
title: str = "" | ||
description: str = "" | ||
links: list[Link] = Field(default_factory=list) |
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
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from fastapi import status | ||
from fastapi.testclient import TestClient | ||
|
||
from stapi_fastapi.models.conformance import CORE | ||
|
||
|
||
def test_conformance(stapi_client: TestClient) -> None: | ||
res = stapi_client.get("/conformance") | ||
|
||
assert res.status_code == status.HTTP_200_OK | ||
assert res.headers["Content-Type"] == "application/json" | ||
|
||
body = res.json() | ||
|
||
assert body["conformsTo"] == [CORE] |
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
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 |
---|---|---|
@@ -1,7 +1,21 @@ | ||
from typing import Any | ||
from typing import Any, Callable | ||
|
||
link_dict = dict[str, Any] | ||
|
||
|
||
def find_link(links: list[link_dict], rel: str) -> link_dict | None: | ||
return next((link for link in links if link["rel"] == rel), None) | ||
|
||
|
||
def assert_link( | ||
req: str, | ||
body: dict[str, Any], | ||
rel: str, | ||
path: str, | ||
url_for: Callable[[str], str], | ||
media_type: str = "application/json", | ||
) -> None: | ||
link = find_link(body["links"], rel) | ||
assert link, f"{req} Link[rel={rel}] should exist" | ||
assert link["type"] == media_type | ||
assert link["href"] == url_for(path) |