2022-12-20 09:25:12 +00:00
|
|
|
from backend.adapters.orm import metadata, start_mappers
|
2022-12-26 10:21:05 +00:00
|
|
|
from backend.model.student import Student
|
2022-12-20 09:25:12 +00:00
|
|
|
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
|
2022-12-20 09:37:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_tribe_mapper_can_save_tribe(session):
|
|
|
|
tribe_infos = ("tribe1", "2nd")
|
|
|
|
tribe = Tribe(*tribe_infos)
|
|
|
|
session.add(tribe)
|
|
|
|
|
|
|
|
rows = list(session.execute("SELECT name, level FROM 'tribes'"))
|
|
|
|
assert rows == []
|
|
|
|
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
rows = list(session.execute("SELECT name, level FROM 'tribes'"))
|
|
|
|
assert rows == [tribe_infos]
|
|
|
|
|
|
|
|
|
|
|
|
def test_tribe_mapper_can_save_and_load_tribe(session):
|
|
|
|
tribe_infos = ("tribe1", "2nd")
|
|
|
|
tribe = Tribe(*tribe_infos)
|
|
|
|
|
|
|
|
assert session.query(Tribe).all() == []
|
|
|
|
session.add(tribe)
|
|
|
|
|
|
|
|
assert session.query(Tribe).all() == [tribe]
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
assert session.query(Tribe).all() == [tribe]
|
2022-12-26 10:21:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_students_mapper_can_load_student(session):
|
|
|
|
session.execute("INSERT INTO tribes (name, level) VALUES " "('tribe1', '2nd')")
|
|
|
|
|
|
|
|
session.execute(
|
|
|
|
"INSERT INTO students (id, name, tribe_name) VALUES "
|
|
|
|
"('1', 'student1', 'tribe1'),"
|
|
|
|
"('2', 'student2', 'tribe1')"
|
|
|
|
)
|
|
|
|
|
|
|
|
tribe = session.query(Tribe).one()
|
|
|
|
expected = [
|
|
|
|
(Student("1", "student1", tribe)),
|
|
|
|
(Student("2", "student2", tribe)),
|
|
|
|
]
|
|
|
|
|
|
|
|
with session.no_autoflush:
|
|
|
|
students = session.query(Student).all()
|
|
|
|
|
|
|
|
assert set(tribe.students) == set(expected)
|
|
|
|
assert students == expected
|