mirror of
https://github.com/EChachati/SQLModel-CRUD-manager.git
synced 2025-08-14 19:10:18 +08:00
Add New functionalities
This commit is contained in:
@ -49,6 +49,18 @@ def test_get(last_id):
|
||||
assert hero.is_alive is True
|
||||
|
||||
|
||||
def test_get_or_404(last_id):
|
||||
hero = crud.get_or_404(last_id)
|
||||
assert hero.id == last_id
|
||||
assert hero.name == HERO_NAME
|
||||
assert hero.secret_name == SECRET_NAME
|
||||
assert hero.age is None
|
||||
assert hero.is_alive is True
|
||||
|
||||
with pytest.raises(HTTPException):
|
||||
crud.get_or_404(last_id + 1)
|
||||
|
||||
|
||||
def test_get_by_ids(last_id):
|
||||
heroes = crud.get_by_ids([last_id])
|
||||
assert len(heroes) == 1
|
||||
@ -68,10 +80,39 @@ def test_get_by_field(last_id):
|
||||
assert heroes.is_alive is True
|
||||
|
||||
|
||||
def test_get_by_fields(last_id):
|
||||
heroes = crud.get_by_fields({"name": HERO_NAME, "secret_name": SECRET_NAME})
|
||||
assert heroes.id == last_id
|
||||
assert heroes.name == HERO_NAME
|
||||
assert heroes.secret_name == SECRET_NAME
|
||||
assert heroes.age is None
|
||||
assert heroes.is_alive is True
|
||||
|
||||
|
||||
def test_get_or_create(last_id):
|
||||
hero = HeroCreate(name=HERO_NAME, secret_name=SECRET_NAME)
|
||||
hero = crud.get_or_create(hero, search_field="name")
|
||||
assert hero.id is not None
|
||||
assert hero.id == last_id
|
||||
assert hero.name == HERO_NAME
|
||||
|
||||
hero = HeroCreate(name="NotExistent", secret_name=SECRET_NAME)
|
||||
hero = crud.get_or_create(hero, search_field="name")
|
||||
assert hero.id is not None
|
||||
assert hero.id != last_id
|
||||
assert hero.name == "NotExistent"
|
||||
|
||||
hero = HeroCreate(name="NotExistentV2", secret_name=SECRET_NAME)
|
||||
with pytest.raises(HTTPException):
|
||||
crud.get_or_create(hero, search_field="id")
|
||||
with pytest.raises(HTTPException):
|
||||
crud.get_or_create(hero, search_field="NotExistent")
|
||||
|
||||
|
||||
def test_list(last_id):
|
||||
heroes = crud.list()
|
||||
assert len(heroes) == 1
|
||||
assert heroes[0].id == last_id
|
||||
assert len(heroes) == 2
|
||||
assert heroes[-1].id == last_id
|
||||
assert heroes[0].name == HERO_NAME
|
||||
assert heroes[0].secret_name == SECRET_NAME
|
||||
assert heroes[0].age is None
|
||||
@ -98,4 +139,4 @@ def test_delete(last_id):
|
||||
assert hero.age == 30
|
||||
assert hero.is_alive is True
|
||||
with pytest.raises(HTTPException):
|
||||
crud.get(last_id)
|
||||
crud.get_or_404(last_id)
|
||||
|
Reference in New Issue
Block a user