recopytex/tests/integration/test_orm.py

66 lines
1.7 KiB
Python
Raw Normal View History

2022-12-20 09:25:12 +00:00
from backend.adapters.orm import metadata, start_mappers
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
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]
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