Skip to content

Commit ab0f51e

Browse files
committed
refactor
1 parent 3d4e456 commit ab0f51e

File tree

4 files changed

+85
-80
lines changed

4 files changed

+85
-80
lines changed

tests/test_api/test_api_sqla_with_includes.py

Lines changed: 1 addition & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
from datetime import datetime, timezone
44
from itertools import chain, zip_longest
55
from json import dumps
6-
from typing import Any, Dict, List
7-
from unittest.mock import Mock
6+
from typing import Dict, List
87
from uuid import UUID, uuid4
98

109
from fastapi import FastAPI, status
@@ -14,8 +13,6 @@
1413
from sqlalchemy import select
1514
from sqlalchemy.ext.asyncio import AsyncSession
1615

17-
from fastapi_jsonapi.data_layers.filtering.sqlalchemy import Node
18-
from fastapi_jsonapi.exceptions.json_api import InvalidType
1916
from fastapi_jsonapi.views.view_base import ViewBase
2017
from tests.common import is_postgres_tests
2118
from tests.fixtures.app import build_app_custom
@@ -2178,80 +2175,4 @@ async def test_sort(
21782175
}
21792176

21802177

2181-
# TODO: move to it's own test module
2182-
class TestSQLAFilteringModule:
2183-
def test_user_type_cast_success(self):
2184-
class UserType:
2185-
def __init__(self, *args, **kwargs):
2186-
self.value = "success"
2187-
2188-
class ModelSchema(BaseModel):
2189-
user_type: UserType
2190-
2191-
class Config:
2192-
arbitrary_types_allowed = True
2193-
2194-
node = Node(
2195-
model=Mock(),
2196-
filter_={
2197-
"name": "user_type",
2198-
"op": "eq",
2199-
"val": Any,
2200-
},
2201-
schema=ModelSchema,
2202-
)
2203-
2204-
model_column_mock = Mock()
2205-
model_column_mock.eq = lambda clear_value: clear_value
2206-
2207-
clear_value = node.create_filter(
2208-
schema_field=ModelSchema.__fields__["user_type"],
2209-
model_column=model_column_mock,
2210-
operator=Mock(),
2211-
value=Any,
2212-
)
2213-
assert isinstance(clear_value, UserType)
2214-
assert clear_value.value == "success"
2215-
2216-
def test_user_type_cast_fail(self):
2217-
class UserType:
2218-
def __init__(self, *args, **kwargs):
2219-
msg = "Cast failed"
2220-
raise ValueError(msg)
2221-
2222-
class ModelSchema(BaseModel):
2223-
user_type: UserType
2224-
2225-
class Config:
2226-
arbitrary_types_allowed = True
2227-
2228-
node = Node(
2229-
model=Mock(),
2230-
filter_=Mock(),
2231-
schema=ModelSchema,
2232-
)
2233-
2234-
with raises(InvalidType) as exc_info:
2235-
node.create_filter(
2236-
schema_field=ModelSchema.__fields__["user_type"],
2237-
model_column=Mock(),
2238-
operator=Mock(),
2239-
value=Any,
2240-
)
2241-
2242-
assert exc_info.value.as_dict == {
2243-
"detail": "Can't cast filter value `typing.Any` to arbitrary type.",
2244-
"meta": [
2245-
{
2246-
"detail": "Cast failed",
2247-
"source": {"pointer": ""},
2248-
"status_code": status.HTTP_409_CONFLICT,
2249-
"title": "Conflict",
2250-
},
2251-
],
2252-
"status_code": status.HTTP_409_CONFLICT,
2253-
"title": "Invalid type.",
2254-
}
2255-
2256-
22572178
# todo: test errors

tests/test_data_layers/__init__.py

Whitespace-only changes.

tests/test_data_layers/test_filtering/__init__.py

Whitespace-only changes.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
from typing import Any
2+
from unittest.mock import Mock
3+
4+
from fastapi import status
5+
from pydantic import BaseModel
6+
from pytest import raises # noqa PT013
7+
8+
from fastapi_jsonapi.data_layers.filtering.sqlalchemy import Node
9+
from fastapi_jsonapi.exceptions.json_api import InvalidType
10+
11+
12+
class TestNode:
13+
def test_user_type_cast_success(self):
14+
class UserType:
15+
def __init__(self, *args, **kwargs):
16+
self.value = "success"
17+
18+
class ModelSchema(BaseModel):
19+
user_type: UserType
20+
21+
class Config:
22+
arbitrary_types_allowed = True
23+
24+
node = Node(
25+
model=Mock(),
26+
filter_={
27+
"name": "user_type",
28+
"op": "eq",
29+
"val": Any,
30+
},
31+
schema=ModelSchema,
32+
)
33+
34+
model_column_mock = Mock()
35+
model_column_mock.eq = lambda clear_value: clear_value
36+
37+
clear_value = node.create_filter(
38+
schema_field=ModelSchema.__fields__["user_type"],
39+
model_column=model_column_mock,
40+
operator=Mock(),
41+
value=Any,
42+
)
43+
assert isinstance(clear_value, UserType)
44+
assert clear_value.value == "success"
45+
46+
def test_user_type_cast_fail(self):
47+
class UserType:
48+
def __init__(self, *args, **kwargs):
49+
msg = "Cast failed"
50+
raise ValueError(msg)
51+
52+
class ModelSchema(BaseModel):
53+
user_type: UserType
54+
55+
class Config:
56+
arbitrary_types_allowed = True
57+
58+
node = Node(
59+
model=Mock(),
60+
filter_=Mock(),
61+
schema=ModelSchema,
62+
)
63+
64+
with raises(InvalidType) as exc_info:
65+
node.create_filter(
66+
schema_field=ModelSchema.__fields__["user_type"],
67+
model_column=Mock(),
68+
operator=Mock(),
69+
value=Any,
70+
)
71+
72+
assert exc_info.value.as_dict == {
73+
"detail": "Can't cast filter value `typing.Any` to arbitrary type.",
74+
"meta": [
75+
{
76+
"detail": "Cast failed",
77+
"source": {"pointer": ""},
78+
"status_code": status.HTTP_409_CONFLICT,
79+
"title": "Conflict",
80+
},
81+
],
82+
"status_code": status.HTTP_409_CONFLICT,
83+
"title": "Invalid type.",
84+
}

0 commit comments

Comments
 (0)