Skip to content

Commit a679a1a

Browse files
committed
refactor
1 parent 09f72f3 commit a679a1a

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
@@ -4,8 +4,7 @@
44
from collections import defaultdict
55
from itertools import chain, zip_longest
66
from json import dumps
7-
from typing import Any, Dict, List
8-
from unittest.mock import Mock
7+
from typing import Dict, List
98
from uuid import UUID, uuid4
109

1110
from fastapi import FastAPI, status
@@ -15,8 +14,6 @@
1514
from sqlalchemy import select
1615
from sqlalchemy.ext.asyncio import AsyncSession
1716

18-
from fastapi_jsonapi.data_layers.filtering.sqlalchemy import Node
19-
from fastapi_jsonapi.exceptions.json_api import InvalidType
2017
from fastapi_jsonapi.views.view_base import ViewBase
2118
from tests.common import is_postgres_tests
2219
from tests.fixtures.app import build_app_custom
@@ -2353,80 +2350,4 @@ async def test_sort(
23532350
}
23542351

23552352

2356-
# TODO: move to it's own test module
2357-
class TestSQLAFilteringModule:
2358-
def test_user_type_cast_success(self):
2359-
class UserType:
2360-
def __init__(self, *args, **kwargs):
2361-
self.value = "success"
2362-
2363-
class ModelSchema(BaseModel):
2364-
user_type: UserType
2365-
2366-
class Config:
2367-
arbitrary_types_allowed = True
2368-
2369-
node = Node(
2370-
model=Mock(),
2371-
filter_={
2372-
"name": "user_type",
2373-
"op": "eq",
2374-
"val": Any,
2375-
},
2376-
schema=ModelSchema,
2377-
)
2378-
2379-
model_column_mock = Mock()
2380-
model_column_mock.eq = lambda clear_value: clear_value
2381-
2382-
clear_value = node.create_filter(
2383-
schema_field=ModelSchema.__fields__["user_type"],
2384-
model_column=model_column_mock,
2385-
operator=Mock(),
2386-
value=Any,
2387-
)
2388-
assert isinstance(clear_value, UserType)
2389-
assert clear_value.value == "success"
2390-
2391-
def test_user_type_cast_fail(self):
2392-
class UserType:
2393-
def __init__(self, *args, **kwargs):
2394-
msg = "Cast failed"
2395-
raise ValueError(msg)
2396-
2397-
class ModelSchema(BaseModel):
2398-
user_type: UserType
2399-
2400-
class Config:
2401-
arbitrary_types_allowed = True
2402-
2403-
node = Node(
2404-
model=Mock(),
2405-
filter_=Mock(),
2406-
schema=ModelSchema,
2407-
)
2408-
2409-
with raises(InvalidType) as exc_info:
2410-
node.create_filter(
2411-
schema_field=ModelSchema.__fields__["user_type"],
2412-
model_column=Mock(),
2413-
operator=Mock(),
2414-
value=Any,
2415-
)
2416-
2417-
assert exc_info.value.as_dict == {
2418-
"detail": "Can't cast filter value `typing.Any` to arbitrary type.",
2419-
"meta": [
2420-
{
2421-
"detail": "Cast failed",
2422-
"source": {"pointer": ""},
2423-
"status_code": status.HTTP_409_CONFLICT,
2424-
"title": "Conflict",
2425-
},
2426-
],
2427-
"status_code": status.HTTP_409_CONFLICT,
2428-
"title": "Invalid type.",
2429-
}
2430-
2431-
24322353
# 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)