Feat: start sqlite tribe repository
This commit is contained in:
parent
566ba8e2f5
commit
6fbe238e59
18
backend/adapters/sqlite.py
Normal file
18
backend/adapters/sqlite.py
Normal 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)
|
29
backend/repository/tribe_sqlite_repository.py
Normal file
29
backend/repository/tribe_sqlite_repository.py
Normal 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
|
@ -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()
|
||||||
|
31
tests/integration/test_repository_sqlite.py
Normal file
31
tests/integration/test_repository_sqlite.py
Normal 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
|
Loading…
Reference in New Issue
Block a user