Feat: add orm for Tribes

This commit is contained in:
Bertrand Benjamin 2022-12-20 10:25:12 +01:00
parent 77b26f1180
commit 723092c38f
4 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,17 @@
from sqlalchemy import Column, MetaData, String, Table
from sqlalchemy.orm import mapper
from backend.model.tribe import Tribe as TribeModel
metadata = MetaData()
tribes = Table(
"tribes",
metadata,
Column("name", String(255), primary_key=True),
Column("level", String(255)),
)
def start_mappers():
tribes_mapper = mapper(TribeModel, tribes)

View File

@ -20,3 +20,8 @@ class Tribe:
def register_student(self, student: Student) -> None:
self.students.append(student)
def __eq__(self, other: object) -> bool:
if isinstance(other, Tribe):
return self.name == other.name and self.level == other.level
return False

19
tests/conftest.py Normal file
View File

@ -0,0 +1,19 @@
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import clear_mappers, sessionmaker
from backend.adapters.orm import metadata, start_mappers
@pytest.fixture
def in_memory_db():
engine = create_engine("sqlite:///:memory:")
metadata.create_all(engine)
return engine
@pytest.fixture
def session(in_memory_db):
start_mappers()
yield sessionmaker(bind=in_memory_db)()
clear_mappers()

View File

@ -0,0 +1,15 @@
from backend.adapters.orm import metadata, start_mappers
from backend.model.tribe import Tribe
def test_tribes_mapper_can_load_tribe(session):
session.execute(
"INSERT INTO tribes (name, level) VALUES "
"('tribe1', '2nd'),"
"('tribe2', '1ST')"
)
expected = [
(Tribe("tribe1", "2nd")),
(Tribe("tribe2", "1ST")),
]
assert session.query(Tribe).all() == expected