import pytest from backend.model.tribe import Tribe from backend.repository.tribe_sqlite_repository import ( TribeRepositoryError, TribeSQLiteRepository, ) def test_get_tribe(memory_sqlite_conn, populate_db): with populate_db(memory_sqlite_conn) as (prebuild_tribes, _): name = prebuild_tribes[0].name tribe_repo = TribeSQLiteRepository(memory_sqlite_conn) tribes = tribe_repo.get(name) assert prebuild_tribes[0] == tribes def test_get_tribe_not_exists(memory_sqlite_conn): tribe_repo = TribeSQLiteRepository(memory_sqlite_conn) with pytest.raises(TribeRepositoryError): tribe_repo.get("Tribe0") 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() assert prebuild_tribes == listed_tribes def test_add_tribe(memory_sqlite_conn): tribe_repo = TribeSQLiteRepository(memory_sqlite_conn) tribe_infos = ("tribe1", "2nd") tribe = Tribe(*tribe_infos) tribe_repo.add(tribe) memory_sqlite_conn.commit() cursor = memory_sqlite_conn.cursor() cursor.execute( """ SELECT * FROM tribes WHERE name=? """, ("tribe1",), ) row = cursor.fetchone() assert row == tribe_infos 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) existing_tribe = prebuild_tribes[0] with pytest.raises(TribeRepositoryError): tribe_repo.add(existing_tribe) def test_update_tribe(memory_sqlite_conn, populate_db): with populate_db(memory_sqlite_conn) as (prebuild_tribes, _): tribe_repo = TribeSQLiteRepository(memory_sqlite_conn) name = prebuild_tribes[0].name new_tribe = Tribe("Tribe0", "Term") tribe_repo.update(name, new_tribe) memory_sqlite_conn.commit() prebuild_tribes[0] = new_tribe assert tribe_repo.list() == prebuild_tribes 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) name = prebuild_tribes[0].name new_tribe = Tribe("Tribe0", "Term") with pytest.raises(TribeRepositoryError): tribe_repo.update("iouiou", new_tribe) 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)