1414class InitTestData :
1515 """初始化测试数据"""
1616
17- def __init__ (self ):
17+ def __init__ (self , session ):
1818 self .fake = Faker ('zh_CN' )
19+ self .session = session
1920
20- @staticmethod
21- async def create_dept ():
21+ async def create_dept (self ):
2222 """自动创建部门"""
23- async with async_db_session .begin () as db :
23+ async with self . session .begin () as db :
2424 department_obj = Dept (name = 'test' , create_user = 1 )
2525 db .add (department_obj )
2626 log .info ('部门 test 创建成功' )
2727
28- @staticmethod
29- async def create_role ():
28+ async def create_role (self ):
3029 """自动创建角色"""
31- async with async_db_session .begin () as db :
30+ async with self . session .begin () as db :
3231 role_obj = Role (name = 'test' , create_user = 1 )
3332 role_obj .menus .append (Menu (name = 'test' , create_user = 1 ))
3433 db .add (role_obj )
3534 log .info ('角色 test 创建成功' )
3635
37- @staticmethod
38- async def create_test_user ():
36+ async def create_test_user (self ):
3937 """创建测试用户"""
4038 username = 'test'
4139 password = 'test'
@@ -48,13 +46,12 @@ async def create_test_user():
4846 is_superuser = True ,
4947 dept_id = 1 ,
5048 )
51- async with async_db_session .begin () as db :
49+ async with self . session .begin () as db :
5250 user_obj .roles .append (await db .get (Role , 1 ))
5351 db .add (user_obj )
5452 log .info (f'测试用户创建成功,账号:{ username } ,密码:{ password } ' )
5553
56- @staticmethod
57- async def create_superuser_by_yourself ():
54+ async def create_superuser_by_yourself (self ):
5855 """手动创建管理员账户"""
5956 log .info ('开始创建自定义管理员用户' )
6057 print ('请输入用户名:' )
@@ -78,7 +75,7 @@ async def create_superuser_by_yourself():
7875 is_superuser = True ,
7976 dept_id = 1 ,
8077 )
81- async with async_db_session .begin () as db :
78+ async with self . session .begin () as db :
8279 user_obj .roles .append (await db .get (Role , 1 ))
8380 db .add (user_obj )
8481 log .info (f'自定义管理员用户创建成功,账号:{ username } ,密码:{ password } ' )
@@ -96,7 +93,7 @@ async def fake_user(self):
9693 is_superuser = False ,
9794 dept_id = 1 ,
9895 )
99- async with async_db_session .begin () as db :
96+ async with self . session .begin () as db :
10097 user_obj .roles .append (await db .get (Role , 1 ))
10198 db .add (user_obj )
10299 log .info (f'普通用户创建成功,账号:{ username } ,密码:{ password } ' )
@@ -115,7 +112,7 @@ async def fake_no_active_user(self):
115112 is_superuser = False ,
116113 dept_id = 1 ,
117114 )
118- async with async_db_session .begin () as db :
115+ async with self . session .begin () as db :
119116 user_obj .roles .append (await db .get (Role , 1 ))
120117 db .add (user_obj )
121118 log .info (f'普通锁定用户创建成功,账号:{ username } ,密码:{ password } ' )
@@ -133,7 +130,7 @@ async def fake_superuser(self):
133130 is_superuser = True ,
134131 dept_id = 1 ,
135132 )
136- async with async_db_session .begin () as db :
133+ async with self . session .begin () as db :
137134 user_obj .roles .append (await db .get (Role , 1 ))
138135 db .add (user_obj )
139136 log .info (f'管理员用户创建成功,账号:{ username } ,密码:{ password } ' )
@@ -152,7 +149,7 @@ async def fake_no_active_superuser(self):
152149 is_superuser = True ,
153150 dept_id = 1 ,
154151 )
155- async with async_db_session .begin () as db :
152+ async with self . session .begin () as db :
156153 user_obj .roles .append (await db .get (Role , 1 ))
157154 db .add (user_obj )
158155 log .info (f'管理员锁定用户创建成功,账号:{ username } ,密码:{ password } ' )
@@ -172,6 +169,6 @@ async def init_data(self):
172169
173170
174171if __name__ == '__main__' :
175- init = InitTestData ()
172+ init = InitTestData (session = async_db_session )
176173 loop = asyncio .get_event_loop ()
177174 loop .run_until_complete (init .init_data ())
0 commit comments