From ef6fda4ffc4c3837b959cc17e87e00ee18f5e423 Mon Sep 17 00:00:00 2001 From: Khoroshevskyi Date: Mon, 8 Jul 2024 14:55:07 -0400 Subject: [PATCH] Added delete user method --- pepdbagent/exceptions.py | 5 +++++ pepdbagent/modules/user.py | 20 ++++++++++++++++++- tests/test_namespace.py | 39 +++++++++++++++++++++++++++++++++++++- 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/pepdbagent/exceptions.py b/pepdbagent/exceptions.py index 8aa6a89..64e5278 100644 --- a/pepdbagent/exceptions.py +++ b/pepdbagent/exceptions.py @@ -112,3 +112,8 @@ def __init__(self, msg=""): class HistoryNotFoundError(PEPDatabaseAgentError): def __init__(self, msg=""): super().__init__(f"""History does not exist. {msg}""") + + +class UserNotFoundError(PEPDatabaseAgentError): + def __init__(self, msg=""): + super().__init__(f"""User does not exist. {msg}""") diff --git a/pepdbagent/modules/user.py b/pepdbagent/modules/user.py index eea8f84..125c7c1 100644 --- a/pepdbagent/modules/user.py +++ b/pepdbagent/modules/user.py @@ -7,7 +7,11 @@ from pepdbagent.const import PKG_NAME from pepdbagent.db_utils import BaseEngine, Projects, Stars, User -from pepdbagent.exceptions import ProjectAlreadyInFavorites, ProjectNotInFavorites +from pepdbagent.exceptions import ( + ProjectAlreadyInFavorites, + ProjectNotInFavorites, + UserNotFoundError, +) from pepdbagent.models import AnnotationList, AnnotationModel _LOGGER = logging.getLogger(PKG_NAME) @@ -214,3 +218,17 @@ def exists( return True else: return False + + def delete(self, namespace: str) -> None: + """ + Delete user from the database with all related data + + :param namespace: user namespace + :return: None + """ + if not self.exists(namespace): + raise UserNotFoundError + + with Session(self._sa_engine) as session: + session.execute(delete(User).where(User.namespace == namespace)) + session.commit() diff --git a/tests/test_namespace.py b/tests/test_namespace.py index 83fa4d1..196f6e8 100644 --- a/tests/test_namespace.py +++ b/tests/test_namespace.py @@ -63,7 +63,7 @@ def test_namespace_stats(self): ) class TestFavorites: """ - Test function within user class + Test method related to favorites """ def test_add_projects_to_favorites(self): @@ -156,3 +156,40 @@ def test_annotation_favorite_number(self, namespace, name): assert prj_annot.stars_number == 1 else: assert prj_annot.stars_number == 0 + + +@pytest.mark.skipif( + not PEPDBAgentContextManager().db_setup(), + reason="DB is not setup", +) +class TestUser: + """ + Test methods within user class + """ + + def test_create_user(self): + with PEPDBAgentContextManager(add_data=True) as agent: + + user = agent.user.create_user("test_user") + + assert agent.user.exists("test_user") + + def test_delete_user(self): + with PEPDBAgentContextManager(add_data=True) as agent: + + test_user = "test_user" + agent.user.create_user(test_user) + assert agent.user.exists(test_user) + agent.user.delete(test_user) + assert not agent.user.exists(test_user) + + def test_delete_user_deletes_projects(self): + with PEPDBAgentContextManager(add_data=True) as agent: + + test_user = "namespace1" + + assert agent.user.exists(test_user) + agent.user.delete(test_user) + assert not agent.user.exists(test_user) + results = agent.namespace.get(query=test_user) + assert len(results.results) == 0