1- import os
2-
31from fastapi import FastAPI
42from sqlalchemy import Column , Integer , \
53 ForeignKey , Table
64from sqlalchemy .orm import declarative_base , sessionmaker , relationship
75
6+ from src .fastapi_quickcrud .misc .type import SqlType
87from src .fastapi_quickcrud .crud_router import crud_router_builder
98
10- TEST_DATABASE_URL = os .
environ .
get (
'TEST_DATABASE_URL' ,
'postgresql://postgres:[email protected] :5432/postgres' )
11-
129app = FastAPI ()
1310
1411Base = declarative_base ()
1512metadata = Base .metadata
1613
1714from sqlalchemy import create_engine
1815
19-
2016from sqlalchemy .pool import StaticPool
2117engine = create_engine ('sqlite://' , echo = True ,
2218 connect_args = {"check_same_thread" : False }, pool_recycle = 7200 , poolclass = StaticPool )
2319session = sessionmaker (autocommit = False , autoflush = False , bind = engine )
2420
21+
2522def get_transaction_session ():
2623 try :
2724 db = session ()
@@ -34,6 +31,7 @@ def get_transaction_session():
3431 Column ('left_id' , ForeignKey ('test_left.id' )),
3532 Column ('right_id' , ForeignKey ('test_right.id' ))
3633 )
34+
3735association_table_second = Table ('test_association_second' , Base .metadata ,
3836 Column ('left_id_second' , ForeignKey ('test_left.id' )),
3937 Column ('right_id_second' , ForeignKey ('test_right_second.id' ))
@@ -43,57 +41,54 @@ def get_transaction_session():
4341class Child (Base ):
4442 __tablename__ = 'test_right'
4543 id = Column (Integer , primary_key = True )
46- parent = relationship ("Parent" ,
47- secondary = association_table ,
48- back_populates = "children" )
49-
50-
51- class ChildSecond (Base ):
52- __tablename__ = 'test_right_second'
53- id = Column (Integer , primary_key = True )
54- parent_second = relationship ("Parent" ,
55- secondary = association_table_second ,
56- back_populates = "children_second" )
5744
5845
5946class Parent (Base ):
6047 __tablename__ = 'test_left'
6148 id = Column (Integer , primary_key = True )
6249 children = relationship ("Child" ,
63- secondary = association_table ,
64- back_populates = "parent" )
50+ secondary = association_table )
6551 children_second = relationship ("ChildSecond" ,
66- secondary = association_table_second ,
67- back_populates = "parent_second" )
52+ secondary = association_table_second )
53+
54+
55+ class ChildSecond (Base ):
56+ __tablename__ = 'test_right_second'
57+ id = Column (Integer , primary_key = True )
6858
6959
7060crud_route_child = crud_router_builder (db_session = get_transaction_session ,
7161 db_model = Child ,
7262 prefix = "/child" ,
73- tags = ["child" ]
63+ tags = ["child" ],
64+
7465 )
7566
7667crud_route_association_table_second = crud_router_builder (db_session = get_transaction_session ,
7768 db_model = association_table_second ,
7869 prefix = "/association_table_second" ,
79- tags = ["association_table_second" ]
70+ tags = ["association_table_second" ],
71+
8072 )
8173
8274crud_route_child_second = crud_router_builder (db_session = get_transaction_session ,
83- db_model = ChildSecond ,
75+ db_model = Child ,
8476 prefix = "/child_second" ,
85- tags = ["child_second" ]
77+ tags = ["child_second" ],
78+
8679 )
8780
8881crud_route_parent = crud_router_builder (db_session = get_transaction_session ,
89- db_model = Parent ,
82+ db_model = Parent ,
9083 prefix = "/parent" ,
91- tags = ["parent" ]
84+ tags = ["parent" ],
85+
9286 )
9387crud_route_association = crud_router_builder (db_session = get_transaction_session ,
9488 db_model = association_table ,
9589 prefix = "/association" ,
96- tags = ["association" ]
90+ tags = ["association" ],
91+
9792 )
9893from starlette .testclient import TestClient
9994
@@ -158,6 +153,43 @@ def test_get_parent_many_with_join():
158153 "id" : 1
159154 }
160155
156+ response = client .get ('/parent?join_foreign_table=test_right_second' , headers = headers )
157+ assert response .status_code == 200
158+ assert response .json () == [
159+ {
160+ "test_right_second_foreign" : [
161+ {
162+ "id" : 1
163+ }
164+ ],
165+ "id" : 1
166+ },
167+ {
168+ "test_right_second_foreign" : [
169+ {
170+ "id" : 2
171+ }
172+ ],
173+ "id" : 2
174+ },
175+ {
176+ "test_right_second_foreign" : [
177+ {
178+ "id" : 3
179+ }
180+ ],
181+ "id" : 3
182+ },
183+ {
184+ "test_right_second_foreign" : [
185+ {
186+ "id" : 4
187+ }
188+ ],
189+ "id" : 4
190+ }
191+ ]
192+
161193 response = client .get ('/parent/1?join_foreign_table=test_right_second' , headers = headers )
162194 assert response .status_code == 200
163195 assert response .json () == {
@@ -169,6 +201,62 @@ def test_get_parent_many_with_join():
169201 "id" : 1
170202 }
171203
204+ response = client .get ('/parent?join_foreign_table=test_right&join_foreign_table=test_right_second' , headers = headers )
205+ assert response .status_code == 200
206+ assert response .json () == [
207+ {
208+ "test_right_foreign" : [
209+ {
210+ "id" : 1
211+ }
212+ ],
213+ "test_right_second_foreign" : [
214+ {
215+ "id" : 1
216+ }
217+ ],
218+ "id" : 1
219+ },
220+ {
221+ "test_right_foreign" : [
222+ {
223+ "id" : 2
224+ }
225+ ],
226+ "test_right_second_foreign" : [
227+ {
228+ "id" : 2
229+ }
230+ ],
231+ "id" : 2
232+ },
233+ {
234+ "test_right_foreign" : [
235+ {
236+ "id" : 3
237+ }
238+ ],
239+ "test_right_second_foreign" : [
240+ {
241+ "id" : 3
242+ }
243+ ],
244+ "id" : 3
245+ },
246+ {
247+ "test_right_foreign" : [
248+ {
249+ "id" : 4
250+ }
251+ ],
252+ "test_right_second_foreign" : [
253+ {
254+ "id" : 4
255+ }
256+ ],
257+ "id" : 4
258+ }
259+ ]
172260 response = client .get ('/parent/1?join_foreign_table=test_right&join_foreign_table=test_right_second' ,
173261 headers = headers )
174262 assert response .status_code == 200
@@ -210,14 +298,20 @@ def test_get_child_many_without_join():
210298 }
211299 ]
212300
301+ response = client .get ('/child/1' , headers = headers )
302+ assert response .status_code == 200
303+ assert response .json () == {
304+ "id" : 1
305+ }
306+
213307
214- def test_get_child_many_with_join ():
308+ def test_get_association_many_with_join ():
215309 headers = {
216310 'accept' : '*/*' ,
217311 'Content-Type' : 'application/json' ,
218312 }
219313
220- response = client .get ('/child ?join_foreign_table=test_left' , headers = headers )
314+ response = client .get ('/association ?join_foreign_table=test_left' , headers = headers )
221315 assert response .status_code == 200
222316 assert response .json () == [
223317 {
@@ -226,41 +320,38 @@ def test_get_child_many_with_join():
226320 "id" : 1
227321 }
228322 ],
229- "id" : 1
323+ "left_id" : 1 ,
324+ "right_id" : 1
230325 },
231326 {
232327 "test_left_foreign" : [
233328 {
234329 "id" : 2
235330 }
236331 ],
237- "id" : 2
332+ "left_id" : 2 ,
333+ "right_id" : 2
238334 },
239335 {
240336 "test_left_foreign" : [
241337 {
242338 "id" : 3
243339 }
244340 ],
245- "id" : 3
341+ "left_id" : 3 ,
342+ "right_id" : 3
246343 },
247344 {
248345 "test_left_foreign" : [
249346 {
250347 "id" : 4
251348 }
252349 ],
253- "id" : 4
350+ "left_id" : 4 ,
351+ "right_id" : 4
254352 }
255353 ]
256354
257-
258- def test_get_association_many_with_join ():
259- headers = {
260- 'accept' : '*/*' ,
261- 'Content-Type' : 'application/json' ,
262- }
263-
264355 response = client .get ('/association?join_foreign_table=test_left' , headers = headers )
265356 assert response .status_code == 200
266357 assert response .json () == [
@@ -529,62 +620,15 @@ def test_get_child_many_second_with_join():
529620 "id" : 1
530621 }
531622
532- response = client .get ('/child_second?join_foreign_table=test_left' , headers = headers )
533- assert response .status_code == 200
534- assert response .json () == [
535- {
536- "test_left_foreign" : [
537- {
538- "id" : 1
539- }
540- ],
541- "id" : 1
542- },
543- {
544- "test_left_foreign" : [
545- {
546- "id" : 2
547- }
548- ],
549- "id" : 2
550- },
551- {
552- "test_left_foreign" : [
553- {
554- "id" : 3
555- }
556- ],
557- "id" : 3
558- },
559- {
560- "test_left_foreign" : [
561- {
562- "id" : 4
563- }
564- ],
565- "id" : 4
566- }
567- ]
568-
569- response = client .get ('/child_second/1?join_foreign_table=test_left' , headers = headers )
570- assert response .status_code == 200
571- assert response .json () == {
572- "test_left_foreign" : [
573- {
574- "id" : 1
575- }
576- ],
577- "id" : 1
578- }
579-
580623
581624def setup_module (module ):
582- Child .__table__ .create (engine , checkfirst = True )
583- ChildSecond .__table__ .create (engine , checkfirst = True )
584- Parent .__table__ .create (engine , checkfirst = True )
585- association_table .create (engine , checkfirst = True )
586- association_table_second .create (engine , checkfirst = True )
587- db = session ()
625+ Child .__table__ .create (module .engine , checkfirst = True )
626+ ChildSecond .__table__ .create (module .engine , checkfirst = True )
627+ Parent .__table__ .create (module .engine , checkfirst = True )
628+ association_table .create (module .engine , checkfirst = True )
629+ association_table_second .create (module .engine , checkfirst = True )
630+ db = module .session ()
631+
588632 db .add (Child (id = 1 ))
589633 db .add (Child (id = 2 ))
590634 db .add (Child (id = 3 ))
@@ -611,11 +655,16 @@ def setup_module(module):
611655 db .execute (association_table_second .insert ().values (left_id_second = 2 , right_id_second = 2 ))
612656 db .execute (association_table_second .insert ().values (left_id_second = 3 , right_id_second = 3 ))
613657 db .execute (association_table_second .insert ().values (left_id_second = 4 , right_id_second = 4 ))
614-
658+ q = db .execute ('''
659+ SELECT
660+ name
661+ FROM
662+ sqlite_master
663+ ''' )
664+
665+ available_tables = q .fetchall ()
615666 db .commit ()
616667
617- print ()
618-
619668
620669def teardown_module (module ):
621670 association_table .drop (engine , checkfirst = True )
0 commit comments