recopytex/tests/integration/test_repository_sqlite.py

120 lines
2.7 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.adapters.sqlite import create_db
from backend.model.tribe import Tribe
from backend.repository.tribe_sqlite_repository import TribeSQLiteRepository
2022-12-26 18:05:50 +00:00
def populate_tribe(conn) -> list[Tribe]:
2022-12-26 17:07:23 +00:00
cursor = conn.cursor()
tribes = [
("Tribe1", "2nd"),
("Tribe2", "2nd"),
("Tribe3", "1ST"),
]
cursor.executemany(
"""
INSERT INTO tribes(name, level) VALUES (?, ?)
""",
tribes,
)
conn.commit()
return [Tribe(*t) for t in tribes]
2022-12-26 18:05:50 +00:00
def test_get_tribe(sqlite_conn):
create_db(sqlite_conn)
prebuild_tribes = populate_tribe(sqlite_conn)
tribe_repo = TribeSQLiteRepository(sqlite_conn)
tribes = tribe_repo.get("Tribe1")
assert prebuild_tribes[0] == tribes
def test_get_tribe_not_exists(sqlite_conn):
create_db(sqlite_conn)
prebuild_tribes = populate_tribe(sqlite_conn)
tribe_repo = TribeSQLiteRepository(sqlite_conn)
with pytest.raises(ValueError):
tribe_repo.get("Tribe0")
2022-12-26 17:07:23 +00:00
def test_list_tribes(sqlite_conn):
create_db(sqlite_conn)
prebuild_tribes = populate_tribe(sqlite_conn)
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):
create_db(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):
create_db(sqlite_conn)
prebuild_tribes = populate_tribe(sqlite_conn)
tribe_repo = TribeSQLiteRepository(sqlite_conn)
tribe_infos = ("Tribe1", "2nd")
tribe = Tribe(*tribe_infos)
with pytest.raises(sqlite3.IntegrityError):
tribe_repo.add(tribe)
2022-12-26 18:39:44 +00:00
def test_update_tribe(sqlite_conn):
create_db(sqlite_conn)
prebuild_tribes = populate_tribe(sqlite_conn)
tribe_repo = TribeSQLiteRepository(sqlite_conn)
tribe_repo.update("Tribe1", Tribe("Tribe0", "Term"))
sqlite_conn.commit()
expected = [
Tribe("Tribe0", "Term"),
Tribe("Tribe2", "2nd"),
Tribe("Tribe3", "1ST"),
]
assert tribe_repo.list() == expected
def test_delete_tribe(sqlite_conn):
create_db(sqlite_conn)
prebuild_tribes = populate_tribe(sqlite_conn)
tribe_repo = TribeSQLiteRepository(sqlite_conn)
tribe_repo.delete(prebuild_tribes[0])
sqlite_conn.commit()
expected = [
Tribe("Tribe2", "2nd"),
Tribe("Tribe3", "1ST"),
]
assert tribe_repo.list() == expected