recopytex/tests/integration/test_repository_tribe_sqlit...

118 lines
2.8 KiB
Python
Raw Normal View History

2022-12-26 18:21:00 +00:00
import sqlite3
2022-12-26 18:05:50 +00:00
import pytest
2022-12-26 17:07:23 +00:00
from backend.model.tribe import Tribe
from backend.repository.tribe_sqlite_repository import (
TribeRepositoryError,
TribeSQLiteRepository,
)
2022-12-27 07:13:34 +00:00
from tests.model.fakes import build_tribes
2022-12-26 17:07:23 +00:00
2022-12-27 07:13:34 +00:00
def populate_tribes(conn) -> list[Tribe]:
2022-12-26 17:07:23 +00:00
cursor = conn.cursor()
2022-12-27 07:13:34 +00:00
tribes = build_tribes(3)
2022-12-26 17:07:23 +00:00
cursor.executemany(
"""
INSERT INTO tribes(name, level) VALUES (?, ?)
""",
2022-12-27 07:13:34 +00:00
[t.to_tuple() for t in tribes],
2022-12-26 17:07:23 +00:00
)
conn.commit()
2022-12-27 07:13:34 +00:00
return tribes
2022-12-26 17:07:23 +00:00
2022-12-26 18:05:50 +00:00
def test_get_tribe(sqlite_conn):
2022-12-27 07:13:34 +00:00
prebuild_tribes = populate_tribes(sqlite_conn)
name = prebuild_tribes[0].name
2022-12-26 18:05:50 +00:00
tribe_repo = TribeSQLiteRepository(sqlite_conn)
2022-12-27 07:13:34 +00:00
tribes = tribe_repo.get(name)
2022-12-26 18:05:50 +00:00
assert prebuild_tribes[0] == tribes
def test_get_tribe_not_exists(sqlite_conn):
2022-12-27 07:13:34 +00:00
prebuild_tribes = populate_tribes(sqlite_conn)
2022-12-26 18:05:50 +00:00
tribe_repo = TribeSQLiteRepository(sqlite_conn)
with pytest.raises(TribeRepositoryError):
2022-12-26 18:05:50 +00:00
tribe_repo.get("Tribe0")
2022-12-26 17:07:23 +00:00
def test_list_tribes(sqlite_conn):
2022-12-27 07:13:34 +00:00
prebuild_tribes = populate_tribes(sqlite_conn)
2022-12-26 17:07:23 +00:00
tribe_repo = TribeSQLiteRepository(sqlite_conn)
tribes = tribe_repo.list()
assert prebuild_tribes == tribes
2022-12-26 18:21:00 +00:00
def test_add_tribe(sqlite_conn):
tribe_repo = TribeSQLiteRepository(sqlite_conn)
tribe_infos = ("tribe1", "2nd")
tribe = Tribe(*tribe_infos)
tribe_repo.add(tribe)
sqlite_conn.commit()
cursor = sqlite_conn.cursor()
cursor.execute(
"""
SELECT * FROM tribes WHERE name=?
""",
("tribe1",),
)
row = cursor.fetchone()
assert row == tribe_infos
def test_add_tribe_fail_exists(sqlite_conn):
2022-12-27 07:13:34 +00:00
prebuild_tribes = populate_tribes(sqlite_conn)
2022-12-26 18:21:00 +00:00
tribe_repo = TribeSQLiteRepository(sqlite_conn)
2022-12-27 07:13:34 +00:00
existing_tribe = prebuild_tribes[0]
with pytest.raises(TribeRepositoryError):
2022-12-27 07:13:34 +00:00
tribe_repo.add(existing_tribe)
2022-12-26 18:39:44 +00:00
def test_update_tribe(sqlite_conn):
2022-12-27 07:13:34 +00:00
prebuild_tribes = populate_tribes(sqlite_conn)
2022-12-26 18:39:44 +00:00
tribe_repo = TribeSQLiteRepository(sqlite_conn)
2022-12-27 07:13:34 +00:00
name = prebuild_tribes[0].name
new_tribe = Tribe("Tribe0", "Term")
tribe_repo.update(name, new_tribe)
2022-12-26 18:39:44 +00:00
sqlite_conn.commit()
2022-12-27 07:13:34 +00:00
prebuild_tribes[0] = new_tribe
assert tribe_repo.list() == prebuild_tribes
2022-12-26 18:39:44 +00:00
def test_update_tribe_not_exists(sqlite_conn):
prebuild_tribes = populate_tribes(sqlite_conn)
tribe_repo = TribeSQLiteRepository(sqlite_conn)
name = prebuild_tribes[0].name
new_tribe = Tribe("Tribe0", "Term")
with pytest.raises(TribeRepositoryError):
tribe_repo.update("iouiou", new_tribe)
2022-12-26 18:39:44 +00:00
def test_delete_tribe(sqlite_conn):
2022-12-27 07:13:34 +00:00
prebuild_tribes = populate_tribes(sqlite_conn)
2022-12-26 18:39:44 +00:00
tribe_repo = TribeSQLiteRepository(sqlite_conn)
2022-12-27 07:13:34 +00:00
deleted_tribe = prebuild_tribes.pop()
deleted_tribe.name = "iouiou"
with pytest.raises(TribeRepositoryError):
tribe_repo.delete(deleted_tribe)