Feat: add TribeSQLAlchemyRepository
This commit is contained in:
parent
21821c275e
commit
b3bb1f0cc8
23
backend/repository/abstract_repository.py
Normal file
23
backend/repository/abstract_repository.py
Normal file
@ -0,0 +1,23 @@
|
||||
import abc
|
||||
|
||||
|
||||
class AbstractRepository(abc.ABC):
|
||||
@abc.abstractmethod
|
||||
def add(self, element):
|
||||
raise NotImplementedError
|
||||
|
||||
@abc.abstractmethod
|
||||
def update(self, element):
|
||||
raise NotImplementedError
|
||||
|
||||
@abc.abstractmethod
|
||||
def list(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@abc.abstractmethod
|
||||
def get(self, reference):
|
||||
raise NotImplementedError
|
||||
|
||||
@abc.abstractmethod
|
||||
def delete(self, reference):
|
||||
raise NotImplementedError
|
23
backend/repository/tribe_sqlalchemy_repository.py
Normal file
23
backend/repository/tribe_sqlalchemy_repository.py
Normal file
@ -0,0 +1,23 @@
|
||||
from backend.model.tribe import Tribe
|
||||
from backend.repository.abstract_repository import AbstractRepository
|
||||
|
||||
|
||||
class TribeSQLAlchemyRepository(AbstractRepository):
|
||||
def __init__(self, session) -> None:
|
||||
self.session = session
|
||||
|
||||
def add(self, tribe: Tribe) -> None:
|
||||
self.session.add(tribe)
|
||||
|
||||
def update(self, name: str, tribe: Tribe) -> None:
|
||||
self.session.query(Tribe).filter_by(name=name).update(tribe.to_dict())
|
||||
|
||||
def get(self, name: str) -> Tribe:
|
||||
return self.session.query(Tribe).filter_by(name=name).one()
|
||||
|
||||
def list(self) -> list[Tribe]:
|
||||
return self.session.query(Tribe).all()
|
||||
|
||||
def delete(self, tribe: Tribe) -> None:
|
||||
the_tribe = self.get(tribe.name)
|
||||
self.session.delete(the_tribe)
|
73
tests/integration/test_repository_tribe.py
Normal file
73
tests/integration/test_repository_tribe.py
Normal file
@ -0,0 +1,73 @@
|
||||
from backend.model.tribe import Tribe
|
||||
from backend.repository.tribe_sqlalchemy_repository import TribeSQLAlchemyRepository
|
||||
|
||||
|
||||
def test_add_tribe(session):
|
||||
tribe_infos = ("tribe1", "2nd")
|
||||
tribe = Tribe(*tribe_infos)
|
||||
tribe_repo = TribeSQLAlchemyRepository(session)
|
||||
|
||||
tribe_repo.add(tribe)
|
||||
session.commit()
|
||||
|
||||
rows = list(session.execute("SELECT name, level FROM 'tribes'"))
|
||||
assert rows == [tribe_infos]
|
||||
|
||||
|
||||
def test_list_tribes(session):
|
||||
session.execute(
|
||||
"INSERT INTO tribes (name, level) VALUES "
|
||||
"('tribe1', '2nd'),"
|
||||
"('tribe2', '1ST')"
|
||||
)
|
||||
expected = [
|
||||
(Tribe("tribe1", "2nd")),
|
||||
(Tribe("tribe2", "1ST")),
|
||||
]
|
||||
tribe_repo = TribeSQLAlchemyRepository(session)
|
||||
assert tribe_repo.list() == expected
|
||||
|
||||
|
||||
def test_update_tribe(session):
|
||||
session.execute(
|
||||
"INSERT INTO tribes (name, level) VALUES "
|
||||
"('tribe1', '2nd'),"
|
||||
"('tribe2', '1ST')"
|
||||
)
|
||||
tribe_repo = TribeSQLAlchemyRepository(session)
|
||||
tribe_repo.update("tribe1", Tribe("tribe3", "Term"))
|
||||
session.commit()
|
||||
|
||||
expected = [
|
||||
(Tribe("tribe3", "Term")),
|
||||
(Tribe("tribe2", "1ST")),
|
||||
]
|
||||
assert tribe_repo.list() == expected
|
||||
|
||||
|
||||
def test_get_tribes(session):
|
||||
session.execute(
|
||||
"INSERT INTO tribes (name, level) VALUES "
|
||||
"('tribe1', '2nd'),"
|
||||
"('tribe2', '1ST')"
|
||||
)
|
||||
expected = Tribe("tribe1", "2nd")
|
||||
tribe_repo = TribeSQLAlchemyRepository(session)
|
||||
assert tribe_repo.get("tribe1") == expected
|
||||
|
||||
|
||||
def test_delete_tribes(session):
|
||||
session.execute(
|
||||
"INSERT INTO tribes (name, level) VALUES "
|
||||
"('tribe1', '2nd'),"
|
||||
"('tribe2', '1ST')"
|
||||
)
|
||||
expected = [
|
||||
(Tribe("tribe2", "1ST")),
|
||||
]
|
||||
tribe_repo = TribeSQLAlchemyRepository(session)
|
||||
|
||||
tribe_repo.delete(Tribe("tribe1", "2nd"))
|
||||
session.commit()
|
||||
|
||||
assert tribe_repo.list() == expected
|
Loading…
Reference in New Issue
Block a user