Feat: add add tribe

This commit is contained in:
Bertrand Benjamin 2022-12-26 19:21:00 +01:00
parent fe92433311
commit f73ad3a34d
3 changed files with 48 additions and 3 deletions

View File

@ -6,8 +6,8 @@ def create_tribe_table(conn) -> None:
cursor.execute( cursor.execute(
""" """
CREATE TABLE IF NOT EXISTS tribes( CREATE TABLE IF NOT EXISTS tribes(
name TEXT PRIMARY KEY UNIQUE, name VARCHAR PRIMARY KEY UNIQUE,
level TEXT level VARCHAR
) )
""" """
) )

View File

@ -7,7 +7,16 @@ class TribeSQLiteRepository(AbstractRepository):
self.conn = conn self.conn = conn
def add(self, tribe: Tribe) -> None: def add(self, tribe: Tribe) -> None:
pass cursor = self.conn.cursor()
cursor.execute(
"""
INSERT INTO tribes(name, level) VALUES (?, ?)
""",
(
tribe.name,
tribe.level,
),
)
def update(self, name: str, tribe: Tribe) -> None: def update(self, name: str, tribe: Tribe) -> None:
pass pass

View File

@ -1,3 +1,5 @@
import sqlite3
import pytest import pytest
from backend.adapters.sqlite import create_db from backend.adapters.sqlite import create_db
@ -50,3 +52,37 @@ def test_list_tribes(sqlite_conn):
tribes = tribe_repo.list() tribes = tribe_repo.list()
assert prebuild_tribes == tribes assert prebuild_tribes == tribes
def test_add_tribe(sqlite_conn):
create_db(sqlite_conn)
tribe_repo = TribeSQLiteRepository(sqlite_conn)
tribe_infos = ("tribe1", "2nd")
tribe = Tribe(*tribe_infos)
tribe_repo.add(tribe)
sqlite_conn.commit()
cursor = sqlite_conn.cursor()
cursor.execute(
"""
SELECT * FROM tribes WHERE name=?
""",
("tribe1",),
)
row = cursor.fetchone()
assert row == tribe_infos
def test_add_tribe_fail_exists(sqlite_conn):
create_db(sqlite_conn)
prebuild_tribes = populate_tribe(sqlite_conn)
tribe_repo = TribeSQLiteRepository(sqlite_conn)
tribe_infos = ("Tribe1", "2nd")
tribe = Tribe(*tribe_infos)
with pytest.raises(sqlite3.IntegrityError):
tribe_repo.add(tribe)