2022-12-26 18:05:50 +00:00
|
|
|
import pytest
|
|
|
|
|
2022-12-26 17:07:23 +00:00
|
|
|
from backend.model.tribe import Tribe
|
2022-12-29 16:47:40 +00:00
|
|
|
from backend.repository.tribe_sqlite_repository import (
|
|
|
|
TribeRepositoryError,
|
|
|
|
TribeSQLiteRepository,
|
|
|
|
)
|
2022-12-27 07:13:34 +00:00
|
|
|
|
2022-12-26 18:05:50 +00:00
|
|
|
|
2022-12-31 13:28:46 +00:00
|
|
|
def test_get_tribe(memory_sqlite_conn, populate_db):
|
|
|
|
with populate_db(memory_sqlite_conn) as (prebuild_tribes, _):
|
|
|
|
name = prebuild_tribes[0].name
|
2022-12-26 18:05:50 +00:00
|
|
|
|
2022-12-31 13:28:46 +00:00
|
|
|
tribe_repo = TribeSQLiteRepository(memory_sqlite_conn)
|
|
|
|
tribes = tribe_repo.get(name)
|
2022-12-26 18:05:50 +00:00
|
|
|
|
2022-12-31 13:28:46 +00:00
|
|
|
assert prebuild_tribes[0] == tribes
|
2022-12-26 18:05:50 +00:00
|
|
|
|
|
|
|
|
2022-12-31 13:28:46 +00:00
|
|
|
def test_get_tribe_not_exists(memory_sqlite_conn):
|
|
|
|
tribe_repo = TribeSQLiteRepository(memory_sqlite_conn)
|
2022-12-29 16:47:40 +00:00
|
|
|
with pytest.raises(TribeRepositoryError):
|
2022-12-26 18:05:50 +00:00
|
|
|
tribe_repo.get("Tribe0")
|
|
|
|
|
|
|
|
|
2022-12-31 13:28:46 +00:00
|
|
|
def test_list_tribes(memory_sqlite_conn, populate_db):
|
|
|
|
with populate_db(memory_sqlite_conn) as (prebuild_tribes, _):
|
|
|
|
tribe_repo = TribeSQLiteRepository(memory_sqlite_conn)
|
|
|
|
listed_tribes = tribe_repo.list()
|
2022-12-26 17:07:23 +00:00
|
|
|
|
2022-12-31 13:28:46 +00:00
|
|
|
assert prebuild_tribes == listed_tribes
|
2022-12-26 17:07:23 +00:00
|
|
|
|
2022-12-26 18:21:00 +00:00
|
|
|
|
2022-12-31 13:28:46 +00:00
|
|
|
def test_add_tribe(memory_sqlite_conn):
|
|
|
|
tribe_repo = TribeSQLiteRepository(memory_sqlite_conn)
|
2022-12-26 18:21:00 +00:00
|
|
|
|
|
|
|
tribe_infos = ("tribe1", "2nd")
|
|
|
|
tribe = Tribe(*tribe_infos)
|
|
|
|
tribe_repo.add(tribe)
|
2022-12-31 13:28:46 +00:00
|
|
|
memory_sqlite_conn.commit()
|
2022-12-26 18:21:00 +00:00
|
|
|
|
2022-12-31 13:28:46 +00:00
|
|
|
cursor = memory_sqlite_conn.cursor()
|
2022-12-26 18:21:00 +00:00
|
|
|
cursor.execute(
|
|
|
|
"""
|
|
|
|
SELECT * FROM tribes WHERE name=?
|
|
|
|
""",
|
|
|
|
("tribe1",),
|
|
|
|
)
|
|
|
|
|
|
|
|
row = cursor.fetchone()
|
|
|
|
assert row == tribe_infos
|
|
|
|
|
|
|
|
|
2022-12-31 13:28:46 +00:00
|
|
|
def test_add_tribe_fail_exists(memory_sqlite_conn, populate_db):
|
|
|
|
with populate_db(memory_sqlite_conn) as (prebuild_tribes, _):
|
|
|
|
tribe_repo = TribeSQLiteRepository(memory_sqlite_conn)
|
2022-12-26 18:21:00 +00:00
|
|
|
|
2022-12-31 13:28:46 +00:00
|
|
|
existing_tribe = prebuild_tribes[0]
|
|
|
|
with pytest.raises(TribeRepositoryError):
|
|
|
|
tribe_repo.add(existing_tribe)
|
2022-12-26 18:39:44 +00:00
|
|
|
|
|
|
|
|
2022-12-31 13:28:46 +00:00
|
|
|
def test_update_tribe(memory_sqlite_conn, populate_db):
|
2022-12-26 18:39:44 +00:00
|
|
|
|
2022-12-31 13:28:46 +00:00
|
|
|
with populate_db(memory_sqlite_conn) as (prebuild_tribes, _):
|
|
|
|
tribe_repo = TribeSQLiteRepository(memory_sqlite_conn)
|
2022-12-27 07:13:34 +00:00
|
|
|
|
2022-12-31 13:28:46 +00:00
|
|
|
name = prebuild_tribes[0].name
|
|
|
|
new_tribe = Tribe("Tribe0", "Term")
|
|
|
|
tribe_repo.update(name, new_tribe)
|
|
|
|
memory_sqlite_conn.commit()
|
2022-12-26 18:39:44 +00:00
|
|
|
|
2022-12-31 13:28:46 +00:00
|
|
|
prebuild_tribes[0] = new_tribe
|
|
|
|
assert tribe_repo.list() == prebuild_tribes
|
2022-12-26 18:39:44 +00:00
|
|
|
|
|
|
|
|
2022-12-31 13:28:46 +00:00
|
|
|
def test_update_tribe_not_exists(memory_sqlite_conn, populate_db):
|
|
|
|
with populate_db(memory_sqlite_conn) as (prebuild_tribes, _):
|
|
|
|
tribe_repo = TribeSQLiteRepository(memory_sqlite_conn)
|
2022-12-30 06:45:26 +00:00
|
|
|
|
2022-12-31 13:28:46 +00:00
|
|
|
name = prebuild_tribes[0].name
|
|
|
|
new_tribe = Tribe("Tribe0", "Term")
|
|
|
|
with pytest.raises(TribeRepositoryError):
|
|
|
|
tribe_repo.update("iouiou", new_tribe)
|
2022-12-30 06:45:26 +00:00
|
|
|
|
|
|
|
|
2022-12-31 13:28:46 +00:00
|
|
|
def test_delete_tribe(memory_sqlite_conn, populate_db):
|
|
|
|
with populate_db(memory_sqlite_conn) as (prebuild_tribes, _):
|
|
|
|
tribe_repo = TribeSQLiteRepository(memory_sqlite_conn)
|
|
|
|
deleted_tribe = prebuild_tribes.pop()
|
|
|
|
deleted_tribe.name = "iouiou"
|
|
|
|
with pytest.raises(TribeRepositoryError):
|
|
|
|
tribe_repo.delete(deleted_tribe)
|