Skip to content

Commit 54c7c66

Browse files
committed
Add docs and test cases
1 parent 1843907 commit 54c7c66

3 files changed

Lines changed: 117 additions & 7 deletions

File tree

docs/usage/crud.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,34 @@ print(f"用户ID: {user.id}") # 可以立即获取主键
2525
### 批量创建
2626

2727
```python
28-
# 批量创建
28+
# 使用 Pydantic 模型批量创建
2929
users_data = [
3030
UserCreate(name="用户1", email="user1@example.com"),
3131
UserCreate(name="用户2", email="user2@example.com"),
3232
UserCreate(name="用户3", email="user3@example.com")
3333
]
3434
users = await user_crud.create_models(session, users_data)
3535

36-
# 使用字典批量创建(高性能方式)
36+
# 使用字典批量创建
3737
users_dict = [
3838
{"name": "用户4", "email": "user4@example.com"},
3939
{"name": "用户5", "email": "user5@example.com"}
4040
]
41-
users = await user_crud.bulk_create_models(session, users_dict)
41+
users = await user_crud.create_models(session, users_dict)
42+
43+
# 混合使用 Pydantic 模型和字典
44+
users_mixed = [
45+
UserCreate(name="用户6", email="user6@example.com"),
46+
{"name": "用户7", "email": "user7@example.com"},
47+
]
48+
users = await user_crud.create_models(session, users_mixed)
49+
50+
# 使用字典批量创建(高性能方式)
51+
users_bulk = [
52+
{"name": "用户8", "email": "user8@example.com"},
53+
{"name": "用户9", "email": "user9@example.com"}
54+
]
55+
users = await user_crud.bulk_create_models(session, users_bulk)
4256
```
4357

4458
## 查询操作

sqlalchemy_crud_plus/crud.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ async def create_model(
7979
Create a new instance of a model.
8080
8181
:param session: The SQLAlchemy async session
82-
:param obj: The Pydantic schema containing data to be saved
82+
:param obj: A Pydantic schema or dictionary containing the data to be saved
8383
:param flush: If `True`, flush all object changes to the database
8484
:param commit: If `True`, commits the transaction immediately
85-
:param kwargs: Additional model data not included in the pydantic schema
85+
:param kwargs: Additional model data not included in the pydantic schema or dict
8686
:return:
8787
"""
8888
obj_data = obj if isinstance(obj, dict) else obj.model_dump()
@@ -111,10 +111,10 @@ async def create_models(
111111
Create new instances of a model.
112112
113113
:param session: The SQLAlchemy async session
114-
:param objs: The Pydantic schema list containing data to be saved
114+
:param objs: A list of Pydantic schemas or dictionaries containing the data to be saved
115115
:param flush: If `True`, flush all object changes to the database
116116
:param commit: If `True`, commits the transaction immediately
117-
:param kwargs: Additional model data not included in the pydantic schema
117+
:param kwargs: Additional model data not included in the pydantic schema or dict
118118
:return:
119119
"""
120120
ins_list = []

tests/test_create.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,99 @@ async def test_bulk_create_models_with_commit(db: AsyncSession, crud_ins: CRUDPl
159159
assert len(results) == 2
160160
assert results[0].name == 'bulk_commit_1'
161161
assert results[1].name == 'bulk_commit_2'
162+
163+
164+
@pytest.mark.asyncio
165+
async def test_create_model_with_dict(db: AsyncSession, crud_ins: CRUDPlus[Ins]):
166+
async with db.begin():
167+
data = {'name': 'dict_item'}
168+
result = await crud_ins.create_model(db, data)
169+
170+
assert result.name == 'dict_item'
171+
assert result.id is not None
172+
173+
174+
@pytest.mark.asyncio
175+
async def test_create_model_with_dict_and_flush(db: AsyncSession, crud_ins: CRUDPlus[Ins]):
176+
async with db.begin():
177+
data = {'name': 'dict_flush_item'}
178+
result = await crud_ins.create_model(db, data, flush=True)
179+
180+
assert result.name == 'dict_flush_item'
181+
assert result.id is not None
182+
183+
184+
@pytest.mark.asyncio
185+
async def test_create_model_with_dict_and_commit(db: AsyncSession, crud_ins: CRUDPlus[Ins]):
186+
data = {'name': 'dict_commit_item'}
187+
result = await crud_ins.create_model(db, data, commit=True)
188+
189+
assert result.name == 'dict_commit_item'
190+
assert result.id is not None
191+
192+
193+
@pytest.mark.asyncio
194+
async def test_create_model_with_dict_and_kwargs(db: AsyncSession, crud_ins: CRUDPlus[Ins]):
195+
async with db.begin():
196+
data = {'name': 'dict_kwargs_item'}
197+
result = await crud_ins.create_model(db, data, is_deleted=True)
198+
199+
assert result.name == 'dict_kwargs_item'
200+
assert result.is_deleted is True
201+
202+
203+
@pytest.mark.asyncio
204+
async def test_create_models_with_dict(db: AsyncSession, crud_ins: CRUDPlus[Ins]):
205+
async with db.begin():
206+
data = [{'name': f'dict_batch_{i}'} for i in range(3)]
207+
results = await crud_ins.create_models(db, data)
208+
209+
assert len(results) == 3
210+
assert all(r.name.startswith('dict_batch_') for r in results)
211+
assert all(r.id is not None for r in results)
212+
213+
214+
@pytest.mark.asyncio
215+
async def test_create_models_with_dict_and_flush(db: AsyncSession, crud_ins: CRUDPlus[Ins]):
216+
async with db.begin():
217+
data = [{'name': f'dict_flush_batch_{i}'} for i in range(2)]
218+
results = await crud_ins.create_models(db, data, flush=True)
219+
220+
assert len(results) == 2
221+
assert all(r.id is not None for r in results)
222+
223+
224+
@pytest.mark.asyncio
225+
async def test_create_models_with_dict_and_commit(db: AsyncSession, crud_ins: CRUDPlus[Ins]):
226+
data = [{'name': f'dict_commit_batch_{i}'} for i in range(2)]
227+
results = await crud_ins.create_models(db, data, commit=True)
228+
229+
assert len(results) == 2
230+
assert all(r.id is not None for r in results)
231+
232+
233+
@pytest.mark.asyncio
234+
async def test_create_models_with_dict_and_kwargs(db: AsyncSession, crud_ins: CRUDPlus[Ins]):
235+
async with db.begin():
236+
data = [{'name': f'dict_kwargs_batch_{i}'} for i in range(2)]
237+
results = await crud_ins.create_models(db, data, is_deleted=True)
238+
239+
assert len(results) == 2
240+
assert all(r.is_deleted is True for r in results)
241+
242+
243+
@pytest.mark.asyncio
244+
async def test_create_models_with_mixed_input(db: AsyncSession, crud_ins: CRUDPlus[Ins]):
245+
async with db.begin():
246+
data = [
247+
CreateIns(name='schema_item'),
248+
{'name': 'dict_item'},
249+
CreateIns(name='schema_item_2'),
250+
]
251+
results = await crud_ins.create_models(db, data)
252+
253+
assert len(results) == 3
254+
assert results[0].name == 'schema_item'
255+
assert results[1].name == 'dict_item'
256+
assert results[2].name == 'schema_item_2'
257+
assert all(r.id is not None for r in results)

0 commit comments

Comments
 (0)