mirror of
https://github.com/EChachati/SQLModel-CRUD-manager.git
synced 2025-08-14 19:10:18 +08:00
Add get_by_ids
This commit is contained in:
@ -52,6 +52,25 @@ class CRUDManager:
|
|||||||
)
|
)
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
|
def get_by_ids(self, ids: list[int]) -> list[ModelType]:
|
||||||
|
"""
|
||||||
|
The function retrieves a list of model objects from the database based
|
||||||
|
on their primary keys.
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
|
||||||
|
* `ids`: The parameter `ids` is a list of integers. It is used to
|
||||||
|
identify a list of objects in the database based on their primary key
|
||||||
|
values.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
The `get_by_ids` method is returning a list of objects of type
|
||||||
|
`ModelType`.
|
||||||
|
"""
|
||||||
|
query = select(self.model).where(self.model.id.in_(ids))
|
||||||
|
return self.db.exec(query).all()
|
||||||
|
|
||||||
def list(self, query: QueryLike = None) -> list[ModelType]:
|
def list(self, query: QueryLike = None) -> list[ModelType]:
|
||||||
"""
|
"""
|
||||||
The function returns a list of all the records in the database that
|
The function returns a list of all the records in the database that
|
||||||
|
@ -49,6 +49,16 @@ def test_get(last_id):
|
|||||||
assert hero.is_alive is True
|
assert hero.is_alive is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_by_ids(last_id):
|
||||||
|
heroes = crud.get_by_ids([last_id])
|
||||||
|
assert len(heroes) == 1
|
||||||
|
assert heroes[0].id == last_id
|
||||||
|
assert heroes[0].name == HERO_NAME
|
||||||
|
assert heroes[0].secret_name == SECRET_NAME
|
||||||
|
assert heroes[0].age is None
|
||||||
|
assert heroes[0].is_alive is True
|
||||||
|
|
||||||
|
|
||||||
def test_list(last_id):
|
def test_list(last_id):
|
||||||
heroes = crud.list()
|
heroes = crud.list()
|
||||||
assert len(heroes) == 1
|
assert len(heroes) == 1
|
||||||
|
Reference in New Issue
Block a user