Skip to content

Commit

Permalink
Added delete user method
Browse files Browse the repository at this point in the history
  • Loading branch information
khoroshevskyi committed Jul 8, 2024
1 parent 6490e23 commit ef6fda4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pepdbagent/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}""")
20 changes: 19 additions & 1 deletion pepdbagent/modules/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
39 changes: 38 additions & 1 deletion tests/test_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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

0 comments on commit ef6fda4

Please sign in to comment.