feat: add update and delete for tribes

This commit is contained in:
Bertrand Benjamin 2022-12-26 19:39:44 +01:00
parent f73ad3a34d
commit dfd0bb2b81
2 changed files with 50 additions and 4 deletions

View File

@ -7,8 +7,7 @@ class TribeSQLiteRepository(AbstractRepository):
self.conn = conn self.conn = conn
def add(self, tribe: Tribe) -> None: def add(self, tribe: Tribe) -> None:
cursor = self.conn.cursor() self.conn.execute(
cursor.execute(
""" """
INSERT INTO tribes(name, level) VALUES (?, ?) INSERT INTO tribes(name, level) VALUES (?, ?)
""", """,
@ -19,7 +18,16 @@ class TribeSQLiteRepository(AbstractRepository):
) )
def update(self, name: str, tribe: Tribe) -> None: def update(self, name: str, tribe: Tribe) -> None:
pass self.conn.execute(
"""
UPDATE tribes SET name=:newname, level=:newlevel WHERE name=:name
""",
{
"newname": tribe.name,
"newlevel": tribe.level,
"name": name,
},
)
def get(self, name: str) -> Tribe: def get(self, name: str) -> Tribe:
cursor = self.conn.cursor() cursor = self.conn.cursor()
@ -48,4 +56,11 @@ class TribeSQLiteRepository(AbstractRepository):
return [Tribe(*r) for r in rows] return [Tribe(*r) for r in rows]
def delete(self, tribe: Tribe) -> None: def delete(self, tribe: Tribe) -> None:
pass self.conn.execute(
"""
DELETE FROM tribes WHERE name=:name
""",
{
"name": tribe.name,
},
)

View File

@ -86,3 +86,34 @@ def test_add_tribe_fail_exists(sqlite_conn):
tribe = Tribe(*tribe_infos) tribe = Tribe(*tribe_infos)
with pytest.raises(sqlite3.IntegrityError): with pytest.raises(sqlite3.IntegrityError):
tribe_repo.add(tribe) tribe_repo.add(tribe)
def test_update_tribe(sqlite_conn):
create_db(sqlite_conn)
prebuild_tribes = populate_tribe(sqlite_conn)
tribe_repo = TribeSQLiteRepository(sqlite_conn)
tribe_repo.update("Tribe1", Tribe("Tribe0", "Term"))
sqlite_conn.commit()
expected = [
Tribe("Tribe0", "Term"),
Tribe("Tribe2", "2nd"),
Tribe("Tribe3", "1ST"),
]
assert tribe_repo.list() == expected
def test_delete_tribe(sqlite_conn):
create_db(sqlite_conn)
prebuild_tribes = populate_tribe(sqlite_conn)
tribe_repo = TribeSQLiteRepository(sqlite_conn)
tribe_repo.delete(prebuild_tribes[0])
sqlite_conn.commit()
expected = [
Tribe("Tribe2", "2nd"),
Tribe("Tribe3", "1ST"),
]
assert tribe_repo.list() == expected