@@ -118,11 +118,11 @@ class TestModel(base): # type: ignore[misc,valid-type]
118
118
url = app ["admin" ].router ["test_get_one" ].url_for ()
119
119
async with admin_client .get (url , params = {"id" : 1 }, headers = h ) as resp :
120
120
assert resp .status == 200
121
- assert await resp .json () == {"data" : {"id" : 1 , "binary" : "foo" }}
121
+ assert await resp .json () == {"data" : {"id" : "1" , "binary" : "foo" }}
122
122
123
123
async with admin_client .get (url , params = {"id" : 2 }, headers = h ) as resp :
124
124
assert resp .status == 200
125
- assert await resp .json () == {"data" : {"id" : 2 , "binary" : "\x01 �\x02 " }}
125
+ assert await resp .json () == {"data" : {"id" : "2" , "binary" : "\x01 �\x02 " }}
126
126
127
127
128
128
def test_fk (base : type [DeclarativeBase ], mock_engine : AsyncEngine ) -> None :
@@ -146,6 +146,51 @@ class TestChildModel(base): # type: ignore[misc,valid-type]
146
146
"source" : "id" , "target" : "id" }) | {"show_create" : True }}
147
147
148
148
149
+ async def test_fk_output (
150
+ base : DeclarativeBase , aiohttp_client : Callable [[web .Application ], Awaitable [TestClient ]],
151
+ login : _Login
152
+ ) -> None :
153
+ class TestModel (base ): # type: ignore[misc,valid-type]
154
+ __tablename__ = "test"
155
+ id : Mapped [int ] = mapped_column (primary_key = True )
156
+
157
+ class TestModelParent (base ): # type: ignore[misc,valid-type]
158
+ __tablename__ = "parent"
159
+ id : Mapped [int ] = mapped_column (primary_key = True )
160
+ child_id : Mapped [int ] = mapped_column (sa .ForeignKey (TestModel .id ))
161
+
162
+ app = web .Application ()
163
+ engine = create_async_engine ("sqlite+aiosqlite:///:memory:" )
164
+ db = async_sessionmaker (engine , expire_on_commit = False )
165
+ async with engine .begin () as conn :
166
+ await conn .run_sync (base .metadata .create_all )
167
+ async with db .begin () as sess :
168
+ child = TestModel ()
169
+ sess .add (child )
170
+ async with db .begin () as sess :
171
+ sess .add (TestModelParent (child_id = child .id ))
172
+
173
+ schema : aiohttp_admin .Schema = {
174
+ "security" : {
175
+ "check_credentials" : check_credentials ,
176
+ "secure" : False
177
+ },
178
+ "resources" : ({"model" : SAResource (engine , TestModel )},
179
+ {"model" : SAResource (engine , TestModelParent )})
180
+ }
181
+ app ["admin" ] = aiohttp_admin .setup (app , schema )
182
+
183
+ admin_client = await aiohttp_client (app )
184
+ assert admin_client .app
185
+ h = await login (admin_client )
186
+
187
+ url = app ["admin" ].router ["parent_get_one" ].url_for ()
188
+ async with admin_client .get (url , params = {"id" : 1 }, headers = h ) as resp :
189
+ assert resp .status == 200
190
+ # child_id must be converted to str ID.
191
+ assert await resp .json () == {"data" : {"id" : "1" , "child_id" : "1" }}
192
+
193
+
149
194
def test_relationship (base : type [DeclarativeBase ], mock_engine : AsyncEngine ) -> None :
150
195
class TestMany (base ): # type: ignore[misc,valid-type]
151
196
__tablename__ = "many"
@@ -335,31 +380,31 @@ class TestModel(base): # type: ignore[misc,valid-type]
335
380
"sort" : json .dumps ({"field" : "id" , "order" : "DESC" }), "filter" : "{}" }
336
381
async with admin_client .get (url , params = p , headers = h ) as resp :
337
382
assert resp .status == 200
338
- assert await resp .json () == {"data" : [{"id" : 8 , "num" : 8 , "other" : "bar" },
339
- {"id" : 5 , "num" : 5 , "other" : "foo" }], "total" : 2 }
383
+ assert await resp .json () == {"data" : [{"id" : "8" , "num" : 8 , "other" : "bar" },
384
+ {"id" : "5" , "num" : 5 , "other" : "foo" }], "total" : 2 }
340
385
341
386
url = app ["admin" ].router ["test_get_one" ].url_for ()
342
387
async with admin_client .get (url , params = {"id" : 8 }, headers = h ) as resp :
343
388
assert resp .status == 200
344
- assert await resp .json () == {"data" : {"id" : 8 , "num" : 8 , "other" : "bar" }}
389
+ assert await resp .json () == {"data" : {"id" : "8" , "num" : 8 , "other" : "bar" }}
345
390
346
391
url = app ["admin" ].router ["test_get_many" ].url_for ()
347
- async with admin_client .get (url , params = {"ids" : "[5, 8]" }, headers = h ) as resp :
392
+ async with admin_client .get (url , params = {"ids" : '["5", "8"]' }, headers = h ) as resp :
348
393
assert resp .status == 200
349
- assert await resp .json () == {"data" : [{"id" : 5 , "num" : 5 , "other" : "foo" },
350
- {"id" : 8 , "num" : 8 , "other" : "bar" }]}
394
+ assert await resp .json () == {"data" : [{"id" : "5" , "num" : 5 , "other" : "foo" },
395
+ {"id" : "8" , "num" : 8 , "other" : "bar" }]}
351
396
352
397
url = app ["admin" ].router ["test_create" ].url_for ()
353
398
p = {"data" : json .dumps ({"num" : 12 , "other" : "this" })}
354
399
async with admin_client .post (url , params = p , headers = h ) as resp :
355
400
assert resp .status == 200
356
- assert await resp .json () == {"data" : {"id" : 12 , "num" : 12 , "other" : "this" }}
401
+ assert await resp .json () == {"data" : {"id" : "12" , "num" : 12 , "other" : "this" }}
357
402
358
403
url = app ["admin" ].router ["test_update" ].url_for ()
359
404
p1 = {"id" : 5 , "data" : json .dumps ({"id" : 5 , "other" : "that" }), "previousData" : "{}" }
360
405
async with admin_client .put (url , params = p1 , headers = h ) as resp :
361
406
assert resp .status == 200
362
- assert await resp .json () == {"data" : {"id" : 5 , "num" : 5 , "other" : "that" }}
407
+ assert await resp .json () == {"data" : {"id" : "5" , "num" : 5 , "other" : "that" }}
363
408
364
409
365
410
async def test_datetime (
@@ -396,14 +441,14 @@ class TestModel(base): # type: ignore[misc,valid-type]
396
441
url = app ["admin" ].router ["test_get_one" ].url_for ()
397
442
async with admin_client .get (url , params = {"id" : 1 }, headers = h ) as resp :
398
443
assert resp .status == 200
399
- assert await resp .json () == {"data" : {"id" : 1 , "date" : "2023-04-23" ,
444
+ assert await resp .json () == {"data" : {"id" : "1" , "date" : "2023-04-23" ,
400
445
"time" : "2023-01-02 03:04:00" }}
401
446
402
447
url = app ["admin" ].router ["test_create" ].url_for ()
403
448
p = {"data" : json .dumps ({"date" : "2024-05-09" , "time" : "2020-11-12 03:04:05" })}
404
449
async with admin_client .post (url , params = p , headers = h ) as resp :
405
450
assert resp .status == 200
406
- assert await resp .json () == {"data" : {"id" : 2 , "date" : "2024-05-09" ,
451
+ assert await resp .json () == {"data" : {"id" : "2" , "date" : "2024-05-09" ,
407
452
"time" : "2020-11-12 03:04:05" }}
408
453
409
454
@@ -473,11 +518,11 @@ class TestModel(base): # type: ignore[misc,valid-type]
473
518
p = {"data" : json .dumps ({"foo" : True , "bar" : 5 })}
474
519
async with admin_client .post (url , params = p , headers = h ) as resp :
475
520
assert resp .status == 200
476
- assert await resp .json () == {"data" : {"id" : 1 , "foo" : True , "bar" : 5 }}
521
+ assert await resp .json () == {"data" : {"id" : "1" , "foo" : True , "bar" : 5 }}
477
522
p = {"data" : json .dumps ({"foo" : None , "bar" : - 1 })}
478
523
async with admin_client .post (url , params = p , headers = h ) as resp :
479
524
assert resp .status == 200
480
- assert await resp .json () == {"data" : {"id" : 2 , "foo" : None , "bar" : - 1 }}
525
+ assert await resp .json () == {"data" : {"id" : "2" , "foo" : None , "bar" : - 1 }}
481
526
482
527
p = {"data" : json .dumps ({"foo" : 5 , "bar" : "foo" })}
483
528
async with admin_client .post (url , params = p , headers = h ) as resp :
0 commit comments