Feat: rename populate and use fakers
This commit is contained in:
parent
de9a4bc4be
commit
4e13d0e32f
@ -28,3 +28,6 @@ class Tribe:
|
|||||||
|
|
||||||
def to_dict(self) -> dict:
|
def to_dict(self) -> dict:
|
||||||
return {"name": self.name, "level": self.level}
|
return {"name": self.name, "level": self.level}
|
||||||
|
|
||||||
|
def to_tuple(self) -> tuple:
|
||||||
|
return (self.name, self.level)
|
||||||
|
@ -5,39 +5,38 @@ import pytest
|
|||||||
from backend.adapters.sqlite import create_db
|
from backend.adapters.sqlite import create_db
|
||||||
from backend.model.tribe import Tribe
|
from backend.model.tribe import Tribe
|
||||||
from backend.repository.tribe_sqlite_repository import TribeSQLiteRepository
|
from backend.repository.tribe_sqlite_repository import TribeSQLiteRepository
|
||||||
|
from tests.model.fakes import build_tribes
|
||||||
|
|
||||||
|
|
||||||
def populate_tribe(conn) -> list[Tribe]:
|
def populate_tribes(conn) -> list[Tribe]:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
tribes = [
|
tribes = build_tribes(3)
|
||||||
("Tribe1", "2nd"),
|
|
||||||
("Tribe2", "2nd"),
|
|
||||||
("Tribe3", "1ST"),
|
|
||||||
]
|
|
||||||
cursor.executemany(
|
cursor.executemany(
|
||||||
"""
|
"""
|
||||||
INSERT INTO tribes(name, level) VALUES (?, ?)
|
INSERT INTO tribes(name, level) VALUES (?, ?)
|
||||||
""",
|
""",
|
||||||
tribes,
|
[t.to_tuple() for t in tribes],
|
||||||
)
|
)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
return [Tribe(*t) for t in tribes]
|
return tribes
|
||||||
|
|
||||||
|
|
||||||
def test_get_tribe(sqlite_conn):
|
def test_get_tribe(sqlite_conn):
|
||||||
create_db(sqlite_conn)
|
create_db(sqlite_conn)
|
||||||
prebuild_tribes = populate_tribe(sqlite_conn)
|
prebuild_tribes = populate_tribes(sqlite_conn)
|
||||||
|
|
||||||
|
name = prebuild_tribes[0].name
|
||||||
|
|
||||||
tribe_repo = TribeSQLiteRepository(sqlite_conn)
|
tribe_repo = TribeSQLiteRepository(sqlite_conn)
|
||||||
tribes = tribe_repo.get("Tribe1")
|
tribes = tribe_repo.get(name)
|
||||||
|
|
||||||
assert prebuild_tribes[0] == tribes
|
assert prebuild_tribes[0] == tribes
|
||||||
|
|
||||||
|
|
||||||
def test_get_tribe_not_exists(sqlite_conn):
|
def test_get_tribe_not_exists(sqlite_conn):
|
||||||
create_db(sqlite_conn)
|
create_db(sqlite_conn)
|
||||||
prebuild_tribes = populate_tribe(sqlite_conn)
|
prebuild_tribes = populate_tribes(sqlite_conn)
|
||||||
|
|
||||||
tribe_repo = TribeSQLiteRepository(sqlite_conn)
|
tribe_repo = TribeSQLiteRepository(sqlite_conn)
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
@ -46,7 +45,7 @@ def test_get_tribe_not_exists(sqlite_conn):
|
|||||||
|
|
||||||
def test_list_tribes(sqlite_conn):
|
def test_list_tribes(sqlite_conn):
|
||||||
create_db(sqlite_conn)
|
create_db(sqlite_conn)
|
||||||
prebuild_tribes = populate_tribe(sqlite_conn)
|
prebuild_tribes = populate_tribes(sqlite_conn)
|
||||||
|
|
||||||
tribe_repo = TribeSQLiteRepository(sqlite_conn)
|
tribe_repo = TribeSQLiteRepository(sqlite_conn)
|
||||||
tribes = tribe_repo.list()
|
tribes = tribe_repo.list()
|
||||||
@ -78,42 +77,37 @@ def test_add_tribe(sqlite_conn):
|
|||||||
|
|
||||||
def test_add_tribe_fail_exists(sqlite_conn):
|
def test_add_tribe_fail_exists(sqlite_conn):
|
||||||
create_db(sqlite_conn)
|
create_db(sqlite_conn)
|
||||||
prebuild_tribes = populate_tribe(sqlite_conn)
|
prebuild_tribes = populate_tribes(sqlite_conn)
|
||||||
|
|
||||||
tribe_repo = TribeSQLiteRepository(sqlite_conn)
|
tribe_repo = TribeSQLiteRepository(sqlite_conn)
|
||||||
|
|
||||||
tribe_infos = ("Tribe1", "2nd")
|
existing_tribe = prebuild_tribes[0]
|
||||||
tribe = Tribe(*tribe_infos)
|
|
||||||
with pytest.raises(sqlite3.IntegrityError):
|
with pytest.raises(sqlite3.IntegrityError):
|
||||||
tribe_repo.add(tribe)
|
tribe_repo.add(existing_tribe)
|
||||||
|
|
||||||
|
|
||||||
def test_update_tribe(sqlite_conn):
|
def test_update_tribe(sqlite_conn):
|
||||||
create_db(sqlite_conn)
|
create_db(sqlite_conn)
|
||||||
prebuild_tribes = populate_tribe(sqlite_conn)
|
prebuild_tribes = populate_tribes(sqlite_conn)
|
||||||
|
|
||||||
tribe_repo = TribeSQLiteRepository(sqlite_conn)
|
tribe_repo = TribeSQLiteRepository(sqlite_conn)
|
||||||
tribe_repo.update("Tribe1", Tribe("Tribe0", "Term"))
|
|
||||||
|
name = prebuild_tribes[0].name
|
||||||
|
new_tribe = Tribe("Tribe0", "Term")
|
||||||
|
tribe_repo.update(name, new_tribe)
|
||||||
sqlite_conn.commit()
|
sqlite_conn.commit()
|
||||||
|
|
||||||
expected = [
|
prebuild_tribes[0] = new_tribe
|
||||||
Tribe("Tribe0", "Term"),
|
assert tribe_repo.list() == prebuild_tribes
|
||||||
Tribe("Tribe2", "2nd"),
|
|
||||||
Tribe("Tribe3", "1ST"),
|
|
||||||
]
|
|
||||||
assert tribe_repo.list() == expected
|
|
||||||
|
|
||||||
|
|
||||||
def test_delete_tribe(sqlite_conn):
|
def test_delete_tribe(sqlite_conn):
|
||||||
create_db(sqlite_conn)
|
create_db(sqlite_conn)
|
||||||
prebuild_tribes = populate_tribe(sqlite_conn)
|
prebuild_tribes = populate_tribes(sqlite_conn)
|
||||||
|
|
||||||
tribe_repo = TribeSQLiteRepository(sqlite_conn)
|
tribe_repo = TribeSQLiteRepository(sqlite_conn)
|
||||||
tribe_repo.delete(prebuild_tribes[0])
|
deleted_tribe = prebuild_tribes.pop()
|
||||||
|
tribe_repo.delete(deleted_tribe)
|
||||||
sqlite_conn.commit()
|
sqlite_conn.commit()
|
||||||
|
|
||||||
expected = [
|
assert tribe_repo.list() == prebuild_tribes
|
||||||
Tribe("Tribe2", "2nd"),
|
|
||||||
Tribe("Tribe3", "1ST"),
|
|
||||||
]
|
|
||||||
assert tribe_repo.list() == expected
|
|
Loading…
Reference in New Issue
Block a user