diff --git a/backend/repository/tribe_sqlite_repository.py b/backend/repository/tribe_sqlite_repository.py index cc2a4e9..1b9c2fd 100644 --- a/backend/repository/tribe_sqlite_repository.py +++ b/backend/repository/tribe_sqlite_repository.py @@ -28,6 +28,13 @@ class TribeSQLiteRepository(AbstractRepository): ) def update(self, name: str, tribe: Tribe) -> None: + tribes = self.list() + + if name not in map(lambda x: x.name, tribes): + raise TribeRepositoryError( + f"The tribe {name} doesn't exists. Can't update it" + ) + self.conn.execute( """ UPDATE tribes SET name=:newname, level=:newlevel WHERE name=:name @@ -66,6 +73,12 @@ class TribeSQLiteRepository(AbstractRepository): return [Tribe(*r) for r in rows] def delete(self, tribe: Tribe) -> None: + tribes = self.list() + if tribe.name not in map(lambda x: x.name, tribes): + raise TribeRepositoryError( + f"The tribe {tribe.name} doesn't exists. Can't delete it." + ) + self.conn.execute( """ DELETE FROM tribes WHERE name=:name diff --git a/tests/integration/test_repository_tribe_sqlite.py b/tests/integration/test_repository_tribe_sqlite.py index 6e8f709..2b4f80c 100644 --- a/tests/integration/test_repository_tribe_sqlite.py +++ b/tests/integration/test_repository_tribe_sqlite.py @@ -96,12 +96,22 @@ def test_update_tribe(sqlite_conn): assert tribe_repo.list() == prebuild_tribes +def test_update_tribe_not_exists(sqlite_conn): + prebuild_tribes = populate_tribes(sqlite_conn) + + tribe_repo = TribeSQLiteRepository(sqlite_conn) + + name = prebuild_tribes[0].name + new_tribe = Tribe("Tribe0", "Term") + with pytest.raises(TribeRepositoryError): + tribe_repo.update("iouiou", new_tribe) + + def test_delete_tribe(sqlite_conn): prebuild_tribes = populate_tribes(sqlite_conn) tribe_repo = TribeSQLiteRepository(sqlite_conn) deleted_tribe = prebuild_tribes.pop() - tribe_repo.delete(deleted_tribe) - sqlite_conn.commit() - - assert tribe_repo.list() == prebuild_tribes + deleted_tribe.name = "iouiou" + with pytest.raises(TribeRepositoryError): + tribe_repo.delete(deleted_tribe)