Feat: start sqlite tribe repository

This commit is contained in:
Bertrand Benjamin 2022-12-26 18:07:23 +01:00
parent 566ba8e2f5
commit 6fbe238e59
4 changed files with 87 additions and 0 deletions

View File

@ -0,0 +1,18 @@
import sqlite3
def create_tribe_table(conn) -> None:
cursor = conn.cursor()
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS tribes(
name TEXT PRIMARY KEY UNIQUE,
level TEXT
)
"""
)
conn.commit()
def create_db(conn) -> None:
create_tribe_table(conn)

View File

@ -0,0 +1,29 @@
from backend.model.tribe import Tribe
from backend.repository.abstract_repository import AbstractRepository
class TribeSQLiteRepository(AbstractRepository):
def __init__(self, conn) -> None:
self.conn = conn
def add(self, tribe: Tribe) -> None:
pass
def update(self, name: str, tribe: Tribe) -> None:
pass
def get(self, name: str) -> Tribe:
pass
def list(self) -> list[Tribe]:
cursor = self.conn.cursor()
cursor.execute(
"""
SELECT * FROM tribes
"""
)
rows = cursor.fetchall()
return [Tribe(*r) for r in rows]
def delete(self, tribe: Tribe) -> None:
pass

View File

@ -1,3 +1,5 @@
import sqlite3
import pytest import pytest
from sqlalchemy import create_engine from sqlalchemy import create_engine
from sqlalchemy.orm import clear_mappers, sessionmaker from sqlalchemy.orm import clear_mappers, sessionmaker
@ -17,3 +19,10 @@ def session(in_memory_db):
start_mappers() start_mappers()
yield sessionmaker(bind=in_memory_db)() yield sessionmaker(bind=in_memory_db)()
clear_mappers() clear_mappers()
@pytest.fixture
def sqlite_conn():
conn = sqlite3.connect(":memory:")
yield conn
conn.close()

View File

@ -0,0 +1,31 @@
from backend.adapters.sqlite import create_db
from backend.model.tribe import Tribe
from backend.repository.tribe_sqlite_repository import TribeSQLiteRepository
def populate_tribe(conn) -> None:
cursor = conn.cursor()
tribes = [
("Tribe1", "2nd"),
("Tribe2", "2nd"),
("Tribe3", "1ST"),
]
cursor.executemany(
"""
INSERT INTO tribes(name, level) VALUES (?, ?)
""",
tribes,
)
conn.commit()
return [Tribe(*t) for t in tribes]
def test_list_tribes(sqlite_conn):
create_db(sqlite_conn)
prebuild_tribes = populate_tribe(sqlite_conn)
tribe_repo = TribeSQLiteRepository(sqlite_conn)
tribes = tribe_repo.list()
assert prebuild_tribes == tribes