diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 9b3d9f8..c405b87 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 1.0.36 +current_version = 1.0.37 commit = True tag = True message = Bump version: {current_version} → {new_version} [ci skip] diff --git a/pvsite_datamodel/__init__.py b/pvsite_datamodel/__init__.py index d1ee9a0..ae014a0 100644 --- a/pvsite_datamodel/__init__.py +++ b/pvsite_datamodel/__init__.py @@ -13,4 +13,4 @@ UserSQL, ) -__version__ = "1.0.36" +__version__ = "1.0.37" diff --git a/pvsite_datamodel/read/user.py b/pvsite_datamodel/read/user.py index 5c443c4..0f4e8fd 100644 --- a/pvsite_datamodel/read/user.py +++ b/pvsite_datamodel/read/user.py @@ -26,10 +26,17 @@ def get_user_by_email(session: Session, email: str, make_new_user_if_none: bool if user is None and make_new_user_if_none is True: logger.info(f"User with email {email} not found, so making one") - # making a new site group - site_group = SiteGroupSQL(site_group_name=f"site_group_for_{email}") - session.add(site_group) - session.commit() + # checking for site_group + site_group = ( + session.query(SiteGroupSQL) + .filter(SiteGroupSQL.site_group_name == f"site_group_for_{email}") + .first() + ) + # making a new site group if one doesn't exist + if site_group is None: + site_group = SiteGroupSQL(site_group_name=f"site_group_for_{email}") + session.add(site_group) + session.commit() # make a new user user = UserSQL(email=email, site_group_uuid=site_group.site_group_uuid) diff --git a/pyproject.toml b/pyproject.toml index 1b13635..8d42d22 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,7 @@ + [tool.poetry] name = "pvsite-datamodel" -version = "1.0.36" +version = "1.0.37" description = "SDK for interacting with the PVSite database" authors = ["Open Climate Fix"] readme = "README.md" diff --git a/tests/read/test_get_user_by_email.py b/tests/read/test_get_user_by_email.py index e42c44f..f145c89 100644 --- a/tests/read/test_get_user_by_email.py +++ b/tests/read/test_get_user_by_email.py @@ -1,4 +1,4 @@ -from pvsite_datamodel import UserSQL +from pvsite_datamodel import SiteGroupSQL, UserSQL from pvsite_datamodel.read import get_user_by_email from pvsite_datamodel.write.user_and_site import create_site_group, create_user @@ -6,6 +6,12 @@ class TestGetUserByEmail: """Test for get_user_by_email function""" + def test_get_user_by_email_no_users_site_group_exists(self, db_session): + _ = create_site_group(db_session=db_session, site_group_name="site_group_for_test@test.com") + _ = get_user_by_email(session=db_session, email="test@test.com") + assert len(db_session.query(UserSQL).all()) == 1 + assert len(db_session.query(SiteGroupSQL).all()) == 1 + def test_get_user_by_email_no_users(self, db_session): user = get_user_by_email(session=db_session, email="test@test.com") assert user.email == "test@test.com" @@ -13,10 +19,10 @@ def test_get_user_by_email_no_users(self, db_session): def test_get_user_by_email_with_users(self, db_session): site_group = create_site_group(db_session=db_session) - user = create_user( + _ = create_user( session=db_session, site_group_name=site_group.site_group_name, email="test_1@test.com" ) - user = create_user( + _ = create_user( session=db_session, site_group_name=site_group.site_group_name, email="test_2@test.com" )