Skip to content

Commit

Permalink
Refactor assign_model_name_to_site function and add error handling fo… (
Browse files Browse the repository at this point in the history
#186)

* Refactor assign_model_name_to_site function and add error handling for nonexistent site and test cases

* Fix linter

* Fix linter

* Remove not found test case in method and readapt exception message
  • Loading branch information
hanaawad24 authored Dec 2, 2024
1 parent efb5486 commit 18a053d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pvsite_datamodel/write/user_and_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,11 @@ def delete_site_group(session: Session, site_group_name: str) -> str:


def assign_model_name_to_site(session: Session, site_uuid, model_name):
"""
Assign a model name to a site.
"""Assign model to site.
:param session: database session
:param site_uuid: site uuid
:param model_name: name of the model
"""

site = get_site_by_uuid(session=session, site_uuid=site_uuid)
Expand Down
10 changes: 10 additions & 0 deletions tests/write/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,13 @@ def test_assign_model_name_to_site(db_session):
assign_model_name_to_site(db_session, site.site_uuid, "test_model_2")

assert site.ml_model.name == "test_model_2"


def test_assign_model_to_nonexistent_site(db_session):
"""Test assigning a model to a nonexistent site"""
nonexistent_site_uuid = str(uuid.uuid4())

with pytest.raises(
KeyError, match=f"Site uuid {nonexistent_site_uuid} not found in sites table"
):
assign_model_name_to_site(db_session, nonexistent_site_uuid, "test_model")

0 comments on commit 18a053d

Please sign in to comment.