-
-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADD] test_extendable_pydantic_fastapi: unit tests
- Loading branch information
1 parent
291a534
commit 8ec5c45
Showing
6 changed files
with
77 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
httpx | ||
extendable-pydantic @ git+https://github.com/lmignon/extendable-pydantic.git@refs/pull/7/head |
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 @@ | ||
from . import test_coordinate |
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,71 @@ | ||
# Copyright 2023 ACSONE SA/NV | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
from requests import Response | ||
|
||
from odoo.tests.common import tagged | ||
|
||
from odoo.addons.extendable_fastapi.tests.common import FastAPITransactionCase | ||
|
||
from ..routers import demo_pydantic_router | ||
from ..schemas import Coordinate | ||
|
||
|
||
@tagged("post_install", "-at_install") | ||
class TestCoordinate(FastAPITransactionCase): | ||
@classmethod | ||
def setUpClass(cls) -> None: | ||
super().setUpClass() | ||
|
||
def test_app_components(self): | ||
with self._create_test_client(router=demo_pydantic_router) as test_client: | ||
to_openapi = test_client.app.openapi() | ||
# Check post input and output types | ||
self.assertEqual( | ||
to_openapi["paths"]["/demo_pydantic"]["post"]["requestBody"]["content"][ | ||
"application/json" | ||
]["schema"]["$ref"], | ||
"#/components/schemas/Coordinate", | ||
) | ||
self.assertEqual( | ||
to_openapi["paths"]["/demo_pydantic"]["post"]["responses"]["200"][ | ||
"content" | ||
]["application/json"]["schema"]["$ref"], | ||
"#/components/schemas/CoordinateSearchResponse", | ||
) | ||
|
||
# Check Pydantic model extension | ||
self.assertEqual( | ||
set( | ||
to_openapi["components"]["schemas"]["Coordinate"][ | ||
"properties" | ||
].keys() | ||
), | ||
{"lat", "lon", "name", "description"}, | ||
) | ||
self.assertEqual( | ||
to_openapi["components"]["schemas"]["CoordinateSearchResponse"][ | ||
"properties" | ||
]["items"]["items"]["$ref"], | ||
"#/components/schemas/Coordinate", | ||
) | ||
|
||
def test_post_coordinates(self): | ||
name = "Bievre" | ||
desc = "Small town in Belgium" | ||
pydantic_data = Coordinate(lat=49.94, lon=5.02, name=name, description=desc) | ||
# Assert that class was correctly extended | ||
self.assertTrue(pydantic_data.name) | ||
self.assertTrue(pydantic_data.description) | ||
|
||
with self._create_test_client(router=demo_pydantic_router) as test_client: | ||
response: Response = test_client.post( | ||
"/demo_pydantic", content=pydantic_data.model_dump_json() | ||
) | ||
self.assertEqual(response.status_code, 200) | ||
res = response.json() | ||
self.assertEqual(res["total"], 1) | ||
coordinate = res["items"][0] | ||
self.assertEqual(coordinate["lat"], 49.94) | ||
self.assertEqual(coordinate["lon"], 5.02) | ||
self.assertEqual(coordinate["name"], name) | ||
self.assertEqual(coordinate["description"], desc) |