"""Real-Postgres tests for `record_links.issues_for_path`: an issue naming the file in its body or a log comes first; an open issue in the file's area comes next; a closed one there, another project's and a plain task are left out, as is everything for a reader without access; and a path is matched literally.""" import pytest from sqlalchemy import select from grimoire.models import async_session from grimoire.models.note import Note from grimoire.models.project import Project from grimoire.models.system import RecordSystem, System from grimoire.models.task_log import TaskLog from grimoire.services import record_links from tests.helpers import ensure_user pytestmark = [pytest.mark.integration, pytest.mark.usefixtures("_dispose_engine")] PATH = "src/app/lock_guard.py" async def _world(): async with async_session() as s: owner = await ensure_user(s, "record_links_owner") stranger = await ensure_user(s, "record_links_stranger") project = Project(user_id=owner.id, title="record links project") other = Project(user_id=owner.id, title="record links other") s.add_all([project, other]) await s.flush() area = System(user_id=owner.id, project_id=project.id, name="locks", path_patterns=["src/app/**"]) s.add(area) await s.flush() def issue(title, status="done", body="", pid=project.id, uid=owner.id, kind="issue"): return Note(user_id=uid, project_id=pid, title=title, status=status, task_kind=kind, body=body) made = { "in_body": issue("names it in its body", body=f"fixed in {PATH}"), "in_log": issue("names it in a log"), "open_area": issue("open in the area", status="todo"), "closed_area": issue("closed in the area"), "other_project": issue("other project", body=f"see {PATH}", pid=other.id), "work_task": issue("a work task naming it", body=f"edit {PATH}", kind="work"), # `_` is a single-character wildcard to LIKE; matched literally it # must not stand in for the `_` in the path. "near_miss": issue("a near miss", body="src/app/lockXguard.py"), } s.add_all(made.values()) await s.flush() s.add(TaskLog(task_id=made["in_log"].id, user_id=owner.id, content=f"touched {PATH}")) s.add_all(RecordSystem(note_id=made[k].id, system_id=area.id) for k in ("open_area", "closed_area")) await s.commit() ids = {k: n.id for k, n in made.items()} return owner.id, project.id, ids, stranger.id async def test_issues_naming_the_file_come_first_then_open_ones_in_its_area(): uid, pid, ids, _stranger = await _world() found = await record_links.issues_for_path(uid, pid, PATH, limit=5) got = [(link, int(n.id)) for link, n in found] named = {i for link, i in got if link == record_links.NAMES_FILE} assert named == {ids["in_body"], ids["in_log"]} assert got[:2] == [(link, i) for link, i in got if link == record_links.NAMES_FILE] assert (record_links.OPEN_IN_AREA, ids["open_area"]) in got left_out = {"closed_area", "other_project", "work_task", "near_miss"} assert not {ids[k] for k in left_out} & {i for _l, i in got} async def test_the_limit_and_the_exclusions_hold(): uid, pid, ids, stranger = await _world() assert len(await record_links.issues_for_path(uid, pid, PATH, limit=1)) == 1 found = await record_links.issues_for_path( uid, pid, PATH, limit=5, exclude={ids["in_body"], ids["in_log"]}, ) assert [int(n.id) for _l, n in found] == [ids["open_area"]] assert await record_links.issues_for_path(uid, 0, PATH, limit=5) == [] # A reader who cannot read the project is shown none of its issues. assert await record_links.issues_for_path(stranger, pid, PATH, limit=5) == [] async def test_a_tasks_areas_name_their_notes_and_open_issues_but_not_the_task_or_closed_work(): uid, pid, ids, stranger = await _world() async with async_session() as s: area = (await s.execute( select(System.id).where(System.project_id == pid) )).scalar_one() task = Note(user_id=uid, project_id=pid, title="taking this up", status="in_progress", task_kind="work") note = Note(user_id=uid, project_id=pid, title="how locks work here", note_type="note") s.add_all([task, note]) await s.flush() s.add_all(RecordSystem(note_id=n, system_id=area) for n in (task.id, note.id)) await s.commit() task_id, note_id = task.id, note.id found = await record_links.area_records(uid, task_id, ["note", "task:issue"]) assert found == {"note": [note_id], "task:issue": [ids["open_area"]]} assert await record_links.area_records(uid, task_id, ["note"]) == {"note": [note_id]} assert await record_links.area_records(stranger, task_id, ["note", "task:issue"]) == {}