1313@fixture (scope = "module" , autouse = True )
1414def setup_tear_down (sqla_connection ):
1515 faker = Faker (seed = 0 )
16- sqla_connection .execute (
17- text (
18- "create table if not exists public.user "
19- "(id serial primary key, name varchar)"
16+ with sqla_connection .begin ():
17+ sqla_connection .execute (
18+ text (
19+ "create table if not exists public.user "
20+ "(id serial primary key, name varchar)"
21+ )
2022 )
21- )
22- sqla_connection .execute (
23- text (
23+ sqla_connection .execute (
24+ text (
25+ """
26+ create table if not exists note (
27+ user_id integer,
28+ id serial,
29+ content text,
30+ primary key (user_id, id),
31+ foreign key (user_id) references public.user (id)
32+ )
2433 """
25- create table if not exists note (
26- user_id integer,
27- id serial,
28- content text,
29- primary key (user_id, id),
30- foreign key (user_id) references public.user (id)
31- )
32- """
34+ )
3335 )
34- )
35- metadata = MetaData ()
36- user = Table ("user" , metadata , autoload_with = sqla_connection )
37- note = Table ("note" , metadata , autoload_with = sqla_connection )
38- user_params = [{"name" : faker .name ()} for _ in range (1 , 43 )]
39- note_params = [
40- {"user_id" : i % 42 + 1 , "content" : faker .text ()} for i in range (0 , 22 * 42 )
41- ]
42- sqla_connection .execute (user .insert (), * user_params )
43- sqla_connection .execute (note .insert (), * note_params )
36+ metadata = MetaData ()
37+ user = Table ("user" , metadata , autoload_with = sqla_connection )
38+ note = Table ("note" , metadata , autoload_with = sqla_connection )
39+ user_params = [{"name" : faker .name ()} for _ in range (1 , 43 )]
40+ note_params = [
41+ {"user_id" : i % 42 + 1 , "content" : faker .text ()} for i in range (0 , 22 * 42 )
42+ ]
43+ sqla_connection .execute (user .insert (), user_params )
44+ sqla_connection .execute (note .insert (), note_params )
4445 yield
45- sqla_connection .execute (text ("drop table note cascade" ))
46- sqla_connection .execute (text ("drop table public.user cascade" ))
46+ with sqla_connection .begin ():
47+ sqla_connection .execute (text ("drop table note cascade" ))
48+ sqla_connection .execute (text ("drop table public.user cascade" ))
4749
4850
4951@fixture
@@ -80,7 +82,7 @@ class Note(Base):
8082def test_pagination (session , user_cls , offset , limit , total_pages , page_number ):
8183 from fastapi_sqla import Paginate
8284
83- query = session .query (user_cls ).options (joinedload (" notes" ))
85+ query = session .query (user_cls ).options (joinedload (user_cls . notes ))
8486 result = Paginate (session , offset , limit )(query )
8587
8688 assert result .meta .total_items == 42
@@ -99,7 +101,7 @@ def test_pagination_with_legacy_query_count(
99101):
100102 from fastapi_sqla import Paginate
101103
102- query = session .query (user_cls ).options (joinedload (" notes" ))
104+ query = session .query (user_cls ).options (joinedload (user_cls . notes ))
103105 result = Paginate (session , offset , limit )(query )
104106
105107 assert result .meta .total_items == 42
@@ -154,13 +156,17 @@ class UserWithMeta(User):
154156 @app .get ("/v1/users" , response_model = Page [UserWithNotes ])
155157 def sqla_13_all_users (session : Session = Depends (), paginate : Paginate = Depends ()):
156158 query = (
157- session .query (user_cls ).options (joinedload ("notes" )).order_by (user_cls .id )
159+ session .query (user_cls )
160+ .options (joinedload (user_cls .notes ))
161+ .order_by (user_cls .id )
158162 )
159163 return paginate (query )
160164
161165 @app .get ("/v2/users" , response_model = Page [UserWithNotes ])
162166 def sqla_14_all_users (paginate : Paginate = Depends ()):
163- query = select (user_cls ).options (joinedload ("notes" )).order_by (user_cls .id )
167+ query = (
168+ select (user_cls ).options (joinedload (user_cls .notes )).order_by (user_cls .id )
169+ )
164170 return paginate (query )
165171
166172 @app .get ("/v2/users-with-notes-count" , response_model = Page [UserWithNotesCount ])
@@ -182,8 +188,7 @@ def query_with_JSON_result(paginate: Paginate = Depends()):
182188 user_cls .id ,
183189 user_cls .name ,
184190 cast (
185- func .format ('{"notes_count": %s}' , func .count (note_cls .id )),
186- JSON ,
191+ func .format ('{"notes_count": %s}' , func .count (note_cls .id )), JSON
187192 ).label ("meta" ),
188193 )
189194 .join (note_cls )
0 commit comments