From f73ad3a34d054a8729b8a4905e37b95623c3ef48 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Mon, 26 Dec 2022 19:21:00 +0100 Subject: [PATCH] Feat: add add tribe --- backend/adapters/sqlite.py | 4 +-- backend/repository/tribe_sqlite_repository.py | 11 +++++- tests/integration/test_repository_sqlite.py | 36 +++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/backend/adapters/sqlite.py b/backend/adapters/sqlite.py index 97e4fbc..820890d 100644 --- a/backend/adapters/sqlite.py +++ b/backend/adapters/sqlite.py @@ -6,8 +6,8 @@ def create_tribe_table(conn) -> None: cursor.execute( """ CREATE TABLE IF NOT EXISTS tribes( - name TEXT PRIMARY KEY UNIQUE, - level TEXT + name VARCHAR PRIMARY KEY UNIQUE, + level VARCHAR ) """ ) diff --git a/backend/repository/tribe_sqlite_repository.py b/backend/repository/tribe_sqlite_repository.py index da8d31c..c43de87 100644 --- a/backend/repository/tribe_sqlite_repository.py +++ b/backend/repository/tribe_sqlite_repository.py @@ -7,7 +7,16 @@ class TribeSQLiteRepository(AbstractRepository): self.conn = conn 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: pass diff --git a/tests/integration/test_repository_sqlite.py b/tests/integration/test_repository_sqlite.py index 9a83ee7..9fec9a0 100644 --- a/tests/integration/test_repository_sqlite.py +++ b/tests/integration/test_repository_sqlite.py @@ -1,3 +1,5 @@ +import sqlite3 + import pytest from backend.adapters.sqlite import create_db @@ -50,3 +52,37 @@ def test_list_tribes(sqlite_conn): tribes = tribe_repo.list() 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)